Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(formApiClientFactory): rearrange types #2061

Merged
merged 5 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ import {
Status,
} from 'src/types'
import {
DuplicateFormBody,
EditFormFieldParams,
EncryptSubmissionDto,
FieldCreateDto,
FieldUpdateDto,
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import {
Status,
} from 'src/types'
import {
DuplicateFormBody,
EditFormFieldParams,
FieldCreateDto,
FieldUpdateDto,
SettingsUpdateDto,
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 3 additions & 5 deletions src/app/modules/form/admin-form/admin-form.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import {
ResponseMode,
} from '../../../../types'
import {
DuplicateFormBody,
EncryptSubmissionDto,
EndPageUpdateDto,
ErrorDto,
FieldCreateDto,
FieldUpdateDto,
FormFieldDto,
FormUpdateParams,
PermissionsUpdateDto,
SettingsUpdateDto,
StartPageUpdateDto,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/form/admin-form/admin-form.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
8 changes: 3 additions & 5 deletions src/app/modules/form/admin-form/admin-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import {
Permission,
} from '../../../../types'
import {
DuplicateFormBody,
EditFormFieldParams,
EndPageUpdateDto,
FieldCreateDto,
FieldUpdateDto,
FormUpdateParams,
SettingsUpdateDto,
StartPageUpdateDto,
} from '../../../../types/api'
Expand Down Expand Up @@ -62,11 +65,6 @@ import {
FieldNotFoundError,
InvalidFileTypeError,
} from './admin-form.errors'
import {
DuplicateFormBody,
EditFormFieldParams,
FormUpdateParams,
} from './admin-form.types'
import {
getUpdatedFormFields,
processDuplicateOverrideProps,
Expand Down
44 changes: 0 additions & 44 deletions src/app/modules/form/admin-form/admin-form.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Result } from 'neverthrow'

import { EditFieldActions } from '../../../../shared/constants'
import {
IFieldSchema,
IForm,
Expand All @@ -23,19 +22,6 @@ export type AssertFormFn = (
form: IPopulatedForm,
) => Result<true, ForbiddenFormError>

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']
Expand All @@ -46,34 +32,4 @@ export type OverrideProps = {
publicKey?: string
}

export type EditFormFieldParams = {
field: IFieldSchema
} & (
| {
action: {
name: Exclude<EditFieldActions, EditFieldActions.Reorder>
}
}
| {
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<IFieldSchema[], EditFieldError>
3 changes: 1 addition & 2 deletions src/app/modules/form/admin-form/admin-form.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -38,8 +39,6 @@ import {
} from './admin-form.errors'
import {
AssertFormFn,
DuplicateFormBody,
EditFormFieldParams,
EditFormFieldResult,
OverrideProps,
PermissionLevel,
Expand Down
3 changes: 2 additions & 1 deletion src/app/modules/form/public-form/public-form.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PrivateFormErrorDto,
PublicFormAuthRedirectDto,
PublicFormAuthValidateEsrvcIdDto,
PublicFormViewDto,
} from '../../../../types/api'
import { createLoggerWithLabel } from '../../../config/logger'
import { isMongoError } from '../../../utils/handle-mongo-error'
Expand All @@ -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)
Expand Down
18 changes: 0 additions & 18 deletions src/app/modules/form/public-form/public-form.types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<PublicForm, 'form_fields'> {
form_fields: IPossiblyPrefilledField[] | IFieldSchema[]
}

export type PublicFormViewDto = {
form: PossiblyPrefilledPublicForm
spcpSession?: SpcpSession
isIntranetUser?: boolean
myInfoError?: true
}
3 changes: 2 additions & 1 deletion src/app/modules/myinfo/__tests__/myinfo.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IHashes,
IMyInfoHashSchema,
IPopulatedForm,
IPossiblyPrefilledField,
MyInfoAttribute,
} from 'src/types'

Expand All @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions src/app/modules/myinfo/myinfo.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IHashes,
IMyInfoHashSchema,
IPopulatedForm,
IPossiblyPrefilledField,
} from '../../../types'
import config from '../../config/config'
import FeatureManager, {
Expand Down Expand Up @@ -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: (
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/myinfo/myinfo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IHashes,
IMyInfoHashSchema,
IPopulatedForm,
IPossiblyPrefilledField,
MyInfoAttribute,
} from '../../../types'
import { createLoggerWithLabel } from '../../config/logger'
Expand Down Expand Up @@ -47,7 +48,6 @@ import {
import {
IMyInfoRedirectURLArgs,
IMyInfoServiceConfig,
IPossiblyPrefilledField,
MyInfoParsedRelayState,
} from './myinfo.types'
import {
Expand Down
7 changes: 0 additions & 7 deletions src/app/modules/myinfo/myinfo.types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { LeanDocument } from 'mongoose'

import {
AuthType,
Environment,
IFieldSchema,
IFormSchema,
IMyInfo,
MyInfoAttribute,
Expand All @@ -23,10 +20,6 @@ export interface IMyInfoRedirectURLArgs {
requestedAttributes: MyInfoAttribute[]
}

export interface IPossiblyPrefilledField extends LeanDocument<IFieldSchema> {
fieldValue?: string
}

export type MyInfoHashPromises = Partial<
Record<MyInfoAttribute, Promise<string>>
>
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/myinfo/myinfo.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IFormSchema,
IHashes,
IMyInfo,
IPossiblyPrefilledField,
MapRouteError,
} from '../../../types'
import { createLoggerWithLabel } from '../../config/logger'
Expand Down Expand Up @@ -39,7 +40,6 @@ import {
MyInfoMissingHashError,
} from './myinfo.errors'
import {
IPossiblyPrefilledField,
MyInfoComparePromises,
MyInfoCookiePayload,
MyInfoCookieState,
Expand Down
18 changes: 18 additions & 0 deletions src/types/api/field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EditFieldActions } from '../../shared/constants'
import { IFieldSchema } from '../field'

export type EditFormFieldParams = {
field: IFieldSchema
} & (
| {
action: {
name: Exclude<EditFieldActions, EditFieldActions.Reorder>
}
}
| {
action: {
name: EditFieldActions.Reorder
position: number
}
}
)
Loading