Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ import { HealthModule } from './health/health.module';
CommonModule,
RedisModule,
BlockchainModule,
ProgressModule,
CategoriesModule,
// Register the custom JWT Auth Middleware module
JwtAuthModule.registerAsync({
Expand All @@ -104,7 +103,7 @@ import { HealthModule } from './health/health.module';
HealthModule,
],
controllers: [AppController],
providers: [AppService],
providers: [AppService, TransactionLogger],
})
export class AppModule implements NestModule {
/**
Expand Down
14 changes: 14 additions & 0 deletions backend/src/middleware/transaction/transaction.logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class TransactionLogger {
private readonly logger = new Logger('Transaction');

log(message: string) {
this.logger.log(message);
}

error(message: string, error: any) {
this.logger.error(`${message}: ${error.message}`, error.stack);
}
}
36 changes: 36 additions & 0 deletions backend/src/middleware/transaction/transaction.manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { DataSource, QueryRunner } from 'typeorm';

export class TransactionManager {
private queryRunner: QueryRunner;

constructor(private readonly dataSource: DataSource) {
this.queryRunner = this.dataSource.createQueryRunner();
}

async startTransaction(isolation: 'READ COMMITTED' | 'REPEATABLE READ' | 'SERIALIZABLE' = 'READ COMMITTED') {
await this.queryRunner.connect();
await this.queryRunner.startTransaction(isolation);
}

async commitTransaction() {
await this.queryRunner.commitTransaction();
await this.queryRunner.release();
}

async rollbackTransaction() {
await this.queryRunner.rollbackTransaction();
await this.queryRunner.release();
}

async createSavepoint(name: string) {
await this.queryRunner.query(`SAVEPOINT ${name}`);
}

async rollbackToSavepoint(name: string) {
await this.queryRunner.query(`ROLLBACK TO SAVEPOINT ${name}`);
}

getManager() {
return this.queryRunner.manager;
}
}
37 changes: 37 additions & 0 deletions backend/src/middleware/transaction/transaction.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { DataSource } from 'typeorm';
import { TransactionManager } from './transaction.manager';
import { TransactionLogger } from './transaction.logger';

@Injectable()
export class TransactionMiddleware implements NestMiddleware {
constructor(private readonly dataSource: DataSource, private readonly logger: TransactionLogger) {}

async use(req: Request, res: Response, next: NextFunction) {
const manager = new TransactionManager(this.dataSource);

try {
await manager.startTransaction();

// Attach transaction manager to request for manual control if needed
(req as any).transactionManager = manager;

res.on('finish', async () => {
if (res.statusCode >= 200 && res.statusCode < 400) {
await manager.commitTransaction();
this.logger.log('Transaction committed successfully');
} else {
await manager.rollbackTransaction();
this.logger.log('Transaction rolled back due to error');
}
});

next();
} catch (error) {
await manager.rollbackTransaction();
this.logger.error('Transaction failed', error);
next(error);
}
}
}
Loading