Skip to content

Commit

Permalink
ability to check allowed value
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmeirlevy committed Dec 21, 2023
1 parent dba78f7 commit ad6bc94
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
26 changes: 17 additions & 9 deletions apps/api/controllers/account-fields.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class AccountFieldsController implements BaseController {
}

@BackendMethod({ allowed: true })
async getFieldState(accountId: string, fieldId: string): Promise<{ isAllowed: boolean, state?: FieldState }> {
async getFieldState(accountId: string, fieldId: string, allowedValue?: number | string): Promise<{ isAllowed: boolean, state?: FieldState }> {
const accountPlanRepo = remult.repo(AccountPlan);
const accountPlan = await accountPlanRepo.findFirst({ accountId });

Expand All @@ -40,15 +40,23 @@ export class AccountFieldsController implements BaseController {

const state = accountPlan.state[fieldId];

let isAllowed = false;

switch (state.kind) {
case FieldKind.Boolean:
isAllowed = !!state.currentValue;
break;
case FieldKind.Number:
const diff = Number(state.targetLimit) - Number(state.currentValue);
isAllowed = diff > 0 && (!allowedValue || diff >= Number(allowedValue))
break;
case FieldKind.String:
isAllowed = typeof allowedValue === 'undefined' ? true : state.currentValue === allowedValue;
break
}

return {
isAllowed: state && (
state.kind === FieldKind.Boolean ?
!!state.currentValue :
(state.kind === FieldKind.Number ?
(state.currentValue as number) < (state.targetLimit as number) :
true
)
),
isAllowed,
state
};
}
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prici/sdk",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"main": "index.ts",
"homepage": "https://github.com/prici-io/prici",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class AccountFieldsController {
}

@BackendMethod({ allowed: true })
async getFieldState(accountId: string, fieldId: string): Promise<{isAllowed: boolean, state?: FieldState}> {
async getFieldState(accountId: string, fieldId: string, allowedValue?: number | string): Promise<{isAllowed: boolean, state?: FieldState}> {
return {
isAllowed: true,
state: {
Expand Down
2 changes: 1 addition & 1 deletion libs/shared-remult/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prici/shared-remult",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"main": "index.ts",
"homepage": "https://github.com/prici-io/prici",
Expand Down

0 comments on commit ad6bc94

Please sign in to comment.