diff --git a/backend/src/modules/alerts/serde/rule-deserializer/rule-deserializer.service.ts b/backend/src/modules/alerts/serde/rule-deserializer/rule-deserializer.service.ts index 6382e9a4d..7120d4f12 100644 --- a/backend/src/modules/alerts/serde/rule-deserializer/rule-deserializer.service.ts +++ b/backend/src/modules/alerts/serde/rule-deserializer/rule-deserializer.service.ts @@ -62,8 +62,14 @@ 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), }; } @@ -71,8 +77,14 @@ export class RuleDeserializerService { 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, }; } } diff --git a/backend/src/modules/alerts/serde/rule-serializer/rule-serializer.service.spec.ts b/backend/src/modules/alerts/serde/rule-serializer/rule-serializer.service.spec.ts index 15ff6970f..ad3a5b5a3 100644 --- a/backend/src/modules/alerts/serde/rule-serializer/rule-serializer.service.spec.ts +++ b/backend/src/modules/alerts/serde/rule-serializer/rule-serializer.service.spec.ts @@ -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', @@ -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', @@ -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', @@ -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'); }); @@ -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'); }); diff --git a/backend/src/modules/alerts/serde/rule-serializer/rule-serializer.service.ts b/backend/src/modules/alerts/serde/rule-serializer/rule-serializer.service.ts index b800d31d7..123b7b0fb 100644 --- a/backend/src/modules/alerts/serde/rule-serializer/rule-serializer.service.ts +++ b/backend/src/modules/alerts/serde/rule-serializer/rule-serializer.service.ts @@ -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(), }, }; } diff --git a/common/types/alerts/alerts.schema.ts b/common/types/alerts/alerts.schema.ts index df23a9a83..f1d7302bb 100644 --- a/common/types/alerts/alerts.schema.ts +++ b/common/types/alerts/alerts.schema.ts @@ -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 = diff --git a/frontend/pages/alerts/edit-alert/[alertId].tsx b/frontend/pages/alerts/edit-alert/[alertId].tsx index f2f5c0f61..92df31005 100644 --- a/frontend/pages/alerts/edit-alert/[alertId].tsx +++ b/frontend/pages/alerts/edit-alert/[alertId].tsx @@ -490,10 +490,10 @@ function AlertSettings({ alert }: { alert: Alert }) { return null; } -function returnAmountComparator(from: string | null, to: string | null) { +function returnAmountComparator(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; } diff --git a/frontend/pages/alerts/new-alert.tsx b/frontend/pages/alerts/new-alert.tsx index a7f6c132b..4c37ffecc 100644 --- a/frontend/pages/alerts/new-alert.tsx +++ b/frontend/pages/alerts/new-alert.tsx @@ -49,8 +49,8 @@ interface FormData { to: string; }; acctBalPctRule?: { - from: string; - to: string; + from: number; + to: number; }; eventRule?: { standard: string; @@ -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', @@ -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(comparator: Alerts.Comparator, { from, to }: { from: T; to: T }) { switch (comparator) { case 'EQ': return { @@ -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':