Skip to content

Commit

Permalink
fix: local time zone fix, all last updated times
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Nov 3, 2024
1 parent 57ce2f3 commit 2d3fd7f
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/data/repositories/IncidentActionD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const incidentActionPlanIds = {
} as const;

export type IncidentActionPlanDataValues = {
lastUpdated: Maybe<Date>;
id: string;
iapType: Maybe<string>;
phoecLevel: Maybe<string>;
Expand Down Expand Up @@ -76,6 +77,7 @@ export class IncidentActionD2Repository implements IncidentActionRepository {

private fields = {
event: true,
updatedAt: true,
dataValues: {
dataElement: { id: true, code: true },
value: true,
Expand All @@ -97,7 +99,8 @@ export class IncidentActionD2Repository implements IncidentActionRepository {

const plan: IncidentActionPlanDataValues = mapDataElementsToIncidentActionPlan(
events.instances[0].event,
events.instances[0].dataValues
events.instances[0].dataValues,
events.instances[0].updatedAt
);

return plan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class IncidentManagementTeamD2Repository implements IncidentManagementTea
},
trackedEntity: true,
event: true,
updatedAt: true,
},
})
)
Expand Down
12 changes: 2 additions & 10 deletions src/data/repositories/SystemD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,14 @@ export class SystemD2Repository implements SystemRepository {
new Date(lastAnalyticsTablePartitionSuccess) >
new Date(info.lastAnalyticsTableSuccess)
) {
return getDateAsLocaleDateTimeString(new Date(lastAnalyticsTablePartitionSuccess));
return getDateAsLocaleDateTimeString(lastAnalyticsTablePartitionSuccess);
}
//Else, return the lastAnalyticsTableSuccess time
else if (info.lastAnalyticsTableSuccess) {
return getDateAsLocaleDateTimeString(new Date(info.lastAnalyticsTableSuccess));
return getDateAsLocaleDateTimeString(info.lastAnalyticsTableSuccess.toISOString());
} else {
return "Unable to fetch last analytics runtime";
}

//@ts-ignore
if (info.lastAnalyticsTablePartitionSuccess)
return getDateAsLocaleDateTimeString(
//@ts-ignore
new Date(info.lastAnalyticsTablePartitionSuccess)
);
else return "Unable to fetch last analytics runtime";
});
}
}
9 changes: 7 additions & 2 deletions src/data/repositories/utils/DateTimeHelper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import moment from "moment";
import { Maybe } from "../../../utils/ts-utils";

export function getCurrentTimeString(): string {
Expand Down Expand Up @@ -26,9 +27,9 @@ export function getDateAsMonthYearString(date: Date): string {
}
}

export function getDateAsLocaleDateTimeString(date: Date): string {
export function getDateAsLocaleDateTimeString(date: string): string {
try {
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`;
return new Date(`${date}Z`).toString();
} catch (e) {
console.debug(e);
return "";
Expand All @@ -43,3 +44,7 @@ export function getDateAsLocaleDateString(date: Date): string {
return "";
}
}

export function getISODateAsLocaleDateString(date: string): Date {
return moment.utc(date).local().toDate();
}
10 changes: 7 additions & 3 deletions src/data/repositories/utils/DiseaseOutbreakMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import _ from "../../../domain/entities/generic/Collection";
import { SelectedPick } from "@eyeseetea/d2-api/api";
import { D2TrackedEntityAttributeSchema } from "../../../types/d2-api";
import { D2TrackerEnrollment } from "@eyeseetea/d2-api/api/trackerEnrollments";
import { getCurrentTimeString } from "./DateTimeHelper";
import { getCurrentTimeString, getISODateAsLocaleDateString } from "./DateTimeHelper";

type D2TrackedEntityAttribute = {
trackedEntityAttribute: SelectedPick<
Expand Down Expand Up @@ -47,8 +47,12 @@ export function mapTrackedEntityAttributesToDiseaseOutbreak(
status: trackedEntity.enrollments?.[0]?.status ?? "ACTIVE", //Zebra Outbreak has only one enrollment
name: fromMap("name"),
dataSource: dataSource,
created: trackedEntity.createdAt ? new Date(trackedEntity.createdAt) : undefined,
lastUpdated: trackedEntity.updatedAt ? new Date(trackedEntity.updatedAt) : undefined,
created: trackedEntity.createdAt
? getISODateAsLocaleDateString(trackedEntity.createdAt)
: undefined,
lastUpdated: trackedEntity.updatedAt
? getISODateAsLocaleDateString(trackedEntity.updatedAt)
: undefined,
createdByName: undefined,
hazardType: getHazardTypeByCode(fromMap("hazardType")),
mainSyndromeCode: fromMap("mainSyndrome"),
Expand Down
4 changes: 3 additions & 1 deletion src/data/repositories/utils/IncidentActionMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {

export function mapDataElementsToIncidentActionPlan(
id: Id,
dataValues: DataValue[]
dataValues: DataValue[],
updatedAt: Maybe<string>
): IncidentActionPlanDataValues {
const iapType = getValueById(dataValues, incidentActionPlanIds.iapType);
const phoecLevel = getValueById(dataValues, incidentActionPlanIds.phoecLevel);
Expand All @@ -59,6 +60,7 @@ export function mapDataElementsToIncidentActionPlan(
responseStrategies: responseStrategies,
expectedResults: expectedResults,
responseActivitiesNarrative: responseActivitiesNarrative,
lastUpdated: updatedAt ? new Date(updatedAt) : undefined,
};

return incidentActionPlan;
Expand Down
10 changes: 10 additions & 0 deletions src/data/repositories/utils/IncidentManagementTeamMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SelectedPick } from "@eyeseetea/d2-api/api";
import { D2DataElementSchema } from "@eyeseetea/d2-api/2.36";
import { RTSL_ZEBRA_INCIDENT_MANAGEMENT_TEAM_BUILDER_IDS_WITHOUT_ROLES } from "../consts/IncidentManagementTeamBuilderConstants";
import { Role } from "../../../domain/entities/incident-management-team/Role";
import { getDateAsLocaleDateTimeString, getISODateAsLocaleDateString } from "./DateTimeHelper";

export function mapD2EventsToIncidentManagementTeam(
d2Events: D2TrackerEvent[],
Expand Down Expand Up @@ -48,8 +49,17 @@ export function mapD2EventsToIncidentManagementTeam(
[]
);

const sortedByUpdatedDates = d2Events.sort(function (a, b) {
if (!a.updatedAt) return -1;
if (!b.updatedAt) return 1;
return a.updatedAt < b.updatedAt ? -1 : a.updatedAt > b.updatedAt ? 1 : 0;
});

return new IncidentManagementTeam({
teamHierarchy: teamHierarchy,
lastUpdated: sortedByUpdatedDates[0]?.updatedAt
? getISODateAsLocaleDateString(sortedByUpdatedDates[0]?.updatedAt)
: undefined,
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/domain/entities/incident-action-plan/ActionPlan.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Maybe } from "../../../utils/ts-utils";
import { Struct } from "../generic/Struct";
import { Id } from "../Ref";

export type ActionPlanIAPType = "Initial" | "Update" | "Final";
export type ActionPlanPhoecLevel = "Response" | "Watch" | "Alert";

export type ActionPlanAttrs = {
lastUpdated: Maybe<Date>;
id: Id;
iapType: string;
phoecLevel: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export type IncidentActionOptions = {
};

interface IncidentActionPlanAttrs extends Ref {
lastUpdated: Date;
actionPlan: Maybe<ActionPlan>;
responseActions: ResponseAction[];
incidentActionOptions: IncidentActionOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Maybe } from "../../../utils/ts-utils";
import { Struct } from "../generic/Struct";
import { TeamMember } from "./TeamMember";

interface IncidentManagementTeamAttrs {
lastUpdated: Maybe<Date>;
teamHierarchy: TeamMember[];
}

Expand Down
3 changes: 2 additions & 1 deletion src/domain/usecases/SaveEntityUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { TeamMemberRepository } from "../repositories/TeamMemberRepository";
import { saveDiseaseOutbreak } from "./utils/disease-outbreak/SaveDiseaseOutbreak";
import { RoleRepository } from "../repositories/RoleRepository";
import { Configurations } from "../entities/AppConfigurations";
import moment from "moment";

export class SaveEntityUseCase {
constructor(
Expand Down Expand Up @@ -75,7 +76,7 @@ export class SaveEntityUseCase {
) {
const updatedDiseaseOutbreakEvent: DiseaseOutbreakEventBaseAttrs = {
...diseaseOutbreakEventBase,
lastUpdated: new Date(),
lastUpdated: moment.utc().toDate(),
incidentManagerName: updatedIncidentManager,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function getIncidentAction(
.getIncidentActionPlan(diseaseOutbreakId)
.flatMap(incidentActionPlan => {
const actionPlan = new ActionPlan({
lastUpdated: incidentActionPlan?.lastUpdated,
id: incidentActionPlan?.id ?? "",
iapType: incidentActionPlan?.iapType ?? "",
phoecLevel: incidentActionPlan?.phoecLevel ?? "",
Expand Down Expand Up @@ -72,7 +73,7 @@ export function getIncidentAction(

const incidentAction = new IncidentActionPlan({
id: diseaseOutbreakId,
lastUpdated: new Date(),

actionPlan: actionPlan,
responseActions: responseActions,
incidentActionOptions: {
Expand Down
9 changes: 5 additions & 4 deletions src/webapp/pages/event-tracker/useDiseaseOutbreakEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
DiseaseOutbreakEvent,
} from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import {
getDateAsLocaleDateString,
getDateAsLocaleDateTimeString,
getDateAsMonthYearString,
getISODateAsLocaleDateString,
} from "../../../data/repositories/utils/DateTimeHelper";

import { User } from "../../components/user-selector/UserSelector";
Expand Down Expand Up @@ -72,7 +71,7 @@ export function useDiseaseOutbreakEvent(id: Id) {
{
label: "Last updated",
value: diseaseOutbreakEvent.lastUpdated
? getDateAsLocaleDateTimeString(diseaseOutbreakEvent.lastUpdated)
? diseaseOutbreakEvent.lastUpdated.toString()
: "",
},
dataSourceLabelValue,
Expand Down Expand Up @@ -106,7 +105,9 @@ export function useDiseaseOutbreakEvent(id: Id) {
if (diseaseOutbreakEvent.riskAssessment) {
return diseaseOutbreakEvent.riskAssessment.grading.map(riskAssessmentGrading => ({
riskAssessmentDate: riskAssessmentGrading.lastUpdated
? getDateAsLocaleDateString(riskAssessmentGrading.lastUpdated)
? getISODateAsLocaleDateString(
riskAssessmentGrading.lastUpdated.toISOString()
).toDateString()
: "",
grade: RiskAssessmentGrading.getTranslatedLabel(
riskAssessmentGrading.getGrade().getOrThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ export function mapFormStateToDiseaseOutbreakEventData(
const diseaseOutbreakEventBase: DiseaseOutbreakEventBaseAttrs = {
id: diseaseOutbreakEvent?.id || "",
status: diseaseOutbreakEvent?.status || "ACTIVE",
created: diseaseOutbreakEvent?.created || new Date(),
lastUpdated: diseaseOutbreakEvent?.lastUpdated || new Date(),
created: diseaseOutbreakEvent?.created,
lastUpdated: diseaseOutbreakEvent?.lastUpdated,
createdByName: diseaseOutbreakEvent?.createdByName || currentUserName,
...diseaseOutbreakEventEditableData,
};
Expand Down
5 changes: 3 additions & 2 deletions src/webapp/pages/form-page/mapFormStateToEntityData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ function mapFormStateToDiseaseOutbreakEvent(
const diseaseOutbreakEventBase: DiseaseOutbreakEventBaseAttrs = {
id: diseaseOutbreakEvent?.id || "",
status: diseaseOutbreakEvent?.status || "ACTIVE",
created: diseaseOutbreakEvent?.created || new Date(),
lastUpdated: diseaseOutbreakEvent?.lastUpdated || new Date(),
created: diseaseOutbreakEvent?.created,
lastUpdated: diseaseOutbreakEvent?.lastUpdated,
createdByName: diseaseOutbreakEvent?.createdByName || currentUserName,
...diseaseOutbreakEventEditableData,
};
Expand Down Expand Up @@ -523,6 +523,7 @@ function mapFormStateToIncidentActionPlan(
)?.value as string;

const incidentActionPlan: ActionPlanAttrs = {
lastUpdated: new Date(),
iapType: iapType,
phoecLevel: phoecLevel,
id: formData.entity?.id ?? "",
Expand Down
12 changes: 9 additions & 3 deletions src/webapp/pages/incident-action-plan/useIncidentActionPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useCallback, useEffect, useMemo, useState } from "react";
import { Id } from "../../../domain/entities/Ref";
import { Maybe } from "../../../utils/ts-utils";
import { useAppContext } from "../../contexts/app-context";
import { getDateAsLocaleDateTimeString } from "../../../data/repositories/utils/DateTimeHelper";
import { TableColumn, TableRowType } from "../../components/table/BasicTable";
import {
getIAPTypeByCode,
Expand Down Expand Up @@ -91,7 +90,14 @@ export function useIncidentActionPlan(id: Id) {
const incidentActionExists = !!incidentActionPlan?.actionPlan?.id;
const incidentActionOptions = incidentActionPlan?.incidentActionOptions;
const currentEventTracker = getCurrentEventTracker();
if (incidentActionExists && currentEventTracker) {
if (
incidentActionExists &&
currentEventTracker &&
(currentEventTracker.incidentActionPlan?.actionPlan?.lastUpdated !==
incidentActionPlan.actionPlan?.lastUpdated ||
currentEventTracker.incidentActionPlan?.responseActions.length !==
incidentActionPlan.responseActions.length)
) {
const updatedEventTracker = new DiseaseOutbreakEvent({
...currentEventTracker,
incidentActionPlan: incidentActionPlan,
Expand Down Expand Up @@ -128,7 +134,7 @@ const getIncidentActionFormSummary = (incidentActionPlan: Maybe<IncidentActionPl

const iapTypeCode = incidentActionPlan.actionPlan?.iapType ?? "";
const phoecLevelCode = incidentActionPlan.actionPlan?.phoecLevel ?? "";
const lastUpdated = getDateAsLocaleDateTimeString(new Date()); //TO DO : Fetch sync time from datastore once implemented
const lastUpdated = incidentActionPlan.actionPlan?.lastUpdated?.toString() ?? "";

return [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export function useIMTeamBuilder(id: Id): State {
[incidentManagementTeam?.teamHierarchy, incidentManagementTeamHierarchyItems]
);

const lastUpdated = getDateAsLocaleDateTimeString(new Date()); //TO DO : Fetch sync time from datastore once implemented
const lastUpdated = incidentManagementTeam?.lastUpdated?.toString() ?? "";

return {
globalMessage,
Expand Down

0 comments on commit 2d3fd7f

Please sign in to comment.