Skip to content

Commit 0714d9b

Browse files
authored
fix: dormant options changes (#2387)
1 parent 48ac2e7 commit 0714d9b

File tree

4 files changed

+21
-66
lines changed

4 files changed

+21
-66
lines changed

services/workflows-service/scripts/alerts/generate-alerts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ export const ALERT_DEFINITIONS = {
476476
id: 'DORMANT',
477477
fnName: 'evaluateDormantAccount',
478478
subjects: ['counterpartyId'],
479+
options: {
480+
timeAmount: 180,
481+
timeUnit: TIME_UNITS.days,
482+
},
479483
},
480484
},
481485
} as const satisfies Record<string, Parameters<typeof getAlertDefinitionCreateData>[0]>;

services/workflows-service/src/data-analytics/data-analytics.service.ts

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
HighTransactionTypePercentage,
88
TransactionLimitHistoricAverageOptions,
99
TPeerGroupTransactionAverageOptions,
10+
TDormantAccountOptions,
1011
} from './types';
1112
import { AggregateType, TIME_UNITS } from './consts';
1213
import { Prisma, TransactionDirection } from '@prisma/client';
@@ -49,6 +50,7 @@ export class DataAnalyticsService {
4950

5051
case 'evaluateDormantAccount':
5152
return await this[inlineRule.fnName]({
53+
...inlineRule.options,
5254
projectId,
5355
});
5456
}
@@ -220,53 +222,6 @@ export class DataAnalyticsService {
220222
`);
221223
}
222224

223-
// async evaluateTransactionLimitHistoricAverageInbound({
224-
// projectId,
225-
// transactionDirection,
226-
// paymentMethod,
227-
// minimumCount,
228-
// minimumTransactionAmount,
229-
// transactionFactor,
230-
// }: TransactionLimitHistoricAverageOptions) {
231-
// if (!['=', '!='].includes(paymentMethod.operator)) {
232-
// throw new Error('Invalid operator');
233-
// }
234-
235-
// return await this._executeQuery<Array<{ counterpartyId: string }>>(Prisma.sql`
236-
// WITH transactionsData AS (
237-
// SELECT
238-
// "counterpartyBeneficiaryId" ,
239-
// count(*) AS count,
240-
// avg("transactionBaseAmount") AS avg
241-
// FROM
242-
// "TransactionRecord" tr
243-
// WHERE
244-
// "transactionDirection"::text = ${transactionDirection}
245-
// AND "paymentMethod"::text ${Prisma.raw(paymentMethod.operator)} ${paymentMethod.value}
246-
// AND "projectId" = ${projectId}
247-
// GROUP BY
248-
// "counterpartyBeneficiaryId"
249-
// HAVING COUNT(*) > ${minimumCount}
250-
// )
251-
// SELECT
252-
// tr."counterpartyBeneficiaryId" as "counterpartyId"
253-
// FROM
254-
// "TransactionRecord" tr
255-
// JOIN transactionsData td ON
256-
// tr."counterpartyBeneficiaryId" = td."counterpartyBeneficiaryId"
257-
// WHERE
258-
// tr."transactionDirection"::text = ${transactionDirection}
259-
// AND "projectId" = ${projectId}
260-
// AND "paymentMethod"::text ${Prisma.raw(paymentMethod.operator)} ${paymentMethod.value}
261-
// AND "transactionBaseAmount" > ${minimumTransactionAmount}
262-
// AND "transactionBaseAmount" > (
263-
// ${transactionFactor} * avg
264-
// )
265-
// GROUP BY
266-
// tr."counterpartyBeneficiaryId"
267-
// `);
268-
// }
269-
270225
async evaluatePaymentUnexpected({
271226
projectId,
272227
factor = 2,
@@ -291,30 +246,19 @@ export class DataAnalyticsService {
291246
return results;
292247
}
293248

294-
async evaluateDormantAccount({ projectId }: { projectId: string }) {
295-
// const query: Prisma.Sql = Prisma.sql`
296-
// SELECT
297-
// tr."counterpartyBeneficiaryId" as "counterpartyId",
298-
// COUNT(
299-
// CASE WHEN tr."transactionDate" >= CURRENT_DATE - INTERVAL '180 days' THEN
300-
// tr."id"
301-
// END) AS "totalTransactionWithinSixMonths",
302-
// COUNT(tr."id") AS "totalTransactionAllTime"
303-
// FROM
304-
// "TransactionRecord" AS "tr"
305-
// WHERE
306-
// tr."projectId" = '${projectId}'
307-
// AND tr."counterpartyBeneficiaryId" IS NOT NULL
308-
// GROUP BY
309-
// tr."counterpartyBeneficiaryId";
310-
// `;
249+
async evaluateDormantAccount({ projectId, timeAmount, timeUnit }: TDormantAccountOptions) {
250+
if (!projectId) {
251+
throw new Error('projectId is required');
252+
}
311253

312254
const query: Prisma.Sql = Prisma.sql`
313255
WITH transactions AS (
314256
SELECT
315257
tr."counterpartyBeneficiaryId" AS "counterpartyId",
316258
count(
317-
CASE WHEN tr."transactionDate" >= CURRENT_DATE - INTERVAL '180 days' THEN
259+
CASE WHEN tr."transactionDate" >= CURRENT_DATE - INTERVAL '${Prisma.raw(
260+
`${timeAmount} ${timeUnit}`,
261+
)}' THEN
318262
tr."id"
319263
END) AS "totalTransactionWithinSixMonths",
320264
count(tr."id") AS "totalTransactionAllTime"

services/workflows-service/src/data-analytics/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type InlineRule = {
2828
}
2929
| {
3030
fnName: 'evaluateDormantAccount';
31+
options: Omit<TDormantAccountOptions, 'projectId'>;
3132
}
3233
);
3334

@@ -94,3 +95,9 @@ export type TPeerGroupTransactionAverageOptions = TransactionLimitHistoricAverag
9495
timeUnit?: TimeUnit;
9596
timeAmount?: number;
9697
};
98+
99+
export type TDormantAccountOptions = {
100+
projectId: TProjectId;
101+
timeAmount: number;
102+
timeUnit: TimeUnit;
103+
};

0 commit comments

Comments
 (0)