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(webhook-services): migrate webhook services to neverthrow #1529

Merged
merged 16 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 14 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
18 changes: 0 additions & 18 deletions src/app/factories/webhook-verified-content.factory.js

This file was deleted.

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 @@ -31,7 +31,7 @@ import { IPopulatedUser, IUserSchema } from '../../types/user'
import { MB } from '../constants/filesize'
import { OverrideProps } from '../modules/form/admin-form/admin-form.types'
import { transformEmails } from '../modules/form/form.utils'
import { validateWebhookUrl } from '../modules/webhook/webhook.utils'
import { validateWebhookUrl } from '../modules/webhook/webhook.validation'

import getAgencyModel from './agency.server.model'
import {
Expand Down
13 changes: 13 additions & 0 deletions src/app/models/submission.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
IEncryptSubmissionModel,
ISubmissionModel,
ISubmissionSchema,
IWebhookResponse,
IWebhookResponseSchema,
MyInfoAttribute,
SubmissionCursorData,
Expand Down Expand Up @@ -185,6 +186,18 @@ EncryptSubmissionSchema.methods.getWebhookView = function (
}
}

EncryptSubmissionSchema.statics.addWebhookResponse = function (
this: IEncryptSubmissionModel,
submissionId: string,
webhookResponse: IWebhookResponse,
): Promise<IEncryptedSubmissionSchema | null> {
return this.findOneAndUpdate(
orbitalsqwib marked this conversation as resolved.
Show resolved Hide resolved
orbitalsqwib marked this conversation as resolved.
Show resolved Hide resolved
{ _id: submissionId },
{ $push: { webhookResponses: webhookResponse } },
{ new: true, setDefaultsOnInsert: true, runValidators: true },
).exec()
}

EncryptSubmissionSchema.statics.findSingleMetadata = function (
this: IEncryptSubmissionModel,
formId: string,
Expand Down
15 changes: 1 addition & 14 deletions src/app/modules/myinfo/myinfo.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IPopulatedForm,
MapRouteError,
} from '../../../types'
import { hasProp } from '../../utils/has-prop'
import { DatabaseError, MissingFeatureError } from '../core/core.errors'
import { FormNotFoundError } from '../form/form.errors'
import {
Expand Down Expand Up @@ -303,20 +304,6 @@ export const validateMyInfoForm = (
return ok(form as IMyInfoForm)
}

/**
* Utility to narrow type of an object by determining whether
* it contains the given property.
* @param obj Object
* @param prop Property to check
*/
export const hasProp = <K extends string>(
// eslint-disable-next-line @typescript-eslint/ban-types
obj: object | Record<string, unknown>,
prop: K,
): obj is Record<K, unknown> => {
return prop in obj
}

/**
* Type guard for MyInfo cookie.
* @param cookie Unknown object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { mapVerifyMyInfoError } from '../../myinfo/myinfo.util'
import { SpcpFactory } from '../../spcp/spcp.factory'
import { getPopulatedUserById } from '../../user/user.service'
import { VerifiedContentFactory } from '../../verified-content/verified-content.factory'
import { pushData as webhookPushData } from '../../webhook/webhook.service'
import { WebhookFactory } from '../../webhook/webhook.factory'
import {
getProcessedResponses,
sendEmailConfirmations,
Expand Down Expand Up @@ -360,12 +360,16 @@ export const handleEncryptedSubmission: RequestHandler = async (req, res) => {
})

// Fire webhooks if available
// Note that we push data to webhook endpoints on a best effort basis
// As such, we should not await on these post requests
const webhookUrl = form.webhook?.url
const submissionWebhookView = submission.getWebhookView()
if (webhookUrl) {
// Note that we push data to webhook endpoints on a best effort basis
// As such, we should not await on these post requests
void webhookPushData(webhookUrl, submissionWebhookView)
void WebhookFactory.sendWebhook(
submission,
webhookUrl,
).andThen((response) =>
WebhookFactory.saveWebhookRecord(submission._id, response),
)
}

// Send Email Confirmations
Expand Down
Loading