Files
sf-sim/packages/sim-entrada-eventos/aplication/Sim.usecases.ts

146 lines
3.9 KiB
TypeScript
Raw Normal View History

import { OrderRepository } from "sim-shared/infrastructure/OrderRepository.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, OrderType } 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
}
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-14 17:30:55 +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.
*/
async save(args: { iccid: string, imei: string, compañia: string }) {
2026-01-14 17:30:55 +01:00
const activationEvent = <SimEvents.save>{
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])
}
async activation(args: { iccid: string, compañia: string, offer: string }) {
2026-01-27 14:48:44 +01:00
const activationEvent = <SimEvents.activation>{
2026-01-26 15:04:17 +01:00
key: `sim.${args.compañia}.activate`,
payload: {
iccid: args.iccid,
offer: args.offer
}
}
2026-01-27 14:48:44 +01:00
console.log("[d] Activation ", activationEvent)
return this.eventBus.publish([activationEvent])
}
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])
}
// alias por si acaso
public terminate = this.cancelation;
/**
* alias de bloquear / suspender en objenious
*/
async pause(args: { iccid: string, compañia: string }) {
const cancelationEvent = <SimEvents.pause>{
key: `sim.${args.compañia}.pause`,
payload: {
iccid: args.iccid
}
}
return this.eventBus.publish([cancelationEvent])
}
async free(args: { iccid: string, compañia: string }) {
const cancelationEvent = <SimEvents.free>{
key: `sim.${args.compañia}.free`,
payload: {
iccid: args.iccid
}
}
return this.eventBus.publish([cancelationEvent])
}
}