Skip to content

Commit

Permalink
Add scoring support to api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoferlund committed Dec 14, 2023
1 parent 0aaad2d commit 814650e
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions packages/api/src/praise/services/praise.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,15 @@ export class PraiseService {
data: PraiseCreateInputDto | PraiseForwardInputDto,
): Promise<Praise[]> => {
let forwarder: UserAccount | undefined;
const { giver, receiverIds, reason, reasonRaw, sourceId, sourceName } =
data;
const {
giver,
receiverIds,
reason,
reasonRaw,
sourceId,
sourceName,
score,
} = data;
if ('forwarder' in data) {
const { forwarder: forwarderFromDto } = data as PraiseForwardInputDto;
forwarder = forwarderFromDto;
Expand Down Expand Up @@ -274,6 +281,16 @@ export class PraiseService {
})
.lean();

const directQuantificationEnanbledSetting =
(await this.settingsService.findOneByKey(
'DISCORD_BOT_DIRECT_PRAISE_QUANTIFICATION_ENABLED',
)) as Setting;

const directQuantificationEnabled =
directQuantificationEnanbledSetting?.value === 'true' &&
score &&
score > 0;

const newPraise = await this.praiseModel.insertMany(
receivers.map((receiver) => ({
reason,
Expand All @@ -283,13 +300,27 @@ export class PraiseService {
sourceId,
sourceName,
receiver: receiver._id,
score: directQuantificationEnabled ? score : undefined,
})),
);

const findPraisePromises = newPraise.map((praise) =>
this.findOneById(praise._id),
);

return Promise.all(findPraisePromises);
const createdPraise = Promise.all(findPraisePromises);

if (directQuantificationEnabled) {
await this.quantificationModel.insertMany(
newPraise.map((praise) => ({
score: data.score,
scoreRealized: data.score,
praise: praise._id,
quantifier: giverAccount.user,
})),
);
}

return createdPraise;
};
}

0 comments on commit 814650e

Please sign in to comment.