Skip to content

Commit

Permalink
fix: [DHIS2-18004] sort events in rules engine by occurredAt and crea…
Browse files Browse the repository at this point in the history
…tedAt (#3788)
  • Loading branch information
JoakimSM authored Oct 1, 2024
1 parent 380f6b6 commit 2bb485e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class VariableService {

this.defaultValues = defaultValues;

this.structureEvents = getStructureEvents(dateUtils.compareDates);
this.structureEvents = getStructureEvents(dateUtils.compareDates, onProcessValue);
}

getVariables({
Expand Down
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -16,29 +18,25 @@ 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
.filter(event => event.occurredAt &&
[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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow
import { typeof typeKeys } from '../../constants';
import { typeof eventStatuses } from './constants';
import type { DataElements, TrackedEntityAttributes, OrgUnit } from '../../rulesEngine.types';

Expand Down Expand Up @@ -89,3 +90,5 @@ export type VariableServiceInput = {|
|};

export type CompareDates = (firstRulesDate: ?string, secondRulesDate: ?string) => number;

export type ProcessValue = (value: any, type: $Values<typeKeys>) => any;
16 changes: 5 additions & 11 deletions src/core_modules/capture-core/rules/converters/dateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};

0 comments on commit 2bb485e

Please sign in to comment.