From 5a25b342f64afb91ccd85725963248f4c08f0740 Mon Sep 17 00:00:00 2001 From: Chika Date: Fri, 14 Oct 2022 12:59:16 +0300 Subject: [PATCH] process/payments: fix seasons payments --- .../payments/lib/__tests__/seasons.test.js | 23 +++++++----- process/payments/lib/seasons.ts | 4 -- process/payments/reactions.ts | 37 +++++++++---------- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/process/payments/lib/__tests__/seasons.test.js b/process/payments/lib/__tests__/seasons.test.js index d538a58..dd15625 100644 --- a/process/payments/lib/__tests__/seasons.test.js +++ b/process/payments/lib/__tests__/seasons.test.js @@ -1,20 +1,25 @@ -import { getSeasonsIndex } from '../seasons'; +import { getPositionIndex } from '../seasons'; describe('process/payments/lib/seasons', () => { - describe('[function] getSeasonsIndex', () => { + describe('[function] getPositionIndex', () => { test('should return 0', () => { - const result = getSeasonsIndex(0); + const result = getPositionIndex(100_000_001, 0); expect(result).toEqual(0); }); - test('should return 7', () => { - const result = getSeasonsIndex(7); - expect(result).toEqual(7); + test('should return 0', () => { + const result = getPositionIndex(100_000_001, 7); + expect(result).toEqual(0); }); - test('should return 7', () => { - const result = getSeasonsIndex(55); - expect(result).toEqual(7); + test('should return 1', () => { + const result = getPositionIndex(100_000_001, 8); + expect(result).toEqual(1); + }); + + test('should return 1', () => { + const result = getPositionIndex(100_000_001, 11); + expect(result).toEqual(1); }); }); }); diff --git a/process/payments/lib/seasons.ts b/process/payments/lib/seasons.ts index 66794fa..2dfd6a2 100644 --- a/process/payments/lib/seasons.ts +++ b/process/payments/lib/seasons.ts @@ -11,10 +11,6 @@ const SEASONS_PERIODS: Record> = { }; export const SEASONS_PERIOD_NUMBER = 12; -export function getSeasonsIndex(paymentsIndex: number) { - return paymentsIndex - Math.floor(paymentsIndex / SEASONS_PERIOD_NUMBER) * SEASONS_PERIOD_NUMBER; -} - export function getPositionIndex(seasonType: SeasonType, seasonsIndex: number) { let positionIndex = 0; diff --git a/process/payments/reactions.ts b/process/payments/reactions.ts index 4ee4d7e..fe76c1f 100644 --- a/process/payments/reactions.ts +++ b/process/payments/reactions.ts @@ -9,6 +9,7 @@ import type RootStore from 'stores/root'; import type { Row } from 'stores/tables/payments/types'; import { difference, shift } from 'tools/array'; import * as seasonsTools from './lib/seasons'; +import { SEASONS_PERIOD_NUMBER } from './lib/seasons'; import validatePaymentsTable from './validation'; export default function paymentsReactions( @@ -409,11 +410,10 @@ export default function paymentsReactions( leasingPeriod, shift(seasons, shiftNumber) ); - const rows: Row[] = payments.map((value) => ({ + const rows: Row[] = payments.map((value, i) => ({ value, - status: 'Default', + status: i < SEASONS_PERIOD_NUMBER ? 'Default' : 'Disabled', })); - const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); @@ -434,10 +434,11 @@ export default function paymentsReactions( reaction( () => { const payments = toJS($tables.payments.values); + const seasons = payments.slice(1, SEASONS_PERIOD_NUMBER + 1); - return payments; + return seasons; }, - (nextPayments, prevPayments) => { + (nextSeasons, prevSeasons) => { const graphType = $calculation.getElementValue('radioGraphType'); if (graphType !== 100_000_003) return; @@ -446,21 +447,17 @@ export default function paymentsReactions( if (!seasonType || !highSeasonStartOption) return; const shiftNumber = Number.parseInt(highSeasonStartOption.label, 10) - 2; - const unshiftedNextPayments = shift(nextPayments.slice(1, -1), -shiftNumber); - const unshiftedPrevPayments = shift(prevPayments.slice(1, -1), -shiftNumber); + const unshiftedNextSeasons = shift(nextSeasons, -shiftNumber); + const unshiftedPrevSeasons = shift(prevSeasons, -shiftNumber); - const changes = difference(unshiftedNextPayments, unshiftedPrevPayments); + const changes = difference(unshiftedNextSeasons, unshiftedPrevSeasons); if (changes === null || changes.length > 1) return; + const [changeIndex] = changes; + const positionIndex = seasonsTools.getPositionIndex(seasonType, changeIndex); - const nextSeasons = unshiftedNextPayments.slice(0, seasonsTools.SEASONS_PERIOD_NUMBER + 1); - const values = seasonsTools.getSeasonsValues(seasonType, nextSeasons); - - const positionIndex = seasonsTools.getPositionIndex( - seasonType, - seasonsTools.getSeasonsIndex(changeIndex) - ); - values[positionIndex] = unshiftedNextPayments[changeIndex]; + const values = seasonsTools.getSeasonsValues(seasonType, unshiftedNextSeasons); + values[positionIndex] = unshiftedNextSeasons[changeIndex]; const seasons = seasonsTools.generateSeasons(seasonType, values); const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); @@ -468,11 +465,11 @@ export default function paymentsReactions( leasingPeriod, shift(seasons, shiftNumber) ); - const rows: Row[] = payments.map((value) => ({ - value, - status: 'Default', - })); + const rows: Row[] = payments.map((value, i) => ({ + value, + status: i < SEASONS_PERIOD_NUMBER ? 'Default' : 'Disabled', + })); const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc');