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

100% Statements 73/73
100% Branches 1/1
100% Functions 2/2
100% Lines 71/71

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x 1x 1x 1x 1x 1x 67x 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 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 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 datesDateBetween = new AmaliaFunctionDefault<
  [ComputeEngineDayjsInput, ComputeEngineDayjsInput, ComputeEngineDayjsInput],
  boolean
>({
  name: AmaliaFunctionKeys.dateBetween,
  category: AmaliaFunctionCategory.DATES,
  nbParamsRequired: 3,
  description: 'Return true if a date is between two dates (both dates are included)',
 
  exec: (candidateDate, beginDate, endDate) =>
    dayjs(candidateDate, 'X').isBetween(dayjs(beginDate, 'X'), dayjs(endDate, 'X'), undefined, '[]'),
 
  params: [
    {
      name: 'checkDate',
      description:
        'Date to check if in the interval: 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: 'startDate',
      description:
        'Start of the interval: 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: 'endDate',
      description:
        'End of the interval: 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 true',
      formula: 'dateBetween(toDate("2023-10-16"), toDate("2023-10-01"), toDate("2023-11-04"))' as AmaliaFormula,
      result: true,
    },
    {
      desc: 'Returns true',
      formula: 'dateBetween(toDate("2023-04-01"), toDate("2023-04-01"), toDate("2023-04-30"))' as AmaliaFormula,
      result: true,
    },
    {
      desc: 'Returns true if Close Date is between the start and the end of the statement',
      formula:
        'dateBetween(opportunity.closeDate, statementPeriod.startDate, statementPeriod.endDate)' as AmaliaFormula,
    },
  ],
});