Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 1 addition & 57 deletions eslint-suppressions.json
Original file line number Diff line number Diff line change
@@ -1,57 +1 @@
{
"src/server/api/bannerRouter.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 1
}
},
"src/server/selection/ab.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 1
}
},
"src/server/selection/epsilonGreedySelection.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 1
}
},
"src/server/selection/helpers.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 1
}
},
"src/server/selection/selectVariant.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 1
}
},
"src/server/tests/banners/bannerSelection.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 3
}
},
"src/server/tests/epics/epicSelection.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 1
}
},
"src/server/tests/headers/headerSelection.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 2
}
},
"src/shared/lib/geolocation.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 1
}
},
"src/shared/lib/placeholders.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 2
}
},
"src/shared/types/abTests/banner.ts": {
"@typescript-eslint/no-unnecessary-condition": {
"count": 1
}
}
}
{}
2 changes: 1 addition & 1 deletion src/server/api/bannerRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const buildBannerRouter = (
const design = getDesignForVariant(variant, bannerDesigns.get());

const contributionAmounts = choiceCardAmounts.get();
const requiredCountry = targeting.countryCode ?? 'GB';
const requiredCountry = targeting.countryCode || 'GB';
const requiredRegion = countryCodeToCountryGroupId(requiredCountry);
const targetingMvtId = targeting.mvtId || 1;
const variantAmounts = selectAmountsTestVariant(
Expand Down
4 changes: 0 additions & 4 deletions src/server/selection/ab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ export const selectAmountsTestVariant = (
const assignmentIndex = getRandomNumber(`${seed + mvtId}`) % variants.length;
const variant = variants[assignmentIndex];

if (!variant) {
return undefined;
}

// Test is running and user has been successfully assigned to a control/variant
return {
testName: liveTestName ?? `${testName}_AB_TEST`,
Expand Down
4 changes: 0 additions & 4 deletions src/server/selection/epsilonGreedySelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export function selectVariantWithHighestMean<V extends Variant, T extends Test<V
? sortedVariants[0]
: sortedVariants[Math.floor(Math.random() * bestVariants)]; // more than 1

if (!variant) {
return undefined;
}

return test.variants.find((v) => v.name === variant.variantName);
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/selection/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { BanditVariantData } from './banditData';

export function selectRandomVariant<V extends Variant, T extends Test<V>>(test: T): V | undefined {
const randomVariantIndex = Math.floor(Math.random() * test.variants.length);
const randomVariantData = test.variants[randomVariantIndex];
const randomVariantData = test.variants.length && test.variants[randomVariantIndex];

if (!randomVariantData) {
logError(`Failed to select random variant for bandit test: ${test.name}`);
Expand Down
10 changes: 4 additions & 6 deletions src/server/selection/selectVariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ export const selectVariant = <V extends Variant, T extends Test<V>>(
} else {
// No configured methodology, default to AB test
const variant = selectVariantUsingMVT<V, T>(test, mvtId);
if (variant) {
return {
test,
variant,
};
}
return {
test,
variant,
};
}
return;
};
14 changes: 3 additions & 11 deletions src/server/tests/banners/bannerSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ const purchaseMatches = (
}

const { product, userType } = purchaseInfo;
const productValid = product && testPurchaseInfo?.product.includes(product);
const userValid = userType && testPurchaseInfo?.userType.includes(userType);
const productValid = testPurchaseInfo?.product.includes(product);
const userValid = testPurchaseInfo?.userType.includes(userType);

return productValid && userValid;
};
Expand Down Expand Up @@ -245,15 +245,7 @@ export const selectBannerTest = (
purchaseMatches(test, targeting.purchaseInfo, targeting.isSignedIn) &&
canShowBannerAgain(targeting, test, bannerDeployTimes, now, deploySchedule) &&
correctSignedInStatus(targeting.isSignedIn, test.signedInStatus) &&
pageContextMatches(
targeting,
test.contextTargeting ?? {
tagIds: [],
sectionIds: [],
excludedTagIds: [],
excludedSectionIds: [],
},
) &&
pageContextMatches(targeting, test.contextTargeting) &&
consentStatusMatches(targeting.hasConsented, test.consentStatus) &&
abandonedBasketMatches(test.bannerChannel, targeting.abandonedBasket) &&
matchesFrontsOnlyRequirement(test, targeting)
Expand Down
8 changes: 4 additions & 4 deletions src/server/tests/epics/epicSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const deviceTypeMatchesFilter = (userDeviceType: UserDeviceType): Filter

type FilterResults = Record<string, boolean>;

export type Debug = Record<string, FilterResults>;
export type Debug = Record<string, FilterResults | undefined>;

export interface Result {
result?: {
Expand Down Expand Up @@ -216,9 +216,9 @@ export const findTestAndVariant = (
const test = tests.find((test) =>
filters.every((filter) => {
const got = filter.test(test, targeting);

if (debug[test.name]) {
debug[test.name][filter.id] = got;
const debugResults = debug[test.name];
if (debugResults) {
debugResults[filter.id] = got;
} else {
debug[test.name] = { [filter.id]: got };
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/tests/headers/headerSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ const purchaseMatches = (
}

const { product, userType } = purchaseInfo;
const productValid = product && testPurchaseInfo?.product.includes(product);
const userValid = userType && testPurchaseInfo?.userType.includes(userType);
const productValid = testPurchaseInfo?.product.includes(product);
const userValid = testPurchaseInfo?.userType.includes(userType);

return productValid && userValid;
};
Expand Down
2 changes: 1 addition & 1 deletion src/shared/lib/geolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ export const isGWCheckoutUrl = (baseUrl: string): boolean =>
export const addRegionIdToSupportUrl = (originalUrl: string, countryCode?: string): string => {
if (countryCode) {
const supportRegionId = countryCodeToSupportRegionId(countryCode);
if (supportRegionId && !isGWCheckoutUrl(originalUrl)) {
if (!isGWCheckoutUrl(originalUrl)) {
return originalUrl.replace(
/(support\.theguardian\.com)\/(contribute-in-epic|contribute|subscribe|checkout)/,
(_, domain, path) => `${domain}/${supportRegionId.toLowerCase()}/${path}`,
Expand Down
8 changes: 3 additions & 5 deletions src/shared/lib/placeholders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { countryCodeToCountryGroupId, getCountryName, getLocalCurrencySymbol } f

// Map each placeholder to a rule defining how to substitute it, if possible
type PlaceholderRule = (params: { countryCode?: string; prices?: Prices }) => string | undefined;
type PlaceholderRules = Record<string, PlaceholderRule>;
type PlaceholderRules = Record<string, PlaceholderRule | undefined>;

const priceSubstitution = (
product: GuardianProduct,
Expand All @@ -14,10 +14,8 @@ const priceSubstitution = (
if (prices) {
const countryGroupId = countryCodeToCountryGroupId(countryCode);
const localPrices = prices[countryGroupId];
if (localPrices) {
const localCurrencySymbol = getLocalCurrencySymbol(countryCode);
return `${localCurrencySymbol}${localPrices[product][ratePlan].price}`;
}
const localCurrencySymbol = getLocalCurrencySymbol(countryCode);
return `${localCurrencySymbol}${localPrices[product][ratePlan].price}`;
}
return undefined;
};
Expand Down
2 changes: 1 addition & 1 deletion src/shared/types/abTests/banner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface BannerDesignName {
type BannerUi = BannerTemplate | BannerDesignName;

export function uiIsDesign(ui: BannerUi): ui is BannerDesignName {
return (ui as BannerDesignName).designName !== undefined;
return typeof ui === 'object' && 'designName' in ui;
}

export const bannerVariantFromToolSchema = z.object({
Expand Down