Skip to content

Commit

Permalink
feat: Delete user endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 2, 2023
1 parent 2d948a6 commit 6575cdb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
30 changes: 30 additions & 0 deletions api.planx.uk/modules/user/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
);
}
};
19 changes: 19 additions & 0 deletions api.planx.uk/modules/user/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
description: Email address of the user to be deleted
responses:
"200":
$ref: "#/components/responses/SuccessMessage"
"500":
$ref: "#/components/responses/ErrorMessage"
8 changes: 7 additions & 1 deletion api.planx.uk/modules/user/routes.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 6575cdb

Please sign in to comment.