All files / libs/amalia-lang/formula/evaluate/shared/src/functions/dates/end-of-semester index.ts

98.27% Statements 57/58
80% Branches 4/5
100% Functions 2/2
100% Lines 55/55

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x 1x 1x 1x 1x 1x 1x 1x 67x 68x 68x 68x 68x 68x 68x 68x 80x 80x 80x 13x 13x 13x 13x 13x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 1x  
import { AmaliaFunctionCategory, AmaliaFunctionKeys, type AmaliaFormula } from '@amalia/amalia-lang/formula/types';
import { TokenType } from '@amalia/amalia-lang/tokens/types';
import { FormatsEnum } from '@amalia/data-capture/fields/types';
import { dayjs } from '@amalia/ext/dayjs';
 
import { AmaliaFunctionDefault } from '../../AmaliaFunction';
import { type ComputeEngineDayjsInput } from '../dates.types';
 
export const datesEndOfSemester = new AmaliaFunctionDefault<[ComputeEngineDayjsInput], number>({
  name: AmaliaFunctionKeys.endOfSemester,
  category: AmaliaFunctionCategory.DATES,
 
  nbParamsRequired: 1,
  description: 'Return the last day of semester of a given date',
 
  exec: (timestamp: ComputeEngineDayjsInput): number => {
    const date = dayjs(timestamp, 'X');
 
    return dayjs(
      date
        .month(date.month() >= 6 ? 9 : 3)
        .endOf('quarter')
        .startOf('day'),
    ).unix();
  },
 
  params: [
    {
      name: 'date',
      description:
        'Date to apply function on: variables, properties, fields, keywords or date with format toDate("YYYY-MM-DD")',
      validTokenTypes: [TokenType.VARIABLE, TokenType.PROPERTY, TokenType.FIELD, TokenType.KEYWORD, TokenType.FUNCTION],
      validTokenValues: {
        [TokenType.FUNCTION]: [AmaliaFunctionKeys.toDate],
      },
      validFormats: [FormatsEnum.date, FormatsEnum['date-time']],
    },
  ],
 
  examples: [
    {
      desc: 'Returns "2023-12-31"',
      formula: 'endOfSemester(toDate("2023-07-19"))' as AmaliaFormula,
      result: 1_703_980_800,
    },
    {
      desc: "Returns last day of semester of the user's plan assignment start date.",
      formula: 'endOfSemester(planAssignement.effectiveAsOf)' as AmaliaFormula,
    },
    {
      desc: 'Returns last day of semester of opportunity close date.',
      formula: 'endOfSemester(opportunity.closeDate)' as AmaliaFormula,
    },
  ],
});