2026-02-24 11:27:47 +01:00
|
|
|
import { OrderRepository } from "sim-shared/infrastructure/OrderRepository.js";
|
2026-02-25 12:20:52 +01:00
|
|
|
import { Result } from "sim-shared/domain/Result.js";
|
2026-02-24 11:27:47 +01:00
|
|
|
import assert from "node:assert";
|
2026-02-09 13:24:04 +01:00
|
|
|
import { EventBus } from "sim-shared/domain/EventBus.port";
|
|
|
|
|
import { SimEvents } from "sim-shared/domain/SimEvents";
|
2026-02-24 11:27:47 +01:00
|
|
|
import { uuidv7 } from "uuidv7";
|
2026-02-25 12:20:52 +01:00
|
|
|
import { CreateOrderDTO, OrderTracking, OrderType, OrderTypeOptions } from "sim-shared/domain/Order.js";
|
2026-01-13 15:41:59 +01:00
|
|
|
|
|
|
|
|
/**
|
2026-02-24 11:27:47 +01:00
|
|
|
* Casos de uso de tarjetas sim. Garantiza que todos los metodos usan el mismo bus de mensajes
|
|
|
|
|
* y repositorio de registro de las ordenes.
|
2026-01-13 15:41:59 +01:00
|
|
|
*/
|
|
|
|
|
export class SimUsecases {
|
2026-02-24 11:27:47 +01:00
|
|
|
private eventBus: EventBus;
|
|
|
|
|
private orderRepository: OrderRepository;
|
2026-01-13 15:41:59 +01:00
|
|
|
|
|
|
|
|
constructor(args: {
|
2026-02-24 11:27:47 +01:00
|
|
|
eventBus: EventBus,
|
|
|
|
|
orderRepository: OrderRepository
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
) {
|
|
|
|
|
this.eventBus = args.eventBus
|
2026-02-24 11:27:47 +01:00
|
|
|
this.orderRepository = args.orderRepository
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 12:20:52 +01:00
|
|
|
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<T extends any>(event: SimEvents.general): Promise<Result<string, OrderTracking<T>>> {
|
|
|
|
|
if (event.headers?.message_id == undefined) {
|
|
|
|
|
return <Result<string, any>>{
|
|
|
|
|
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 <Result<string, any>>{
|
|
|
|
|
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<T>(order)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
}
|
2026-02-24 11:27:47 +01:00
|
|
|
|
|
|
|
|
async test(args: { iccid: string }) {
|
|
|
|
|
assert(args.iccid != undefined)
|
|
|
|
|
const event = <SimEvents.general>{
|
2026-02-25 12:20:52 +01:00
|
|
|
key: `sim.test.unknown`,
|
2026-02-24 11:27:47 +01:00
|
|
|
payload: {
|
|
|
|
|
iccid: args.iccid
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-25 12:20:52 +01:00
|
|
|
const eventWithId = this.addMessage_id(event)
|
2026-02-24 11:27:47 +01:00
|
|
|
|
2026-02-25 12:20:52 +01:00
|
|
|
const publish = await this.eventBus.publish([eventWithId])
|
|
|
|
|
await this.saveOrder(eventWithId)
|
|
|
|
|
return eventWithId
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-14 17:30:55 +01:00
|
|
|
/**
|
2026-02-24 11:27:47 +01:00
|
|
|
* WIP
|
2026-01-14 17:30:55 +01:00
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-02-03 14:32:46 +01:00
|
|
|
async save(args: { iccid: string, imei: string, compañia: string }) {
|
2026-01-14 17:30:55 +01:00
|
|
|
const activationEvent = <SimEvents.save>{
|
2026-02-03 14:32:46 +01:00
|
|
|
key: `sim.${args.compañia}.save`,
|
2026-01-14 17:30:55 +01:00
|
|
|
payload: {
|
|
|
|
|
iccid: args.iccid,
|
|
|
|
|
imei: args.imei
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.eventBus.publish([activationEvent])
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 16:39:34 +01:00
|
|
|
async activation(args: { iccid: string, compañia: string, offer: string }) {
|
2026-01-27 14:48:44 +01:00
|
|
|
|
2026-02-03 14:32:46 +01:00
|
|
|
const activationEvent = <SimEvents.activation>{
|
2026-01-26 15:04:17 +01:00
|
|
|
key: `sim.${args.compañia}.activate`,
|
2026-01-13 15:41:59 +01:00
|
|
|
payload: {
|
2026-02-03 14:32:46 +01:00
|
|
|
iccid: args.iccid,
|
|
|
|
|
offer: args.offer
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-25 12:20:52 +01:00
|
|
|
|
|
|
|
|
const activationWithId = this.addMessage_id(activationEvent)
|
|
|
|
|
console.log("[d] Activation ", activationWithId)
|
|
|
|
|
await this.eventBus.publish([activationWithId])
|
2026-02-25 17:42:16 +01:00
|
|
|
await this.saveOrder(activationWithId)
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-03 14:32:46 +01:00
|
|
|
async preActivation(args: { iccid: string, compañia: string }) {
|
|
|
|
|
|
|
|
|
|
const preActivationEvent = <SimEvents.preActivation>{
|
|
|
|
|
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
|
|
|
|
|
*/
|
2026-01-30 10:42:48 +01:00
|
|
|
async cancelation(args: { iccid: string, compañia: string }) {
|
|
|
|
|
|
2026-02-25 17:42:16 +01:00
|
|
|
const cancelationEvent = <SimEvents.cancel>{
|
2026-02-03 17:26:12 +01:00
|
|
|
key: `sim.${args.compañia}.cancel`,
|
2026-01-30 10:42:48 +01:00
|
|
|
payload: {
|
|
|
|
|
iccid: args.iccid
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-25 17:42:16 +01:00
|
|
|
|
|
|
|
|
const cancelationWithId = this.addMessage_id(cancelationEvent)
|
|
|
|
|
console.log("[d] Cancelation ", cancelationWithId)
|
|
|
|
|
await this.eventBus.publish([cancelationWithId])
|
|
|
|
|
await this.saveOrder(cancelationWithId)
|
|
|
|
|
return cancelationWithId
|
2026-01-30 10:42:48 +01:00
|
|
|
}
|
2026-02-03 14:32:46 +01:00
|
|
|
// alias por si acaso
|
|
|
|
|
public terminate = this.cancelation;
|
2026-01-13 15:41:59 +01:00
|
|
|
|
2026-02-03 14:32:46 +01:00
|
|
|
/**
|
|
|
|
|
* alias de bloquear / suspender en objenious
|
|
|
|
|
*/
|
|
|
|
|
async pause(args: { iccid: string, compañia: string }) {
|
2026-02-25 17:42:16 +01:00
|
|
|
const pauseEvent = <SimEvents.pause>{
|
2026-02-03 14:32:46 +01:00
|
|
|
key: `sim.${args.compañia}.pause`,
|
2026-01-14 10:19:49 +01:00
|
|
|
payload: {
|
|
|
|
|
iccid: args.iccid
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-25 17:42:16 +01:00
|
|
|
const pauseWithId = this.addMessage_id(pauseEvent)
|
|
|
|
|
await this.eventBus.publish([pauseWithId])
|
|
|
|
|
await this.saveOrder(pauseWithId)
|
|
|
|
|
return pauseWithId
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
2026-02-25 17:42:16 +01:00
|
|
|
|
2026-02-03 14:32:46 +01:00
|
|
|
async free(args: { iccid: string, compañia: string }) {
|
2026-01-14 10:19:49 +01:00
|
|
|
const cancelationEvent = <SimEvents.free>{
|
2026-02-03 14:32:46 +01:00
|
|
|
key: `sim.${args.compañia}.free`,
|
2026-01-14 10:19:49 +01:00
|
|
|
payload: {
|
|
|
|
|
iccid: args.iccid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.eventBus.publish([cancelationEvent])
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|