2026-04-17 14:06:41 +02:00
import { Result } from "sim-shared/domain/Result.js" ;
2026-04-22 12:31:46 +02:00
import { NosHttpClient } from "./NosHttpClient.js" ;
2026-04-17 14:06:41 +02:00
import { NosApi } from "#domain/NosAPI.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 < E , T > ( promise : Promise < AxiosResponse < T > > ) : Promise < Result < string , T > > {
try {
const res = await promise
return {
data : res.data
}
} catch ( e ) {
if ( axios . isAxiosError ( e ) ) {
const error = e as AxiosError
return {
2026-04-21 10:11:21 +02:00
error : error.code + " : " + String ( error . response ? . statusText )
2026-04-17 14:06:41 +02:00
}
} else {
return {
2026-04-21 10:11:21 +02:00
error : String ( e )
2026-04-17 14:06:41 +02:00
}
}
}
}
public async getLineInfo ( iccid : string ) : Promise < Result < string , NosApi.LineData > > {
const PATH = "/subscribers/" + iccid
2026-04-21 10:11:21 +02:00
console . log ( "PAth" , PATH )
const lineRequest = this . httpClient . get < NosApi.LineDataResponseOK > ( PATH )
const lineResponse = await this . manageNosRequest < string , NosApi.LineDataResponseOK > ( lineRequest )
if ( lineResponse . error != undefined ) {
return lineResponse
} else {
return {
data : lineResponse.data.content
}
}
}
2026-04-21 12:50:54 +02:00
/ * *
* 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
* /
2026-04-21 10:11:21 +02:00
public async getLinePage ( args : {
limit? : number ,
offset? : number ,
filter? : string ,
orderBy? : string
} ) : Promise < Result < string , any > > {
const PATH = "/subscribers"
const LIMIT = 100
const options = {
2026-04-21 12:50:54 +02:00
limit : args.limit ? ? LIMIT ,
offset : args.offset ? ? 0 ,
2026-04-21 10:11:21 +02:00
filter : args.filter ,
orderBy : args.orderBy
}
const pageRequest = this . httpClient . get ( PATH , {
params : options
} )
const pageResponse = await this . manageNosRequest < string , any > ( pageRequest )
if ( pageResponse . error != undefined ) {
return pageResponse
} else {
return {
data : pageResponse.data.content
}
}
}
public async getLinesInfo ( iccid : string [ ] ) /*Promise<Result<string, NosApi.LineData>>*/ {
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 ,
}
2026-04-17 14:06:41 +02:00
const req = this . httpClient . post < NosApi.LineDataResponseOK > ( PATH )
const resp = await this . manageNosRequest < string , NosApi.LineDataResponseOK > ( req )
if ( resp . error != undefined ) {
return resp
} else {
return {
2026-04-21 10:11:21 +02:00
//@ts-expect-error
2026-04-17 14:06:41 +02:00
data : resp.data.content
}
}
}
public async activateSim ( iccid : string ) : Promise < Result < string , NosApi.ActivationData > > {
const PATH = '/provisioning'
const PRODUCT_ID = 1330 // No se que es, preguntar a Ivan
const data = {
productSetId : PRODUCT_ID
}
const req = this . httpClient . post < NosApi.ActivateResponseOK > ( PATH , data )
const resp = await this . manageNosRequest < string , NosApi.ActivateResponseOK > ( 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"
}
2026-04-22 15:22:37 +02:00
const req = this . httpClient . patch < NosApi.BarResponseOk > ( PATH , data )
2026-04-17 14:06:41 +02:00
const resp = await this . manageNosRequest < string , NosApi.BarResponseOk > ( 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"
}
2026-04-22 15:22:37 +02:00
const req = this . httpClient . patch < NosApi.BarResponseOk > ( PATH , data )
2026-04-17 14:06:41 +02:00
const resp = await this . manageNosRequest < string , NosApi.BarResponseOk > ( req )
if ( resp . error != undefined ) {
return resp
} else {
return {
data : resp.data.content
}
}
}
}