import DataMigrationBase from "./DataMigrationBase"; import SortOrder from "Common/Types/BaseDatabase/SortOrder"; import LIMIT_MAX from "Common/Types/Database/LimitMax"; import ProjectService from "Common/Server/Services/ProjectService"; import ScheduledMaintenanceService from "Common/Server/Services/ScheduledMaintenanceService"; import ScheduledMaintenanceStateTimelineService from "Common/Server/Services/ScheduledMaintenanceStateTimelineService"; import QueryHelper from "Common/Server/Types/Database/QueryHelper"; import Project from "Common/Models/DatabaseModels/Project"; import ScheduledMaintenance from "Common/Models/DatabaseModels/ScheduledMaintenance"; import ScheduledMaintenanceStateTimeline from "Common/Models/DatabaseModels/ScheduledMaintenanceStateTimeline"; export default class AddStartDateToScheduledEventsStateTimeline extends DataMigrationBase { public constructor() { super("AddStartDateToScheduledEventsStateTimeline"); } public override async migrate(): Promise { // get all the users with email isVerified true. const projects: Array = await ProjectService.findBy({ query: {}, select: { _id: true, }, skip: 0, limit: LIMIT_MAX, props: { isRoot: true, }, }); for (const project of projects) { // add ended scheduled maintenance state for each of these projects. // first fetch resolved state. Ended state order is -1 of resolved state. const scheduledEvents: Array = await ScheduledMaintenanceService.findBy({ query: { projectId: project.id!, }, select: { _id: true, }, skip: 0, limit: LIMIT_MAX, props: { isRoot: true, }, }); for (const scheduledEvent of scheduledEvents) { const scheduledMaintenanceStateTimelines: Array = await ScheduledMaintenanceStateTimelineService.findBy({ query: { scheduledMaintenanceId: scheduledEvent.id!, startsAt: QueryHelper.isNull(), }, select: { _id: true, createdAt: true, }, skip: 0, limit: LIMIT_MAX, props: { isRoot: true, }, sort: { createdAt: SortOrder.Ascending, }, }); for ( let i: number = 0; i < scheduledMaintenanceStateTimelines.length; i++ ) { const statusTimeline: ScheduledMaintenanceStateTimeline | undefined = scheduledMaintenanceStateTimelines[i]; await ScheduledMaintenanceStateTimelineService.updateOneById({ id: statusTimeline!.id!, data: { startsAt: statusTimeline!.createdAt!, }, props: { isRoot: true, }, }); } } } } public override async rollback(): Promise { return; } }