From 8b5d20c5df364df3daed5e9e41fba411be333248 Mon Sep 17 00:00:00 2001 From: seaerchin Date: Mon, 12 Jul 2021 12:17:31 +0800 Subject: [PATCH] refactor(admin-form.service): splits field specific logic from checking field type --- .../form/admin-form/admin-form.service.ts | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/app/modules/form/admin-form/admin-form.service.ts b/src/app/modules/form/admin-form/admin-form.service.ts index ab584ce26d..0f12f68982 100644 --- a/src/app/modules/form/admin-form/admin-form.service.ts +++ b/src/app/modules/form/admin-form/admin-form.service.ts @@ -10,6 +10,7 @@ import { VALID_UPLOAD_FILE_TYPES, } from '../../../../shared/constants' import { + BasicField, FormLogoState, FormMetaView, FormSettings, @@ -19,6 +20,7 @@ import { IFormDocument, IFormSchema, ILogicSchema, + IMobileField, IPopulatedForm, IUserSchema, LogicDto, @@ -1091,9 +1093,35 @@ export const shouldUpdateFormField = ( ): ResultAsync< IPopulatedForm, PossibleDatabaseError | SmsLimitExceededError +> => { + switch (formField.fieldType) { + case BasicField.Mobile: { + // NOTE: This casting is safe and we require this casting because we do not declare explicit discriminants on the extended type + return isMobileFieldUpdateAllowed(formField as IMobileField, form) + } + default: + return okAsync(form) + } +} + +/** + * Checks whether the mobile update should be allowed based on whether the mobile field is verified + * and (if verified), the admin's free sms counts + * @param mobileField The mobile field to check + * @param form The form that the field belongs to + * @returns ok(form) if the update is valid + * @returns err(PossibleDatabaseError) if an error occurred while retrieving counts from database + * @returns err(SmsLimitExceededError) if the admin of the form has exceeded their free sms quota + */ +const isMobileFieldUpdateAllowed = ( + mobileField: IMobileField, + form: IPopulatedForm, +): ResultAsync< + IPopulatedForm, + PossibleDatabaseError | SmsLimitExceededError > => { // Field can always update if it's not a verifiable field or if the form has been onboarded - if (!isVerifiableMobileField(formField) || isOnboardedForm(form)) { + if (!isVerifiableMobileField(mobileField) || isOnboardedForm(form)) { return okAsync(form) }