All files / libs/amalia-lang/formula/evaluate/shared/src/functions/misc/match-users-assigned index.ts

100% Statements 83/83
33.33% Branches 1/3
33.33% Functions 1/3
100% Lines 83/83

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 72 73 74 75 76 77 78 79 80 81 82 83 841x 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 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 {
  type ComputedFunctionArgs_MATCH_USERS_ASSIGNED,
  type ComputeEngineResult,
} from '@amalia/payout-calculation/types';
 
import { CalculationParser } from '../../../CalculationParser';
import { AmaliaFunctionRawArgs } from '../../AmaliaFunction';
import { getValueOrFormula } from '../../utils';
 
export const miscMatchUsersAssigned = new AmaliaFunctionRawArgs<ComputeEngineResult>({
  name: AmaliaFunctionKeys.MATCH_USERS_ASSIGNED,
  category: AmaliaFunctionCategory.MISC,
 
  nbParamsRequired: 3,
  description: 'Return true if the ID of a team member matches a field or property',
 
  execMock: () => 1,
 
  generateComputedFunctionResult: (args) =>
    ({
      scope: getValueOrFormula(args[0]),
      userField: getValueOrFormula(args[1]),
      recordOwner: getValueOrFormula(args[2]),
      date: args[3] ? getValueOrFormula<string>(args[3]) : undefined,
      filterCurrentUserAssignments: args[4] ? getValueOrFormula<boolean>(args[4]) : undefined,
    }) satisfies ComputedFunctionArgs_MATCH_USERS_ASSIGNED,
 
  exec: (args, _, scope) => CalculationParser.getFunctionResult(args, scope, AmaliaFunctionKeys.MATCH_USERS_ASSIGNED),
 
  params: [
    {
      name: 'role.scope.depth',
      description: `Range of users to match for depending on role, scope and depth:
      - Role can be EMPLOYEE, MANAGER or MEMBER (both).
      - Scope can be ALL or SUB (OPTIONAL); by default it is ALL.
      - Depth is any integer for the range of levels to include from the location of the current user with 0 representing infinity (OPTIONAL); by default it's 0.`,
    },
    {
      name: 'userField',
      description: 'Field of the user, to compare the record to such as user.externalId.',
    },
    {
      name: 'recordOwner',
      description: 'Field of the record containing the owner.',
    },
    {
      name: 'date',
      description:
        'Field containing the date to check against assignments (OPTIONAL); by default it is not filtered on a date.',
      defaultValue: null,
    },
    {
      name: 'filterCurrentUserAssignments',
      description:
        'Either true or false; if true, filter results by current user assignment (OPTIONAL); by default it is true.',
      defaultValue: true,
    },
  ],
 
  examples: [
    {
      desc: `If the user is a manager, then returns true if the opportunity owner is one of the members (employee or manager) of the user's immediate team or of all children teams.
      If the user is an employee, then returns true if the opportunity owner is one of the members (employee or manager) on the user's immediate team.`,
      formula: 'MATCH_USERS_ASSIGNED("MEMBER", user.externalId, opportunity.owner)' as AmaliaFormula,
    },
    {
      desc: "If the user is a manager, then returns true if the opportunity owner is a manager of the user's subteams. If the user is an employee, then it returns false.",
      formula: 'MATCH_USERS_ASSIGNED("MANAGER.SUB.1", user.externalId, opportunity.owner)' as AmaliaFormula,
    },
    {
      desc: `If the user is a manager, then returns true if the opportunity owner is a member (employee or manager) of the user's immediate team and the children team one level below.
        If the user is an employee, then returns true only if the opportunity owner is a member of my immediate team.`,
      formula: 'MATCH_USERS_ASSIGNED("MEMBER.ALL.2", user.externalId, opportunity.owner)' as AmaliaFormula,
    },
    {
      desc: `If the user is a manager, then returns true if the opportunity owner is an employee of the user's immediate team or in all children teams even if they weren't the team manager at the time of signature date.
        If the user is an employee, then returns true if the opportunity owner is an employee on the user's immediate team.`,
      formula:
        'MATCH_USERS_ASSIGNED("EMPLOYEE", user.externalId, opportunity.owner, opportunity.signatureDate, false)' as AmaliaFormula,
    },
  ],
});