189 lines
5.5 KiB
TypeScript
189 lines
5.5 KiB
TypeScript
import { ActionData, ActivationData } from "#domain/DTOs/objeniousapi.js"
|
|
import { HttpClient } from "sim-shared/infrastructure/HTTPClient.js"
|
|
import { AxiosError } from "axios"
|
|
import { Result } from "sim-shared/domain/Result.js"
|
|
import { ObjeniousOperation, IOperationsRepository as OperationsRepositoryPort } from "sim-shared/domain/operationsRepository.port.js"
|
|
|
|
// TODO:
|
|
// - Pasar a un archivo de DTOs
|
|
// - Mucha repeticion por funcion, deberia hacer una plantilla
|
|
|
|
export class SimUseCases {
|
|
private readonly httpClient: HttpClient
|
|
private readonly operationRepository: OperationsRepositoryPort
|
|
constructor(args: {
|
|
httpClient: HttpClient,
|
|
operationRepository: OperationsRepositoryPort
|
|
}) {
|
|
this.httpClient = args.httpClient
|
|
this.operationRepository = args.operationRepository
|
|
}
|
|
|
|
private async logOperation(data: ObjeniousOperation) {
|
|
await this.operationRepository.createOperation({
|
|
...data
|
|
})
|
|
}
|
|
|
|
public activate(activationData: ActivationData): () => Promise<Result<string, boolean>> {
|
|
const OPERATION_URL = "/actions/activateLine"
|
|
return async () => {
|
|
const req = this.httpClient.client.post(OPERATION_URL, {
|
|
...activationData
|
|
})
|
|
|
|
try {
|
|
const response = await req
|
|
|
|
if (response.status == 200) {
|
|
console.log("Activacion con exito", response.data)
|
|
|
|
const operation: ObjeniousOperation = {
|
|
operation: "activate",
|
|
iccids: String(activationData.identifier.identifiers),
|
|
status: "noMassID",
|
|
request_id: response.data.requestId
|
|
}
|
|
|
|
this.logOperation(operation).then().catch(e => console.error(e))
|
|
|
|
return <Result<string, boolean>>{
|
|
error: undefined,
|
|
data: true
|
|
}
|
|
|
|
|
|
} else {
|
|
// muy mejorable el control de errores
|
|
return {
|
|
error: String(response.status),
|
|
data: undefined
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error("[Sim.usecase] Error activando ", (error as AxiosError).response?.status)
|
|
return {
|
|
error: "Error general de la peticion",
|
|
data: undefined
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public preActivate(preActivateData: ActionData): () => Promise<Result<string, boolean>> {
|
|
const OPERATION_URL = "/actions/preactivateLine"
|
|
return async () => {
|
|
const req = this.httpClient.client.post(OPERATION_URL, {
|
|
...preActivateData
|
|
})
|
|
|
|
try {
|
|
const resp = await req
|
|
if (resp.status == 200) {
|
|
console.log("Sim preactivada con exito", resp.data)
|
|
const operation: ObjeniousOperation = {
|
|
operation: "preActivate",
|
|
iccids: String(preActivateData.identifier.identifiers),
|
|
status: "noMassID",
|
|
request_id: resp.data.requestId
|
|
}
|
|
|
|
this.logOperation(operation).then().catch(e => console.error(e))
|
|
return <Result<string, boolean>>{
|
|
error: undefined,
|
|
data: true
|
|
}
|
|
} else {
|
|
return <Result<string, boolean>>{
|
|
error: String(resp.status),
|
|
data: undefined
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error("Error preactivacion", preActivateData)
|
|
return <Result<string, boolean>>{
|
|
error: "Error preactivando la sim" + preActivateData.identifier,
|
|
data: undefined
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public reActivate(pauseData: ActionData): () => Promise<Result<string, boolean>> {
|
|
const OPERATION_URL = "/actions/reactivateLine"
|
|
return async () => {
|
|
const req = this.httpClient.client.post(OPERATION_URL, {
|
|
...pauseData
|
|
})
|
|
|
|
try {
|
|
const e = await req
|
|
console.log("Sim reactivada con exito", e.data)
|
|
return <Result<string, boolean>>{
|
|
error: undefined,
|
|
data: true
|
|
}
|
|
} catch (error) {
|
|
console.error("Error reactivacion", error)
|
|
return <Result<string, boolean>>{
|
|
error: "Error reactivando la sim" + pauseData.identifier,
|
|
data: undefined
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public suspend(suspendData: ActionData): () => Promise<Result<string, boolean>> {
|
|
const OPERATION_URL = "/actions/suspendLine"
|
|
return async () => {
|
|
const req = this.httpClient.client.post(OPERATION_URL, {
|
|
...suspendData
|
|
})
|
|
|
|
try {
|
|
const e = await req
|
|
console.log("Sim pausada/suspendida con exito", e.data)
|
|
return <Result<string, boolean>>{
|
|
error: undefined,
|
|
data: true
|
|
}
|
|
} catch (error) {
|
|
console.error("[Pausa Use case] Error pausa")
|
|
return {
|
|
error: "Error general pausando/suspendiendo la sim" + suspendData.identifier,
|
|
data: undefined
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 cancelada con exito", e.data)
|
|
return <Result<string, boolean>>{
|
|
error: undefined,
|
|
data: true
|
|
}
|
|
} catch (error) {
|
|
console.error("Error pausa", error)
|
|
return <Result<string, boolean>>{
|
|
error: "Error cancelando/terminate la sim" + terminationData.identifier,
|
|
data: undefined
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|