Refactor increment methods in ProjectService to use atomicIncrementColumnValueByOne for better code reuse

This commit is contained in:
Nawaz Dhandala
2026-02-06 14:32:30 +00:00
parent a27f3953ab
commit 7eb84c2fb0
2 changed files with 32 additions and 35 deletions

View File

@@ -1679,6 +1679,18 @@ class DatabaseService<TBaseModel extends BaseModel> extends BaseService {
});
}
@CaptureSpan()
protected async atomicIncrementColumnValueByOne(data: {
id: ObjectID;
columnName: keyof TBaseModel;
}): Promise<void> {
await this.getRepository().increment(
{ _id: data.id.toString() } as any,
data.columnName as string,
1,
);
}
@CaptureSpan()
public async searchBy({
skip,

View File

@@ -1593,14 +1593,11 @@ export class ProjectService extends DatabaseService<Model> {
public async incrementAndGetIncidentCounter(
projectId: ObjectID,
): Promise<number> {
// 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<Model> {
public async incrementAndGetAlertCounter(
projectId: ObjectID,
): Promise<number> {
// 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<Model> {
public async incrementAndGetScheduledMaintenanceCounter(
projectId: ObjectID,
): Promise<number> {
// 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<Model> {
public async incrementAndGetIncidentEpisodeCounter(
projectId: ObjectID,
): Promise<number> {
// 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<Model> {
public async incrementAndGetAlertEpisodeCounter(
projectId: ObjectID,
): Promise<number> {
// 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: {