Skip to content

Commit

Permalink
Refactor: split account balance num / percent
Browse files Browse the repository at this point in the history
  • Loading branch information
luixo committed Nov 8, 2022
1 parent 351d1bf commit f3c0ad1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,29 @@ export class RuleDeserializerService {
return {
type: 'ACCT_BAL_PCT',
contract: rule.affected_account_id,
from: rule.comparator_range.from,
to: rule.comparator_range.to,
from:
rule.comparator_range.from === null
? undefined
: Number(rule.comparator_range.from),
to:
rule.comparator_range.to === null
? undefined
: Number(rule.comparator_range.to),
};
}

private toAcctBalNumDto(rule: AcctBalMatchingRule): Alerts.AcctBalNumRule {
return {
type: 'ACCT_BAL_NUM',
contract: rule.affected_account_id,
from: rule.comparator_range.from,
to: rule.comparator_range.to,
from:
rule.comparator_range.from === null
? undefined
: rule.comparator_range.from,
to:
rule.comparator_range.to === null
? undefined
: rule.comparator_range.to,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('RuleSerializerService', () => {
type: 'ACCT_BAL_NUM' as const,
contract: 'pagoda.near',
from: '0',
to: null,
to: undefined,
},
expected: {
rule: 'STATE_CHANGE_ACCOUNT_BALANCE',
Expand All @@ -92,7 +92,7 @@ describe('RuleSerializerService', () => {
type: 'ACCT_BAL_NUM' as const,
contract: 'pagoda.near',
to: '330',
from: null,
from: undefined,
},
expected: {
rule: 'STATE_CHANGE_ACCOUNT_BALANCE',
Expand Down Expand Up @@ -130,8 +130,8 @@ describe('RuleSerializerService', () => {
service.toAcctBalJson({
type: 'ACCT_BAL_PCT',
contract: 'pagoda.near',
from: '0',
to: null,
from: 0,
to: undefined,
}),
).toStrictEqual({
rule: 'STATE_CHANGE_ACCOUNT_BALANCE',
Expand All @@ -149,8 +149,8 @@ describe('RuleSerializerService', () => {
service.toAcctBalJson({
type: 'ACCT_BAL_PCT',
contract: 'pagoda.near',
from: null,
to: null,
from: undefined,
to: undefined,
});
}).toThrow('Invalid range');
});
Expand All @@ -160,8 +160,8 @@ describe('RuleSerializerService', () => {
service.toAcctBalJson({
type: 'ACCT_BAL_PCT',
contract: 'pagoda.near',
from: '3',
to: '0',
from: 3,
to: 0,
});
}).toThrow('Invalid range');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export class RuleSerializerService {
affected_account_id: rule.contract,
comparator_kind: this.ruleTypeToComparatorKind(rule.type),
comparator_range: {
from: rule.from,
to: rule.to,
from: rule.from === undefined ? null : rule.from.toString(),
to: rule.to === undefined ? null : rule.to.toString(),
},
};
}
Expand Down
8 changes: 4 additions & 4 deletions common/types/alerts/alerts.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export type EventRule = {
export type AcctBalPctRule = {
type: 'ACCT_BAL_PCT';
contract: string;
from: string | null;
to: string | null;
from?: number;
to?: number;
};
export type AcctBalNumRule = {
type: 'ACCT_BAL_NUM';
contract: string;
from: string | null;
to: string | null;
from?: string;
to?: string;
};

export type Rule =
Expand Down
6 changes: 3 additions & 3 deletions frontend/pages/alerts/edit-alert/[alertId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,10 @@ function AlertSettings({ alert }: { alert: Alert }) {
return null;
}

function returnAmountComparator(from: string | null, to: string | null) {
function returnAmountComparator<T extends string | number>(from?: T, to?: T) {
if (from === to) return amountComparators.EQ;
if (from === null) return amountComparators.LTE;
if (to === null) return amountComparators.GTE;
if (from === undefined) return amountComparators.LTE;
if (to === undefined) return amountComparators.GTE;
return amountComparators.RANGE;
}

Expand Down
12 changes: 6 additions & 6 deletions frontend/pages/alerts/new-alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ interface FormData {
to: string;
};
acctBalPctRule?: {
from: string;
to: string;
from: number;
to: number;
};
eventRule?: {
standard: string;
Expand Down Expand Up @@ -426,7 +426,7 @@ const NewAlert: NextPageWithLayout = () => {
form.clearErrors('acctBalPctRule.to');
}}
{...form.register('acctBalPctRule.from', {
setValueAs: (value) => sanitizeNumber(value),
valueAsNumber: true,
required: 'Please enter a percentage',
validate: {
maxValue: (value) => Number(value) <= 100 || 'Must be 100 or less',
Expand Down Expand Up @@ -650,7 +650,7 @@ function returnAcctBalNumBody(comparator: Alerts.Comparator, { from, to }: { fro
return returnAcctBalBody(comparator, { from, to });
}

function returnAcctBalBody(comparator: Alerts.Comparator, { from, to }: { from: string; to: string }) {
function returnAcctBalBody<T extends string | number>(comparator: Alerts.Comparator, { from, to }: { from: T; to: T }) {
switch (comparator) {
case 'EQ':
return {
Expand All @@ -660,11 +660,11 @@ function returnAcctBalBody(comparator: Alerts.Comparator, { from, to }: { from:
case 'GTE':
return {
from,
to: null,
to: undefined,
};
case 'LTE':
return {
from: null,
from: undefined,
to: from,
};
case 'RANGE':
Expand Down

0 comments on commit f3c0ad1

Please sign in to comment.