-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from vtex-apps/chore/refactor-for-publication
Chore/refactor for publication
- Loading branch information
Showing
15 changed files
with
198 additions
and
3,240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,6 @@ | |
"extends": "vtex", | ||
"root": true, | ||
"env": { | ||
"node": true, | ||
"es6": true, | ||
"jest": true | ||
"node": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,32 @@ | ||
import type { InstanceOptions, IOContext} from '@vtex/api' | ||
import { ExternalClient } from '@vtex/api' | ||
import { InstanceOptions, IOContext, ExternalClient } from '@vtex/api' | ||
|
||
const BASE_URL = (name: string) => ({ | ||
STAGING: `http://${name}.staging-ads.peps.relevanc.io`, | ||
PRODUCTION: `http://${name}.ads.peps.relevanc.io`, | ||
}) | ||
|
||
export default class RelevanC extends ExternalClient { | ||
constructor(context: IOContext, options?: InstanceOptions) { | ||
super( | ||
'http://exito.staging-ads.peps.relevanc.io/sponsored-offers?sourcePageNumber=0&keyOrigin=', | ||
context, | ||
options | ||
) | ||
super('https://ads/peps.relevanc.io', context, options) | ||
} | ||
|
||
public async getOffers(params: string): Promise<any> { | ||
return this.http.get(params.toString(), { | ||
public async getSponsoredOffers( | ||
production: boolean, | ||
serverName: string, | ||
params: SponsoredProductsPayload | ||
): Promise<SponsoredProductsResponse> { | ||
const URL = production | ||
? BASE_URL(serverName).PRODUCTION | ||
: BASE_URL(serverName).STAGING | ||
|
||
return this.http.get(`${URL}/sponsored-offers`, { | ||
headers: { | ||
'x-vtex-use-https': 'true', | ||
}, | ||
metric: 'status-get-raw', | ||
params: { | ||
...params, | ||
}, | ||
metric: 'relevanc-get-offers', | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const DESKTOP = 'PC_LR_PLR' | ||
export const MOBILE = 'MOB_LR_PLR' | ||
export enum ACTION { | ||
ADD = 'add', | ||
REMOVE = 'remove', | ||
PROMOTE = 'promote', | ||
DEMOTE = 'demote', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,74 @@ | ||
export default async function before( | ||
import { ACTION, DESKTOP } from '../contants' | ||
|
||
export async function before( | ||
_: unknown, | ||
args: SearchParams, | ||
ctx: Context | ||
): Promise<SearchParams> { | ||
const { | ||
clients: { relevanC }, | ||
clients: { relevanC, apps }, | ||
vtex: { logger }, | ||
} = ctx | ||
// This is just an example. Change the implementation of this function however you like :) | ||
console.log(args.query) | ||
|
||
const searchQuery = args.query | ||
const sponsoredProducts = await relevanC.getOffers( | ||
`${searchQuery}&adSpaceId=PC_LR_PLR` | ||
|
||
if (!searchQuery) { | ||
const message = 'Query was undefined' | ||
|
||
logger.error({ | ||
message, | ||
}) | ||
|
||
throw new Error(message) | ||
} | ||
|
||
const settings: AppSettings = await apps.getAppSettings( | ||
process.env.VTEX_APP_ID as string | ||
) | ||
console.log(sponsoredProducts) | ||
|
||
if (sponsoredProducts.offers.length) { | ||
console.log('hola cesarin') | ||
const myDynamicRules: DynamicRule[] = [] | ||
for (const spProduct of sponsoredProducts.offers) { | ||
myDynamicRules.push({ | ||
action: 'promote', | ||
type: 'id', | ||
value: spProduct.productId, | ||
}) | ||
} | ||
console.log(myDynamicRules) | ||
|
||
return { ...args, dynamicRules: myDynamicRules } | ||
if (!Object.keys(settings).length) { | ||
const message = 'Settings for RelevanC Integration not found' | ||
|
||
logger.error({ | ||
message, | ||
}) | ||
|
||
throw new Error(message) | ||
} | ||
const customRules: DynamicRule[] = [ | ||
{ | ||
action: 'add', | ||
type: 'id', | ||
value: '1253', | ||
}, | ||
{ | ||
action: 'promote', | ||
type: 'id', | ||
value: '1253', | ||
}, | ||
|
||
const { | ||
adServerName, | ||
boostType, | ||
maxOffersToDisplay, | ||
addAllProducts, | ||
production, | ||
} = settings | ||
|
||
const { offers } = await relevanC.getSponsoredOffers( | ||
production, | ||
adServerName, | ||
{ | ||
action: 'remove', | ||
type: 'id', | ||
value: '1336358', | ||
}, | ||
|
||
// { | ||
// action: 'promote', | ||
// type: 'id', | ||
// value: '857952', | ||
// }, | ||
// { | ||
// action: 'promote', | ||
// type: 'id', | ||
// value: '308625', | ||
// }, | ||
] | ||
console.log(customRules) | ||
|
||
return { ...args, dynamicRules: customRules } | ||
sourcePageNumber: 0, | ||
keyOrigin: searchQuery, | ||
adSpaceId: DESKTOP, | ||
} | ||
) | ||
|
||
if (!offers.length) { | ||
return args | ||
} | ||
|
||
const dynamicRules = offers.reduce((rules: DynamicRule[], offer, index) => { | ||
if (index < maxOffersToDisplay) { | ||
rules.push({ | ||
action: addAllProducts ? ACTION.ADD : ACTION.PROMOTE, | ||
type: boostType, | ||
value: offer.productId, | ||
}) | ||
} | ||
|
||
return rules | ||
}, []) | ||
|
||
return { ...args, dynamicRules } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { before } from './before' | ||
|
||
export const resolvers = { | ||
Query: { | ||
before, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"stack": "nodejs", | ||
"memory": 256, | ||
"minReplicas": 3, | ||
"workers": 1 | ||
} | ||
"stack": "nodejs", | ||
"memory": 512, | ||
"minReplicas": 3, | ||
"workers": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type AppSettings = { | ||
adServerName: string | ||
boostType: BoostType | ||
maxOffersToDisplay: number | ||
addAllProducts: boolean | ||
production: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
type BaseSponsoredProductsPaylaod = { | ||
adSpaceId: 'PC_LR_PLR' | 'MOB_LR_PLR' | ||
sourcePageNumber: number | ||
} | ||
|
||
type KeyIdSponsoredProductsPayload = BaseSponsoredProductsPaylaod & { | ||
keyId: string | ||
filterCategoryCodePath: string | ||
} | ||
|
||
type KeyOriginSponsoredProductsPayload = BaseSponsoredProductsPaylaod & { | ||
keyOrigin: string | ||
filterCategoryCodePath?: string | ||
} | ||
|
||
type SponsoredProductsPayload = | ||
| KeyIdSponsoredProductsPayload | ||
| KeyOriginSponsoredProductsPayload | ||
|
||
type Offer = { | ||
algoSelectionType: | ||
| 'NOTSELECTED' | ||
| 'EXPLORED' | ||
| 'UNEXPLORED' | ||
| 'LONGTAIL' | ||
| 'SHORTTAIL' | ||
isSponsored: boolean | ||
offerId: number | ||
productId: string | ||
tagName: string | ||
tag: string | ||
} | ||
|
||
type SponsoredProductsResponse = { | ||
nbResults: number | ||
offers: Offer[] | ||
} |
Oops, something went wrong.