Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
feat: editing users
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonHowe committed Jul 15, 2020
1 parent eca1c71 commit 4021a1c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/api/src/modules/users/actions/editUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import knex from "../../../../db/knex";

import User from "../types/User";

export default async <T extends keyof Omit<User, "password">>(
property: T,
id: number,
newValue: User[T]
) => {
if (!property || !id || !newValue) return null;
const result = await knex<User>("users")
.select()
.first()
.where({ id })
.update({ [property]: newValue });
if (!result) return null;

return result;
};
8 changes: 8 additions & 0 deletions packages/api/src/modules/users/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand All @@ -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"),
Expand Down

0 comments on commit 4021a1c

Please sign in to comment.