All files / libs/payout-collaboration/workflows/shared/src/lib is-closing-period-started.ts

100% Statements 26/26
100% Branches 5/5
100% Functions 1/1
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x 1x 1x 1x 4x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 4x 4x 4x 4x  
import { dayjs } from '@amalia/ext/dayjs';
import { type WorkflowClosingTimeFrame } from '@amalia/payout-collaboration/workflows/types';
 
export const isClosingPeriodStarted = (
  periodStartTimestamp: number,
  closingPeriodStartTimeframe: WorkflowClosingTimeFrame | null,
) => {
  if (!closingPeriodStartTimeframe) {
    return true;
  }
 
  const currentDate = dayjs.utc();
 
  const periodStartDate = dayjs.utc(periodStartTimestamp, 'X');
 
  const dateForecastWithMonthAdded = periodStartDate.utc().add(closingPeriodStartTimeframe.months, 'month');
 
  const numberOfDaysInMonth = dateForecastWithMonthAdded.daysInMonth();
 
  const closingPeriodStartDate =
    closingPeriodStartTimeframe.days >= numberOfDaysInMonth
      ? dateForecastWithMonthAdded.utc().endOf('month').startOf('day')
      : dateForecastWithMonthAdded.utc().add(closingPeriodStartTimeframe.days, 'days');
 
  return currentDate.isAfter(closingPeriodStartDate);
};