Skip to content

Commit

Permalink
fix(Goal): calculate last state comment
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Jul 23, 2024
1 parent 6d19c0a commit 7729cf1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 50 deletions.
50 changes: 1 addition & 49 deletions src/utils/db/calculatedGoalsFields.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Activity, Reaction, Role, User, Ghost, Comment, State } from '@prisma/client';
import { Role } from '@prisma/client';

import { calcAchievedWeight } from '../recalculateCriteriaScore';
import { ReactionsMap } from '../../types/reactions';
import { safeGetUserName } from '../getUserName';

interface UserActivity {
activity: Activity & { user: User; ghost: Ghost | null };
}

export const addCommonCalculatedGoalFields = (goal: any) => {
const _shortId = `${goal.projectId}-${goal.scopeId}`;
Expand All @@ -16,52 +10,10 @@ export const addCommonCalculatedGoalFields = (goal: any) => {
? calcAchievedWeight(goal.goalAchieveCriteria)
: goal.completedCriteriaWeight;

const lastCommentWithUpdateState: Comment &
UserActivity & { reactions: (Reaction & UserActivity)[]; state: State } = goal.comments?.[0];

let reactions: ReactionsMap = {};
if (lastCommentWithUpdateState) {
const limit = 10;
reactions = lastCommentWithUpdateState.reactions?.reduce<ReactionsMap>((acc, cur) => {
const data = {
activityId: cur.activityId,
name: safeGetUserName(cur.activity),
};

if (acc[cur.emoji]) {
acc[cur.emoji].count += 1;
acc[cur.emoji].authors.push(data);
} else {
acc[cur.emoji] = {
count: 1,
authors: [data],
remains: 0,
};
}

return acc;
}, {});

for (const key in reactions) {
if (key in reactions) {
const { authors } = reactions[key];

if (authors.length > limit) {
reactions[key].authors = authors.slice(0, limit);
reactions[key].remains = authors.length - limit;
}
}
}
}

return {
_shortId,
_hasAchievementCriteria,
_achivedCriteriaWeight,
_lastComment: {
...lastCommentWithUpdateState,
reactions,
},
};
};

Expand Down
3 changes: 2 additions & 1 deletion trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { parseCrewLoginFromText } from '../../src/utils/crew';
import { getLocalUsersByCrewLogin } from '../../src/utils/db/crewIntegration';
import { getShortId } from '../../src/utils/getShortId';
import { criteriaQuery } from '../queries/criteria';
import { extendQuery } from '../utils';
import { applyLastStateUpdateComment, extendQuery } from '../utils';
import { commentsByGoalIdQuery, reactionsForGoalComments } from '../queries/comments';
import { ReactionsMap } from '../../src/types/reactions';
import { safeGetUserName } from '../../src/utils/getUserName';
Expand Down Expand Up @@ -354,6 +354,7 @@ export const goal = router({
return {
...goal,
...addCalculatedGoalsFields(goal, activityId, role),
...applyLastStateUpdateComment(goal),
_hasPrivateDeps,
_project: goal.project ? addCalculatedProjectFields(goal.project, activityId, role) : null,
_relations: makeGoalRelationMap(
Expand Down
54 changes: 54 additions & 0 deletions trpc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { ColumnType } from 'kysely';
import prisma from '@prisma/client';

import { Timestamp } from '../generated/kysely/types';
import { ReactionsMap } from '../src/types/reactions';
import { safeGetUserName } from '../src/utils/getUserName';

import { db } from './connection/kysely';

interface UserActivity {
activity: prisma.Activity & { user: prisma.User; ghost: prisma.Ghost | null };
}

export type DBQuery = ReturnType<(typeof db)['selectFrom']>;

export const extendQuery = <T extends DBQuery>(qb: T, ...extenders: Array<(arg: T) => T>): T => {
Expand Down Expand Up @@ -35,3 +42,50 @@ export type ExtractTypeFromGenerated<T> = {
? Date | null
: T[K];
};

export const applyLastStateUpdateComment = (goal: any) => {
const lastCommentWithUpdateState: prisma.Comment &
UserActivity & { reactions: (prisma.Reaction & UserActivity)[]; state: prisma.State } = goal.comments?.[0];

let reactions: ReactionsMap = {};
if (lastCommentWithUpdateState) {
const limit = 10;
reactions = lastCommentWithUpdateState.reactions?.reduce<ReactionsMap>((acc, cur) => {
const data = {
activityId: cur.activityId,
name: safeGetUserName(cur.activity),
};

if (acc[cur.emoji]) {
acc[cur.emoji].count += 1;
acc[cur.emoji].authors.push(data);
} else {
acc[cur.emoji] = {
count: 1,
authors: [data],
remains: 0,
};
}

return acc;
}, {});

for (const key in reactions) {
if (key in reactions) {
const { authors } = reactions[key];

if (authors.length > limit) {
reactions[key].authors = authors.slice(0, limit);
reactions[key].remains = authors.length - limit;
}
}
}
}

return {
_lastComment: {
...lastCommentWithUpdateState,
reactions,
},
};
};

0 comments on commit 7729cf1

Please sign in to comment.