Skip to content

Commit

Permalink
fix(generate-alerts.ts): import missing types and add missing type an…
Browse files Browse the repository at this point in the history
…notation for TransactionRecordType

fix(alert.service.ts): add error handling when checking alert and log error message and definition ID

fix(data-analytics.service.ts): fix formatting and indentation for better readability
feat(data-analytics.service.ts): add support for evaluating payment unexpected based on customer expected amount
feat(data-analytics.service.ts): add support for evaluating dormant accounts based on transaction history
feat(data-analytics.service.ts): add support for evaluating customers' transaction type based on various criteria

feat(types.ts): add support for new transaction record type and generic async function type
feat(types.ts): add new options for TCustomersTransactionTypeOptions to support more flexible customer transaction filtering
feat(types.ts): add new options for TDormantAccountOption to support evaluating dormant accounts
feat(types.ts): add new evaluate functions for evaluating transactions against dynamic rules and dormant accounts
  • Loading branch information
liorzblrn authored and liorzam committed Mar 5, 2024
1 parent 6f00953 commit 00aa41e
Show file tree
Hide file tree
Showing 4 changed files with 413 additions and 40 deletions.
167 changes: 165 additions & 2 deletions services/workflows-service/scripts/alerts/generate-alerts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { InlineRule, TransactionsAgainstDynamicRulesType } from '../../src/data-analytics/types';
import {
InlineRule,
TCustomersTransactionTypeOptions,
TransactionsAgainstDynamicRulesType,
} from '../../src/data-analytics/types';
import {
AlertSeverity,
AlertState,
Expand All @@ -9,6 +13,7 @@ import {
Prisma,
PrismaClient,
Project,
TransactionRecordType,
} from '@prisma/client';
import { faker } from '@faker-js/faker';
import { AggregateType } from '../../src/data-analytics/consts';
Expand Down Expand Up @@ -121,7 +126,165 @@ export const getRuleDefinitions = () => {
} as TransactionsAgainstDynamicRulesType,
} as const satisfies InlineRule;

const rules = [_PAY_HCA_CC, _PAY_HCA_APM, _STRUC_CC, _STRUC_APM];
const _HCAI_CC = {
id: '_HCAI_CC',
fnName: 'evaluateTransactionsAgainstDynamicRules',
subjects: ['businessId', 'counterpartyOriginatorId'],
options: {
groupByBusiness: true,
groupByCounterparty: true,

havingAggregate: AggregateType.SUM,

direction: 'inbound',
excludedCounterpartyIds: ['9999999999999999', '999999******9999'],

paymentMethods: [PaymentMethod.credit_card],
excludePaymentMethods: false,

timeAmount: 7,
timeUnit: 'days',

amountThreshold: 3000,
} as TransactionsAgainstDynamicRulesType,
} as const satisfies InlineRule;

const _HACI_APM = {
id: '_HACI_APM',
fnName: 'evaluateTransactionsAgainstDynamicRules',
subjects: ['businessId', 'counterpartyOriginatorId'],
options: {
groupByBusiness: true,
groupByCounterparty: true,
havingAggregate: AggregateType.SUM,

direction: 'inbound',
excludedCounterpartyIds: ['9999999999999999', '999999******9999'],

paymentMethods: [PaymentMethod.credit_card],
excludePaymentMethods: true,

timeAmount: 7,
timeUnit: 'days',

amountThreshold: 3000,
} as TransactionsAgainstDynamicRulesType,
} as const satisfies InlineRule;

const _HVIC_CC = {
id: 'HVIC_CC',
fnName: 'evaluateTransactionsAgainstDynamicRules',
subjects: ['businessId', 'counterpartyOriginatorId'],
options: {
groupByBusiness: true,
havingAggregate: AggregateType.COUNT,

direction: 'inbound',
excludedCounterpartyIds: ['9999999999999999', '999999******9999'],

paymentMethods: [PaymentMethod.credit_card],
excludePaymentMethods: false,

timeAmount: 7,
timeUnit: 'days',

amountThreshold: 2,
} as TransactionsAgainstDynamicRulesType,
} as const satisfies InlineRule;

const _HVIC_APM = {
id: 'HVIC_CC',
fnName: 'evaluateTransactionsAgainstDynamicRules',
subjects: ['businessId', 'counterpartyOriginatorId'],
options: {
groupByBusiness: true,
havingAggregate: AggregateType.COUNT,

direction: 'inbound',
excludedCounterpartyIds: ['9999999999999999', '999999******9999'],

paymentMethods: [PaymentMethod.credit_card],
excludePaymentMethods: true,

timeAmount: 7,
timeUnit: 'days',

amountThreshold: 2,
} as TransactionsAgainstDynamicRulesType,
} as const satisfies InlineRule;

const _CHVC_C = {
id: 'CHVC_C',
fnName: 'evaluateCustomersTransactionType',
subjects: ['businessId'],
options: {
transactionType: [TransactionRecordType.chargeback],
threshold: 14,
timeAmount: 7,
timeUnit: 'days',
isPerBrand: false,
havingAggregate: AggregateType.COUNT,
} as TCustomersTransactionTypeOptions,
} as const satisfies InlineRule;

const _SHCAC_C = {
id: 'HVIC_CC',
fnName: 'evaluateCustomersTransactionType',
subjects: ['businessId'],
options: {
transactionType: [TransactionRecordType.chargeback],
threshold: 5_000,
timeAmount: 7,
timeUnit: 'days',
isPerBrand: false,
havingAggregate: AggregateType.SUM,
} as TCustomersTransactionTypeOptions,
} as const satisfies InlineRule;

const _CHCR_C = {
id: 'CHCR_C',
fnName: 'evaluateCustomersTransactionType',
subjects: ['businessId'],
options: {
transactionType: [TransactionRecordType.refund],
paymentMethods: [PaymentMethod.credit_card],
threshold: 14,
timeAmount: 7,
timeUnit: 'days',
isPerBrand: false,
havingAggregate: AggregateType.COUNT,
} as TCustomersTransactionTypeOptions,
} as const satisfies InlineRule;

const _SHCAR_C = {
id: 'SHCAR_C',
fnName: 'evaluateCustomersTransactionType',
subjects: ['businessId'],
options: {
transactionType: [TransactionRecordType.refund],
paymentMethods: [PaymentMethod.credit_card],
threshold: 5_000,
timeAmount: 7,
timeUnit: 'days',
isPerBrand: false,
havingAggregate: AggregateType.SUM,
} as TCustomersTransactionTypeOptions,
} as const satisfies InlineRule;

const rules = [
_PAY_HCA_CC,
_PAY_HCA_APM,
_STRUC_CC,
_STRUC_APM,
_HCAI_CC,
_HACI_APM,
_HVIC_CC,
_HVIC_APM,
_CHVC_C,
_SHCAC_C,
_CHCR_C,
_SHCAR_C,
];

const mergedRules: Record<string, InlineRule> = {};

Expand Down
13 changes: 10 additions & 3 deletions services/workflows-service/src/alert/alert.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,17 @@ export class AlertService {
const alertDefinitions = await this.getAllAlertDefinitions();

for (const definition of alertDefinitions) {
const triggered = await this.checkAlert(definition);
try {
const triggered = await this.checkAlert(definition);

if (triggered) {
this.logger.log(`Alert triggered for alert definition '${definition.id}'`);
if (triggered) {
this.logger.log(`Alert triggered for alert definition '${definition.id}'`);
}
} catch (error) {
this.logger.error('Error while checking alert', {
error,
definitionId: definition.id,
});
}
}
}
Expand Down
Loading

0 comments on commit 00aa41e

Please sign in to comment.