Skip to content

Commit

Permalink
fix(GoalCriteria): calc achieve progress through all criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Jul 17, 2023
1 parent fe0fee1 commit a347a57
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/components/CriteriaForm/CriteriaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export const CriteriaForm: React.FC<CriteriaFormProps> = ({ onSubmit, goalId, va
goalId,
title: '',
weight: '',
goalAsGriteria: null,
goalAsGriteria: undefined,
},
});

Expand All @@ -229,7 +229,7 @@ export const CriteriaForm: React.FC<CriteriaFormProps> = ({ onSubmit, goalId, va
reset({
title: '',
weight: '',
goalAsGriteria: null,
goalAsGriteria: undefined,
goalId,
});
}, [goalId, reset]);
Expand Down
2 changes: 1 addition & 1 deletion src/schema/criteria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const criteriaSchema = z.object({
.object({
id: z.string(),
})
.nullish(),
.optional(),
});

export const updateCriteriaState = z.object({
Expand Down
42 changes: 34 additions & 8 deletions trpc/queries/goals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Estimate, EstimateToGoal, Goal, Prisma, StateType } from '@prisma/client';
import { Estimate, EstimateToGoal, Goal, GoalAchieveCriteria, Prisma, State, StateType } from '@prisma/client';

import { QueryWithFilters } from '../../src/schema/common';

Expand Down Expand Up @@ -433,6 +433,38 @@ export const goalDeepQuery = {
},
} as const;

const maxPossibleCriteriaWeight = 100;

const calcAchievedWeight = (list: (GoalAchieveCriteria & { goalAsCriteria: Goal & { state: State } })[]): number => {
const { achivedWithWeight, comletedWithoutWeight, anyWithoutWeight, allWeight } = list.reduce(
(acc, value) => {
acc.allWeight += value.weight;

if (!value.weight) {
acc.anyWithoutWeight += 1;
}
if (value.isDone || (value.goalAsCriteria && value.goalAsCriteria.state?.type === StateType.Completed)) {
acc.achivedWithWeight += value.weight;

if (!value.weight) {
acc.comletedWithoutWeight += 1;
}
}

return acc;
},
{ achivedWithWeight: 0, comletedWithoutWeight: 0, anyWithoutWeight: 0, allWeight: 0 },
);

const remainingtWeight = maxPossibleCriteriaWeight - allWeight;
const quantityByWeightlessCriteria = remainingtWeight / anyWithoutWeight;

return Math.min(
achivedWithWeight + Math.ceil(quantityByWeightlessCriteria * comletedWithoutWeight),
maxPossibleCriteriaWeight,
);
};

export const addCalclulatedGoalsFields = (goal: any, activityId: string) => {
const _isOwner = goal.ownerId === activityId;
const _isParticipant = goal.participants?.some((participant: any) => participant?.id === activityId);
Expand All @@ -442,13 +474,7 @@ export const addCalclulatedGoalsFields = (goal: any, activityId: string) => {
const _lastEstimate = goal.estimate?.length ? goal.estimate[goal.estimate.length - 1].estimate : undefined;
const _shortId = `${goal.projectId}-${goal.scopeId}`;
const _hasAchievementCriteria = goal.goalAchiveCriteria?.length;
const _achivedCriteriaWeight = goal.goalAchiveCriteria?.reduce((sum: number, curr: any) => {
if (curr.isDone || (curr.goalAsCriteria && curr.goalAsCriteria.state?.type === StateType.Completed)) {
return sum + curr.weight;
}

return sum;
}, 0);
const _achivedCriteriaWeight = calcAchievedWeight(goal.goalAchiveCriteria ?? []);

let parentOwner = false;
function checkParent(project?: any) {
Expand Down

0 comments on commit a347a57

Please sign in to comment.