Skip to content

Commit

Permalink
Add an ednpoint to delete a user
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Apr 8, 2024
1 parent e46ca92 commit ef61e3f
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 27 deletions.
29 changes: 23 additions & 6 deletions dbschema/default.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module default {
multi tokens: RefreshToken {
constraint exclusive;
on target delete allow;
on source delete delete target;
};

required password: str;
Expand All @@ -18,7 +19,7 @@ module default {
};

classes := .<students[is Class];
assignments := .<updatedBy[is Assignment];
changes := .<user[is Change];
}

type RefreshToken {
Expand Down Expand Up @@ -56,6 +57,7 @@ module default {
default := datetime_current();
readonly := true;
};
on target delete allow;
};

single school := .<classes[is School];
Expand All @@ -76,6 +78,7 @@ module default {
};
required user: User {
readonly := true;
on target delete delete source;
};
required created: datetime {
default := datetime_current();
Expand All @@ -87,7 +90,9 @@ module default {
};

reviewedAt: datetime;
reviewedBy: User;
reviewedBy: User {
on target delete allow;
};
}

type Assignment {
Expand All @@ -97,15 +102,27 @@ module default {
required dueDate: datetime;
required fromDate: datetime;

required multi updates: datetime {
default := datetime_current();
multi updates: Change {
on target delete allow;
};
required multi updatedBy: User;

multi completedBy: User;
multi completedBy: User {
on target delete allow;
};

required class: Class {
readonly := true;
};
}

type Change {
required user: User {
on target delete delete source;
};
required time: datetime {
default := datetime_current();
};

assignments := .<updates[is Assignment];
}
}
30 changes: 30 additions & 0 deletions dbschema/migrations/00014.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE MIGRATION m1poydrr4wqnyyw2kuxdyz2gkxhomo27tud2b5imhloezqgvhd7spa
ONTO m1g7zzumkuvubppu53mcmxqcs73v2xyvueiz3cshqj75qd4uy2qafq
{
ALTER TYPE default::Assignment {
ALTER LINK completedBy {
ON TARGET DELETE ALLOW;
};
ALTER LINK updatedBy {
ON TARGET DELETE ALLOW;
};
};
ALTER TYPE default::Class {
ALTER LINK students {
ON TARGET DELETE ALLOW;
};
};
ALTER TYPE default::JoinRequest {
ALTER LINK reviewedBy {
ON TARGET DELETE ALLOW;
};
ALTER LINK user {
ON TARGET DELETE DELETE SOURCE;
};
};
ALTER TYPE default::User {
ALTER LINK tokens {
ON SOURCE DELETE DELETE TARGET;
};
};
};
32 changes: 32 additions & 0 deletions dbschema/migrations/00015.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE MIGRATION m1hmgl6ihppy3vtahtblnbmpvhbrn7vtjhloj5f7f2e6qgwsn7eqgq
ONTO m1poydrr4wqnyyw2kuxdyz2gkxhomo27tud2b5imhloezqgvhd7spa
{
ALTER TYPE default::User {
DROP LINK assignments;
};
ALTER TYPE default::Assignment {
DROP LINK updatedBy;
};
ALTER TYPE default::Assignment {
DROP PROPERTY updates;
};
CREATE TYPE default::Change {
CREATE REQUIRED LINK user: default::User {
ON TARGET DELETE DELETE SOURCE;
};
CREATE REQUIRED PROPERTY time: std::datetime {
SET default := (std::datetime_current());
};
};
ALTER TYPE default::Assignment {
CREATE MULTI LINK updates: default::Change {
ON TARGET DELETE ALLOW;
};
};
ALTER TYPE default::Change {
CREATE LINK assignments := (.<updates[IS default::Assignment]);
};
ALTER TYPE default::User {
CREATE LINK changes := (.<user[IS default::Change]);
};
};
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { registerRouter } from "routes/auth/register";
import { classRouter } from "routes/classes";
import { moderationRouter } from "routes/moderation";
import { schoolRouter } from "routes/school";
import { deleteUser } from "routes/user/delete";
import { userInfoRouter } from "routes/user/info";
import { changeUserDetailsRouter } from "routes/user/settings";
import { edgedb } from "../dbschema/edgeql-js/imports";
Expand All @@ -35,13 +36,13 @@ const app = new Elysia()
},
)
.group("/auth", (app) =>
app.use(registerRouter).use(refreshTokenRouter).use(accessTokenRouter),
)
.group("/user", (app) =>
app
.use(registerRouter)
.use(refreshTokenRouter)
.use(accessTokenRouter)
.group("/me", (app) => app.use(changeUserDetailsRouter)),
.use(userInfoRouter)
.group("/me", (app) => app.use(changeUserDetailsRouter).use(deleteUser)),
)
.group("/user", (app) => app.use(userInfoRouter).use(changeUserDetailsRouter))
.listen(3000);

console.log(
Expand Down
8 changes: 5 additions & 3 deletions src/routes/assignments/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ export const createAssignment = new Elysia()
filter_single: e.op(classNameMatches, "and", schoolMatches),
};
}),
updatedBy: e.select(e.User, (u) => ({
filter_single: e.op(u.username, "=", auth.username),
})),
updates: e.insert(e.Change, {
user: e.select(e.User, (u) => ({
filter_single: e.op(u.username, "=", auth.username),
})),
}),
});
const insertResult = await promiseResult(() => insertQuery.run(client));
if (insertResult.isError) {
Expand Down
21 changes: 8 additions & 13 deletions src/routes/assignments/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { client } from "index";
import { removeDuplicates } from "utils/arrays/duplicates";
import { filterTruthy } from "utils/arrays/filter";
import { areSameValue } from "utils/arrays/general";
import { merge } from "utils/arrays/merge";
import {
normalDateToCustom,
stringToNormal,
Expand Down Expand Up @@ -123,8 +122,10 @@ export const listAssignments = new Elysia().use(HttpStatusCode()).get(
description: true,
dueDate: true,
fromDate: true,
updates: true,
updatedBy: () => ({ username: true }),
updates: () => ({
user: () => ({ username: true, displayname: true }),
time: true,
}),
id: true,
};
});
Expand Down Expand Up @@ -164,16 +165,10 @@ export const listAssignments = new Elysia().use(HttpStatusCode()).get(
description: assignment.description,
from: normalDateToCustom(assignment.fromDate),
due: normalDateToCustom(assignment.dueDate),
updates: merge(
{
key: "user",
array: assignment.updatedBy.map((u) => u.username),
},
{
key: "timestamp",
array: assignment.updates.map((d) => d.getTime()),
},
),
updates: assignment.updates.map((upd) => ({
user: upd.user,
time: upd.time.getTime(),
})),
}));

return responseBuilder("success", {
Expand Down
42 changes: 42 additions & 0 deletions src/routes/user/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import e from "@edgedb";
import {
DATABASE_DELETE_FAILED,
MUST_BE_GENERATED_BY_LOGIN_NOT_REFRESH,
UNAUTHORIZED,
} from "constants/responses";
import Elysia from "elysia";
import { HttpStatusCode } from "elysia-http-status-code";
import { client } from "index";
import { auth } from "plugins/auth";
import { promiseResult } from "utils/errors";
import { responseBuilder } from "utils/response";

export const deleteUser = new Elysia()
.use(auth)
.use(HttpStatusCode())
.delete("/", async ({ auth, httpStatus, set }) => {
if (!auth.isAuthorized) {
set.status = httpStatus.HTTP_401_UNAUTHORIZED;
return UNAUTHORIZED;
}
if (auth.createdBy !== "login") {
set.status = httpStatus.HTTP_403_FORBIDDEN;
return MUST_BE_GENERATED_BY_LOGIN_NOT_REFRESH;
}

const query = e.delete(e.User, (u) => ({
filter_single: e.op(u.username, "=", auth.username),
}));

const result = await promiseResult(() => query.run(client));

if (result.isError || !result.data) {
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR;
return DATABASE_DELETE_FAILED;
}

return responseBuilder("success", {
message: "Successfully deleted user",
data: null,
});
});
3 changes: 3 additions & 0 deletions src/utils/arrays/merge.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// NOTE: Currently this is dead code
// 08.04.24

interface MergeInput<T, K extends string> {
array: T[];
key: K;
Expand Down

0 comments on commit ef61e3f

Please sign in to comment.