From 4021a1c05eaad7dc00673482c4cee55cfe324eaf Mon Sep 17 00:00:00 2001 From: xWafl <73sampleperson@gmail.com> Date: Wed, 15 Jul 2020 08:25:24 -0400 Subject: [PATCH] feat: editing users --- .../api/src/modules/users/actions/editUser.ts | 19 +++++++++++++++++++ packages/api/src/modules/users/router.ts | 8 ++++++++ 2 files changed, 27 insertions(+) create mode 100644 packages/api/src/modules/users/actions/editUser.ts diff --git a/packages/api/src/modules/users/actions/editUser.ts b/packages/api/src/modules/users/actions/editUser.ts new file mode 100644 index 0000000..7de012c --- /dev/null +++ b/packages/api/src/modules/users/actions/editUser.ts @@ -0,0 +1,19 @@ +import knex from "../../../../db/knex"; + +import User from "../types/User"; + +export default async >( + property: T, + id: number, + newValue: User[T] +) => { + if (!property || !id || !newValue) return null; + const result = await knex("users") + .select() + .first() + .where({ id }) + .update({ [property]: newValue }); + if (!result) return null; + + return result; +}; diff --git a/packages/api/src/modules/users/router.ts b/packages/api/src/modules/users/router.ts index ad0df38..57ad200 100644 --- a/packages/api/src/modules/users/router.ts +++ b/packages/api/src/modules/users/router.ts @@ -16,6 +16,7 @@ import changePassword from "./actions/changePassword"; import { requireAdmin } from "../auth/middleware/requireAdmin"; import getAllUsers from "./actions/getAllUsers"; import deleteUser from "./actions/deleteUser"; +import editUser from "./actions/editUser"; const router = new Router({ prefix: "/users" }); @@ -35,6 +36,13 @@ router.delete("/deleteUser", requireAdmin(), async (ctx, next) => { await next(); }); +// TODO: Add schema validation for this +router.patch("/editUser", requireAdmin(), async (ctx, next) => { + const { property, id, newValue } = ctx.request.body; + await editUser(property, id, newValue); + await next(); +}); + router.post( "/createUser", validateSchema(registerBody, "body"),