Skip to content

Commit

Permalink
refactor(handler): Improve database query conditions for deletion ope…
Browse files Browse the repository at this point in the history
…rations
  • Loading branch information
wajeht committed Sep 7, 2024
1 parent c44706f commit 1b17422
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,17 @@ export async function getAppEditPageHandler(req: Request, res: Response) {
export async function postDeleteAppChannelHandler(req: Request, res: Response) {
const { aid, cid } = req.params;

await db('app_channels').where({ id: cid }).del();
await db('app_channels')
.where('app_channels.id', cid)
.andWhere(function () {
this.whereExists(function () {
this.select('apps.id')
.from('apps')
.where('apps.id', aid)
.andWhere('apps.user_id', req.session?.user?.id);
});
})
.delete();

return res.redirect(`/apps/${aid}/channels?toast=🗑️ deleted`);
}
Expand All @@ -421,7 +431,17 @@ export async function postDeleteAppChannelHandler(req: Request, res: Response) {
export async function postDeleteAppNotificationHandler(req: Request, res: Response) {
const { id, nid } = req.params;

await db('notifications').where({ id: nid }).del();
await db('notifications')
.where('notifications.id', nid)
.andWhere(function () {
this.whereExists(function () {
this.select('apps.id')
.from('apps')
.where('apps.id', id)
.andWhere('apps.user_id', req.session?.user?.id);
});
})
.delete();

req.flash('info', '🗑️ deleted');

Expand Down

0 comments on commit 1b17422

Please sign in to comment.