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

chore: clean up payments routes, documentation, payments utils #6035

Merged
merged 1 commit into from
Apr 4, 2023
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
103 changes: 2 additions & 101 deletions src/app/modules/form/admin-form/admin-form.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
LogicDto,
LogicIfValue,
LogicType,
PaymentsUpdateDto,
PermissionsUpdateDto,
PreviewFormViewDto,
PrivateFormErrorDto,
Expand All @@ -42,12 +41,7 @@ import {
StartPageUpdateDto,
SubmissionCountQueryDto,
} from '../../../../../shared/types'
import {
IEncryptedFormDocument,
IForm,
IFormDocument,
IPopulatedForm,
} from '../../../../types'
import { IForm, IFormDocument, IPopulatedForm } from '../../../../types'
import {
EncryptSubmissionDto,
FormUpdateParams,
Expand Down Expand Up @@ -98,11 +92,7 @@ import { EditFieldError } from './admin-form.errors'
import { updateSettingsValidator } from './admin-form.middlewares'
import * as AdminFormService from './admin-form.service'
import { PermissionLevel } from './admin-form.types'
import {
mapRouteError,
verifyUserBetaflag,
verifyValidUnicodeString,
} from './admin-form.utils'
import { mapRouteError, verifyValidUnicodeString } from './admin-form.utils'

// NOTE: Refer to this for documentation: https://github.com/sideway/joi-date/blob/master/API.md
const Joi = BaseJoi.extend(JoiDate) as typeof BaseJoi
Expand Down Expand Up @@ -2704,92 +2694,3 @@ export const handleUpdateTwilio = [
validateTwilioCredentials,
updateTwilioCredentials,
] as ControllerHandler[]

/**
* NOTE: Exported for testing.
* Private handler for PUT /forms/:formId/payment
* @precondition Must be preceded by request validation
* @security session
*
* @returns 200 with updated payments
* @returns 403 when current user does not have permissions to update the payments
* @returns 404 when form cannot be found
* @returns 410 when updating the payments for an archived form
* @returns 422 when user in session cannot be retrieved from the database
* @returns 500 when database error occurs
*/
export const _handleUpdatePayments: ControllerHandler<
{ formId: string },
IEncryptedFormDocument['payments_field'] | ErrorDto,
Comment on lines -2721 to -2723
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions moved to admin-form.payments.controller in tandem with moving payments routes to admin-form.payments.routes.

PaymentsUpdateDto
> = (req, res) => {
const { formId } = req.params
const sessionUserId = (req.session as AuthedSessionData).user._id

// Step 1: Retrieve currently logged in user.
return (
UserService.getPopulatedUserById(sessionUserId)
// Step 2: Check if user has 'payment' betaflag
.andThen((user) => verifyUserBetaflag(user, 'payment'))
.andThen((user) =>
// Step 2: Retrieve form with write permission check.
AuthService.getFormAfterPermissionChecks({
user,
formId,
level: PermissionLevel.Write,
}),
)
// Step 3: User has permissions, proceed to allow updating of start page
.andThen(() => AdminFormService.updatePayments(formId, req.body))
.map((updatedPayments) =>
res.status(StatusCodes.OK).json(updatedPayments),
)
.mapErr((error) => {
logger.error({
message: 'Error occurred when updating payments',
meta: {
action: '_handleUpdatePayments',
...createReqMeta(req),
userId: sessionUserId,
formId,
body: req.body,
},
error,
})
const { errorMessage, statusCode } = mapRouteError(error)
return res.status(statusCode).json({ message: errorMessage })
})
)
}

/**
* Handler for PUT /forms/:formId/payment
*/
export const handleUpdatePayments = [
celebrate({
[Segments.BODY]: {
enabled: Joi.boolean().required(),
amount_cents: Joi.alternatives().conditional('enabled', [
{
is: true,
then: Joi.number().integer().positive(),
},
{
is: false,
then: Joi.forbidden(),
},
]),
description: Joi.alternatives().conditional('enabled', [
{
is: true,
then: Joi.string().allow(''),
},
{
is: false,
then: Joi.forbidden(),
},
]),
},
}),
_handleUpdatePayments,
] as ControllerHandler[]
Loading