Skip to content

Commit

Permalink
ロジックを修正
Browse files Browse the repository at this point in the history
  • Loading branch information
gentksb committed Jul 4, 2024
1 parent e3e1768 commit 232e676
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,28 @@ export const isExpenseEligibleForSplitting = ({
return false;
}

const { cost, users } = expense;
const { cost, users, created_at } = expense;
const splitRate = parseFloat(
(parseInt(users?.[0]?.owed_share ?? "0") / parseInt(cost)).toPrecision(2)
);
const isCurrentMonth = (created_at: string | undefined) => {
if (created_at) {
return new Date(created_at).getMonth() === new Date().getMonth();
}
console.error("created_atが不正です", created_at);
return false;
};

// グループIDが一致し、割り勘でない、かつ、割り勘率が0,1,USER1_RATE,USER2_RATE以外の場合は処理対象とする
// payment:true -> 精算レコード(個々の支払い終了ではない)
// 一度いじったものを再度いじらないようにするため、payment以前の対象は除外したいが判定が難しいので今月内のものだけを対象とする
return (
expense.payment === false &&
expense.group_id?.toString() === SPLITWISE_GROUP_ID &&
splitRate !== 0 &&
splitRate !== 1 &&
splitRate !== parseFloat(USER1_RATE) &&
splitRate !== parseFloat(USER2_RATE)
splitRate !== parseFloat(USER2_RATE) &&
isCurrentMonth(created_at)
);
};
11 changes: 11 additions & 0 deletions test/splitExpense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ describe("補正対象判定処理テスト", () => {
isExpenseEligibleForSplittingWrapper(nonTargetGroupExpense)
).toBeFalsy();
});

test("前月のデータは対象としない", () => {
const nonTargetGroupExpense: components["schemas"]["expense"] = {
...basicExpense,
created_at: "2021-08-31T00:00:00Z",
};
expect(
isExpenseEligibleForSplittingWrapper(nonTargetGroupExpense)
).toBeFalsy();
});
});

describe("割り勘補正処理テスト", () => {
Expand Down Expand Up @@ -177,6 +187,7 @@ const basicExpense: components["schemas"]["expense"] = {
},
],
payment: false,
created_at: new Date().toISOString(),
};

const reSplittedExpenseBalance = {
Expand Down

0 comments on commit 232e676

Please sign in to comment.