Skip to content

Fix/assign quantifiers only once #222

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

Merged
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- Modify settings architecture so Periods have their own unique settings. New periods are created with the "global" settings as default values. #138 #116
- Verify no quantifiers have been assigned in period before assigning

## [0.2.0] - 2022-03-31

Expand Down
7 changes: 7 additions & 0 deletions packages/api/src/period/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
findPeriodDetailsDto,
getPeriodDateRangeQuery,
getPreviousPeriodEndDate,
verifyAnyPraiseAssigned,
} from './utils';
import { PeriodModel } from './entities';
import { periodDocumentTransformer } from './transformers';
Expand Down Expand Up @@ -412,6 +413,12 @@ export const assignQuantifiers = async (
'Quantifiers can only be assigned on OPEN periods.'
);

const anyPraiseAssigned = await verifyAnyPraiseAssigned(period);
if (anyPraiseAssigned)
throw new BadRequestError(
'Some praise has already been assigned for this period'
);

const assignedQuantifiers: AssignQuantifiersDryRunOutput =
await assignQuantifiersDryRun(req.params.periodId);

Expand Down
29 changes: 28 additions & 1 deletion packages/api/src/period/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { calculateQuantificationsCompositeScore } from '@praise/utils';
import { periodsettingListTransformer } from '@periodsettings/transformers';
import { PeriodSettingsModel } from '@periodsettings/entities';
import { settingValue } from '@shared/settings';
import { sum } from 'lodash';
import { sum, some } from 'lodash';
import mongoose from 'mongoose';
import { PeriodModel } from './entities';
import {
Expand Down Expand Up @@ -186,3 +186,30 @@ export const getPeriodDateRangeQuery = async (
$gt: await getPreviousPeriodEndDate(period),
$lte: period.endDate,
});

/**
* Check if any praise in the given period has already been assigned to quantifiers
*
* @param period
* @returns
*/
export const verifyAnyPraiseAssigned = async (
period: PeriodDocument
): Promise<boolean> => {
const periodDateRangeQuery = await getPeriodDateRangeQuery(period as Period);

const praises = await PraiseModel.find({
createdAt: periodDateRangeQuery,
});

const quantifiersPerPraiseReceiver = (await settingValue(
'PRAISE_QUANTIFIERS_PER_PRAISE_RECEIVER',
period._id
)) as number;

const praisesAssigned = praises.map(
(praise) => praise.quantifications.length === quantifiersPerPraiseReceiver
);

return some(praisesAssigned);
};