Todos los test de orders pasan

This commit is contained in:
2026-02-23 13:35:36 +01:00
parent fc319372be
commit 5c64c84e2a
5 changed files with 63 additions and 31 deletions

View File

@@ -6,6 +6,15 @@ import { CreateOrderDTO, OrderTracking } from "../domain/Order.js";
import { Result } from "../domain/Result.js";
import { PgClient } from "./PgClient.js";
/**
* Agrupa todas las operaciones de *Order*.
* Las *Order* son seguimientos de operaciones que han entrado correctamente a cualquier cola
* de mensajes independientemente del pais/empresa objetivo de la tarjeta.
*
* Todas las operaciones devuelven un tipo Result<Error,Data> para gestionar los errores
* de acceso a la BDD, para las operaciones correctas se devuleve Error = undefined, para
* las erroneas Data = undefined.
*/
export class OrderRepository {
constructor(
private readonly pgClient: PgClient
@@ -34,7 +43,6 @@ export class OrderRepository {
* Se asume que se va a devolver una lista del tipo T
*/
private async getAll<T extends QueryResultRow>(queryPromise: Promise<QueryResult<T>>) {
try {
const queryResult = await queryPromise
return <Result<string, T[]>>{
@@ -48,15 +56,15 @@ export class OrderRepository {
}
/**
* TODO: OrderTracking necestia un tipo para la estructura del mensaje almacenado
* El tipo <T> representa el contenido del mensaje de los order
*/
public async getOrderById(data: { id: number }) {
public async getOrderById<T>(data: { id: number }): Promise<Result<string, OrderTracking<T>>> {
const query = `
SELECT * FROM order_tracking
WHERE id = $1
`
`
const values = [data.id]
const queryPromise = this.pgClient.query<OrderTracking<any>>(query, values)
const queryPromise = this.pgClient.query<OrderTracking<T>>(query, values)
const result = await this.getFirst(queryPromise);
return result
}
@@ -64,30 +72,33 @@ export class OrderRepository {
/**
* Busqueda según la id de RabbitMq
*/
public async getOrderByQueueId(data: { correlation_id: string }, pool?: PoolClient) {
public async getOrderByQueueId<T>(data: { correlation_id: string }, pool?: PoolClient) {
const query = `
SELECT * FROM order_tracking
WHERE correlation_id = $1
`
const values = [data.correlation_id]
const queryPromise = this.pgClient.query<OrderTracking<any>>(query, values)
const queryPromise = this.pgClient.query<OrderTracking<T>>(query, values)
const result = await this.getFirst(queryPromise);
return result
}
/**
* TODO:
* - variable para el limit
*/
public async getPendingOrders() {
public async getPendingOrders<T>(options?: {
limit?: number
}) {
const client = await this.pgClient.connect();
const query = `
SELECT * FROM order_tracking
WHERE finish_date IS NULL
ORDER BY start_date ASC
`
if (options?.limit != undefined) {
}
const values: string[] = []
const queryPromise = client.query(query, values)
const queryPromise = client.query<OrderTracking<T>>(query, values)
const result = await this.getAll(queryPromise)
client.release()
return result
@@ -116,7 +127,8 @@ export class OrderRepository {
RETURNING id, correlation_id, status, start_date;
`
const values = [data.correlation_id, data.exchange, data.routing_key, data.order_type, data.payload]
const queryPromise = client.query(query, values)
const queryPromise = client.query<{ id: number, correlation_id: string, status: string, start_date: string }>(query, values)
// TODO comprobar si start_date convierte a Date por defecto, añadir enum de status
const result = await this.getFirst(queryPromise)
if (result.error == undefined) {