Repositorio para llevar el registro de operaciones a objenious
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import { IOperationsRepository, ObjeniousOperation, ObjeniousOperationChange } from "#domain/operationsRepository.port";
|
||||
import { Result } from "#shared/domain/Result";
|
||||
import { PgClient } from "#shared/infrastructure/PgClient";
|
||||
|
||||
|
||||
|
||||
export class OperationsRepository implements IOperationsRepository {
|
||||
|
||||
constructor(
|
||||
private readonly pgClient: PgClient
|
||||
) {
|
||||
}
|
||||
|
||||
async createOperation(data: ObjeniousOperation): Promise<Result<string, ObjeniousOperation>> {
|
||||
const query = `
|
||||
INSERT INTO objenious_operation (operacion, iccids, status, max_rety)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING *`;
|
||||
const iccids = data.iccids.join(",")
|
||||
const values = [data.operation, data.iccids, data.status, data.max_retry];
|
||||
const { rows } = await this.pgClient.query(query, values);
|
||||
return <Result<string, ObjeniousOperation>>{
|
||||
data: rows[0]
|
||||
}
|
||||
}
|
||||
|
||||
async updateOperation(data: ObjeniousOperationChange): Promise<Result<string, ObjeniousOperation>> {
|
||||
const client = await this.pgClient.connect();
|
||||
const { new_status, error, new_request_id, new_mass_action_id, id } = data
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
|
||||
// 1. Actualizar objenious_operation
|
||||
const updateOpQuery = `
|
||||
UPDATE objenious_operation
|
||||
SET status = $1, error = $2, request_id = $3, mass_action_id = $4, last_change_date = now(),
|
||||
end_date = CASE WHEN $1 IN ('finished', 'error') THEN now() ELSE end_date END
|
||||
WHERE id = $5`;
|
||||
const operation = await client.query<ObjeniousOperation>(updateOpQuery, [new_status, error, new_request_id, new_mass_action_id, id]);
|
||||
|
||||
// 2. Nuevo registro en objenious_operation_change
|
||||
const insertChangeQuery = `
|
||||
INSERT INTO objenious_operation_change (operation_id, new_status, error, new_request_id, new_mass_action_id)
|
||||
VALUES ($1, $2, $3, $4, $5)`;
|
||||
await client.query(insertChangeQuery, [id, new_status, error, new_request_id, new_mass_action_id]);
|
||||
await client.query('COMMIT');
|
||||
|
||||
return <Result<string, ObjeniousOperation>>{
|
||||
data: operation.rows[0]
|
||||
}
|
||||
} catch (e) {
|
||||
await client.query('ROLLBACK');
|
||||
return <Result<string, ObjeniousOperation>>{
|
||||
data: undefined,
|
||||
error: e
|
||||
}
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async getPendingOperations(): Promise<Result<string, ObjeniousOperation[]>> {
|
||||
// Aprovecha el índice 'pending_operations'
|
||||
const query = `SELECT * FROM objenious_operation WHERE end_date IS NULL ORDER BY start_date ASC`;
|
||||
try {
|
||||
const { rows } = await this.pgClient.query<ObjeniousOperation>(query);
|
||||
return {
|
||||
error: undefined,
|
||||
data: rows
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
error: String(e),
|
||||
data: undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user