Skip to content

Commit

Permalink
emit gateway events
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Dec 17, 2023
1 parent e45857b commit 8ed831c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
18 changes: 14 additions & 4 deletions src/api/routes/admin/guilds/#guild_id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
AdminGuildProjection,
Channel,
Guild,
GuildDeleteEvent,
GuildUpdateEvent,
Permissions,
emitEvent,
Expand Down Expand Up @@ -214,7 +215,7 @@ router.delete(
description: "Delete a guild",
right: "ADMIN_DELETE_GUILDS",
responses: {
200: {},
204: {},
400: {
body: "APIErrorResponse",
},
Expand All @@ -225,9 +226,18 @@ router.delete(
}),
async (req: Request, res: Response) => {
const { guild_id } = req.params;
const guild = await Guild.findOneOrFail({ where: { id: guild_id } });
await guild.remove();
res.sendStatus(200);
await Promise.all([
Guild.findOneOrFail({ where: { id: guild_id } }),
Guild.delete({ id: guild_id }), // this will also delete all guild related data
emitEvent({
event: "GUILD_DELETE",
data: {
id: guild_id,
},
guild_id: guild_id,
} as GuildDeleteEvent),
]);
res.sendStatus(204);
},
);

Expand Down
66 changes: 33 additions & 33 deletions src/api/routes/admin/guilds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,6 @@ import { HTTPError } from "lambert-server";
import { ILike, MoreThan } from "typeorm";
const router = Router();

router.post(
"/",
route({
requestBody: "AdminGuildCreateSchema",
right: "ADMIN_CREATE_GUILDS",
responses: {
201: {
body: "AdminGuildResponse",
},
400: {
body: "APIErrorResponse",
},
403: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const body = req.body as AdminGuildCreateSchema;

const ownerId = body.owner_id || req.user_id;

const guild = await Guild.createGuild({
...body,
owner_id: ownerId,
});

await Member.addToGuild(ownerId, guild.id);

res.status(201).json(guild);
},
);

router.get(
"/",
route({
Expand Down Expand Up @@ -116,4 +83,37 @@ router.get(
},
);

router.post(
"/",
route({
requestBody: "AdminGuildCreateSchema",
right: "ADMIN_CREATE_GUILDS",
responses: {
201: {
body: "AdminGuildResponse",
},
400: {
body: "APIErrorResponse",
},
403: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const body = req.body as AdminGuildCreateSchema;

const ownerId = body.owner_id || req.user_id;

const guild = await Guild.createGuild({
...body,
owner_id: ownerId,
});

await Member.addToGuild(ownerId, guild.id);

res.status(201).json(guild);
},
);

export default router;
23 changes: 16 additions & 7 deletions src/api/routes/admin/users/#id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
AdminUserProjection,
FieldErrors,
User,
UserDeleteEvent,
UserUpdateEvent,
emitEvent,
handleFile,
Expand Down Expand Up @@ -138,7 +139,7 @@ router.delete(
description: "Delete a user",
right: "ADMIN_DELETE_USERS",
responses: {
200: {},
204: {},
400: {
body: "APIErrorResponse",
},
Expand All @@ -149,12 +150,20 @@ router.delete(
}),
async (req: Request, res: Response) => {
const { id } = req.params;
const user = await User.findOneOrFail({
where: { id },
select: AdminUserProjection,
});
await user.remove();
res.sendStatus(200);

await Promise.all([
User.findOneOrFail({
where: { id },
}),
User.delete({ id }),
emitEvent({
event: "USER_DELETE",
user_id: req.user_id,
data: { user_id: req.params.id },
} as UserDeleteEvent),
]);

res.sendStatus(204);
},
);

Expand Down

0 comments on commit 8ed831c

Please sign in to comment.