-
Notifications
You must be signed in to change notification settings - Fork 22
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
keyword search for quantifiers & Quantify multiple praise #499
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
937b648
Added option to quantify multiple praises at once
nebs-dev 086e226
Change multiple quantify button & component name
nebs-dev ac0ff3f
Moved select all checkbox, fixed issue with duplicate praise scores, …
nebs-dev 6ad48cb
Merge branch 'main' into enh/quantifiers_keyword_search
nebs-dev da5b6d6
Fix: Added InternalServerError import
kristoferlund 7e27097
move quantify logic from controller to utils, modify icon buttons, fi…
nebs-dev 013ebff
Merge branch 'enh/quantifiers_keyword_search' of https://github.com/c…
nebs-dev 7acf2a1
Fix: Margins and colours
kristoferlund 316f94a
Add: IconRoundButton
kristoferlund 4cdb995
Fix: removed round button code from IconButton
kristoferlund e697acd
Fix: Remove unused imports
kristoferlund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { BadRequestError, NotFoundError } from '@error/errors'; | ||
import { EventLogTypeKey } from '@eventlog/types'; | ||
import { logEvent } from '@eventlog/utils'; | ||
import { PeriodStatusType } from '@period/types'; | ||
import { PraiseModel } from '@praise/entities'; | ||
import { PraiseDocument } from '@praise/types'; | ||
import { getPraisePeriod } from '@praise/utils/core'; | ||
import { UserDocument } from '@user/types'; | ||
import { Types } from 'mongoose'; | ||
|
||
interface BodyParams { | ||
score: number; | ||
dismissed?: boolean; | ||
duplicatePraise?: string; | ||
} | ||
|
||
interface QuantifyPraiseProps { | ||
id: string; | ||
bodyParams: BodyParams; | ||
currentUser: UserDocument; | ||
} | ||
|
||
export const quantifyPraise = async ({ | ||
id, | ||
bodyParams, | ||
currentUser, | ||
}: QuantifyPraiseProps): Promise<PraiseDocument[]> => { | ||
const { score, dismissed, duplicatePraise } = bodyParams; | ||
|
||
const praise = await PraiseModel.findById(id).populate( | ||
'giver receiver forwarder' | ||
); | ||
if (!praise) throw new NotFoundError('Praise'); | ||
|
||
const period = await getPraisePeriod(praise); | ||
if (!period) | ||
throw new BadRequestError('Praise does not have an associated period'); | ||
|
||
if (period.status !== PeriodStatusType.QUANTIFY) | ||
throw new BadRequestError( | ||
'Period associated with praise does have status QUANTIFY' | ||
); | ||
|
||
const quantification = praise.quantifications.find((q) => | ||
q.quantifier.equals(currentUser._id) | ||
); | ||
|
||
if (!quantification) | ||
throw new BadRequestError('User not assigned as quantifier for praise.'); | ||
|
||
let eventLogMessage = ''; | ||
|
||
// Collect all affected praises (i.e. any praises whose scoreRealized will change as a result of this change) | ||
const affectedPraises: PraiseDocument[] = [praise]; | ||
|
||
const praisesDuplicateOfThis = await PraiseModel.find({ | ||
quantifications: { | ||
$elemMatch: { | ||
quantifier: currentUser._id, | ||
duplicatePraise: praise._id, | ||
}, | ||
}, | ||
}).populate('giver receiver forwarder'); | ||
|
||
if (praisesDuplicateOfThis?.length > 0) | ||
affectedPraises.push(...praisesDuplicateOfThis); | ||
|
||
// Modify praise quantification values | ||
if (duplicatePraise) { | ||
if (duplicatePraise === praise._id.toString()) | ||
throw new BadRequestError('Praise cannot be a duplicate of itself'); | ||
|
||
const dp = await PraiseModel.findById(duplicatePraise); | ||
if (!dp) throw new BadRequestError('Duplicate praise item not found'); | ||
|
||
if (praisesDuplicateOfThis?.length > 0) | ||
throw new BadRequestError( | ||
'Praise cannot be marked duplicate when it is the original of another duplicate' | ||
); | ||
|
||
const praisesDuplicateOfAnotherDuplicate = await PraiseModel.find({ | ||
_id: duplicatePraise, | ||
quantifications: { | ||
$elemMatch: { | ||
quantifier: currentUser._id, | ||
duplicatePraise: { $exists: 1 }, | ||
}, | ||
}, | ||
}); | ||
|
||
if (praisesDuplicateOfAnotherDuplicate?.length > 0) | ||
throw new BadRequestError( | ||
'Praise cannot be marked duplicate of another duplicate' | ||
); | ||
|
||
quantification.score = 0; | ||
quantification.dismissed = false; | ||
quantification.duplicatePraise = dp._id; | ||
|
||
eventLogMessage = `Marked the praise with id "${( | ||
praise._id as Types.ObjectId | ||
).toString()}" as duplicate of the praise with id "${( | ||
dp._id as Types.ObjectId | ||
).toString()}"`; | ||
} else if (dismissed) { | ||
quantification.score = 0; | ||
quantification.dismissed = true; | ||
quantification.duplicatePraise = undefined; | ||
|
||
eventLogMessage = `Dismissed the praise with id "${( | ||
praise._id as Types.ObjectId | ||
).toString()}"`; | ||
} else { | ||
quantification.score = score; | ||
quantification.dismissed = false; | ||
quantification.duplicatePraise = undefined; | ||
|
||
eventLogMessage = `Gave a score of ${ | ||
quantification.score | ||
} to the praise with id "${(praise._id as Types.ObjectId).toString()}"`; | ||
} | ||
|
||
await praise.save(); | ||
|
||
await logEvent( | ||
EventLogTypeKey.QUANTIFICATION, | ||
eventLogMessage, | ||
{ | ||
userId: currentUser._id, | ||
}, | ||
period._id | ||
); | ||
|
||
return affectedPraises; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function will need to have same logic used in the
quantify
function to determine all the praises with an affectedscoreRealized
. It probably makes sense to pull that logic out into a utility function and use it in both controllers.The reason for that logic is so the "Duplicate Score" display is updated when the original praise's score is updated. See the screencast below:
Kazam_screencast_00003.mp4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
☝️☝️ @nebs-dev, this remains to be done.