diff --git a/Common/Server/Services/DatabaseService.ts b/Common/Server/Services/DatabaseService.ts index 9bdb319890..a6fb0ee125 100644 --- a/Common/Server/Services/DatabaseService.ts +++ b/Common/Server/Services/DatabaseService.ts @@ -1679,6 +1679,18 @@ class DatabaseService extends BaseService { }); } + @CaptureSpan() + protected async atomicIncrementColumnValueByOne(data: { + id: ObjectID; + columnName: keyof TBaseModel; + }): Promise { + await this.getRepository().increment( + { _id: data.id.toString() } as any, + data.columnName as string, + 1, + ); + } + @CaptureSpan() public async searchBy({ skip, diff --git a/Common/Server/Services/ProjectService.ts b/Common/Server/Services/ProjectService.ts index 460ea30c81..c3b4fa626e 100755 --- a/Common/Server/Services/ProjectService.ts +++ b/Common/Server/Services/ProjectService.ts @@ -1593,14 +1593,11 @@ export class ProjectService extends DatabaseService { public async incrementAndGetIncidentCounter( projectId: ObjectID, ): Promise { - // Atomic increment: SET incidentCounter = incidentCounter + 1 - await this.getRepository().increment( - { _id: projectId.toString() } as any, - "incidentCounter" satisfies keyof Model, - 1, - ); + await this.atomicIncrementColumnValueByOne({ + id: projectId, + columnName: "incidentCounter", + }); - // Read back the new value const project: Model | null = await this.findOneById({ id: projectId, select: { @@ -1624,14 +1621,11 @@ export class ProjectService extends DatabaseService { public async incrementAndGetAlertCounter( projectId: ObjectID, ): Promise { - // Atomic increment: SET alertCounter = alertCounter + 1 - await this.getRepository().increment( - { _id: projectId.toString() } as any, - "alertCounter" satisfies keyof Model, - 1, - ); + await this.atomicIncrementColumnValueByOne({ + id: projectId, + columnName: "alertCounter", + }); - // Read back the new value const project: Model | null = await this.findOneById({ id: projectId, select: { @@ -1655,14 +1649,11 @@ export class ProjectService extends DatabaseService { public async incrementAndGetScheduledMaintenanceCounter( projectId: ObjectID, ): Promise { - // Atomic increment: SET scheduledMaintenanceCounter = scheduledMaintenanceCounter + 1 - await this.getRepository().increment( - { _id: projectId.toString() } as any, - "scheduledMaintenanceCounter" satisfies keyof Model, - 1, - ); + await this.atomicIncrementColumnValueByOne({ + id: projectId, + columnName: "scheduledMaintenanceCounter", + }); - // Read back the new value const project: Model | null = await this.findOneById({ id: projectId, select: { @@ -1686,14 +1677,11 @@ export class ProjectService extends DatabaseService { public async incrementAndGetIncidentEpisodeCounter( projectId: ObjectID, ): Promise { - // Atomic increment: SET incidentEpisodeCounter = incidentEpisodeCounter + 1 - await this.getRepository().increment( - { _id: projectId.toString() } as any, - "incidentEpisodeCounter" satisfies keyof Model, - 1, - ); + await this.atomicIncrementColumnValueByOne({ + id: projectId, + columnName: "incidentEpisodeCounter", + }); - // Read back the new value const project: Model | null = await this.findOneById({ id: projectId, select: { @@ -1717,14 +1705,11 @@ export class ProjectService extends DatabaseService { public async incrementAndGetAlertEpisodeCounter( projectId: ObjectID, ): Promise { - // Atomic increment: SET alertEpisodeCounter = alertEpisodeCounter + 1 - await this.getRepository().increment( - { _id: projectId.toString() } as any, - "alertEpisodeCounter" satisfies keyof Model, - 1, - ); + await this.atomicIncrementColumnValueByOne({ + id: projectId, + columnName: "alertEpisodeCounter", + }); - // Read back the new value const project: Model | null = await this.findOneById({ id: projectId, select: {