Orders en todas las etapas
This commit is contained in:
@@ -72,3 +72,14 @@ export type UpdateOrderDTO =
|
||||
new_status: OrderStatus,
|
||||
reason?: string
|
||||
}
|
||||
|
||||
export type FinishOrderDTO =
|
||||
(
|
||||
{ id: number, correlation_id?: never } |
|
||||
{ id?: never, correlation_id: string }
|
||||
)
|
||||
&
|
||||
{
|
||||
reason?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ export interface IOperationsRepository {
|
||||
|
||||
export type ObjeniousOperation = {
|
||||
id?: number;
|
||||
/** Uuid del mensaje asociado a la operacion */
|
||||
correlation_id?: string;
|
||||
operation: string;
|
||||
retry_count?: number;
|
||||
max_retry?: number;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* TODO: Usar
|
||||
*/
|
||||
import { PoolClient, QueryResult, QueryResultRow } from "pg";
|
||||
import { CreateOrderDTO, OrderTracking, UpdateOrderDTO } from "../domain/Order.js";
|
||||
import { CreateOrderDTO, FinishOrderDTO, OrderTracking, UpdateOrderDTO } from "../domain/Order.js";
|
||||
import { Result } from "../domain/Result.js";
|
||||
import { PgClient } from "./PgClient.js";
|
||||
import assert from "node:assert";
|
||||
@@ -177,7 +177,7 @@ export class OrderRepository {
|
||||
const client = await this.pgClient.connect();
|
||||
await client.query('BEGIN');
|
||||
|
||||
const idType = ('id' in args) ? "correlation_id" : "id"
|
||||
const idType = ('id' in args) ? "id" : "correlation_id"
|
||||
const idValue = (args.id != undefined) ? args.id : args.correlation_id
|
||||
|
||||
// 1. Se consulta la order de base
|
||||
@@ -262,18 +262,29 @@ export class OrderRepository {
|
||||
}
|
||||
|
||||
|
||||
public async finishOrder(args: { id: number, reason?: string }) {
|
||||
public async finishOrder(args: FinishOrderDTO) {
|
||||
const client = await this.pgClient.connect();
|
||||
assert((args.id != undefined) != (args.correlation_id != undefined))
|
||||
await client.query('BEGIN');
|
||||
|
||||
const idType = ('id' in args) ? "id" : "correlation_id"
|
||||
const idValue = (args.id != undefined) ? args.id : args.correlation_id
|
||||
|
||||
// 1. Se consulta la order de base
|
||||
const qCurrentOrder = `
|
||||
SELECT * FROM order_tracking
|
||||
WHERE id = $1
|
||||
WHERE ${idType} = $1
|
||||
`
|
||||
const vCurrentOrder = [args.id]
|
||||
const vCurrentOrder = [idValue]
|
||||
|
||||
const currentOrderResult = await this.getFirst(client.query<OrderTracking<any>>(qCurrentOrder, vCurrentOrder))
|
||||
const orderId = currentOrderResult.data?.id
|
||||
|
||||
if (orderId == undefined) {
|
||||
return {
|
||||
error: "El order a actualizar no existe " + idType + ": " + idValue
|
||||
}
|
||||
}
|
||||
|
||||
if (currentOrderResult.error != undefined) {
|
||||
await client.query("ROLLBACK")
|
||||
@@ -293,7 +304,7 @@ export class OrderRepository {
|
||||
WHERE id = $1
|
||||
RETURNING id, status, update_date;
|
||||
`
|
||||
const vOrderTracking = [args.id]
|
||||
const vOrderTracking = [orderId]
|
||||
const updatedOrderResult = await this.getFirst(
|
||||
client.query<{ id: number, status: string, update_date: string }>(uOrderTracking, vOrderTracking)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user