diff --git a/src/app/modules/form/admin-form/__tests__/admin-form.controller.spec.ts b/src/app/modules/form/admin-form/__tests__/admin-form.controller.spec.ts index d81ecf70a6..5867d3b566 100644 --- a/src/app/modules/form/admin-form/__tests__/admin-form.controller.spec.ts +++ b/src/app/modules/form/admin-form/__tests__/admin-form.controller.spec.ts @@ -61,6 +61,8 @@ import { Status, } from 'src/types' import { + DuplicateFormBody, + EditFormFieldParams, EncryptSubmissionDto, FieldCreateDto, FieldUpdateDto, @@ -92,11 +94,7 @@ import { InvalidFileTypeError, } from '../admin-form.errors' import * as AdminFormService from '../admin-form.service' -import { - DuplicateFormBody, - EditFormFieldParams, - PermissionLevel, -} from '../admin-form.types' +import { PermissionLevel } from '../admin-form.types' jest.mock('src/app/modules/auth/auth.service') const MockAuthService = mocked(AuthService) diff --git a/src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts b/src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts index b6d40fed84..1caf142f5a 100644 --- a/src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts +++ b/src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts @@ -44,6 +44,8 @@ import { Status, } from 'src/types' import { + DuplicateFormBody, + EditFormFieldParams, FieldCreateDto, FieldUpdateDto, SettingsUpdateDto, @@ -86,11 +88,7 @@ import { updateFormSettings, updateStartPage, } from '../admin-form.service' -import { - DuplicateFormBody, - EditFormFieldParams, - OverrideProps, -} from '../admin-form.types' +import { OverrideProps } from '../admin-form.types' import * as AdminFormUtils from '../admin-form.utils' const FormModel = getFormModel(mongoose) diff --git a/src/app/modules/form/admin-form/__tests__/admin-form.utils.spec.ts b/src/app/modules/form/admin-form/__tests__/admin-form.utils.spec.ts index 6c8f30068c..6842b1a9ae 100644 --- a/src/app/modules/form/admin-form/__tests__/admin-form.utils.spec.ts +++ b/src/app/modules/form/admin-form/__tests__/admin-form.utils.spec.ts @@ -12,16 +12,13 @@ import { ResponseMode, Status, } from 'src/types' +import { DuplicateFormBody, EditFormFieldParams } from 'src/types/api' import { generateDefaultField } from 'tests/unit/backend/helpers/generate-form-data' import { ForbiddenFormError } from '../../form.errors' import { EditFieldError } from '../admin-form.errors' -import { - DuplicateFormBody, - EditFormFieldParams, - OverrideProps, -} from '../admin-form.types' +import { OverrideProps } from '../admin-form.types' import { assertHasDeletePermissions, assertHasReadPermissions, diff --git a/src/app/modules/form/admin-form/admin-form.controller.ts b/src/app/modules/form/admin-form/admin-form.controller.ts index 424dbbb43d..ee1f95aadc 100644 --- a/src/app/modules/form/admin-form/admin-form.controller.ts +++ b/src/app/modules/form/admin-form/admin-form.controller.ts @@ -26,12 +26,14 @@ import { ResponseMode, } from '../../../../types' import { + DuplicateFormBody, EncryptSubmissionDto, EndPageUpdateDto, ErrorDto, FieldCreateDto, FieldUpdateDto, FormFieldDto, + FormUpdateParams, PermissionsUpdateDto, SettingsUpdateDto, StartPageUpdateDto, @@ -75,11 +77,7 @@ import { } from './admin-form.constants' import { EditFieldError } from './admin-form.errors' import * as AdminFormService from './admin-form.service' -import { - DuplicateFormBody, - FormUpdateParams, - PermissionLevel, -} from './admin-form.types' +import { PermissionLevel } from './admin-form.types' import { mapRouteError } from './admin-form.utils' // NOTE: Refer to this for documentation: https://github.com/sideway/joi-date/blob/master/API.md diff --git a/src/app/modules/form/admin-form/admin-form.routes.ts b/src/app/modules/form/admin-form/admin-form.routes.ts index e9594cb1cb..415fb45f35 100644 --- a/src/app/modules/form/admin-form/admin-form.routes.ts +++ b/src/app/modules/form/admin-form/admin-form.routes.ts @@ -7,11 +7,11 @@ import { celebrate, Joi as BaseJoi, Segments } from 'celebrate' import { Router } from 'express' import { ResponseMode } from '../../../../types' +import { DuplicateFormBody } from '../../../../types/api' import { withUserAuthentication } from '../../auth/auth.middlewares' import * as EncryptSubmissionController from '../../submission/encrypt-submission/encrypt-submission.controller' import * as AdminFormController from './admin-form.controller' -import { DuplicateFormBody } from './admin-form.types' export const AdminFormsRouter = Router() 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 5f5ea9e6a0..eb5c64a9bf 100644 --- a/src/app/modules/form/admin-form/admin-form.service.ts +++ b/src/app/modules/form/admin-form/admin-form.service.ts @@ -24,9 +24,12 @@ import { Permission, } from '../../../../types' import { + DuplicateFormBody, + EditFormFieldParams, EndPageUpdateDto, FieldCreateDto, FieldUpdateDto, + FormUpdateParams, SettingsUpdateDto, StartPageUpdateDto, } from '../../../../types/api' @@ -62,11 +65,6 @@ import { FieldNotFoundError, InvalidFileTypeError, } from './admin-form.errors' -import { - DuplicateFormBody, - EditFormFieldParams, - FormUpdateParams, -} from './admin-form.types' import { getUpdatedFormFields, processDuplicateOverrideProps, diff --git a/src/app/modules/form/admin-form/admin-form.types.ts b/src/app/modules/form/admin-form/admin-form.types.ts index 5378a59bf5..bde8301348 100644 --- a/src/app/modules/form/admin-form/admin-form.types.ts +++ b/src/app/modules/form/admin-form/admin-form.types.ts @@ -1,6 +1,5 @@ import { Result } from 'neverthrow' -import { EditFieldActions } from '../../../../shared/constants' import { IFieldSchema, IForm, @@ -23,19 +22,6 @@ export type AssertFormFn = ( form: IPopulatedForm, ) => Result -export type DuplicateFormBody = { - title: string -} & ( - | { - responseMode: ResponseMode.Email - emails: string | string[] - } - | { - responseMode: ResponseMode.Encrypt - publicKey: string - } -) - export type OverrideProps = { endPage?: IForm['endPage'] startPage?: IForm['startPage'] @@ -46,34 +32,4 @@ export type OverrideProps = { publicKey?: string } -export type EditFormFieldParams = { - field: IFieldSchema -} & ( - | { - action: { - name: Exclude - } - } - | { - action: { - name: EditFieldActions.Reorder - position: number - } - } -) - -export type FormUpdateParams = { - editFormField?: EditFormFieldParams - authType?: IForm['authType'] - emails?: IForm['emails'] - esrvcId?: IForm['esrvcId'] - form_logics?: IForm['form_logics'] - hasCaptcha?: IForm['hasCaptcha'] - inactiveMessage?: IForm['inactiveMessage'] - permissionList?: IForm['permissionList'] - status?: IForm['status'] - title?: IForm['title'] - webhook?: IForm['webhook'] -} - export type EditFormFieldResult = Result diff --git a/src/app/modules/form/admin-form/admin-form.utils.ts b/src/app/modules/form/admin-form/admin-form.utils.ts index 57c6788265..b51d6d2dc5 100644 --- a/src/app/modules/form/admin-form/admin-form.utils.ts +++ b/src/app/modules/form/admin-form/admin-form.utils.ts @@ -9,6 +9,7 @@ import { ResponseMode, Status, } from '../../../../types' +import { DuplicateFormBody, EditFormFieldParams } from '../../../../types/api' import { createLoggerWithLabel } from '../../../config/logger' import { isPossibleEmailFieldSchema } from '../../../utils/field-validation/field-validation.guards' import { @@ -38,8 +39,6 @@ import { } from './admin-form.errors' import { AssertFormFn, - DuplicateFormBody, - EditFormFieldParams, EditFormFieldResult, OverrideProps, PermissionLevel, diff --git a/src/app/modules/form/public-form/public-form.controller.ts b/src/app/modules/form/public-form/public-form.controller.ts index 586014eac8..722d1b06f5 100644 --- a/src/app/modules/form/public-form/public-form.controller.ts +++ b/src/app/modules/form/public-form/public-form.controller.ts @@ -10,6 +10,7 @@ import { PrivateFormErrorDto, PublicFormAuthRedirectDto, PublicFormAuthValidateEsrvcIdDto, + PublicFormViewDto, } from '../../../../types/api' import { createLoggerWithLabel } from '../../../config/logger' import { isMongoError } from '../../../utils/handle-mongo-error' @@ -36,7 +37,7 @@ import { AuthTypeMismatchError, PrivateFormError } from '../form.errors' import * as FormService from '../form.service' import * as PublicFormService from './public-form.service' -import { PublicFormViewDto, RedirectParams } from './public-form.types' +import { RedirectParams } from './public-form.types' import { mapFormAuthError, mapRouteError } from './public-form.utils' const logger = createLoggerWithLabel(module) diff --git a/src/app/modules/form/public-form/public-form.types.ts b/src/app/modules/form/public-form/public-form.types.ts index 8b138e85e0..14c2f66a93 100644 --- a/src/app/modules/form/public-form/public-form.types.ts +++ b/src/app/modules/form/public-form/public-form.types.ts @@ -1,8 +1,3 @@ -import { IFieldSchema, PublicForm } from 'src/types' - -import { SpcpSession } from '../../../../types/spcp' -import { IPossiblyPrefilledField } from '../../myinfo/myinfo.types' - export type Metatags = { title: string description?: string @@ -16,16 +11,3 @@ export type RedirectParams = { // TODO(#144): Rename Id to formId after all routes have been updated. Id: string } - -// NOTE: This is needed because PublicForm inherits from IFormDocument (where form_fields has type of IFieldSchema). -// However, the form returned back to the client has form_field of two possible types -interface PossiblyPrefilledPublicForm extends Omit { - form_fields: IPossiblyPrefilledField[] | IFieldSchema[] -} - -export type PublicFormViewDto = { - form: PossiblyPrefilledPublicForm - spcpSession?: SpcpSession - isIntranetUser?: boolean - myInfoError?: true -} diff --git a/src/app/modules/myinfo/__tests__/myinfo.service.spec.ts b/src/app/modules/myinfo/__tests__/myinfo.service.spec.ts index 418ec67bac..e2d8c08a83 100644 --- a/src/app/modules/myinfo/__tests__/myinfo.service.spec.ts +++ b/src/app/modules/myinfo/__tests__/myinfo.service.spec.ts @@ -12,6 +12,7 @@ import { IHashes, IMyInfoHashSchema, IPopulatedForm, + IPossiblyPrefilledField, MyInfoAttribute, } from 'src/types' @@ -27,7 +28,7 @@ import { MyInfoMissingAccessTokenError, MyInfoParseRelayStateError, } from '../myinfo.errors' -import { IPossiblyPrefilledField, MyInfoRelayState } from '../myinfo.types' +import { MyInfoRelayState } from '../myinfo.types' import { MOCK_ACCESS_TOKEN, diff --git a/src/app/modules/myinfo/myinfo.factory.ts b/src/app/modules/myinfo/myinfo.factory.ts index d4e66be084..a62fe99902 100644 --- a/src/app/modules/myinfo/myinfo.factory.ts +++ b/src/app/modules/myinfo/myinfo.factory.ts @@ -6,6 +6,7 @@ import { IHashes, IMyInfoHashSchema, IPopulatedForm, + IPossiblyPrefilledField, } from '../../../types' import config from '../../config/config' import FeatureManager, { @@ -33,11 +34,7 @@ import { MyInfoParseRelayStateError, } from './myinfo.errors' import { MyInfoService } from './myinfo.service' -import { - IMyInfoRedirectURLArgs, - IPossiblyPrefilledField, - MyInfoParsedRelayState, -} from './myinfo.types' +import { IMyInfoRedirectURLArgs, MyInfoParsedRelayState } from './myinfo.types' interface IMyInfoFactory { createRedirectURL: ( diff --git a/src/app/modules/myinfo/myinfo.service.ts b/src/app/modules/myinfo/myinfo.service.ts index dae865966a..a0107861e1 100644 --- a/src/app/modules/myinfo/myinfo.service.ts +++ b/src/app/modules/myinfo/myinfo.service.ts @@ -16,6 +16,7 @@ import { IHashes, IMyInfoHashSchema, IPopulatedForm, + IPossiblyPrefilledField, MyInfoAttribute, } from '../../../types' import { createLoggerWithLabel } from '../../config/logger' @@ -47,7 +48,6 @@ import { import { IMyInfoRedirectURLArgs, IMyInfoServiceConfig, - IPossiblyPrefilledField, MyInfoParsedRelayState, } from './myinfo.types' import { diff --git a/src/app/modules/myinfo/myinfo.types.ts b/src/app/modules/myinfo/myinfo.types.ts index d28b697f7c..bea4ab40db 100644 --- a/src/app/modules/myinfo/myinfo.types.ts +++ b/src/app/modules/myinfo/myinfo.types.ts @@ -1,9 +1,6 @@ -import { LeanDocument } from 'mongoose' - import { AuthType, Environment, - IFieldSchema, IFormSchema, IMyInfo, MyInfoAttribute, @@ -23,10 +20,6 @@ export interface IMyInfoRedirectURLArgs { requestedAttributes: MyInfoAttribute[] } -export interface IPossiblyPrefilledField extends LeanDocument { - fieldValue?: string -} - export type MyInfoHashPromises = Partial< Record> > diff --git a/src/app/modules/myinfo/myinfo.util.ts b/src/app/modules/myinfo/myinfo.util.ts index ecb97642a0..6f3febbaee 100644 --- a/src/app/modules/myinfo/myinfo.util.ts +++ b/src/app/modules/myinfo/myinfo.util.ts @@ -12,6 +12,7 @@ import { IFormSchema, IHashes, IMyInfo, + IPossiblyPrefilledField, MapRouteError, } from '../../../types' import { createLoggerWithLabel } from '../../config/logger' @@ -39,7 +40,6 @@ import { MyInfoMissingHashError, } from './myinfo.errors' import { - IPossiblyPrefilledField, MyInfoComparePromises, MyInfoCookiePayload, MyInfoCookieState, diff --git a/src/types/api/field.ts b/src/types/api/field.ts new file mode 100644 index 0000000000..49f0acb370 --- /dev/null +++ b/src/types/api/field.ts @@ -0,0 +1,18 @@ +import { EditFieldActions } from '../../shared/constants' +import { IFieldSchema } from '../field' + +export type EditFormFieldParams = { + field: IFieldSchema +} & ( + | { + action: { + name: Exclude + } + } + | { + action: { + name: EditFieldActions.Reorder + position: number + } + } +) diff --git a/src/types/api/form.ts b/src/types/api/form.ts index d6f0b238f4..256e99d122 100644 --- a/src/types/api/form.ts +++ b/src/types/api/form.ts @@ -1,8 +1,26 @@ import { LeanDocument } from 'mongoose' import { ConditionalPick, Primitive } from 'type-fest' -import { FormField, FormFieldSchema, FormFieldWithId } from '../field' -import { EndPage, FormSettings, Permission, StartPage } from '../form' +import { + FormField, + FormFieldSchema, + FormFieldWithId, + IFieldSchema, + IPossiblyPrefilledField, +} from '../field' +import { + EndPage, + FormSettings, + IForm, + IPopulatedForm, + Permission, + PublicForm, + ResponseMode, + StartPage, +} from '../form' +import { SpcpSession } from '../spcp' + +import { EditFormFieldParams } from './field' export type SettingsUpdateDto = Partial @@ -23,3 +41,45 @@ export type PermissionsUpdateDto = Permission[] export type EndPageUpdateDto = EndPage export type StartPageUpdateDto = StartPage + +export type FormViewDto = { form: IPopulatedForm } + +export type DuplicateFormBody = { + title: string +} & ( + | { + responseMode: ResponseMode.Email + emails: string | string[] + } + | { + responseMode: ResponseMode.Encrypt + publicKey: string + } +) + +export type FormUpdateParams = { + editFormField?: EditFormFieldParams + authType?: IForm['authType'] + emails?: IForm['emails'] + esrvcId?: IForm['esrvcId'] + form_logics?: IForm['form_logics'] + hasCaptcha?: IForm['hasCaptcha'] + inactiveMessage?: IForm['inactiveMessage'] + permissionList?: IForm['permissionList'] + status?: IForm['status'] + title?: IForm['title'] + webhook?: IForm['webhook'] +} + +// NOTE: This is needed because PublicForm inherits from IFormDocument (where form_fields has type of IFieldSchema). +// However, the form returned back to the client has form_field of two possible types +interface PossiblyPrefilledPublicForm extends Omit { + form_fields: IPossiblyPrefilledField[] | IFieldSchema[] +} + +export type PublicFormViewDto = { + form: PossiblyPrefilledPublicForm + spcpSession?: SpcpSession + isIntranetUser?: boolean + myInfoError?: true +} diff --git a/src/types/api/index.ts b/src/types/api/index.ts index 80e863b7f3..483fa1912e 100644 --- a/src/types/api/index.ts +++ b/src/types/api/index.ts @@ -1,5 +1,6 @@ export * from './core' export * from './auth' +export * from './field' export * from './form' export * from './billing' export * from './examples' diff --git a/src/types/field/index.ts b/src/types/field/index.ts index 1a32232cfa..52df4bc36f 100644 --- a/src/types/field/index.ts +++ b/src/types/field/index.ts @@ -30,6 +30,7 @@ export * from './homeNoField' export * from './imageField' export * from './longTextField' export * from './mobileField' +export * from './myinfoField' export * from './nricField' export * from './numberField' export * from './radioField' diff --git a/src/types/field/myinfoField.ts b/src/types/field/myinfoField.ts new file mode 100644 index 0000000000..ae09575b47 --- /dev/null +++ b/src/types/field/myinfoField.ts @@ -0,0 +1,7 @@ +import { LeanDocument } from 'mongoose' + +import { IFieldSchema } from '../field' + +export interface IPossiblyPrefilledField extends LeanDocument { + fieldValue?: string +}