Skip to content

Commit

Permalink
refactor: standardise usage of getUniqueMyInfoAttrs (#636)
Browse files Browse the repository at this point in the history
* ref: rename uniq function

* ref: remove extractRequestedAttributes

* ref: remove getRequestedAttrs middleware
  • Loading branch information
mantariksh committed Nov 19, 2020
1 parent 9e657df commit 3880f6e
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 51 deletions.
3 changes: 1 addition & 2 deletions src/app/controllers/email-submissions.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,11 @@ function concatResponse(formData, attachments) {
*/
exports.saveMetadataToDb = function (req, res, next) {
const { form, attachments, formData } = req
const { requestedAttributes } = res.locals

let submission = new emailSubmission({
form: form._id,
authType: form.authType,
myInfoFields: requestedAttributes,
myInfoFields: form.getUniqueMyInfoAttrs(),
recipientEmails: form.emails,
})

Expand Down
4 changes: 2 additions & 2 deletions src/app/controllers/encrypt-submissions.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function onEncryptSubmissionFailure(err, req, res, submission) {
*/
exports.saveResponseToDb = function (req, res, next) {
const { form, formData, attachmentData } = req
const { verified, requestedAttributes } = res.locals
const { verified } = res.locals
let attachmentMetadata = new Map()
let attachmentUploadPromises = []

Expand Down Expand Up @@ -193,7 +193,7 @@ exports.saveResponseToDb = function (req, res, next) {
const submission = new EncryptSubmission({
form: form._id,
authType: form.authType,
myInfoFields: requestedAttributes,
myInfoFields: form.getUniqueMyInfoAttrs(),
encryptedContent: formData,
verifiedContent: verified,
attachmentMetadata,
Expand Down
17 changes: 8 additions & 9 deletions src/app/controllers/myinfo.server.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import {
WithForm,
} from '../../types'
import { MyInfoFactory } from '../services/myinfo/myinfo.factory'
import {
extractRequestedAttributes,
mapVerifyMyInfoError,
} from '../services/myinfo/myinfo.util'
import { mapVerifyMyInfoError } from '../services/myinfo/myinfo.util'
import { createReqMeta } from '../utils/request'

const logger = createLoggerWithLabel(module)
Expand All @@ -40,7 +37,9 @@ export const addMyInfo: RequestHandler<ParamsDictionary> = async (
const { esrvcId, authType, form_fields: formFields, _id: formId } = form

// Early return if nothing needs to be done.
const requestedAttributes = extractRequestedAttributes(formFields)
const requestedAttributes = (req as WithForm<
typeof req
>).form.getUniqueMyInfoAttrs()
if (!uinFin || authType !== AuthType.SP || requestedAttributes.length === 0) {
return next()
}
Expand Down Expand Up @@ -94,11 +93,11 @@ 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 WithForm<
typeof req
>).form.toJSON()
const { authType, _id: formId } = (req as WithForm<typeof req>).form.toJSON()
const uinFin = (res as ResWithUinFin<typeof res>).locals.uinFin
const requestedAttributes = extractRequestedAttributes(formFields)
const requestedAttributes = (req as WithForm<
typeof req
>).form.getUniqueMyInfoAttrs()
if (authType !== AuthType.SP || requestedAttributes.length === 0) {
return next()
} else if (!uinFin) {
Expand Down
11 changes: 0 additions & 11 deletions src/app/controllers/spcp.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,6 @@ exports.addSpcpSessionInfo = (authClients) => {
}
}

/**
* Get MyInfo attributes that are requested in this particular submission
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.getRequestedAttributes = function (req, res, next) {
const { form } = req
res.locals.requestedAttributes = form.getUniqMyinfoAttrs() || []
return next()
}

/**
* Encrypt and sign verified fields if exist
* @param {Object} req - Express request object
Expand Down
5 changes: 0 additions & 5 deletions src/app/factories/spcp.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const spcpFactory = ({ isEnabled, props }) => {
}

return {
getRequestedAttributes: spcp.getRequestedAttributes,
appendVerifiedSPCPResponses: spcp.appendVerifiedSPCPResponses,
passThroughSpcp: admin.passThroughSpcp,
returnSpcpRedirectURL: spcp.returnSpcpRedirectURL,
Expand All @@ -79,10 +78,6 @@ const spcpFactory = ({ isEnabled, props }) => {
} else {
const errMsg = 'SPCP/MyInfo feature is not enabled'
return {
getRequestedAttributes: (req, res, next) => {
res.locals.requestedAttributes = []
return next()
},
appendVerifiedSPCPResponses: (req, res, next) => next(),
passThroughSpcp: (req, res, next) => next(),
returnSpcpRedirectURL: (req, res) =>
Expand Down
2 changes: 1 addition & 1 deletion src/app/models/form.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ const compileFormModel = (db: Mongoose): IFormModel => {
}

// Method to return myInfo attributes
FormSchema.methods.getUniqMyinfoAttrs = function (this: IFormSchema) {
FormSchema.methods.getUniqueMyInfoAttrs = function (this: IFormSchema) {
if (this.authType !== AuthType.SP) {
return []
}
Expand Down
2 changes: 0 additions & 2 deletions src/app/routes/public-forms.server.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ module.exports = function (app) {
submissions.injectAutoReplyInfo,
spcpFactory.appendVerifiedSPCPResponses,
emailSubmissions.prepareEmailSubmission,
spcpFactory.getRequestedAttributes,
emailSubmissions.saveMetadataToDb,
emailSubmissions.sendAdminEmail,
submissions.sendAutoReply,
Expand Down Expand Up @@ -272,7 +271,6 @@ module.exports = function (app) {
submissions.injectAutoReplyInfo,
webhookVerifiedContentFactory.encryptedVerifiedFields,
encryptSubmissions.prepareEncryptSubmission,
spcpFactory.getRequestedAttributes,
encryptSubmissions.saveResponseToDb,
webhookVerifiedContentFactory.post,
submissions.sendAutoReply,
Expand Down
18 changes: 0 additions & 18 deletions src/app/services/myinfo/myinfo.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import moment from 'moment'
import { createLoggerWithLabel } from '../../../config/logger'
import {
BasicField,
IFieldSchema,
IHashes,
MapRouteError,
MyInfoAttribute,
Expand Down Expand Up @@ -142,23 +141,6 @@ export const hashFieldValues = (
return readOnlyHashPromises
}

/**
* Extracts the MyInfo attributes in an array of form fields.
* @param formFields Array of form fields
* @returns array of the MyInfo attributes present in the form fields
*/
export const extractRequestedAttributes = (
formFields: IFieldSchema[],
): MyInfoAttribute[] => {
const attrs: MyInfoAttribute[] = []
formFields.forEach((field) => {
if (field.myInfo?.attr) {
attrs.push(field.myInfo.attr)
}
})
return attrs
}

/**
* Whether a field contains a MyInfo response
* @param field a processed response with the isVisible attribute
Expand Down
2 changes: 1 addition & 1 deletion src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface IForm {

export interface IFormSchema extends IForm, Document {
getMainFields(): Pick<IFormSchema, '_id' | 'title' | 'status'>
getUniqMyinfoAttrs(): MyInfoAttribute[]
getUniqueMyInfoAttrs(): MyInfoAttribute[]
duplicate(overrideProps: Partial<IForm>): Partial<IFormSchema>
transferOwner(currentOwner: IUserSchema, newOwnerEmail: string): void
}
Expand Down

0 comments on commit 3880f6e

Please sign in to comment.