Refactor de jwt y base de la bdd de pausas-cancelaciones
This commit is contained in:
@@ -1,14 +1,131 @@
|
||||
import { IOperationsRepository, ObjeniousOperation, ObjeniousOperationChange } from "sim-shared/domain/operationsRepository.port.js";
|
||||
import { Result } from "sim-shared/domain/Result.js";
|
||||
import { Result, tryCatch } from "sim-shared/domain/Result.js";
|
||||
import { PgClient } from "sim-shared/infrastructure/PgClient.js";
|
||||
import { ObjeniousLine, ObjeniousLineResponse } from "../domain/objeniousLine.js";
|
||||
import { HttpClient } from "./HTTPClient.js";
|
||||
import assert from "node:assert";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
export class ObjeniousOperationsRepository implements IOperationsRepository {
|
||||
|
||||
constructor(
|
||||
private http: HttpClient,
|
||||
private readonly pgClient: PgClient
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Consulta el estado de una o mas lineas directamente a la API de Objenious
|
||||
*/
|
||||
public async getLinesAPI(
|
||||
identifierType: "ICCID" | "IMSI" | "IMEI" | "MSISDN" | "REFERENCE",
|
||||
identifiers: string[]
|
||||
): Promise<Result<string, ObjeniousLine[]>> {
|
||||
if (identifiers.length == 0) {
|
||||
return {
|
||||
data: []
|
||||
}
|
||||
}
|
||||
|
||||
// Comprobar < MAX_PAGE_SIZE (Poco probable)
|
||||
|
||||
const path = "/lines"
|
||||
const params = {
|
||||
"identifier.identifierType": identifierType,
|
||||
"identifier.identifiers": identifiers.toString()
|
||||
}
|
||||
|
||||
const req = this.http.client.get<ObjeniousLine[]>(path, {
|
||||
params: params
|
||||
})
|
||||
|
||||
const res = await tryCatch(req)
|
||||
|
||||
if (res.error != undefined) {
|
||||
return {
|
||||
error: res.error?.message
|
||||
}
|
||||
}
|
||||
|
||||
const lines = res.data.data
|
||||
|
||||
return {
|
||||
data: lines
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private MAX_PAGE_SIZE = 1000
|
||||
public async * getLinesByStatusAPI(args?: {
|
||||
pageSize?: number,
|
||||
pageNumber?: number,
|
||||
status?: string
|
||||
}): AsyncGenerator<Result<string, ObjeniousLine[]>, Result<string, ObjeniousLine[]>, any> {
|
||||
|
||||
const path = "/lines"
|
||||
const pageSize = args?.pageSize ?? this.MAX_PAGE_SIZE;
|
||||
|
||||
let currentPage = args?.pageNumber ?? 0;
|
||||
let totalPages: number | undefined = undefined; // Como limite de paginas, igual es pasarse pero hasta que se lea
|
||||
|
||||
const params: Record<string, string | number> = {}
|
||||
|
||||
const loadNextLine = async (page: number): Promise<Result<string, ObjeniousLine[]>> => {
|
||||
if (args?.status != undefined) params["simStatus"] = args.status
|
||||
params["pageSize"] = pageSize
|
||||
params["pageNumber"] = page
|
||||
console.log(`[i] Cargando pagina ${currentPage} de ${totalPages ?? "(desc)"}`)
|
||||
const nextPage = await tryCatch<AxiosResponse<ObjeniousLineResponse>>(this.http.client.get(path, {
|
||||
params: params
|
||||
}))
|
||||
|
||||
if (nextPage.error != undefined) {
|
||||
console.error(nextPage.error)
|
||||
return {
|
||||
error: nextPage.error.message
|
||||
}
|
||||
}
|
||||
|
||||
// Se aumenta para la siguiente ejecucion
|
||||
console.log(`[i] Página ${currentPage} completa, total: ${nextPage.data.data.totalPages}`)
|
||||
totalPages = nextPage.data.data.totalPages
|
||||
|
||||
return {
|
||||
data: nextPage.data.data.content
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// El inicio se ejecuta siempre
|
||||
const lines = await loadNextLine(currentPage)
|
||||
|
||||
if (lines.error != undefined) {
|
||||
console.error("[x] Error obteniendo las lineas, cancelando operación");
|
||||
return {
|
||||
error: "Error cargando lineas"
|
||||
}
|
||||
}
|
||||
|
||||
currentPage++;
|
||||
|
||||
yield {
|
||||
data: lines.data
|
||||
}
|
||||
|
||||
// Copia para evitar bucles infinitos por error de la api
|
||||
const maxPages = totalPages
|
||||
assert.ok(maxPages != undefined, "No se ha defindo el numero de paginas") // Nunca deberia pasar pero así se evitan bucles infnitos
|
||||
console.log("maxPages", maxPages)
|
||||
for (let i = currentPage; i < maxPages!; i++) {
|
||||
console.log("Bucle i:", i, "page: ", currentPage)
|
||||
yield await loadNextLine(currentPage);
|
||||
currentPage++;
|
||||
}
|
||||
|
||||
return {
|
||||
data: []
|
||||
}
|
||||
}
|
||||
async createOperation(data: ObjeniousOperation): Promise<Result<string, ObjeniousOperation>> {
|
||||
const query = `
|
||||
INSERT INTO objenious_operation (operation, iccids, status, max_retry, request_id)
|
||||
|
||||
Reference in New Issue
Block a user