From 03b9d3729a09522e37f1657b6efdb5846ae0553f Mon Sep 17 00:00:00 2001 From: kristoferlund Date: Fri, 18 Nov 2022 13:18:17 +0100 Subject: [PATCH] Add check when reassigning quantifiers: Replacement quantifier cannot be assigned to quantify their own received praise. --- .../api/src/period/controllers/assignment.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/api/src/period/controllers/assignment.ts b/packages/api/src/period/controllers/assignment.ts index 068a6aaec..0a2efdc7a 100644 --- a/packages/api/src/period/controllers/assignment.ts +++ b/packages/api/src/period/controllers/assignment.ts @@ -10,6 +10,7 @@ import { PraiseModel } from '@/praise/entities'; import { EventLogTypeKey } from '@/eventlog/types'; import { logEvent } from '@/eventlog/utils'; import { praiseListTransformer } from '@/praise/transformers'; +import { UserAccountModel } from '@/useraccount/entities'; import { PeriodDetailsDto, PeriodStatusType, @@ -217,7 +218,23 @@ export const replaceQuantifier = async ( // Original quantifier 'quantifications.quantifier': currentQuantifierId, - }).distinct('_id'); + }).lean(); + + const newQuantifierAccounts = await UserAccountModel.find({ + user: newQuantifierId, + }).lean(); + + if (newQuantifierAccounts) { + affectedPraiseIds.find((p) => { + for (const ua of newQuantifierAccounts) { + if (ua._id.equals(p.receiver)) { + throw new BadRequestError( + 'Replacement quantifier cannot be assigned to quantify their own received praise.' + ); + } + } + }); + } await PraiseModel.updateMany( {