diff --git a/api.planx.uk/modules/user/controller.ts b/api.planx.uk/modules/user/controller.ts index e7bc07f2a2..9a3355c2d6 100644 --- a/api.planx.uk/modules/user/controller.ts +++ b/api.planx.uk/modules/user/controller.ts @@ -32,3 +32,33 @@ export const createUser: CreateUser = async (req, res, next) => { ); } }; + +export const deleteUserSchema = z.object({ + params: z.object({ + email: z.string().trim().email().toLowerCase(), + }), +}); + +export type DeleteUser = ValidatedRequestHandler< + typeof deleteUserSchema, + UserResponse +>; + +export const deleteUser: DeleteUser = async (req, res, next) => { + try { + const { email } = req.params; + const $client = getClient(); + + const user = await $client.user.getByEmail(email); + if (!user) throw Error(`No user matching email ${email} found`); + + const isSuccessful = await $client.user.delete(user.id); + if (!isSuccessful) throw Error("Request to delete user failed"); + + return res.send({ message: "Successfully deleted user" }); + } catch (error) { + return next( + new ServerError({ message: "Failed to delete user", cause: error }), + ); + } +}; diff --git a/api.planx.uk/modules/user/docs.yaml b/api.planx.uk/modules/user/docs.yaml index d1c77ebcce..398c56e409 100644 --- a/api.planx.uk/modules/user/docs.yaml +++ b/api.planx.uk/modules/user/docs.yaml @@ -41,3 +41,22 @@ paths: $ref: "#/components/responses/SuccessMessage" "500": $ref: "#/components/responses/ErrorMessage" + /user/{email}: + delete: + summary: Delete a user + description: "Requires authentication via a Cloudflare WARP client + \n\n + Please login at [https://api.editor.planx.uk/user](https://api.editor.planx.uk/user)" + tags: ["user"] + parameters: + - in: path + name: email + type: string + format: email + example: bilbo@bagend.sh + description: Email address of the user to be deleted + responses: + "200": + $ref: "#/components/responses/SuccessMessage" + "500": + $ref: "#/components/responses/ErrorMessage" diff --git a/api.planx.uk/modules/user/routes.ts b/api.planx.uk/modules/user/routes.ts index c569a70308..68991ea58e 100644 --- a/api.planx.uk/modules/user/routes.ts +++ b/api.planx.uk/modules/user/routes.ts @@ -1,11 +1,17 @@ import { Router } from "express"; import { usePlatformAdminAuth } from "../auth/middleware"; import { validate } from "../../shared/middleware/validate"; -import { createUserSchema, createUser } from "./controller"; +import { + createUserSchema, + createUser, + deleteUserSchema, + deleteUser, +} from "./controller"; const router = Router(); router.use(usePlatformAdminAuth); router.put("/", validate(createUserSchema), createUser); +router.delete("/:email", validate(deleteUserSchema), deleteUser); export default router;