69 lines
1.6 KiB
TypeScript
69 lines
1.6 KiB
TypeScript
import { ObjeniousLine, ObjeniousLineResponse } from "sim-shared/domain/objeniousLine.js";
|
|
import { tryCatch, Result } from "sim-shared/domain/Result.js";
|
|
import { HttpClient } from "sim-shared/infrastructure/HTTPClient.js";
|
|
|
|
const MAX_PAGE_SIZE = 1000
|
|
|
|
export class VolcadoLineas {
|
|
constructor(
|
|
private readonly httpClient: HttpClient
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Mover al repo
|
|
*/
|
|
private async getLinesByStatus(args?: {
|
|
pageSize?: number,
|
|
pageNumber?: number,
|
|
status?: string
|
|
}): Promise<Result<string, ObjeniousLine[]>> {
|
|
|
|
const path = "/lines"
|
|
const pageSize = args?.pageSize ?? MAX_PAGE_SIZE;
|
|
const status = args?.status ?? null;
|
|
let currentPage = args?.pageNumber ?? 0;
|
|
let totalPages = 1;
|
|
let allLines: ObjeniousLine[] = []
|
|
|
|
const loadNextLine = async () => {
|
|
const nextPage = await tryCatch<ObjeniousLineResponse>(this.httpClient.client.get(path, {
|
|
params: {
|
|
simStatus: status,
|
|
pageSize: pageSize,
|
|
pageNumber: currentPage
|
|
}
|
|
}))
|
|
if (nextPage.error != undefined) {
|
|
return {
|
|
error: nextPage.error.msg.message
|
|
}
|
|
}
|
|
|
|
// Se aumenta para la siguiente ejecucion
|
|
currentPage = nextPage.data.pageNumber + 1
|
|
allLines = [...allLines, ...nextPage.data.content]
|
|
totalPages = nextPage.data.totalPages
|
|
}
|
|
|
|
// El inicio se ejecuta siempre
|
|
await loadNextLine()
|
|
|
|
// Copia para evitar bucles infinitos por error de la api
|
|
const maxPages = totalPages
|
|
|
|
for (let i = currentPage; i < maxPages; i++) {
|
|
await loadNextLine()
|
|
}
|
|
|
|
return {
|
|
data: allLines
|
|
}
|
|
}
|
|
|
|
public async loadLines() {
|
|
|
|
}
|
|
|
|
}
|