Skip to content

Commit

Permalink
feat: add incidentNumber field to Incident model and implement logic …
Browse files Browse the repository at this point in the history
…for auto-incrementing incident numbers
  • Loading branch information
simlarsen committed Jan 12, 2025
1 parent 5ebcba9 commit 1f3cdc0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
32 changes: 32 additions & 0 deletions Common/Models/DatabaseModels/Incident.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,4 +1086,36 @@ export default class Incident extends BaseModel {
nullable: true,
})
public telemetryQuery?: TelemetryQuery = undefined;


@ColumnAccessControl({
create: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CreateProjectIncident,
],
read: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.ReadProjectIncident,
],
update: [

],
})
@Index()
@TableColumn({
isDefaultValueColumn: false,
required: false,
type: TableColumnType.Number,
title: "Incident Number",
description: "Incident Number",
})
@Column({
type: ColumnType.Number,
nullable: true,
})
public incidentNumber?: number = undefined;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class MigrationName1736675947746 implements MigrationInterface {
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"`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import { MigrationName1734435866602 } from "./1734435866602-MigrationName";
import { MigrationName1736364478985 } from "./1736364478985-MigrationName";
import { MigrationName1736364957990 } from "./1736364957990-MigrationName";
import { MigrationName1736365532085 } from "./1736365532085-MigrationName";
import { MigrationName1736675947746 } from "./1736675947746-MigrationName";

export default [
InitialMigration,
Expand Down Expand Up @@ -178,4 +179,5 @@ export default [
MigrationName1736364478985,
MigrationName1736364957990,
MigrationName1736365532085,
MigrationName1736675947746
];
37 changes: 36 additions & 1 deletion Common/Server/Services/IncidentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,51 @@ export class Service extends DatabaseService<Model> {
// store incident metric
}

public async getExistingIncidentNumberForProject(data: {
projectId: ObjectID
}): Promise<number> {
// get last incident number.
const lastIncident: Model | null = await this.findOneBy({
query: {
projectId: data.projectId,
},
select: {
incidentNumber: true,
},
sort: {
createdAt: SortOrder.Descending,
},
props: {
isRoot: true,
},
});

if(!lastIncident) {
return 0;
}

return lastIncident.incidentNumber || 0;
}

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 incidentNumberForThisIncident: number = await this.getExistingIncidentNumberForProject({
projectId: projectId
}) + 1;

const incidentState: IncidentState | null =
await IncidentStateService.findOneBy({
query: {
projectId: createBy.props.tenantId || createBy.data.projectId!,
projectId: projectId,
isCreatedState: true,
},
select: {
Expand All @@ -181,6 +215,7 @@ export class Service extends DatabaseService<Model> {
}

createBy.data.currentIncidentStateId = incidentState.id;
createBy.data.incidentNumber = incidentNumberForThisIncident;

if (
(createBy.data.createdByUserId ||
Expand Down

0 comments on commit 1f3cdc0

Please sign in to comment.