From 685b2d1682c9305691c9b8c35c8bd5d578328469 Mon Sep 17 00:00:00 2001 From: NoodleOfDeath Date: Mon, 29 Jan 2024 13:38:24 -0500 Subject: [PATCH] fix interactions --- .../controllers/category/CategoryController.ts | 16 ++++++++++++++-- .../controllers/publisher/PublisherController.ts | 16 ++++++++++++++-- .../v1/controllers/summary/SummaryController.ts | 9 +++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/server/src/api/v1/controllers/category/CategoryController.ts b/src/server/src/api/v1/controllers/category/CategoryController.ts index 76a0a3f6..cfba24e3 100644 --- a/src/server/src/api/v1/controllers/category/CategoryController.ts +++ b/src/server/src/api/v1/controllers/category/CategoryController.ts @@ -59,12 +59,24 @@ export class CategoryController extends BaseController { ): Promise { const user = req.jwt?.user; const category = await Category.findOne({ where: { name: target } }); + if (!user || !category) { + return undefined; + } + if (body.revert) { + await CategoryInteraction.destroy({ + where: { + targetId: category.id, + type, + userId: user.id, + } + }); + } const interaction = await CategoryInteraction.create({ ...body, remoteAddr: req.ip, - targetId: category?.id, + targetId: category.id, type, - userId: user?.id, + userId: user.id, }); if (!interaction) { throw new InternalError('Failed to create interaction'); diff --git a/src/server/src/api/v1/controllers/publisher/PublisherController.ts b/src/server/src/api/v1/controllers/publisher/PublisherController.ts index 9f79eab2..53f6c6d6 100644 --- a/src/server/src/api/v1/controllers/publisher/PublisherController.ts +++ b/src/server/src/api/v1/controllers/publisher/PublisherController.ts @@ -58,12 +58,24 @@ export class PublisherController extends BaseController { ): Promise { const user = req.jwt?.user; const publisher = await Publisher.findOne({ where: { name: target } }); + if (!user || !publisher) { + return undefined; + } + if (body.revert) { + await PublisherInteraction.destroy({ + where: { + targetId: publisher.id, + type, + userId: user.id, + } + }); + } const interaction = await PublisherInteraction.create({ ...body, remoteAddr: req.ip, - targetId: publisher?.id, + targetId: publisher.id, type, - userId: user?.id, + userId: user.id, }); if (!interaction) { throw new InternalError('Failed to create interaction'); diff --git a/src/server/src/api/v1/controllers/summary/SummaryController.ts b/src/server/src/api/v1/controllers/summary/SummaryController.ts index d3c7f0d7..1ce8dcfc 100644 --- a/src/server/src/api/v1/controllers/summary/SummaryController.ts +++ b/src/server/src/api/v1/controllers/summary/SummaryController.ts @@ -134,6 +134,15 @@ export class SummaryController extends BaseControllerWithPersistentStorageAccess @Body() body: Partial ): Promise { const user = req.jwt?.user; + if (body.revert && user) { + await SummaryInteraction.destroy({ + where: { + targetId, + type, + userId: user.id, + } + }); + } const interaction = await SummaryInteraction.create({ ...body, remoteAddr: req.ip,