Skip to content

Commit 48b8eef

Browse files
authored
refactor: adjusted reach if min and max are the same (#2951)
1 parent 6566a2e commit 48b8eef

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

__tests__/schema/posts/boost.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3456,4 +3456,48 @@ describe('query boostEstimatedReachDaily', () => {
34563456
},
34573457
);
34583458
});
3459+
3460+
it('should fall back to getAdjustedReach when min and max impressions are equal', async () => {
3461+
loggedUser = '1';
3462+
3463+
// Mock the HTTP response where min and max impressions are the same
3464+
const mockFetchParse = fetchParse as jest.Mock;
3465+
mockFetchParse.mockResolvedValue({
3466+
impressions: 200,
3467+
clicks: 15,
3468+
users: 100,
3469+
min_impressions: 75, // Same value
3470+
max_impressions: 75, // Same value
3471+
});
3472+
3473+
const res = await client.query(QUERY, {
3474+
variables: { ...params, budget: 4000, duration: 10 }, // 4000 cores = 40 USD, 10 days
3475+
});
3476+
3477+
expect(res.errors).toBeFalsy();
3478+
// When min_impressions === max_impressions, it should use getAdjustedReach(maxImpressions)
3479+
// getAdjustedReach applies ±8% calculation: 75 ± Math.floor(75 * 0.08) = 75 ± 6
3480+
expect(res.data.boostEstimatedReachDaily).toEqual({
3481+
min: 69, // 75 - Math.floor(75 * 0.08) = 75 - 6 = 69
3482+
max: 81, // 75 + Math.floor(75 * 0.08) = 75 + 6 = 81
3483+
});
3484+
3485+
// Verify the HTTP call was made with correct parameters
3486+
expect(mockFetchParse).toHaveBeenCalledWith(
3487+
`${process.env.SKADI_API_ORIGIN}/promote/post/reach`,
3488+
{
3489+
method: 'POST',
3490+
headers: {
3491+
'Content-Type': 'application/json',
3492+
},
3493+
body: JSON.stringify({
3494+
post_id: 'p1',
3495+
user_id: '1',
3496+
duration: 10 * ONE_DAY_IN_SECONDS,
3497+
budget: 40, // Converted from cores to USD (4000 cores = 40 USD)
3498+
}),
3499+
agent: expect.any(Function),
3500+
},
3501+
);
3502+
});
34593503
});

src/schema/posts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,10 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
21852185
durationInDays: duration,
21862186
});
21872187

2188+
if (minImpressions === maxImpressions) {
2189+
return getAdjustedReach(maxImpressions);
2190+
}
2191+
21882192
const min = Math.max(minImpressions, 0);
21892193
const max = Math.max(maxImpressions, min);
21902194

0 commit comments

Comments
 (0)