Skip to content

Commit

Permalink
added tests for time-range function
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryansinha16 committed Oct 4, 2024
1 parent 7b44b01 commit fd09815
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
39 changes: 38 additions & 1 deletion packages/channel-utils/src/date-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { addInterval, getDayPriorTillTomorrow, getLastXMonths, getTomorrowStartOfDay } from '@repo/utils';
import { prisma, Tier } from '@repo/database';
import { type Insight, prisma, Tier } from '@repo/database';
import { tierConstraints } from '@repo/mappings';

interface LatestInsightResult {
date: Date;
adAccount?: {
organizations: {
tier: Tier;
}[];
};
}

export const timeRanges = async (initial: boolean, adAccountId: string): Promise<{ since: Date; until: Date }[]> => {
const latestInsight = await prisma.insight.findFirst({
select: { date: true, adAccount: { select: { organizations: { select: { tier: true } } } } },
Expand Down Expand Up @@ -52,3 +61,31 @@ export const splitTimeRange = (
periods.push(...splitTimeRange(maxTimePeriodDays, newDateSince, until));
return periods;
};

export const testTimeRange = (
initial: boolean,
latestInsight?: Partial<Insight> & LatestInsightResult,
): { since: Date; until: Date }[] => {
const organizations = latestInsight ? (latestInsight.adAccount?.organizations ?? []) : [];

const organizationWithHighestTier =
organizations.length > 0
? organizations.sort((a, b) => tierConstraints[b.tier].order - tierConstraints[a.tier].order)[0].tier
: undefined;

const maxRecency = tierConstraints[organizationWithHighestTier ?? Tier.Launch].maxRecency;

if (initial) {
const range = getLastXMonths();

if (!organizationWithHighestTier) {
return splitTimeRange(tierConstraints[Tier.Launch].maxRecency, range.since, range.until);
}

return splitTimeRange(maxRecency, range.since, range.until);
}
const furthestDate = addInterval(new Date(), 'day', -maxRecency);
const insightDateWithinLimit = latestInsight && latestInsight.date > furthestDate ? latestInsight.date : furthestDate;
const range = getDayPriorTillTomorrow(insightDateWithinLimit);
return splitTimeRange(maxRecency, range.since, range.until);
};
30 changes: 29 additions & 1 deletion packages/channel-utils/test/date-util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it } from 'node:test';
import * as assert from 'node:assert';
import { splitTimeRange } from '../src/date-utils';
import { Tier } from '@repo/database';
import { splitTimeRange, testTimeRange } from '../src/date-utils';

void describe('splitTimeRange tests', () => {
const maxTimePeriodDays = 90;
Expand Down Expand Up @@ -93,4 +94,31 @@ void describe('splitTimeRange tests', () => {
assert.strictEqual(ranges[2].since.toISOString(), '2021-06-30T00:00:00.000Z');
assert.strictEqual(ranges[2].until.toISOString(), '2021-09-25T00:00:00.000Z');
});

void it('returns split time range when passed with false initial', () => {
const dummyInsight = {
date: new Date('2024-06-25T18:30:00.000Z'),
adAccount: { organizations: [{ tier: Tier.Build }] },
};

const ranges = testTimeRange(false, dummyInsight);
assert.strictEqual(ranges.length, 2);
assert.strictEqual(ranges[0].since.toISOString(), '2024-06-24T00:00:00.000Z');
assert.strictEqual(ranges[0].until.toISOString(), '2024-09-21T00:00:00.000Z');
assert.strictEqual(ranges[1].since.toISOString(), '2024-09-22T00:00:00.000Z');
});

void it('returns split time range when passed with true initial', () => {
const dummyInsight = {
date: new Date('2024-06-25T18:30:00.000Z'),
adAccount: { organizations: [{ tier: Tier.Build }] },
};
const ranges = testTimeRange(true, dummyInsight);
assert.strictEqual(ranges.length, 3);
assert.strictEqual(ranges[0].since.toISOString(), '2024-01-01T00:00:00.000Z');
assert.strictEqual(ranges[0].until.toISOString(), '2024-03-30T00:00:00.000Z');
assert.strictEqual(ranges[1].since.toISOString(), '2024-03-31T00:00:00.000Z');
assert.strictEqual(ranges[1].until.toISOString(), '2024-06-28T00:00:00.000Z');
assert.strictEqual(ranges[2].since.toISOString(), '2024-06-29T00:00:00.000Z');
});
});
2 changes: 1 addition & 1 deletion packages/mappings/src/tier-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const tierConstraints: Record<
Launch: { maxUsers: 1, maxIntegrations: 1, maxRecency: 7, order: 1 },
Build: { maxUsers: 4, maxIntegrations: Infinity, maxRecency: 90, order: 2 },
Grow: { maxUsers: 10, maxIntegrations: Infinity, maxRecency: 365, order: 3 },
Scale: { maxUsers: 100, maxIntegrations: Infinity, maxRecency: Infinity, order: 4 },
Scale: { maxUsers: 100, maxIntegrations: Infinity, maxRecency: 365, order: 4 },
};

export function canAddUser(tier: Tier, userOrganizationCount: number): boolean {
Expand Down

0 comments on commit fd09815

Please sign in to comment.