Todas las acciones de lineas de objenious
This commit is contained in:
@@ -6,21 +6,32 @@ export namespace SimEvents {
|
||||
payload: {
|
||||
iccid: string
|
||||
},
|
||||
options: {
|
||||
}
|
||||
}
|
||||
|
||||
export type activation = DomainEvent & {
|
||||
key: `sim.${string}.activation`,
|
||||
key: `sim.${string}.activate`,
|
||||
payload: {
|
||||
iccid: string,
|
||||
offer?: string
|
||||
},
|
||||
}
|
||||
|
||||
export type preActivation = DomainEvent & {
|
||||
key: `sim.${string}.preActivate`,
|
||||
payload: {
|
||||
iccid: string
|
||||
},
|
||||
}
|
||||
|
||||
export type reActivation = DomainEvent & {
|
||||
key: `sim.${string}.reActivate`,
|
||||
payload: {
|
||||
iccid: string
|
||||
},
|
||||
options: {
|
||||
}
|
||||
}
|
||||
|
||||
export type cancelation = DomainEvent & {
|
||||
key: `sim.${string}.cancelation`,
|
||||
key: `sim.${string}.cancel`,
|
||||
payload: {
|
||||
iccid: string
|
||||
},
|
||||
@@ -29,7 +40,7 @@ export namespace SimEvents {
|
||||
}
|
||||
|
||||
export type pause = DomainEvent & {
|
||||
key: "sim.pause",
|
||||
key: `sim.${string}.pause`,
|
||||
payload: {
|
||||
iccid: string
|
||||
},
|
||||
@@ -38,7 +49,7 @@ export namespace SimEvents {
|
||||
}
|
||||
|
||||
export type free = DomainEvent & {
|
||||
key: "sim.free",
|
||||
key: `sim.${string}.free`,
|
||||
payload: {
|
||||
iccid: string
|
||||
},
|
||||
@@ -47,7 +58,7 @@ export namespace SimEvents {
|
||||
}
|
||||
|
||||
export type save = DomainEvent & {
|
||||
key: "sim.save",
|
||||
key: `sim.${string}.save`,
|
||||
payload: {
|
||||
iccid: string,
|
||||
imei: string
|
||||
|
||||
@@ -64,7 +64,7 @@ export class SimController {
|
||||
}
|
||||
}
|
||||
|
||||
public activateSim() {
|
||||
public activate() {
|
||||
return async (msg: ConsumeMessage) => {
|
||||
let msgData;
|
||||
try {
|
||||
@@ -73,26 +73,83 @@ export class SimController {
|
||||
throw new Error("Error consumiendo el mensaje no es valido" + String(e))
|
||||
}
|
||||
|
||||
if (msgData == undefined || msgData.payload == undefined) throw new Error("Mensaje invalido")
|
||||
const iccid = msgData.payload.iccid
|
||||
const offer = msgData.payload.offer
|
||||
|
||||
if (offer == undefined) throw new Error("Error activando la sim, no se ha especificado la oferta")
|
||||
|
||||
this.tryUseCase(msg, this.useCases.activate({
|
||||
dueDate: this.genDueDate(2 * 60).toISOString(),
|
||||
identifier: {
|
||||
identifierType: "ICCID",
|
||||
identifiers: [iccid]
|
||||
},
|
||||
offer: {
|
||||
code: offer,
|
||||
services: []
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
public suspendSim() {
|
||||
public preActivate() {
|
||||
return async (msg: ConsumeMessage) => {
|
||||
let msgData;
|
||||
try {
|
||||
msgData = this.validateMsg(msg) as SimEvents.pause
|
||||
} catch (e) {
|
||||
throw new Error("Error consumiendo el mensaje no es valido" + String(e))
|
||||
throw new Error("Error de preactivacion consumiendo el mensaje no es valido" + String(e))
|
||||
}
|
||||
|
||||
if (msgData == undefined) {
|
||||
return Promise.reject("Mensaje invalido")
|
||||
}
|
||||
|
||||
const iccid = msgData.payload.iccid
|
||||
this.tryUseCase(msg, this.useCases.preActivate({
|
||||
dueDate: this.genDueDate(2 * 60).toISOString(),
|
||||
identifier: {
|
||||
identifierType: "ICCID",
|
||||
identifiers: [iccid]
|
||||
}
|
||||
}))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public reActivate() {
|
||||
return async (msg: ConsumeMessage) => {
|
||||
let msgData;
|
||||
try {
|
||||
msgData = this.validateMsg(msg) as SimEvents.pause
|
||||
} catch (e) {
|
||||
throw new Error("Error de reactivacion consumiendo el mensaje no es valido" + String(e))
|
||||
}
|
||||
|
||||
if (msgData == undefined) {
|
||||
return Promise.reject("Mensaje invalido")
|
||||
}
|
||||
|
||||
const iccid = msgData.payload.iccid
|
||||
this.tryUseCase(msg, this.useCases.suspend({
|
||||
dueDate: this.genDueDate(2 * 60).toISOString(),
|
||||
identifier: {
|
||||
identifierType: "ICCID",
|
||||
identifiers: [iccid]
|
||||
}
|
||||
}))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public suspend() {
|
||||
return async (msg: ConsumeMessage) => {
|
||||
let msgData;
|
||||
try {
|
||||
msgData = this.validateMsg(msg) as SimEvents.pause
|
||||
} catch (e) {
|
||||
throw new Error("Error de suspension consumiendo el mensaje no es valido" + String(e))
|
||||
}
|
||||
|
||||
if (msgData == undefined) {
|
||||
|
||||
@@ -12,8 +12,11 @@ export class SimRouter {
|
||||
|
||||
constructor(private readonly simController: SimController) {
|
||||
this.routes = new Map([
|
||||
["activate", this.simController.activateSim()],
|
||||
["pause", this.simController.pauseSim()],
|
||||
["activate", this.simController.activate()],
|
||||
["pause", this.simController.suspend()],
|
||||
["cancel", this.simController.terminate()], // terminate
|
||||
["reActivate", this.simController.reActivate()],
|
||||
["preActivate", this.simController.preActivate()]
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { ActivationData } from "#domain/DTOs/objeniousapi.js"
|
||||
import { ActionData, ActivationData } from "#domain/DTOs/objeniousapi.js"
|
||||
import { HttpClient } from "#shared/infrastructure/HTTPClient.js"
|
||||
import { AxiosError } from "axios"
|
||||
import { Result } from "#shared/domain/Result"
|
||||
|
||||
// TODO: Pasar a un archivo de DTOs
|
||||
// TODO:
|
||||
// - Pasar a un archivo de DTOs
|
||||
// - Mucha repeticion por funcion, deberia hacer una plantilla
|
||||
|
||||
|
||||
export class SimUseCases {
|
||||
@@ -21,6 +23,8 @@ export class SimUseCases {
|
||||
public activate(activationData: ActivationData): () => Promise<Result<string, boolean>> {
|
||||
const OPERATION_URL = "/actions/activateLine"
|
||||
return async () => {
|
||||
// TODO: validacion de campos
|
||||
|
||||
const req = this.httpClient.client.post(OPERATION_URL, {
|
||||
...activationData
|
||||
})
|
||||
@@ -52,7 +56,31 @@ export class SimUseCases {
|
||||
}
|
||||
}
|
||||
|
||||
public reactivate(pauseData: ActivationData): () => Promise<Result<string, boolean>> {
|
||||
public preActivate(pauseData: ActionData): () => Promise<Result<string, boolean>> {
|
||||
const OPERATION_URL = "/actions/preactivateLine"
|
||||
return async () => {
|
||||
const req = this.httpClient.client.post(OPERATION_URL, {
|
||||
...pauseData
|
||||
})
|
||||
|
||||
try {
|
||||
const e = await req
|
||||
console.log("Sim preactivada con exito", e.data)
|
||||
return <Result<string, boolean>>{
|
||||
error: undefined,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error preactivacion", error)
|
||||
return <Result<string, boolean>>{
|
||||
error: "Error preactivando la sim" + pauseData.identifier,
|
||||
data: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public reActivate(pauseData: ActionData): () => Promise<Result<string, boolean>> {
|
||||
const OPERATION_URL = "/actions/reactivateLine"
|
||||
return async () => {
|
||||
const req = this.httpClient.client.post(OPERATION_URL, {
|
||||
@@ -61,13 +89,13 @@ export class SimUseCases {
|
||||
|
||||
try {
|
||||
const e = await req
|
||||
console.log("Sim pausada con exito", e.data)
|
||||
console.log("Sim reactivada con exito", e.data)
|
||||
return <Result<string, boolean>>{
|
||||
error: undefined,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error pausa", error)
|
||||
console.error("Error reactivacion", error)
|
||||
return <Result<string, boolean>>{
|
||||
error: "Error reactivando la sim" + pauseData.identifier,
|
||||
data: true
|
||||
@@ -76,7 +104,7 @@ export class SimUseCases {
|
||||
}
|
||||
}
|
||||
|
||||
public suspend(suspendData: ActivationData): () => Promise<Result<string, boolean>> {
|
||||
public suspend(suspendData: ActionData): () => Promise<Result<string, boolean>> {
|
||||
const OPERATION_URL = "/actions/suspendLine"
|
||||
return async () => {
|
||||
const req = this.httpClient.client.post(OPERATION_URL, {
|
||||
@@ -85,7 +113,7 @@ export class SimUseCases {
|
||||
|
||||
try {
|
||||
const e = await req
|
||||
console.log("Sim pausada con exito", e.data)
|
||||
console.log("Sim pausada/suspendida con exito", e.data)
|
||||
return <Result<string, boolean>>{
|
||||
error: undefined,
|
||||
data: true
|
||||
@@ -100,16 +128,19 @@ export class SimUseCases {
|
||||
}
|
||||
}
|
||||
|
||||
public terminate(terminationData: ActivationData): () => Promise<Result<string, boolean>> {
|
||||
public terminate(terminationData: ActionData): () => Promise<Result<string, boolean>> {
|
||||
const OPERATION_URL = "/actions/terminateLine"
|
||||
return async () => {
|
||||
const req = this.httpClient.client.post(OPERATION_URL, {
|
||||
...terminationData
|
||||
})
|
||||
|
||||
// TODO: para cuando estemos listos.
|
||||
throw new Error("Peticion no reversible desactivada de momento")
|
||||
|
||||
try {
|
||||
const e = await req
|
||||
console.log("Sim pausada con exito", e.data)
|
||||
console.log("Sim cancelada con exito", e.data)
|
||||
return <Result<string, boolean>>{
|
||||
error: undefined,
|
||||
data: true
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
export type ActivationData = {
|
||||
export type ActionData = {
|
||||
dueDate: string, // isodate
|
||||
filter?: {} // no se si hace falta
|
||||
identifier: {
|
||||
@@ -8,6 +8,13 @@ export type ActivationData = {
|
||||
}
|
||||
}
|
||||
|
||||
export type ActivationData = ActionData & {
|
||||
offer: {
|
||||
code: string | "SAVEFAMILY1" | "SAVEFAMILY2",
|
||||
services: any[]
|
||||
}
|
||||
}
|
||||
|
||||
export type ResponseError = {
|
||||
error: string,
|
||||
detail: Object[]
|
||||
|
||||
@@ -24,12 +24,50 @@ export class SimController {
|
||||
this.activation = this.activation.bind(this)
|
||||
}
|
||||
|
||||
async preactivation(req: Request, res: Response) {
|
||||
const valido = this.validateBody(req.body, res)
|
||||
if (valido == false) return;
|
||||
|
||||
const { iccid } = req.body
|
||||
const compañia = this.compañiaFromIccid(iccid)
|
||||
|
||||
if (compañia == undefined) {
|
||||
res.status(500).json({
|
||||
errors: {
|
||||
msg: "El iccid no pertenece a una compañia conocida"
|
||||
}
|
||||
})
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
await this.simUseCases.preActivation({ iccid, compañia })
|
||||
|
||||
res.status(200).json({
|
||||
iccid: iccid,
|
||||
operation: "activation"
|
||||
}).send()
|
||||
} catch (err) {
|
||||
console.error("Error activando la sim ", req.body)
|
||||
res.status(500).json({
|
||||
errors: {
|
||||
msg: "Error general de activation"
|
||||
}
|
||||
}).send()
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async activation(req: Request, res: Response) {
|
||||
const valido = this.validateBody(req.body, res)
|
||||
|
||||
if (valido == false) return; // Si no es valido ya se ha enviado el error
|
||||
|
||||
const { iccid } = req.body
|
||||
const { iccid, offer } = req.body
|
||||
|
||||
//TODO: incluir lo de las offers
|
||||
|
||||
const compañia = this.compañiaFromIccid(iccid)
|
||||
|
||||
if (compañia == undefined) {
|
||||
|
||||
@@ -22,9 +22,9 @@ export class SimUsecases {
|
||||
* Si ya existia se modifican los campos pero no se hace un cambio
|
||||
* de estado.
|
||||
*/
|
||||
async save(args: { iccid: string, imei: string }) {
|
||||
async save(args: { iccid: string, imei: string, compañia: string }) {
|
||||
const activationEvent = <SimEvents.save>{
|
||||
key: "sim.save",
|
||||
key: `sim.${args.compañia}.save`,
|
||||
payload: {
|
||||
iccid: args.iccid,
|
||||
imei: args.imei
|
||||
@@ -34,18 +34,34 @@ export class SimUsecases {
|
||||
return this.eventBus.publish([activationEvent])
|
||||
}
|
||||
|
||||
async activation(args: { iccid: string, compañia: string }) {
|
||||
async activation(args: { iccid: string, compañia: string, offer?: string }) {
|
||||
|
||||
const activationEvent = <SimEvents.general>{
|
||||
const activationEvent = <SimEvents.activation>{
|
||||
key: `sim.${args.compañia}.activate`,
|
||||
payload: {
|
||||
iccid: args.iccid
|
||||
iccid: args.iccid,
|
||||
offer: args.offer
|
||||
}
|
||||
}
|
||||
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
|
||||
*/
|
||||
async cancelation(args: { iccid: string, compañia: string }) {
|
||||
|
||||
const activationEvent = <SimEvents.general>{
|
||||
@@ -57,10 +73,15 @@ export class SimUsecases {
|
||||
console.log("[d] Cancelation ", activationEvent)
|
||||
return this.eventBus.publish([activationEvent])
|
||||
}
|
||||
// alias por si acaso
|
||||
public terminate = this.cancelation;
|
||||
|
||||
async pause(args: { iccid: string }) {
|
||||
/**
|
||||
* alias de bloquear / suspender en objenious
|
||||
*/
|
||||
async pause(args: { iccid: string, compañia: string }) {
|
||||
const cancelationEvent = <SimEvents.pause>{
|
||||
key: "sim.pause",
|
||||
key: `sim.${args.compañia}.pause`,
|
||||
payload: {
|
||||
iccid: args.iccid
|
||||
}
|
||||
@@ -68,9 +89,9 @@ export class SimUsecases {
|
||||
|
||||
return this.eventBus.publish([cancelationEvent])
|
||||
}
|
||||
async free(args: { iccid: string }) {
|
||||
async free(args: { iccid: string, compañia: string }) {
|
||||
const cancelationEvent = <SimEvents.free>{
|
||||
key: "sim.free",
|
||||
key: `sim.${args.compañia}.free`,
|
||||
payload: {
|
||||
iccid: args.iccid
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ simRoutes.post("/save", simController.save)
|
||||
|
||||
simRoutes.post("/activate", simController.activation)
|
||||
|
||||
simRoutes.post("/preActivate", simController.preactivation)
|
||||
|
||||
simRoutes.post("/pause", simController.pause)
|
||||
|
||||
simRoutes.post("/cancel", simController.cancelation)
|
||||
|
||||
Reference in New Issue
Block a user