Skip to content

Commit

Permalink
feat: add incidentNumber field to Incident model and implement data m…
Browse files Browse the repository at this point in the history
…igration for existing incidents
  • Loading branch information
simlarsen committed Jan 12, 2025
1 parent 1f3cdc0 commit 12e67a0
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 25 deletions.
5 changes: 1 addition & 4 deletions Common/Models/DatabaseModels/Incident.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,6 @@ export default class Incident extends BaseModel {
})
public telemetryQuery?: TelemetryQuery = undefined;


@ColumnAccessControl({
create: [
Permission.ProjectOwner,
Expand All @@ -1101,9 +1100,7 @@ export default class Incident extends BaseModel {
Permission.ProjectMember,
Permission.ReadProjectIncident,
],
update: [

],
update: [],
})
@Index()
@TableColumn({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class MigrationName1736675947746 implements MigrationInterface {
public name = 'MigrationName1736675947746'
public name = "MigrationName1736675947746";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "Incident" ADD "incidentNumber" integer`);
await queryRunner.query(`CREATE INDEX "IDX_0eca9ce7d12a4c472386dfc781" ON "Incident" ("incidentNumber") `);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "public"."IDX_0eca9ce7d12a4c472386dfc781"`);
await queryRunner.query(`ALTER TABLE "Incident" DROP COLUMN "incidentNumber"`);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "Incident" ADD "incidentNumber" integer`,
);
await queryRunner.query(
`CREATE INDEX "IDX_0eca9ce7d12a4c472386dfc781" ON "Incident" ("incidentNumber") `,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX "public"."IDX_0eca9ce7d12a4c472386dfc781"`,
);
await queryRunner.query(
`ALTER TABLE "Incident" DROP COLUMN "incidentNumber"`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,5 @@ export default [
MigrationName1736364478985,
MigrationName1736364957990,
MigrationName1736365532085,
MigrationName1736675947746
MigrationName1736675947746,
];
20 changes: 10 additions & 10 deletions Common/Server/Services/IncidentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ export class Service extends DatabaseService<Model> {
}

public async getExistingIncidentNumberForProject(data: {
projectId: ObjectID
}): Promise<number> {
// get last incident number.
projectId: ObjectID;
}): Promise<number> {
// get last incident number.
const lastIncident: Model | null = await this.findOneBy({
query: {
projectId: data.projectId,
Expand All @@ -172,7 +172,7 @@ export class Service extends DatabaseService<Model> {
},
});

if(!lastIncident) {
if (!lastIncident) {
return 0;
}

Expand All @@ -182,17 +182,17 @@ export class Service extends DatabaseService<Model> {
protected override async onBeforeCreate(
createBy: CreateBy<Model>,
): Promise<OnCreate<Model>> {


if (!createBy.props.tenantId && !createBy.props.isRoot) {
throw new BadDataException("ProjectId required to create incident.");
}

const projectId: ObjectID = createBy.props.tenantId || createBy.data.projectId!;
const projectId: ObjectID =
createBy.props.tenantId || createBy.data.projectId!;

const incidentNumberForThisIncident: number = await this.getExistingIncidentNumberForProject({
projectId: projectId
}) + 1;
const incidentNumberForThisIncident: number =
(await this.getExistingIncidentNumberForProject({
projectId: projectId,
})) + 1;

const incidentState: IncidentState | null =
await IncidentStateService.findOneBy({
Expand Down
75 changes: 75 additions & 0 deletions Worker/DataMigrations/AddIncidentNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import DataMigrationBase from "./DataMigrationBase";
import LIMIT_MAX from "Common/Types/Database/LimitMax";
import ProjectService from "Common/Server/Services/ProjectService";
import Project from "Common/Models/DatabaseModels/Project";
import Incident from "Common/Models/DatabaseModels/Incident";
import IncidentService from "Common/Server/Services/IncidentService";
import SortOrder from "Common/Types/BaseDatabase/SortOrder";

export default class AddIncidentNumber extends DataMigrationBase {
public constructor() {
super("AddIncidentNumber");
}

public override async migrate(): Promise<void> {
// get all the users with email isVerified true.

const projects: Array<Project> = 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.

// get all incicents for this project
const incidents: Array<Incident> = await IncidentService.findBy({
query: {
projectId: project.id!,
},
select: {
_id: true,
incidentNumber: true,
},
skip: 0,
limit: LIMIT_MAX,
sort: {
createdAt: SortOrder.Descending
},
props: {
isRoot: true,
},
});

const totalIncidentForProject = incidents.length;
let incidentCounter = totalIncidentForProject; // start from the last incident number

for(const incident of incidents) {
await IncidentService.updateOneById({
id: incident.id!,
data: {
incidentNumber: incidentCounter,
},
props: {
isRoot: true,
},
});

incidentCounter = incidentCounter - 1;
}

}
}

public override async rollback(): Promise<void> {
return;
}
}
2 changes: 2 additions & 0 deletions Worker/DataMigrations/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import AddDefaultAlertSeverityAndStateToExistingProjects from "./AddDefaultAlert
import RefreshDefaultUserNotificationSetting from "./RefreshUserNotificationSetting";
import AddServiceTypeColumnToMetricsTable from "./AddServiceTypeColumnToMetricTable";
import AddIsSubscriptionConfirmedToSubscribers from "./AddIsSubscriptionConfirmedToSubscribers";
import AddIncidentNumber from "./AddIncidentNumber";

// This is the order in which the migrations will be run. Add new migrations to the end of the array.

Expand Down Expand Up @@ -83,6 +84,7 @@ const DataMigrations: Array<DataMigrationBase> = [
new RefreshDefaultUserNotificationSetting(),
new AddServiceTypeColumnToMetricsTable(),
new AddIsSubscriptionConfirmedToSubscribers(),
new AddIncidentNumber()
];

export default DataMigrations;

0 comments on commit 12e67a0

Please sign in to comment.