Skip to content

Commit

Permalink
notify clients on deleted document model
Browse files Browse the repository at this point in the history
  • Loading branch information
lebalz committed Sep 4, 2024
1 parent c0fb73a commit 67a5eb6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/controllers/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,22 @@ export const all: RequestHandler = async (req, res, next) => {

export const destroy: RequestHandler<{ id: string }> = async (req, res, next) => {
try {
const document = await Document.deleteModel(req.user!, req.params.id);
res.json(document);
const model = await Document.deleteModel(req.user!, req.params.id);

const groupIds = model.documentRoot.rootGroupPermissions
.filter((p) => !NoneAccess.has(p.access))
.map((p) => p.studentGroupId);
const userIds = model.documentRoot.rootUserPermissions
.filter((p) => !NoneAccess.has(p.access))
.map((p) => p.userId);
res.notifications = [
{
event: IoEvent.DELETED_RECORD,
message: { type: RecordType.Document, id: model.id },
to: [...groupIds, ...userIds, IoRoom.ADMIN, req.user!.id] // overlappings are handled by socket.io: https://socket.io/docs/v3/rooms/#joining-and-leaving
}
];
res.status(204).send();
} catch (error) {
next(error);
}
Expand Down
27 changes: 22 additions & 5 deletions src/models/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function Document(db: PrismaClient['document']) {
return model;
},

async deleteModel(actor: User, id: string): Promise<DbDocument> {
async deleteModel(actor: User, id: string) {
const record = await this.findModel(actor, id);
if (!record) {
throw new HTTP404Error('Document not found');
Expand All @@ -216,13 +216,30 @@ function Document(db: PrismaClient['document']) {
throw new HTTP403Error('Not authorized');
}

// TODO: Notify connected clients.

return db.delete({
const model = (await db.delete({
where: {
id: id
},
include: {
documentRoot: {
include: {
rootGroupPermissions: {
select: {
access: true,
studentGroupId: true
}
},
rootUserPermissions: {
select: {
access: true,
userId: true
}
}
}
}
}
});
})) satisfies DbDocument;
return model;
},

async all(actor: User): Promise<DbDocument[]> {
Expand Down

0 comments on commit 67a5eb6

Please sign in to comment.