Skip to content

Commit

Permalink
fix(GoalHistory): fix ids string join and split
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Aug 1, 2023
1 parent 4052e3a commit d42a075
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ export const changeGoalProject = async (id: string, newProjectId: string) => {

type RequestParamsBySubject = { [K in keyof HistoryRecordSubject]?: { ids: string[]; sourceIdx: number[] } };

export const goalHistorySeparator = ', ';

export const getGoalHistory = async <T extends GoalHistory & { activity: Activity & { user: User | null } }>(
history: T[],
goalId: string,
) => {
const requestParamsBySubjects = history.reduce<RequestParamsBySubject>(
(acc, { subject, previousValue, nextValue }, index) => {
const allValues = (previousValue ?? '')
.split(',')
.concat((nextValue ?? '').split(','))
.split(goalHistorySeparator)
.concat((nextValue ?? '').split(goalHistorySeparator))
.filter(Boolean);

if (subjectToEnumValue(subject)) {
Expand Down Expand Up @@ -157,7 +159,7 @@ export const getGoalHistory = async <T extends GoalHistory & { activity: Activit
const query = {
where: {
id: {
in: (data || {}).ids,
in: (data || {}).ids?.map((id) => id.trim()),
},
},
};
Expand Down
5 changes: 3 additions & 2 deletions trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
mixHistoryWithComments,
makeGoalRelationMap,
updateGoalWithCalculatedWeight,
goalHistorySeparator,
} from '../../src/utils/db';
import { createEmailJob } from '../../src/utils/worker/create';
import { calculateDiffBetweenArrays } from '../../src/utils/calculateDiffBetweenArrays';
Expand Down Expand Up @@ -511,8 +512,8 @@ export const goal = router({
}

if (tagsToConnect.length || tagsToDisconnect.length) {
const prevIds = actualGoal.tags.map(({ id }) => id).join(', ');
const nextIds = input.tags.map(({ id }) => id).join(', ');
const prevIds = actualGoal.tags.map(({ id }) => id).join(goalHistorySeparator);
const nextIds = input.tags.map(({ id }) => id).join(goalHistorySeparator);

history.push({
subject: 'tags',
Expand Down

0 comments on commit d42a075

Please sign in to comment.