Skip to content

Commit

Permalink
remove domainSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
3ch023 committed Aug 12, 2024
1 parent 6fcdf15 commit 2f534e2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 87 deletions.
17 changes: 2 additions & 15 deletions libs/features/mas/commerce/libs/commerce.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ import {
Commitment,
} from '@pandora/data-models-odm';

declare enum ProviderEnvironment {
STAGE = "STAGE",
PRODUCTION = "PRODUCTION",
LOCAL = "LOCAL"
}

declare enum Landscape {
DRAFT = "DRAFT",
PUBLISHED = "PUBLISHED"
}

type RequiredKey<Type, Key extends keyof Type> = Type & {
[Property in Key]-?: Type[Property];
};
Expand Down Expand Up @@ -137,8 +126,8 @@ declare global {

interface Settings {
country: string;
env: ProviderEnvironment;
landscape: Landscape;
env: string;
landscape: string;
// TODO: ideally, this setting should be processed by price template and belong to price settings
forceTaxExclusive: boolean;
language: string;
Expand Down Expand Up @@ -340,7 +329,6 @@ declare global {
checkoutWorkflow: CheckoutType;
checkoutWorkflowStep: WorkflowStep;
entitlement: boolean;
upgrade: boolean;
modal: boolean;
extraOptions: Partial<Record<keyof CheckoutLinkParameter, any>>;
}
Expand Down Expand Up @@ -582,7 +570,6 @@ declare global {
wcsBufferDelay: number;
wcsBufferLimit: number;
wcsURL: string;
domainSwitch: boolean;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion libs/features/mas/commerce/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const Defaults = Object.freeze({
displayPerUnit: false,
displayRecurrence: true,
displayTax: false,
domainSwitch: false,
env: Env.PRODUCTION,
forceTaxExclusive: false,
language: 'en',
Expand Down
17 changes: 2 additions & 15 deletions libs/features/mas/commerce/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ import {
Commitment,
} from '@pandora/data-models-odm';

declare enum ProviderEnvironment {
STAGE = "STAGE",
PRODUCTION = "PRODUCTION",
LOCAL = "LOCAL"
}

declare enum Landscape {
DRAFT = "DRAFT",
PUBLISHED = "PUBLISHED"
}

type RequiredKey<Type, Key extends keyof Type> = Type & {
[Property in Key]-?: Type[Property];
};
Expand Down Expand Up @@ -137,8 +126,8 @@ declare global {

interface Settings {
country: string;
env: ProviderEnvironment;
landscape: Landscape;
env: string;
landscape: string;
// TODO: ideally, this setting should be processed by price template and belong to price settings
forceTaxExclusive: boolean;
language: string;
Expand Down Expand Up @@ -340,7 +329,6 @@ declare global {
checkoutWorkflow: CheckoutType;
checkoutWorkflowStep: WorkflowStep;
entitlement: boolean;
upgrade: boolean;
modal: boolean;
extraOptions: Partial<Record<keyof CheckoutLinkParameter, any>>;
}
Expand Down Expand Up @@ -582,7 +570,6 @@ declare global {
wcsBufferDelay: number;
wcsBufferLimit: number;
wcsURL: string;
domainSwitch: boolean;
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions libs/features/mas/commerce/src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ function getSettings(config = {}) {
getParameter('wcsBufferLimit', commerce),
Defaults.wcsBufferLimit,
);
const domainSwitch = toBoolean(getParameter('domain.switch', commerce), false);

return {
...getLocaleSettings({ locale }),
Expand All @@ -230,7 +229,6 @@ function getSettings(config = {}) {
wcsBufferLimit,
wcsURL,
landscape,
domainSwitch,
};
}

Expand Down
108 changes: 54 additions & 54 deletions libs/features/mas/commerce/src/wcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,60 +49,60 @@ export function Wcs({ settings }) {
*/
async function resolveWcsOffers(options, promises, reject = true) {
let message = ERROR_MESSAGE_OFFER_NOT_FOUND;
try {
log.debug('Fetching:', options);
options.offerSelectorIds = options.offerSelectorIds.sort();
const url = new URL(settings.wcsURL);
url.searchParams.set('offer_selector_ids', options.offerSelectorIds.join(','));
url.searchParams.set('country', options.country);
url.searchParams.set('language', options.language);
if (options.currency) {
url.searchParams.set('currency', options.currency);
}
url.searchParams.set('locale', options.locale);
url.searchParams.set('promotion_code', options.promotionCode);
url.searchParams.set('landscape', env === Env.STAGE ? 'ALL' : settings.landscape);
url.searchParams.set('api_key', apiKey);
const response = await fetch(url.toString());
if (response.ok) {
const data = await response.json();
log.debug('Fetched:', options, data);
let offers = data.resolvedOffers ?? [];
offers = offers.map(applyPlanType);
// resolve all promises that have offers
promises.forEach(({ resolve }, offerSelectorId) => {
// select offers with current OSI
const resolved = offers
.filter(({ offerSelectorIds }) =>
offerSelectorIds.includes(offerSelectorId),
)
.flat();
// resolve current promise if at least 1 offer is present
if (resolved.length) {
promises.delete(offerSelectorId);
resolve(resolved);
}
});
}
} catch (error) {
// in case of 404 WCS error caused by a request with multiple osis,
// fallback to `fetch-by-one` strategy
if (error.status === 404 && options.offerSelectorIds.length > 1) {
log.debug('Multi-osi 404, fallback to fetch-by-one strategy');
await Promise.allSettled(
options.offerSelectorIds.map((offerSelectorId) =>
resolveWcsOffers(
{ ...options, offerSelectorIds: [offerSelectorId] },
promises,
false, // do not reject promises for missing offers, this will be done below
),
),
);
} else {
log.error('Failed:', options, error);
message = ERROR_MESSAGE_BAD_REQUEST;
}
log.debug('Fetching:', options);
options.offerSelectorIds = options.offerSelectorIds.sort();
const url = new URL(settings.wcsURL);
url.searchParams.set('offer_selector_ids', options.offerSelectorIds.join(','));
url.searchParams.set('country', options.country);
url.searchParams.set('language', options.language);
url.searchParams.set('locale', options.locale);
url.searchParams.set('landscape', env === Env.STAGE ? 'ALL' : settings.landscape);
url.searchParams.set('api_key', apiKey);
if (options.promotionCode) {
url.searchParams.set('promotion_code', options.promotionCode);
}
if (options.currency) {
url.searchParams.set('currency', options.currency);
}

const response = await fetch(url.toString());
if (response.ok) {
const data = await response.json();
log.debug('Fetched:', options, data);
let offers = data.resolvedOffers ?? [];
offers = offers.map(applyPlanType);
// resolve all promises that have offers
promises.forEach(({ resolve }, offerSelectorId) => {
// select offers with current OSI
const resolved = offers
.filter(({ offerSelectorIds }) =>
offerSelectorIds.includes(offerSelectorId),
)
.flat();
// resolve current promise if at least 1 offer is present
if (resolved.length) {
promises.delete(offerSelectorId);
resolve(resolved);
}
});
}
// in case of 404 WCS error caused by a request with multiple osis,
// fallback to `fetch-by-one` strategy
else if (response.status === 404 && options.offerSelectorIds.length > 1) {
log.debug('Multi-osi 404, fallback to fetch-by-one strategy');
await Promise.allSettled(
options.offerSelectorIds.map((offerSelectorId) =>
resolveWcsOffers(
{ ...options, offerSelectorIds: [offerSelectorId] },
promises,
false, // do not reject promises for missing offers, this will be done below
),
),
);
} else {
const error = await response.text();
log.error(`Failed to fetch WCS offer. Status: ${response.status}, message: ${error}`, options);
message = ERROR_MESSAGE_BAD_REQUEST;
}

if (reject && promises.size) {
Expand Down

0 comments on commit 2f534e2

Please sign in to comment.