-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7155fd1
commit 43e222f
Showing
21 changed files
with
357 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { z } from "zod"; | ||
import { ValidatedRequestHandler } from "../../shared/middleware/validate"; | ||
import { resumeApplication } from "./service/resumeApplication"; | ||
import { LowCalSessionData } from "../../types"; | ||
import { findSession, validateSession } from "./service/validateSession"; | ||
import { PaymentRequest } from "@opensystemslab/planx-core/types"; | ||
|
||
interface ResumeApplicationResponse { | ||
message: string; | ||
expiryDate?: string | undefined; | ||
} | ||
|
||
export const resumeApplicationSchema = z.object({ | ||
body: z.object({ | ||
payload: z.object({ | ||
teamSlug: z.string(), | ||
email: z.string().email(), | ||
}), | ||
}), | ||
}); | ||
|
||
export type ResumeApplication = ValidatedRequestHandler< | ||
typeof resumeApplicationSchema, | ||
ResumeApplicationResponse | ||
>; | ||
|
||
export const resumeApplicationController: ResumeApplication = async ( | ||
_req, | ||
res, | ||
next, | ||
) => { | ||
try { | ||
const { teamSlug, email } = res.locals.parsedReq.body.payload; | ||
const response = await resumeApplication(teamSlug, email); | ||
return res.json(response); | ||
} catch (error) { | ||
return next({ | ||
error, | ||
message: `Failed to send "Resume" email. ${(error as Error).message}`, | ||
}); | ||
} | ||
}; | ||
|
||
export interface ValidationResponse { | ||
message: string; | ||
changesFound: boolean | null; | ||
alteredSectionIds?: Array<string>; | ||
reconciledSessionData: Omit<LowCalSessionData, "passport">; | ||
} | ||
|
||
interface LockedSessionResponse { | ||
message: "Session locked"; | ||
paymentRequest?: Partial< | ||
Pick<PaymentRequest, "id" | "payeeEmail" | "payeeName"> | ||
>; | ||
} | ||
|
||
export const validateSessionSchema = z.object({ | ||
body: z.object({ | ||
payload: z.object({ | ||
sessionId: z.string(), | ||
email: z.string().email(), | ||
}), | ||
}), | ||
}); | ||
|
||
export type ValidateSessionController = ValidatedRequestHandler< | ||
typeof validateSessionSchema, | ||
ValidationResponse | LockedSessionResponse | ||
>; | ||
|
||
export const validateSessionController: ValidateSessionController = async ( | ||
_req, | ||
res, | ||
next, | ||
) => { | ||
try { | ||
const { email, sessionId } = res.locals.parsedReq.body.payload; | ||
|
||
const fetchedSession = await findSession({ | ||
sessionId, | ||
email: email.toLowerCase(), | ||
}); | ||
|
||
if (!fetchedSession) { | ||
return next({ | ||
status: 404, | ||
message: "Unable to find your session", | ||
}); | ||
} | ||
|
||
if (fetchedSession.lockedAt) { | ||
return res.status(403).send({ | ||
message: "Session locked", | ||
paymentRequest: { | ||
...fetchedSession.paymentRequests?.[0], | ||
}, | ||
}); | ||
} | ||
const responseData = await validateSession(sessionId, fetchedSession); | ||
|
||
return res.status(200).json(responseData); | ||
} catch (error) { | ||
return next({ | ||
error, | ||
message: "Failed to validate session", | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: Plan✕ API | ||
version: 0.1.0 | ||
tags: | ||
- name: save and return | ||
description: Endpoints used for "Save and Return" functionality | ||
components: | ||
schema: | ||
ResumeApplication: | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
payload: | ||
type: object | ||
properties: | ||
teamSlug: | ||
type: string | ||
email: | ||
type: string | ||
format: email | ||
ValidateSession: | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
payload: | ||
type: object | ||
properties: | ||
sessionId: | ||
type: string | ||
format: uuid | ||
email: | ||
type: string | ||
format: email | ||
responses: | ||
ResumeApplication: | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
message: | ||
required: true | ||
type: string | ||
expiryDate: | ||
required: false | ||
oneOf: | ||
- type: string | ||
- type: "null" | ||
ValidationResponse: | ||
type: object | ||
properties: | ||
message: | ||
type: string | ||
changesFound: | ||
type: boolean | ||
nullable: true | ||
alteredSectionIds: | ||
type: array | ||
items: | ||
type: string | ||
reconciledSessionData: | ||
type: object | ||
properties: | ||
breadcrumbs: | ||
type: object | ||
id: | ||
type: string | ||
# TODO: Add $ref here when documenting payment endpoints | ||
govUkPayment: | ||
required: false | ||
type: object | ||
LockedSessionResponse: | ||
type: object | ||
properties: | ||
message: | ||
type: string | ||
enum: | ||
- Session locked | ||
paymentRequest: | ||
type: object | ||
properties: | ||
id: | ||
type: string | ||
payeeEmail: | ||
type: string | ||
format: email | ||
payeeName: | ||
type: string | ||
paths: | ||
/resume-application: | ||
post: | ||
summary: Resume application | ||
description: Request a "resume" email which lists all of your open applications. This email acts as a "dashboard" for the user. | ||
tags: | ||
- save and return | ||
requestBody: | ||
$ref: "#/components/schema/ResumeApplication" | ||
responses: | ||
"200": | ||
$ref: "#/components/responses/ResumeApplication" | ||
/validate-session: | ||
post: | ||
summary: Validate session | ||
description: Validates the session and reconciles the session's breadcrumbs to account for any differences between the current published flow and the last flow version a user traversed. | ||
tags: | ||
- save and return | ||
requestBody: | ||
$ref: "#/components/schema/ValidateSession" | ||
responses: | ||
"200": | ||
content: | ||
application/json: | ||
schema: | ||
oneOf: | ||
- $ref: "#/components/responses/ValidationResponse" | ||
- $ref: "#/components/responses/LockedSessionResponse" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Router } from "express"; | ||
|
||
import { | ||
resumeApplicationController, | ||
resumeApplicationSchema, | ||
validateSessionController, | ||
validateSessionSchema, | ||
} from "./controller"; | ||
import { sendEmailLimiter } from "../../rateLimit"; | ||
import { validate } from "../../shared/middleware/validate"; | ||
|
||
const router = Router(); | ||
|
||
router.post( | ||
"/resume-application", | ||
sendEmailLimiter, | ||
validate(resumeApplicationSchema), | ||
resumeApplicationController, | ||
); | ||
router.post( | ||
"/validate-session", | ||
validate(validateSessionSchema), | ||
validateSessionController, | ||
); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 14 additions & 33 deletions
47
...anx.uk/saveAndReturn/resumeApplication.ts → ...aveAndReturn/service/resumeApplication.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
api.planx.uk/saveAndReturn/utils.test.ts → ...dules/saveAndReturn/service/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
api.planx.uk/saveAndReturn/utils.ts → ...uk/modules/saveAndReturn/service/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.