diff --git a/packages/rules-engine/src/services/VariableService/VariableService.js b/packages/rules-engine/src/services/VariableService/VariableService.js index 193de2677e..9b596a8190 100644 --- a/packages/rules-engine/src/services/VariableService/VariableService.js +++ b/packages/rules-engine/src/services/VariableService/VariableService.js @@ -85,7 +85,7 @@ export class VariableService { this.defaultValues = defaultValues; - this.structureEvents = getStructureEvents(dateUtils.compareDates); + this.structureEvents = getStructureEvents(dateUtils.compareDates, onProcessValue); } getVariables({ diff --git a/packages/rules-engine/src/services/VariableService/helpers/structureEvents.js b/packages/rules-engine/src/services/VariableService/helpers/structureEvents.js index 7c4fcab7f8..9660397845 100644 --- a/packages/rules-engine/src/services/VariableService/helpers/structureEvents.js +++ b/packages/rules-engine/src/services/VariableService/helpers/structureEvents.js @@ -1,9 +1,11 @@ // @flow +import { typeKeys } from '../../../constants'; import { eventStatuses } from '../constants'; import type { EventData, EventsData, CompareDates, + ProcessValue, } from '../variableService.types'; const createEventsContainer = (events: EventsData) => { @@ -16,20 +18,16 @@ const createEventsContainer = (events: EventsData) => { return { all: events, byStage: eventsDataByStage }; }; -export const getStructureEvents = (compareDates: CompareDates) => { - const compareEvents = (first: EventData, second: EventData): number => { - let result; - if (!first.occurredAt && !second.occurredAt) { - result = 0; - } else if (!first.occurredAt) { - result = 1; - } else if (!second.occurredAt) { - result = -1; - } else { - result = compareDates(first.occurredAt, second.occurredAt); - } - return result; - }; +export const getStructureEvents = (compareDates: CompareDates, processValue: ProcessValue) => { + const compareEvents = (first: EventData, second: EventData): number => + compareDates( + processValue(first.occurredAt, typeKeys.DATE), + processValue(second.occurredAt, typeKeys.DATE), + ) || + compareDates( + processValue(first.createdAt, typeKeys.DATETIME), + processValue(second.createdAt, typeKeys.DATETIME), + ); return (currentEvent: EventData = {}, otherEvents: EventsData = []) => { const otherEventsFiltered = otherEvents @@ -37,8 +35,8 @@ export const getStructureEvents = (compareDates: CompareDates) => { [eventStatuses.COMPLETED, eventStatuses.ACTIVE, eventStatuses.VISITED].includes(event.status) && event.eventId !== currentEvent.eventId, ); - - const events = Object.keys(currentEvent).length !== 0 ? otherEventsFiltered.concat(currentEvent) : otherEventsFiltered; + const events = Object.keys(currentEvent).length ? + otherEventsFiltered.concat(currentEvent) : otherEventsFiltered; const sortedEvents = events.sort(compareEvents); return createEventsContainer(sortedEvents); diff --git a/packages/rules-engine/src/services/VariableService/variableService.types.js b/packages/rules-engine/src/services/VariableService/variableService.types.js index f31caadc2f..9640354c46 100644 --- a/packages/rules-engine/src/services/VariableService/variableService.types.js +++ b/packages/rules-engine/src/services/VariableService/variableService.types.js @@ -1,4 +1,5 @@ // @flow +import { typeof typeKeys } from '../../constants'; import { typeof eventStatuses } from './constants'; import type { DataElements, TrackedEntityAttributes, OrgUnit } from '../../rulesEngine.types'; @@ -89,3 +90,5 @@ export type VariableServiceInput = {| |}; export type CompareDates = (firstRulesDate: ?string, secondRulesDate: ?string) => number; + +export type ProcessValue = (value: any, type: $Values) => any; diff --git a/src/core_modules/capture-core/rules/converters/dateUtils.js b/src/core_modules/capture-core/rules/converters/dateUtils.js index 674d82bcfc..9d4398635f 100644 --- a/src/core_modules/capture-core/rules/converters/dateUtils.js +++ b/src/core_modules/capture-core/rules/converters/dateUtils.js @@ -51,16 +51,10 @@ export const dateUtils: IDateUtils = { return momentToRulesDate(newDateMoment); }, compareDates: (firstRulesDate: ?string, secondRulesDate: ?string): number => { - const diff = dateUtils.daysBetween(secondRulesDate, firstRulesDate); - if (!diff) { - return 0; - } - if (diff < 0) { - return -1; - } - if (diff > 0) { - return 1; - } - return 0; + // Empty input dates will be replaced by "MAX_SAFE_INTEGER" when creating the timestamp. + // This ensures empty input will be bigger than any actual date + const firstDateTimestamp = firstRulesDate ? moment(firstRulesDate).valueOf() : Number.MAX_SAFE_INTEGER; + const secondDateTimestamp = secondRulesDate ? moment(secondRulesDate).valueOf() : Number.MAX_SAFE_INTEGER; + return firstDateTimestamp - secondDateTimestamp; }, };