All files / libs/amalia-lang/formula/evaluate/shared/src/functions/dates/months-ago index.ts

100% Statements 50/50
100% Branches 1/1
100% Functions 1/1
100% Lines 50/50

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 511x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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 datesMonthsAgo = new AmaliaFunctionDefault<[ComputeEngineDayjsInput, number], number>({
  name: AmaliaFunctionKeys.monthsAgo,
  category: AmaliaFunctionCategory.DATES,
  nbParamsRequired: 2,
  description: 'Return a date n months ago from a given date',
 
  exec: (date, numberOfMonths) => dayjs(date, 'X').subtract(numberOfMonths, 'months').unix(),
 
  params: [
    {
      name: 'date',
      description: 'Date: 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']],
    },
    { name: 'numberOfMonthsAgo', description: 'Number of months ago, can be negative for months after' },
  ],
 
  examples: [
    {
      desc: 'Returns "2023-03-19" ',
      formula: 'monthsAgo(toDate("2023-05-19"), 2)' as AmaliaFormula,
      result: 1_679_184_000,
    },
    {
      desc: 'Returns "2023-10-19" ',
      formula: 'monthsAgo(toDate("2023-05-19"), -5)' as AmaliaFormula,
      result: 1_697_673_600,
    },
    {
      desc: 'Returns a date 2 months before the Close Date.',
      formula: 'monthsAgo(opportunity.closeDate, 2)' as AmaliaFormula,
    },
    {
      desc: 'Returns a date 1 month after the statement start date.',
      formula: 'monthsAgo(statementPeriod.startDate, -1)' as AmaliaFormula,
    },
  ],
});