import { OrderRepository } from "sim-shared/infrastructure/OrderRepository.js"; import { Result } from "sim-shared/domain/Result.js"; import assert from "node:assert"; import { EventBus } from "sim-shared/domain/EventBus.port"; import { SimEvents } from "sim-shared/domain/SimEvents"; import { uuidv7 } from "uuidv7"; import { CreateOrderDTO, OrderTracking, OrderType, OrderTypeOptions } from "sim-shared/domain/Order.js"; /** * Casos de uso de tarjetas sim. Garantiza que todos los metodos usan el mismo bus de mensajes * y repositorio de registro de las ordenes. */ export class SimUsecases { private eventBus: EventBus; private orderRepository: OrderRepository; constructor(args: { eventBus: EventBus, orderRepository: OrderRepository } ) { this.eventBus = args.eventBus this.orderRepository = args.orderRepository } private addMessage_id(event: SimEvents.general): SimEvents.general & { headers: { message_id: string } } { const uuid = uuidv7() return { ...event, headers: { ...event.headers, message_id: uuid } } } /** * El tipo T es el tipo del payload del Order */ private async saveOrder(event: SimEvents.general): Promise>> { if (event.headers?.message_id == undefined) { return >{ error: "El evento no tiene una cabecera message_id definido" } } const orderType = (event.key.split(".")[2] as OrderType ?? "unknown") // Estoy pensando en la posibilidad de pasarlo a unknown if (!OrderTypeOptions.has(orderType)) { return >{ error: `El evento no tiene un tipo valido: ${orderType} no existe como tipo valido` } } const order: CreateOrderDTO = { correlation_id: event.headers.message_id, order_type: orderType, routing_key: event.key, payload: event } const result = await this.orderRepository.createOrder(order) return result; } async test(args: { iccid: string }) { assert(args.iccid != undefined) const event = { key: `sim.test.unknown`, payload: { iccid: args.iccid } } const eventWithId = this.addMessage_id(event) const publish = await this.eventBus.publish([eventWithId]) await this.saveOrder(eventWithId) return eventWithId } /** * WIP * Crea una nueva sim de la que no se tenia registro anteriormente * Si ya existia se modifican los campos pero no se hace un cambio * de estado. */ async save(args: { iccid: string, imei: string, compañia: string }) { const activationEvent = { key: `sim.${args.compañia}.save`, payload: { iccid: args.iccid, imei: args.imei } } return this.eventBus.publish([activationEvent]) } async activation(args: { iccid: string, compañia: string, offer: string }) { const activationEvent = { key: `sim.${args.compañia}.activate`, payload: { iccid: args.iccid, offer: args.offer } } const activationWithId = this.addMessage_id(activationEvent) console.log("[d] Activation ", activationWithId) await this.eventBus.publish([activationWithId]) await this.saveOrder(activationWithId) } async preActivation(args: { iccid: string, compañia: string }) { const preActivationEvent = { key: `sim.${args.compañia}.preActivate`, payload: { iccid: args.iccid } } console.log("[d] Pre - activation ", preActivationEvent) return this.eventBus.publish([preActivationEvent]) } /** * Para objenious es terminate */ async cancelation(args: { iccid: string, compañia: string }) { const cancelationEvent = { key: `sim.${args.compañia}.cancel`, payload: { iccid: args.iccid } } const cancelationWithId = this.addMessage_id(cancelationEvent) console.log("[d] Cancelation ", cancelationWithId) await this.eventBus.publish([cancelationWithId]) await this.saveOrder(cancelationWithId) return cancelationWithId } // alias por si acaso public terminate = this.cancelation; /** * alias de bloquear / suspender en objenious */ async pause(args: { iccid: string, compañia: string }) { const pauseEvent = { key: `sim.${args.compañia}.pause`, payload: { iccid: args.iccid } } const pauseWithId = this.addMessage_id(pauseEvent) await this.eventBus.publish([pauseWithId]) await this.saveOrder(pauseWithId) return pauseWithId } async free(args: { iccid: string, compañia: string }) { const cancelationEvent = { key: `sim.${args.compañia}.free`, payload: { iccid: args.iccid } } return this.eventBus.publish([cancelationEvent]) } }