Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check when reassigning quantifiers: Replacement quantifier cannot be assigned to quantify their own received praise. #681

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/api/src/period/controllers/assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
{
Expand Down