Skip to content

Commit 72aa07d

Browse files
authored
Merge pull request #419 from AppQuality/develop
fix(target): update age handling to support multiple age ranges and a…
2 parents cfea0cd + 5aae40b commit 72aa07d

3 files changed

Lines changed: 74 additions & 32 deletions

File tree

src/routes/users/me/campaigns/_get/UserTargetChecker.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class UserTargetChecker {
8989
countries?: string[];
9090
provinces?: string[];
9191
cufs?: { id: number; values: (string | number)[] }[];
92-
age?: { min?: number; max?: number };
92+
age?: { min?: number; max?: number }[];
9393
genders?: number[];
9494
}) {
9595
if (Object.keys(targetRules).length === 0) return true;
@@ -132,8 +132,17 @@ export class UserTargetChecker {
132132
}
133133
}
134134

135-
if (age && age.max && age.min) {
136-
if (this.userAge < age.min || this.userAge > age.max) return false;
135+
if (age && age.length) {
136+
if (
137+
!age.some((ageRule) => {
138+
if (!ageRule.min && !ageRule.max) return true;
139+
const min = ageRule.min ?? -Infinity;
140+
const max = ageRule.max ?? Infinity;
141+
return this.userAge >= min && this.userAge <= max;
142+
})
143+
) {
144+
return false;
145+
}
137146
}
138147

139148
if (genders && !genders.includes(this.userGender)) {

src/routes/users/me/campaigns/_get/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,13 @@ class RouteItem extends UserRoute<{
429429
campaignsWithTarget.map((c) => c.id)
430430
);
431431

432-
const ageMap = new Map(
433-
allowedAges.map((a) => [a.campaign_id, { min: a.min, max: a.max }])
432+
const ages = allowedAges.reduce(
433+
(acc: Record<number, { min: number; max: number }[]>, cur) => {
434+
if (!acc[cur.campaign_id]) acc[cur.campaign_id] = [];
435+
acc[cur.campaign_id].push({ min: cur.min, max: cur.max });
436+
return acc;
437+
},
438+
{}
434439
);
435440

436441
return campaigns.map((campaign) => {
@@ -453,7 +458,7 @@ class RouteItem extends UserRoute<{
453458
return acc;
454459
}, {});
455460

456-
const age = ageMap.get(campaign.id) || { min: 0, max: 999 };
461+
const age = campaign.id in ages ? ages[campaign.id] : undefined;
457462

458463
const genders =
459464
allowedGenders.length > 0
@@ -557,7 +562,7 @@ class RouteItem extends UserRoute<{
557562
countries?: string[];
558563
provinces?: string[];
559564
cufs?: { id: number; values: (string | number)[] }[];
560-
age?: { min?: number; max?: number };
565+
age?: { min?: number; max?: number }[];
561566
genders?: number[];
562567
};
563568
})[]

src/routes/users/me/campaigns/_get/target.spec.ts

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -332,23 +332,48 @@ describe("GET /users/me/campaigns - target", () => {
332332

333333
describe("Target by age", () => {
334334
beforeAll(async () => {
335-
await tryber.tables.WpAppqEvdProfile.do().insert({
336-
id: 1,
337-
wp_user_id: 1,
338-
email: "",
339-
education_id: 1,
340-
employment_id: 1,
341-
country: "Italy",
342-
birth_date: "2000-01-01",
343-
});
335+
await tryber.tables.CampaignDossierDataAge.do().insert([
336+
{
337+
campaign_dossier_data_id: 1,
338+
min: 20,
339+
max: 27,
340+
},
341+
{
342+
campaign_dossier_data_id: 1,
343+
min: 29,
344+
max: 30,
345+
},
346+
{
347+
campaign_dossier_data_id: 1,
348+
min: 50,
349+
max: 60,
350+
},
351+
]);
344352
});
345353
afterAll(async () => {
346-
await tryber.tables.WpAppqEvdProfile.do().delete();
354+
await tryber.tables.CampaignDossierDataAge.do().delete();
347355
});
348356
describe("Tester with right age value", () => {
349-
beforeAll(async () => {});
357+
beforeAll(async () => {
358+
const today = new Date();
359+
const birthDate = new Date(
360+
today.getFullYear() - 29,
361+
today.getMonth(),
362+
today.getDate() - 1
363+
);
364+
const birthDateString = birthDate.toISOString().split("T")[0];
365+
await tryber.tables.WpAppqEvdProfile.do().insert({
366+
id: 1,
367+
wp_user_id: 1,
368+
email: "",
369+
education_id: 1,
370+
employment_id: 1,
371+
country: "Italy",
372+
birth_date: birthDateString,
373+
});
374+
});
350375
afterAll(async () => {
351-
await tryber.tables.CampaignDossierDataAge.do().delete();
376+
await tryber.tables.WpAppqEvdProfile.do().delete();
352377
});
353378

354379
it("Should show the campaign", async () => {
@@ -361,14 +386,25 @@ describe("GET /users/me/campaigns - target", () => {
361386

362387
describe("Tester with wrong age value", () => {
363388
beforeAll(async () => {
364-
await tryber.tables.CampaignDossierDataAge.do().insert({
365-
campaign_dossier_data_id: 1,
366-
max: 200,
367-
min: 100,
389+
const today = new Date();
390+
const birthDate = new Date(
391+
today.getFullYear() - 100,
392+
today.getMonth(),
393+
today.getDate() - 1
394+
);
395+
const birthDateString = birthDate.toISOString().split("T")[0];
396+
await tryber.tables.WpAppqEvdProfile.do().insert({
397+
id: 1,
398+
wp_user_id: 1,
399+
email: "",
400+
education_id: 1,
401+
employment_id: 1,
402+
country: "Italy",
403+
birth_date: birthDateString,
368404
});
369405
});
370406
afterAll(async () => {
371-
await tryber.tables.CampaignDossierDataAge.do().delete();
407+
await tryber.tables.WpAppqEvdProfile.do().delete();
372408
});
373409

374410
it("Should not show the campaign", async () => {
@@ -378,14 +414,6 @@ describe("GET /users/me/campaigns - target", () => {
378414
expect(response.status).toBe(404);
379415
});
380416
});
381-
describe("Campaign without required age value", () => {
382-
it("Should show the campaign", async () => {
383-
const response = await request(app)
384-
.get("/users/me/campaigns")
385-
.set("Authorization", "Bearer tester");
386-
expect(response.status).toBe(200);
387-
});
388-
});
389417
});
390418

391419
describe("Target by gender", () => {

0 commit comments

Comments
 (0)