diff --git a/.env b/.env index f7091a5..29151f1 100644 --- a/.env +++ b/.env @@ -5,8 +5,8 @@ RABBITMQ_PASSWORD=guest ENVIORMENT=development -RABBITMQ_HOST=rabbitmq-sim-broker -# RABBITMQ_HOST=localhost +# RABBITMQ_HOST=rabbitmq-sim-broker +RABBITMQ_HOST=localhost RABBITMQ_PORT=5672 RABBITMQ_USER=guest RABBITMQ_PASSWORD=guest @@ -14,8 +14,8 @@ RABBITMQ_SECURE=false RABBITMQ_VHOST=sim-vhost # Hay cosas que unificar de varios servicios -#POSTGRES_HOST=localhost -POSTGRES_HOST=sim-eventos-postgresql-sim-1 +# POSTGRES_HOST=sim-eventos-postgresql-sim-1 +POSTGRES_HOST=localhost POSTGRES_DB=postgres POSTGRES_DATABASE=postgres POSTGRES_PORT=5433 diff --git a/docs/sim-objenious/Get request by ID.bru b/docs/sim-objenious/Get request by ID.bru index 4c85e9e..bda2942 100644 --- a/docs/sim-objenious/Get request by ID.bru +++ b/docs/sim-objenious/Get request by ID.bru @@ -37,7 +37,7 @@ body:form-urlencoded { } vars:pre-request { - params.id: 14333 + params.id: 14557 } settings { diff --git a/packages/sim-shared/infrastructure/OperationRepository.ts b/packages/sim-shared/infrastructure/ObjeniousOperationRepository.ts similarity index 95% rename from packages/sim-shared/infrastructure/OperationRepository.ts rename to packages/sim-shared/infrastructure/ObjeniousOperationRepository.ts index 581c325..134da3f 100644 --- a/packages/sim-shared/infrastructure/OperationRepository.ts +++ b/packages/sim-shared/infrastructure/ObjeniousOperationRepository.ts @@ -2,7 +2,7 @@ import { IOperationsRepository, ObjeniousOperation, ObjeniousOperationChange } f import { Result } from "sim-shared/domain/Result.js"; import { PgClient } from "sim-shared/infrastructure/PgClient.js"; -export class OperationsRepository implements IOperationsRepository { +export class ObjeniousOperationsRepository implements IOperationsRepository { constructor( private readonly pgClient: PgClient @@ -12,7 +12,7 @@ export class OperationsRepository implements IOperationsRepository { async createOperation(data: ObjeniousOperation): Promise> { const query = ` INSERT INTO objenious_operation (operation, iccids, status, max_retry, request_id) - VALUES ($1, $2, $3, $4, $5) + VALUES ($1, $2, $3, $4, $5) RETURNING *`; const values = [data.operation, data.iccids, data.status, data.max_retry, data.request_id]; const { rows } = await this.pgClient.query(query, values); diff --git a/packages/sim-shared/infrastructure/OrderRepository.test.ts b/packages/sim-shared/infrastructure/OrderRepository.test.ts index 42f25b4..d859a01 100644 --- a/packages/sim-shared/infrastructure/OrderRepository.test.ts +++ b/packages/sim-shared/infrastructure/OrderRepository.test.ts @@ -12,14 +12,28 @@ const order1 = { payload: { iccid: "1234", action: "activate" } } +const order2 = { + correlation_id: "fakeRMQid-5678", + exchange: "fake.ex", + routing_key: "test.order.idk", + order_type: "activate", + payload: { iccid: "5678", action: "activate" } +} -describe("Test OrderRepository", {}, () => { + +describe("Test OrderRepository", {}, (ctx) => { const orderRepo = new OrderRepository(postgresClient) - let testId: number | undefined = undefined; - + let testIds: number[] = [] before(async () => { - const result = await orderRepo.createOrder(order1) - testId = result.data.id + // Order1 + const result1 = await orderRepo.createOrder(order1) + assert(result1.data != undefined) + testIds.push(result1.data.id) + + // Order2 -> Para el test de crearOrder + // const result2 = await orderRepo.createOrder(order2) + // assert(result2.data != undefined) + // testIds.push(result2.data.id) }) it("Insert new Order", async () => { @@ -39,12 +53,13 @@ describe("Test OrderRepository", {}, () => { }) it("Find by valid id should return the order", async () => { - const result = await orderRepo.getOrderById({ id: testId! }) + const result = await orderRepo.getOrderById({ id: testIds[0]! }) assert(result.error == undefined) assert(result.data != undefined) const order = result.data - assert(order.id == testId) + assert(order.id == testIds[0]) + assert(order.correlation_id == order1.correlation_id) }) it("Find by correlation id should return a valid order", async () => { @@ -66,6 +81,9 @@ describe("Test OrderRepository", {}, () => { const resA = await orderRepo.createOrder(orderA) const resB = await orderRepo.createOrder(orderB) + assert(resA.data != undefined) + assert(resB.data != undefined) + const idA = resA.data.id const idB = resB.data.id @@ -74,14 +92,14 @@ describe("Test OrderRepository", {}, () => { assert(result.error == undefined) assert(Array.isArray(result.data)) - // The list should contain at least our 3 orders (testId, idA, idB) + const ids = result.data.map(o => o.id) - assert(ids.includes(testId!)) + assert(ids.includes(testIds[0]!)) assert(ids.includes(idA)) assert(ids.includes(idB)) // Verify ordering (ASC by start_date, which maps to ID order in this sequential test) - const indexTest = result.data.findIndex(o => o.id === testId) + const indexTest = result.data.findIndex(o => o.id === testIds[0]) const indexA = result.data.findIndex(o => o.id === idA) const indexB = result.data.findIndex(o => o.id === idB) @@ -92,7 +110,7 @@ describe("Test OrderRepository", {}, () => { it("Update order status should change status and add history", async () => { const newStatus = "running" const reason = "Test update" - const result = await orderRepo.updateOrder({ id: testId!, new_status: newStatus, reason: reason }) + const result = await orderRepo.updateOrder({ id: testIds[0]!, new_status: newStatus, reason: reason }) assert(result.error == undefined) assert(result.data != undefined) @@ -100,7 +118,7 @@ describe("Test OrderRepository", {}, () => { }) it("Finish order should set status to finished and set finish_date", async () => { - const result = await orderRepo.finishOrder({ id: testId!, reason: "Test finish" }) + const result = await orderRepo.finishOrder({ id: testIds[0]!, reason: "Test finish" }) assert(result.error == undefined) assert(result.data != undefined) @@ -112,6 +130,7 @@ describe("Test OrderRepository", {}, () => { // Create another order for this test const order2 = { ...order1, correlation_id: "fake-error-test" } const createResult = await orderRepo.createOrder(order2) + assert(createResult.data != undefined) const errTestId = createResult.data.id const result = await orderRepo.errorOrder({ @@ -133,6 +152,7 @@ describe("Test OrderRepository", {}, () => { // Create another order for this test const order3 = { ...order1, correlation_id: "fake-dlx-test" } const createResult = await orderRepo.createOrder(order3) + assert(createResult.data != undefined) const dlxTestId = createResult.data.id const result = await orderRepo.errorOrder({ diff --git a/packages/sim-shared/infrastructure/OrderRepository.ts b/packages/sim-shared/infrastructure/OrderRepository.ts index 2a470c8..79f94a5 100644 --- a/packages/sim-shared/infrastructure/OrderRepository.ts +++ b/packages/sim-shared/infrastructure/OrderRepository.ts @@ -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 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(queryPromise: Promise>) { - try { const queryResult = await queryPromise return >{ @@ -48,15 +56,15 @@ export class OrderRepository { } /** - * TODO: OrderTracking necestia un tipo para la estructura del mensaje almacenado + * El tipo representa el contenido del mensaje de los order */ - public async getOrderById(data: { id: number }) { + public async getOrderById(data: { id: number }): Promise>> { const query = ` SELECT * FROM order_tracking WHERE id = $1 - ` + ` const values = [data.id] - const queryPromise = this.pgClient.query>(query, values) + const queryPromise = this.pgClient.query>(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(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>(query, values) + const queryPromise = this.pgClient.query>(query, values) const result = await this.getFirst(queryPromise); return result } /** - * TODO: - * - variable para el limit */ - public async getPendingOrders() { + public async getPendingOrders(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>(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) {