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

171 lines
4.6 KiB
TypeScript

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<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;
}
async test(args: { iccid: string }) {
assert(args.iccid != undefined)
const event = <SimEvents.general>{
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 = <SimEvents.save>{
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 = <SimEvents.activation>{
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])
this.saveOrder(activationWithId)
}
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
*/
async cancelation(args: { iccid: string, compañia: string }) {
const activationEvent = <SimEvents.cancel>{
key: `sim.${args.compañia}.cancel`,
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])
}
}