Skip to content

Commit

Permalink
refactor: migrate public routes to TypeScript (#1595)
Browse files Browse the repository at this point in the history
* ref: move all PublicForm routes to TS

* docs: update Travis build stage name

* docs: update JSdocs for publicform routes
  • Loading branch information
mantariksh authored Apr 9, 2021
1 parent d3ee8c0 commit 685277b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
use: build
script:
- npm run test-frontend
- name: Typescript tests
- name: Backend tests
workspaces:
use: build
script:
Expand Down
77 changes: 68 additions & 9 deletions src/app/modules/form/public-form/public-form.routes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { celebrate, Joi, Segments } from 'celebrate'
import { Router } from 'express'

import * as PublicFormController from './public-form.controller'
Expand All @@ -6,22 +7,80 @@ export const PublicFormRouter = Router()

/**
* Returns the specified form to the user, along with any
* identity information obtained from SingPass/CorpPass,
* and MyInfo details, if any.
* identify information obtained from Singpass/Corppass/MyInfo.
*
* WARNING: TemperatureSG batch jobs rely on this endpoint to
* retrieve the master list of personnel for daily reporting.
* Please strictly ensure backwards compatibility.
* @route GET /:formId/publicform
*
* @route GET /{formId}/publicform
* @group forms - endpoints to serve forms
* @param {string} formId.path.required - the form id
* @consumes application/json
* @produces application/json
* @returns {string} 404 - form is not made public
* @returns {PublicForm.model} 200 - the form, and other information
* @returns 200 with form when form exists and is public
* @returns 404 when form is private or form with given ID does not exist
* @returns 410 when form is archived
* @returns 500 when database error occurs
*/
PublicFormRouter.get(
'/:formId([a-fA-F0-9]{24})/publicform',
PublicFormController.handleGetPublicForm,
)

/**
* Redirect a form to the main index, with the specified path
* suffixed with a hashbang (`/#!`).
* @route GET /{Id}
* @route GET /{Id}/embed
* @route GET /{Id}/preview
* @route GET /{Id}/template
* @route GET /{Id}/use-template
* @route GET /forms/:agency/{Id}
* @route GET /forms/:agency/{Id}/embed
* @route GET /forms/:agency/{Id}/preview
* @route GET /forms/:agency/{Id}/template
* @route GET /forms/:agency/{Id}/use-template
* @group forms - endpoints to serve forms
* @returns 302 - redirects the user to the specified form,
* through the main index, with the form ID specified as a hashbang path
*/
PublicFormRouter.get(
'/:Id([a-fA-F0-9]{24})/:state(preview|template|use-template)?',
PublicFormController.handleRedirect,
)

PublicFormRouter.get(
'/:Id([a-fA-F0-9]{24})/embed',
PublicFormController.handleRedirect,
)

PublicFormRouter.get(
'/forms/:agency/:Id([a-fA-F0-9]{24})/:state(preview|template|use-template)?',
PublicFormController.handleRedirect,
)

PublicFormRouter.get(
'/forms/:agency/:Id([a-fA-F0-9]{24})/embed',
PublicFormController.handleRedirect,
)

/**
* Send feedback for a public form
* @route POST /:formId/feedback
* @returns 200 if feedback was successfully saved
* @returns 400 if form feedback was malformed and hence cannot be saved
* @returns 404 if form with formId does not exist or is private
* @returns 410 if form has been archived
* @returns 500 if database error occurs
*/
PublicFormRouter.post(
'/:formId([a-fA-F0-9]{24})/feedback',
celebrate({
[Segments.BODY]: Joi.object()
.keys({
rating: Joi.number().min(1).max(5).cast('string').required(),
comment: Joi.string().allow('').required(),
})
// Allow other keys for backwards compability as frontend might put
// extra keys in the body.
.unknown(true),
}),
PublicFormController.handleSubmitFeedback,
)
1 change: 0 additions & 1 deletion src/app/routes/index.js

This file was deleted.

121 changes: 0 additions & 121 deletions src/app/routes/public-forms.server.routes.js

This file was deleted.

8 changes: 2 additions & 6 deletions src/loaders/express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { BillingRouter } from '../../app/modules/billing/billing.routes'
import { BounceRouter } from '../../app/modules/bounce/bounce.routes'
import { ExamplesRouter } from '../../app/modules/examples/examples.routes'
import { AdminFormsRouter } from '../../app/modules/form/admin-form/admin-form.routes'
import { PublicFormRouter } from '../../app/modules/form/public-form/public-form.routes'
import { FrontendRouter } from '../../app/modules/frontend/frontend.server.routes'
import { HomeRouter } from '../../app/modules/home/home.routes'
import { MYINFO_ROUTER_PREFIX } from '../../app/modules/myinfo/myinfo.constants'
Expand All @@ -26,7 +27,6 @@ import {
import { SubmissionRouter } from '../../app/modules/submission/submission.routes'
import UserRouter from '../../app/modules/user/user.routes'
import { VfnRouter } from '../../app/modules/verification/verification.routes'
import apiRoutes from '../../app/routes'
import { ApiRouter } from '../../app/routes/api'
import * as IntranetMiddleware from '../../app/services/intranet/intranet.middleware'
import config from '../../config/config'
Expand Down Expand Up @@ -144,11 +144,6 @@ const loadExpressApp = async (connection: Connection) => {
// Log intranet usage
app.use(IntranetMiddleware.logIntranetUsage)

// Mount all API endpoints
apiRoutes.forEach(function (routeFunction) {
routeFunction(app)
})

app.use('/', HomeRouter)
app.use('/frontend', FrontendRouter)
app.use('/auth', AuthRouter)
Expand All @@ -167,6 +162,7 @@ const loadExpressApp = async (connection: Connection) => {
// Use constant for registered routes with MyInfo servers
app.use(MYINFO_ROUTER_PREFIX, MyInfoRouter)
app.use(AdminFormsRouter)
app.use(PublicFormRouter)

// New routes in preparation for API refactor.
app.use('/api', ApiRouter)
Expand Down

0 comments on commit 685277b

Please sign in to comment.