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: extract temporary types into express.locals #683

Merged
merged 1 commit into from
Nov 19, 2020
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
27 changes: 7 additions & 20 deletions src/app/controllers/myinfo.server.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import { ProcessedFieldResponse } from 'src/app/modules/submission/submission.ty
import { createLoggerWithLabel } from '../../config/logger'
import {
AuthType,
IPopulatedForm,
MyInfoAttribute,
SpcpSession,
ResWithHashedFields,
ResWithSpcpSession,
ResWithUinFin,
WithForm,
} from '../../types'
import { MyInfoFactory } from '../services/myinfo/myinfo.factory'
import {
Expand All @@ -23,20 +24,6 @@ import { createReqMeta } from '../utils/request'

const logger = createLoggerWithLabel(module)

// TODO (#42): remove these types when migrating away from middleware pattern
type MyInfoReq<T> = T & {
form: IPopulatedForm
}
type ResWithSpcpSession<T> = T & {
locals: { spcpSession?: SpcpSession }
}
type ResWithUinFin<T> = T & {
uinFin?: string
}
type ResWithHashedFields<T> = T & {
locals: { hashedFields?: Set<MyInfoAttribute> }
}

/**
* Middleware for prefilling MyInfo values.
* @returns next, always. If any error occurs, res.locals.myInfoError is set to true.
Expand All @@ -47,7 +34,7 @@ export const addMyInfo: RequestHandler<ParamsDictionary> = async (
next,
) => {
// TODO (#42): add proper types here when migrating away from middleware pattern
const form = (req as MyInfoReq<typeof req>).form.toJSON()
const form = (req as WithForm<typeof req>).form.toJSON()
const uinFin = (res as ResWithSpcpSession<typeof res>).locals.spcpSession
?.userName
const { esrvcId, authType, form_fields: formFields, _id: formId } = form
Expand All @@ -71,7 +58,7 @@ export const addMyInfo: RequestHandler<ParamsDictionary> = async (
// Step 3: Hash the values and save them
.andThen((prefilledFields) => {
form.form_fields = prefilledFields
;(req as MyInfoReq<typeof req>).form = form
;(req as WithForm<typeof req>).form = form
return MyInfoFactory.saveMyInfoHashes(uinFin, formId, prefilledFields)
})
.map(() => next())
Expand Down Expand Up @@ -107,7 +94,7 @@ export const verifyMyInfoVals: RequestHandler<
{ parsedResponses: ProcessedFieldResponse[] }
> = async (req, res, next) => {
// TODO (#42): add proper types here when migrating away from middleware pattern
const { authType, _id: formId, form_fields: formFields } = (req as MyInfoReq<
const { authType, _id: formId, form_fields: formFields } = (req as WithForm<
typeof req
>).form.toJSON()
const uinFin = (res as ResWithUinFin<typeof res>).locals.uinFin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StatusCodes } from 'http-status-codes'
import JSONStream from 'JSONStream'

import { createLoggerWithLabel } from '../../../../config/logger'
import { IPopulatedForm } from '../../../../types'
import { WithForm } from '../../../../types'
import { createReqMeta } from '../../../utils/request'

import {
Expand All @@ -14,10 +14,6 @@ import {

const logger = createLoggerWithLabel(module)

type WithForm<T> = T & {
form: IPopulatedForm
}

/**
* Handler for GET /:formId([a-fA-F0-9]{24})/adminform/submissions/download
*
Expand Down
21 changes: 21 additions & 0 deletions src/types/express.locals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// TODO (#42): remove these types when migrating away from middleware pattern

import { MyInfoAttribute } from './field/fieldTypes'
import { IPopulatedForm } from './form'
import { SpcpSession } from './spcp'

export type WithForm<T> = T & {
form: IPopulatedForm
}

export type ResWithSpcpSession<T> = T & {
locals: { spcpSession?: SpcpSession }
}

export type ResWithUinFin<T> = T & {
uinFin?: string
}

export type ResWithHashedFields<T> = T & {
locals: { hashedFields?: Set<MyInfoAttribute> }
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './admin_verification'
export * from './config'
export * from './spcp'
export * from './routing'
export * from './express.locals'