Skip to content

Commit

Permalink
feat(GoalCriteria): support accounting partial criteria score w/o weight
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Aug 23, 2024
1 parent 2cbdaa2 commit 51d55fc
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/utils/recalculateCriteriaScore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,39 @@ export const baseCalcCriteriaWeight = <
let anyWithoutWeight = 0;
let allWeight = 0;

for (const { deleted, weight, isDone, criteriaGoal } of criteriaList) {
// `where` filter by `deleted` field doesn't work in *Many queries
if (!deleted) {
allWeight += weight;
// `where` filter by `deleted` field doesn't work in *Many queries
const existingCriteriaList = criteriaList.filter(({ deleted }) => !deleted);

if (!weight) {
anyWithoutWeight += 1;
}
for (const { weight, isDone, criteriaGoal } of existingCriteriaList) {
allWeight += weight;

if (!weight) {
anyWithoutWeight += 1;
}

if (isDone || criteriaGoal?.state?.type === StateType.Completed) {
achivedWithWeight += weight;
if (isDone || criteriaGoal?.state?.type === StateType.Completed) {
achivedWithWeight += weight;

if (!weight) {
comletedWithoutWeight += 1;
}
} else if (criteriaGoal != null && criteriaGoal.completedCriteriaWeight != null) {
if (criteriaGoal.completedCriteriaWeight > 0) {
achivedWithWeight += Math.floor((weight / 100) * criteriaGoal.completedCriteriaWeight);
}
if (!weight) {
comletedWithoutWeight += 1;
}
}
}

const remainingtWeight = maxPossibleCriteriaWeight - allWeight;
const quantityByWeightlessCriteria = anyWithoutWeight > 0 ? remainingtWeight / anyWithoutWeight : 0;

// accounting partial criteria goal score
for (const { weight, isDone, criteriaGoal } of existingCriteriaList) {
if (!isDone && criteriaGoal != null && criteriaGoal.completedCriteriaWeight != null) {
const targetWeight = weight || quantityByWeightlessCriteria;

if (criteriaGoal.completedCriteriaWeight > 0) {
achivedWithWeight += Math.floor((targetWeight / 100) * criteriaGoal.completedCriteriaWeight);
}
}
}

return Math.min(
achivedWithWeight + Math.ceil(quantityByWeightlessCriteria * comletedWithoutWeight),
maxPossibleCriteriaWeight,
Expand Down

0 comments on commit 51d55fc

Please sign in to comment.