All files / libs/payout-collaboration/workflows/shared/src/lib get-closing-period-start-date.ts

0% Statements 0/24
0% Branches 0/1
0% Functions 0/1
0% Lines 0/24

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                                                 
import { dayjs } from '@amalia/ext/dayjs';
import { type WorkflowDefinition } from '@amalia/payout-collaboration/workflows/types';

export const getClosingPeriodStartDate = (
  periodStartTimestamp: number,
  closingPeriodStartTimeframe: WorkflowDefinition['closingStartProps'],
) => {
  if (!closingPeriodStartTimeframe) {
    return null;
  }

  const periodStartDate = dayjs.utc(periodStartTimestamp, 'X');

  const dateForecastWithMonthAdded = periodStartDate.utc().add(closingPeriodStartTimeframe.months, 'month');

  const numberOfDaysInMonth = dateForecastWithMonthAdded.daysInMonth();

  const forecastedStartDate =
    closingPeriodStartTimeframe.days >= numberOfDaysInMonth
      ? dateForecastWithMonthAdded.utc().endOf('month').startOf('day')
      : dateForecastWithMonthAdded.utc().add(closingPeriodStartTimeframe.days, 'days');

  return forecastedStartDate.utc().toDate();
};