177 lines
5.1 KiB
TypeScript
177 lines
5.1 KiB
TypeScript
|
|
import { IOperationsRepository, Objenious, ObjeniousOperation, ObjeniousOperationChange } from "#objenious-shared/domain/operationsRepository.port.js"
|
||
|
|
import { HttpClient } from "#shared/infrastructure/HTTPClient.js";
|
||
|
|
|
||
|
|
export class CheckObjeniousRequests {
|
||
|
|
constructor(
|
||
|
|
private readonly operationsRepository: IOperationsRepository,
|
||
|
|
private readonly httpClient: HttpClient
|
||
|
|
) {
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* TODO: meter a una funcion a parte task con los 3 pasos
|
||
|
|
*/
|
||
|
|
public async getPendingOperations() {
|
||
|
|
const pendingOperations = await this.operationsRepository.getPendingOperations()
|
||
|
|
|
||
|
|
|
||
|
|
if (pendingOperations.error != undefined) {
|
||
|
|
throw new Error("Error obteniendo las tareas pendientes " + pendingOperations.error)
|
||
|
|
}
|
||
|
|
|
||
|
|
if (pendingOperations.data == undefined || pendingOperations.data.length == 0) {
|
||
|
|
//Nada pendiente
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const erroneas = pendingOperations.data
|
||
|
|
.filter((e) => e.request_id == undefined)
|
||
|
|
|
||
|
|
const operacionesValidas = pendingOperations.data
|
||
|
|
.filter((e) => e.request_id != undefined)
|
||
|
|
|
||
|
|
const solicitarMassId = operacionesValidas
|
||
|
|
.filter((e) => e.mass_action_id == undefined)
|
||
|
|
|
||
|
|
const consultarEstado = pendingOperations.data
|
||
|
|
.filter(e => e.mass_action_id != undefined)
|
||
|
|
|
||
|
|
console.log("validas", operacionesValidas)
|
||
|
|
console.log("Solicitando mass id para", solicitarMassId)
|
||
|
|
|
||
|
|
const newMassActions = await this.getMassIdFromRequest(solicitarMassId)
|
||
|
|
|
||
|
|
const merged = [...newMassActions || [], ...consultarEstado]
|
||
|
|
|
||
|
|
console.log("Solicitando status para", merged)
|
||
|
|
|
||
|
|
const result = await this.getMassActionsStatus(merged)
|
||
|
|
}
|
||
|
|
|
||
|
|
private async getMassActionsStatus(requestList: ObjeniousOperation[]) {
|
||
|
|
if (requestList.length == 0) return;
|
||
|
|
|
||
|
|
const operationsList = structuredClone(requestList)
|
||
|
|
const PATH = "/actions/massActions"
|
||
|
|
const updated = []
|
||
|
|
|
||
|
|
const iccids = operationsList
|
||
|
|
.map(e => e.iccids)
|
||
|
|
.flat()
|
||
|
|
|
||
|
|
const iccidSet = new Set<string>(iccids)
|
||
|
|
|
||
|
|
const req = this.httpClient.client.get<Objenious.ResponseGetMassAction[]>(PATH, {
|
||
|
|
params: <Objenious.ParametersGetMassAction>{
|
||
|
|
"identifier.identifierType": "ICCID",
|
||
|
|
"identifier.identifiers": Array.from(iccidSet)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
let res;
|
||
|
|
// 1. Comprobacion de la request.
|
||
|
|
try {
|
||
|
|
res = await req
|
||
|
|
} catch (e) {
|
||
|
|
console.error("Error comprobando el estado de ", iccidSet, e)
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const { data, status } = res
|
||
|
|
|
||
|
|
if (status != 200 || data == undefined) {
|
||
|
|
console.error("Error buscando los massActions")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if (data.length == 0) return;
|
||
|
|
|
||
|
|
// 2. Por cada elemento de la respuesta se comprueba si ha habido un cambio de estado
|
||
|
|
for (const action of data) {
|
||
|
|
const { id, status } = action
|
||
|
|
const original = operationsList.find(e => e.id == id)
|
||
|
|
|
||
|
|
console.log("Comprobando", action, original)
|
||
|
|
|
||
|
|
if (original == undefined) continue;
|
||
|
|
|
||
|
|
if (status != original?.status) {
|
||
|
|
console.log("Actualizando", action, original)
|
||
|
|
const updateData: ObjeniousOperationChange = {
|
||
|
|
operation_id: original.id!,
|
||
|
|
new_status: status,
|
||
|
|
}
|
||
|
|
original.status = status;
|
||
|
|
original.last_change_date = new Date().toISOString()
|
||
|
|
|
||
|
|
if (action.info != undefined) {
|
||
|
|
updateData.info = action.info
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
await this.operationsRepository.updateOperation(updateData)
|
||
|
|
updated.push(action)
|
||
|
|
} catch (e) {
|
||
|
|
console.error("Error actualizando el estado de ", action, e)
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Refrescar los requests hasta que conseguir una Id de mass action
|
||
|
|
* Como no se puede consultar por
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
private async getMassIdFromRequest(requestList: ObjeniousOperation[]) {
|
||
|
|
if (requestList.length == 0) return;
|
||
|
|
|
||
|
|
const PATH = "/actions/requests/"
|
||
|
|
const operationsList = structuredClone(requestList)
|
||
|
|
|
||
|
|
|
||
|
|
for (const request of operationsList) {
|
||
|
|
if (request.id == undefined) continue;
|
||
|
|
|
||
|
|
const req = this.httpClient.client.get<Objenious.ResponseGetRequestById>(PATH + request.request_id, {
|
||
|
|
})
|
||
|
|
|
||
|
|
let res;
|
||
|
|
|
||
|
|
// 1. Comprobacion de la request.
|
||
|
|
try {
|
||
|
|
res = await req
|
||
|
|
} catch (e) {
|
||
|
|
console.error("Error comprobando el estado de ", request, e)
|
||
|
|
//todo actualizar el estado para incluir el error
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. Modificacion del massId si ha habido un cambio
|
||
|
|
const massActionId = res.data.massActionIds[0]
|
||
|
|
try {
|
||
|
|
if (res.status == 200 && res.data != undefined && massActionId != undefined) {
|
||
|
|
const updateData: ObjeniousOperationChange = {
|
||
|
|
operation_id: request.id,
|
||
|
|
new_status: "IN_PROGRESS",
|
||
|
|
new_mass_action_id: String(massActionId)
|
||
|
|
}
|
||
|
|
|
||
|
|
await this.operationsRepository.updateOperation(updateData)
|
||
|
|
//! Se actualiza el registro para futuras operaciones
|
||
|
|
request.mass_action_id = String(massActionId)
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
console.log("Error actualizando ell estado de ", request)
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. Se devuelve la lista de los requests con las actualizaciones
|
||
|
|
return operationsList
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|