Skip to content

Commit 6566a2e

Browse files
authored
refactor: values used for potential reach from users to impressions (#2949)
1 parent d5d327a commit 6566a2e

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

__tests__/schema/posts/boost.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,14 +3100,19 @@ describe('query boostEstimatedReachDaily', () => {
31003100
impressions: 100,
31013101
clicks: 5,
31023102
users: 50,
3103+
min_impressions: 45,
3104+
max_impressions: 55,
31033105
});
31043106

31053107
const res = await client.query(QUERY, {
31063108
variables: { ...params, budget: 2000, duration: 5 }, // Valid budget
31073109
});
31083110

31093111
expect(res.errors).toBeFalsy();
3110-
expect(res.data.boostEstimatedReachDaily).toBeDefined();
3112+
expect(res.data.boostEstimatedReachDaily).toEqual({
3113+
min: 45,
3114+
max: 55,
3115+
});
31113116

31123117
// Verify the HTTP call was made with correct budget conversion
31133118
expect(mockFetchParse).toHaveBeenCalledWith(
@@ -3157,14 +3162,19 @@ describe('query boostEstimatedReachDaily', () => {
31573162
impressions: 200,
31583163
clicks: 10,
31593164
users: 75,
3165+
min_impressions: 68,
3166+
max_impressions: 82,
31603167
});
31613168

31623169
const res = await client.query(QUERY, {
31633170
variables: { ...params, budget: 3000, duration: 15 }, // Valid duration
31643171
});
31653172

31663173
expect(res.errors).toBeFalsy();
3167-
expect(res.data.boostEstimatedReachDaily).toBeDefined();
3174+
expect(res.data.boostEstimatedReachDaily).toEqual({
3175+
min: 68,
3176+
max: 82,
3177+
});
31683178

31693179
// Verify the HTTP call includes both parameters correctly
31703180
expect(mockFetchParse).toHaveBeenCalledWith(
@@ -3195,6 +3205,8 @@ describe('query boostEstimatedReachDaily', () => {
31953205
impressions: 500,
31963206
clicks: 40,
31973207
users: 180,
3208+
min_impressions: 166,
3209+
max_impressions: 194,
31983210
});
31993211

32003212
const res = await client.query(QUERY, {
@@ -3203,8 +3215,8 @@ describe('query boostEstimatedReachDaily', () => {
32033215

32043216
expect(res.errors).toBeFalsy();
32053217
expect(res.data.boostEstimatedReachDaily).toEqual({
3206-
max: 194, // 180 + Math.floor(180 * 0.08) = 180 + 14 = 194
3207-
min: 166, // 180 - Math.floor(180 * 0.08) = 180 - 14 = 166
3218+
min: 166,
3219+
max: 194,
32083220
});
32093221

32103222
// Verify the HTTP call was made correctly with both parameters
@@ -3235,6 +3247,8 @@ describe('query boostEstimatedReachDaily', () => {
32353247
impressions: 50,
32363248
clicks: 3,
32373249
users: 25,
3250+
min_impressions: 23,
3251+
max_impressions: 27,
32383252
});
32393253

32403254
const res = await client.query(QUERY, {
@@ -3243,8 +3257,8 @@ describe('query boostEstimatedReachDaily', () => {
32433257

32443258
expect(res.errors).toBeFalsy();
32453259
expect(res.data.boostEstimatedReachDaily).toEqual({
3246-
max: 27, // 25 + Math.floor(25 * 0.08) = 25 + 2 = 27
3247-
min: 23, // 25 - Math.floor(25 * 0.08) = 25 - 2 = 23
3260+
min: 23,
3261+
max: 27,
32483262
});
32493263

32503264
// Verify the HTTP call was made with minimum budget
@@ -3275,6 +3289,8 @@ describe('query boostEstimatedReachDaily', () => {
32753289
impressions: 50000,
32763290
clicks: 2500,
32773291
users: 15000,
3292+
min_impressions: 13800,
3293+
max_impressions: 16200,
32783294
});
32793295

32803296
const res = await client.query(QUERY, {
@@ -3283,8 +3299,8 @@ describe('query boostEstimatedReachDaily', () => {
32833299

32843300
expect(res.errors).toBeFalsy();
32853301
expect(res.data.boostEstimatedReachDaily).toEqual({
3286-
max: 16200, // 15000 + Math.floor(15000 * 0.08) = 15000 + 1200 = 16200
3287-
min: 13800, // 15000 - Math.floor(15000 * 0.08) = 15000 - 1200 = 13800
3302+
min: 13800,
3303+
max: 16200,
32883304
});
32893305

32903306
// Verify the HTTP call was made with maximum budget
@@ -3315,6 +3331,8 @@ describe('query boostEstimatedReachDaily', () => {
33153331
impressions: 80,
33163332
clicks: 5,
33173333
users: 35,
3334+
min_impressions: 33,
3335+
max_impressions: 37,
33183336
});
33193337

33203338
const res = await client.query(QUERY, {
@@ -3323,8 +3341,8 @@ describe('query boostEstimatedReachDaily', () => {
33233341

33243342
expect(res.errors).toBeFalsy();
33253343
expect(res.data.boostEstimatedReachDaily).toEqual({
3326-
max: 37, // 35 + Math.floor(35 * 0.08) = 35 + 2 = 37
3327-
min: 33, // 35 - Math.floor(35 * 0.08) = 35 - 2 = 33
3344+
min: 33,
3345+
max: 37,
33283346
});
33293347

33303348
// Verify the HTTP call was made with minimum duration
@@ -3355,6 +3373,8 @@ describe('query boostEstimatedReachDaily', () => {
33553373
impressions: 1200,
33563374
clicks: 60,
33573375
users: 450,
3376+
min_impressions: 414,
3377+
max_impressions: 486,
33583378
});
33593379

33603380
const res = await client.query(QUERY, {
@@ -3363,8 +3383,8 @@ describe('query boostEstimatedReachDaily', () => {
33633383

33643384
expect(res.errors).toBeFalsy();
33653385
expect(res.data.boostEstimatedReachDaily).toEqual({
3366-
max: 486, // 450 + Math.floor(450 * 0.08) = 450 + 36 = 486
3367-
min: 414, // 450 - Math.floor(450 * 0.08) = 450 - 36 = 414
3386+
min: 414,
3387+
max: 486,
33683388
});
33693389

33703390
// Verify the HTTP call was made with maximum duration
@@ -3404,6 +3424,8 @@ describe('query boostEstimatedReachDaily', () => {
34043424
impressions: 150,
34053425
clicks: 10,
34063426
users: 65,
3427+
min_impressions: 60,
3428+
max_impressions: 70,
34073429
});
34083430

34093431
const res = await client.query(QUERY, {
@@ -3412,8 +3434,8 @@ describe('query boostEstimatedReachDaily', () => {
34123434

34133435
expect(res.errors).toBeFalsy();
34143436
expect(res.data.boostEstimatedReachDaily).toEqual({
3415-
max: 70, // 65 + Math.floor(65 * 0.08) = 65 + 5 = 70
3416-
min: 60, // 65 - Math.floor(65 * 0.08) = 65 - 5 = 60
3437+
min: 60,
3438+
max: 70,
34173439
});
34183440

34193441
// Verify the HTTP call was made with correct parameters

src/integrations/skadi/api/clients.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type GetCampaignResponse,
99
type GetCampaignsProps,
1010
type PostEstimatedReach,
11+
type PostEstimatedReachResponse,
1112
type PromotedPost,
1213
type PromotedPostList,
1314
type StartPostCampaignResponse,
@@ -111,7 +112,7 @@ export class SkadiApiClient implements ISkadiApiClient {
111112
user_id: string;
112113
budget?: number;
113114
duration?: number;
114-
}) {
115+
}): Promise<PostEstimatedReachResponse> {
115116
return this.garmr.execute(async () => {
116117
const response = await fetchParse<PostEstimatedReach>(
117118
`${this.url}/promote/post/reach`,
@@ -129,6 +130,8 @@ export class SkadiApiClient implements ISkadiApiClient {
129130
impressions: response.impressions ?? 0,
130131
clicks: response.clicks ?? 0,
131132
users: response.users ?? 0,
133+
minImpressions: response.min_impressions ?? 0,
134+
maxImpressions: response.max_impressions ?? 0,
132135
};
133136
});
134137
}
@@ -139,7 +142,7 @@ export class SkadiApiClient implements ISkadiApiClient {
139142
}: Pick<
140143
EstimatedBoostReachParams,
141144
'userId' | 'postId'
142-
>): Promise<PostEstimatedReach> {
145+
>): Promise<PostEstimatedReachResponse> {
143146
const params = {
144147
post_id: postId,
145148
user_id: userId,
@@ -153,7 +156,7 @@ export class SkadiApiClient implements ISkadiApiClient {
153156
userId,
154157
durationInDays,
155158
budget,
156-
}: EstimatedBoostReachParams): Promise<PostEstimatedReach> {
159+
}: EstimatedBoostReachParams): Promise<PostEstimatedReachResponse> {
157160
const params = {
158161
post_id: postId,
159162
user_id: userId,

src/integrations/skadi/api/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ export interface PostEstimatedReach {
99
impressions: number;
1010
clicks: number;
1111
users: number;
12+
min_impressions: number;
13+
max_impressions: number;
14+
}
15+
16+
export interface PostEstimatedReachResponse
17+
extends Pick<PostEstimatedReach, 'impressions' | 'clicks' | 'users'> {
18+
minImpressions: number;
19+
maxImpressions: number;
1220
}
1321

1422
export interface PromotedPost {
@@ -88,10 +96,10 @@ export interface ISkadiApiClient {
8896
}): Promise<{ currentBudget: string }>;
8997
estimatePostBoostReach(
9098
params: Pick<EstimatedBoostReachParams, 'userId' | 'postId'>,
91-
): Promise<PostEstimatedReach>;
99+
): Promise<PostEstimatedReachResponse>;
92100
estimatePostBoostReachDaily(
93101
params: EstimatedBoostReachParams,
94-
): Promise<PostEstimatedReach>;
102+
): Promise<PostEstimatedReachResponse>;
95103
getCampaignById: (
96104
params: GetCampaignByIdProps,
97105
) => Promise<GetCampaignResponse>;

src/schema/posts.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,14 +2177,18 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
21772177
checkPostAlreadyBoosted(post);
21782178
validatePostBoostArgs({ budget, duration });
21792179

2180-
const { users } = await skadiApiClient.estimatePostBoostReachDaily({
2181-
postId,
2182-
userId: ctx.userId,
2183-
budget: coresToUsd(budget),
2184-
durationInDays: duration,
2185-
});
2180+
const { minImpressions, maxImpressions } =
2181+
await skadiApiClient.estimatePostBoostReachDaily({
2182+
postId,
2183+
userId: ctx.userId,
2184+
budget: coresToUsd(budget),
2185+
durationInDays: duration,
2186+
});
21862187

2187-
return getAdjustedReach(users);
2188+
const min = Math.max(minImpressions, 0);
2189+
const max = Math.max(maxImpressions, min);
2190+
2191+
return { min, max };
21882192
},
21892193
postCampaignById: async (
21902194
_,

0 commit comments

Comments
 (0)