Skip to content

Commit 7c9588a

Browse files
Merge pull request #753 from Tusharmahajan12/new_asp_apr24
Referral feedback changes
2 parents 2dcf9d9 + f830df1 commit 7c9588a

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/adapters/postgres/cohort-adapter.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
LessThanOrEqual,
1717
Equal,
1818
Not,
19+
Between,
1920
} from 'typeorm';
2021
import { Cohort } from 'src/cohort/entities/cohort.entity';
2122
import { Fields } from 'src/fields/entities/fields.entity';
@@ -1380,7 +1381,7 @@ export class PostgresCohortService {
13801381
);
13811382

13821383
// Combine the arrays
1383-
const allowedKeys = ['userId', ...cohortAllKeys, ...customFieldsKeys];
1384+
const allowedKeys = ['userId', 'start_year', ...cohortAllKeys, ...customFieldsKeys];
13841385

13851386
const whereClause = {};
13861387
const searchCustomFields = {};
@@ -1565,6 +1566,20 @@ export class PostgresCohortService {
15651566
}
15661567
whereClause[key] = normalizedDate;
15671568
}
1569+
} else if (key === 'start_year') {
1570+
const year = Number(value);
1571+
if (!Number.isInteger(year) || year <= 0) {
1572+
return APIResponse.error(
1573+
response,
1574+
apiId,
1575+
`Invalid start_year value: ${value}. Must be a valid positive integer year.`,
1576+
'Invalid filter value',
1577+
HttpStatus.BAD_REQUEST
1578+
);
1579+
}
1580+
const startOfYear = `${year}-01-01T00:00:00.000Z`;
1581+
const endOfYear = `${year}-12-31T23:59:59.999Z`;
1582+
whereClause['cohort_startDate'] = Between(startOfYear, endOfYear);
15681583
} else if (cohortAllKeys.includes(key)) {
15691584
whereClause[key] = value;
15701585
}

src/cohort/dto/cohort-search.dto.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ export class filtersProperty {
164164
@IsDateString()
165165
cohort_endDate?: string;
166166

167+
@ApiPropertyOptional({
168+
type: Number,
169+
description: 'Filter cohorts by the year of cohort_startDate',
170+
example: 2026,
171+
})
172+
@Expose()
173+
@IsOptional()
174+
@IsNumber()
175+
start_year?: number;
176+
167177
//customFieldsName
168178
@ApiProperty({
169179
type: Object,

src/referrals/referrals.service.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,29 @@ export class ReferralsService {
133133
const [rows, total] = await query.getManyAndCount();
134134

135135
const signupCounts = rows.length
136-
? await this.dataSource.query<{ referralEntityId: string; signups: number }[]>(
137-
`SELECT ua."referralEntityId", COUNT(DISTINCT ua."userId")::int AS signups
136+
? await this.dataSource.query<{ referralEntityId: string; status: string; count: number }[]>(
137+
`SELECT ua."referralEntityId", u."status", COUNT(DISTINCT ua."userId")::int AS count
138138
FROM "UserAttribution" ua
139139
JOIN "Users" u ON u."userId" = ua."userId"
140140
WHERE ua."referralEntityId" = ANY($1)
141-
AND u."status" = 'active'
142-
GROUP BY ua."referralEntityId"`,
141+
AND u."status" IN ('active', 'inactive')
142+
GROUP BY ua."referralEntityId", u."status"`,
143143
[rows.map((r) => r.id)],
144144
)
145145
: [];
146146

147-
const signupMap = new Map(signupCounts.map((s) => [s.referralEntityId, s.signups]));
147+
const activeMap = new Map<string, number>();
148+
const inactiveMap = new Map<string, number>();
149+
for (const row of signupCounts) {
150+
if (row.status === 'active') activeMap.set(row.referralEntityId, row.count);
151+
else if (row.status === 'inactive') inactiveMap.set(row.referralEntityId, row.count);
152+
}
148153

149154
return {
150155
data: rows.map((r) => ({
151156
...this.normalizeReferral({ ...r, referLink: buildReferLink(r.slug) }),
152-
signups: signupMap.get(r.id) ?? 0,
157+
signups: activeMap.get(r.id) ?? 0,
158+
inactive_signups: inactiveMap.get(r.id) ?? 0,
153159
})),
154160
total,
155161
limit,

0 commit comments

Comments
 (0)