import { Result } from "sim-shared/domain/Result.js"; import { NosHttpClient } from "./AlaiHttpClient.js"; import { NosApi } from "#domain/AlaiAPI.js"; import axios, { AxiosError, AxiosResponse } from "axios"; export class NosRepository { constructor( private httpClient: NosHttpClient ) { } /** * E => Tipo de error * T => Tipo de dato para cod 200 * * TODO: * - Mejor gestion de los errores * - E no se aplica todavia por no hacer la transformacion del error */ private async manageNosRequest(promise: Promise>): Promise> { try { const res = await promise return { data: res.data } } catch (e) { if (axios.isAxiosError(e)) { const error = e as AxiosError return { error: error.code + " : " + String(error.response?.statusText) } } else { return { error: String(e) } } } } public async getLineInfo(iccid: string): Promise> { const PATH = "/subscribers/" + iccid console.log("PAth", PATH) const lineRequest = this.httpClient.get(PATH) const lineResponse = await this.manageNosRequest(lineRequest) if (lineResponse.error != undefined) { return lineResponse } else { return { data: lineResponse.data.content } } } /** * El metodo de NOS de paginar las lineas * maximo por pagina 100, default 25 * no devuelve el offset ni el numero de elementos restantes * hay que llevar la cuenta */ public async getLinePage(args: { limit?: number, offset?: number, filter?: string, orderBy?: string }): Promise> { const PATH = "/subscribers" const LIMIT = 100 const options = { limit: args.limit ?? LIMIT, offset: args.offset ?? 0, filter: args.filter, orderBy: args.orderBy } const pageRequest = this.httpClient.get(PATH, { params: options }) const pageResponse = await this.manageNosRequest(pageRequest) if (pageResponse.error != undefined) { return pageResponse } else { return { data: pageResponse.data.content } } } public async getLinesInfo(iccid: string[]) /*Promise>*/ { throw new Error("NOS no permite buscar iccid en bulk, se puede hacer un apaño pero está en proceso") const PATH = "/subscribers" const LIMIT = 100 const steps = Math.ceil(iccid.length / LIMIT) const options = { limit: LIMIT, offset: 0, } const req = this.httpClient.post(PATH) const resp = await this.manageNosRequest(req) if (resp.error != undefined) { return resp } else { return { //@ts-expect-error data: resp.data.content } } } public async activateSim(iccid: string): Promise> { const PATH = '/provisioning' const PRODUCT_ID = 1330 // No se que es, preguntar a Ivan const data = { productSetId: PRODUCT_ID } const req = this.httpClient.post(PATH, data) const resp = await this.manageNosRequest(req) if (resp.error != undefined) { return resp } else { return { data: resp.data.content } } } /** * "A bar is a service provisioning action that results in a subscriber being blocked from accessing an operator's network. The bar remains in place until the operator is sent an unbar request." * Se entiende que un "bar" es una suspension temporal */ public async bar(iccid: string) { const PATH = `/subscribers/${iccid}/products` const data = { product: "BAR DN TOTAL", action: "enable" } const req = this.httpClient.patch(PATH, data) const resp = await this.manageNosRequest(req) if (resp.error != undefined) { return resp } else { return { data: resp.data.content } } } public async unbar(iccid: string) { const PATH = `/subscribers/${iccid}/products` const data = { product: "BAR DN TOTAL", action: "disable" } const req = this.httpClient.patch(PATH, data) const resp = await this.manageNosRequest(req) if (resp.error != undefined) { return resp } else { return { data: resp.data.content } } } }