2026-02-24 11:27:47 +01:00
|
|
|
import { OrderRepository } from "sim-shared/infrastructure/OrderRepository.js";
|
|
|
|
|
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";
|
|
|
|
|
import { CreateOrderDTO, OrderType } 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async test(args: { iccid: string }) {
|
|
|
|
|
assert(args.iccid != undefined)
|
|
|
|
|
const uuid = uuidv7()
|
|
|
|
|
const event = <SimEvents.general>{
|
|
|
|
|
key: `sim.test.test`,
|
|
|
|
|
payload: {
|
|
|
|
|
iccid: args.iccid
|
|
|
|
|
},
|
|
|
|
|
headers: {
|
|
|
|
|
message_id: uuid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const publish = await this.eventBus.publish([event])
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO:
|
|
|
|
|
* De momento solo para mensajes publicados de 1 en 1 y si se les ha añadido cabecera
|
|
|
|
|
* Si se ha saltado el proceso de añadir un ID no se
|
|
|
|
|
*/
|
|
|
|
|
if (publish.success.length == 1) {
|
|
|
|
|
if (event.headers?.message_id != undefined) {
|
|
|
|
|
const orderType = (event.key.split(".")[2] as OrderType ?? "unknown")
|
|
|
|
|
assert(orderType)
|
|
|
|
|
const order: CreateOrderDTO = {
|
|
|
|
|
correlation_id: event.headers.message_id,
|
|
|
|
|
order_type: orderType,
|
|
|
|
|
routing_key: event.key,
|
|
|
|
|
payload: event
|
|
|
|
|
}
|
|
|
|
|
this.orderRepository.createOrder(order)
|
|
|
|
|
}
|
|
|
|
|
}
|
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-01-27 14:48:44 +01:00
|
|
|
console.log("[d] Activation ", activationEvent)
|
2026-01-13 15:41:59 +01:00
|
|
|
return this.eventBus.publish([activationEvent])
|
|
|
|
|
}
|
|
|
|
|
|
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-03 17:26:12 +01:00
|
|
|
const activationEvent = <SimEvents.cancel>{
|
|
|
|
|
key: `sim.${args.compañia}.cancel`,
|
2026-01-30 10:42:48 +01:00
|
|
|
payload: {
|
|
|
|
|
iccid: args.iccid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log("[d] Cancelation ", activationEvent)
|
|
|
|
|
return this.eventBus.publish([activationEvent])
|
|
|
|
|
}
|
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-01-14 10:19:49 +01:00
|
|
|
const cancelationEvent = <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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.eventBus.publish([cancelationEvent])
|
2026-01-13 15:41:59 +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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|