import { AlaiAPI } from "#domain/AlaiAPI.js"; import axios, { AxiosError, AxiosResponse } from "axios"; import { Result } from "sim-shared/domain/Result.js"; import { env } from "#config/env/env.js"; import { HttpClient } from "sim-shared/infrastructure/HTTPClient.js"; import https from "https" type ErrorRepo = { msg: string, stackTrace?: string } export class AlaiRepository { constructor( private httpClient: HttpClient ) { } private async manageRequest(promiseReq: Promise>): Promise> { try { const res = await promiseReq return { data: res.data } } catch (e) { if (axios.isAxiosError(e)) { console.log("ERROR REQUEST ", e.response) const error = e as AxiosError return { error: { msg: error.code + " : " + String(error.response?.statusText), stackTrace: JSON.stringify(error.response?.data) } } } else { return { error: { msg: String(e), stackTrace: String(e) } } } } } public static async login(httpsAgent: https.Agent): Promise> { const alaiUrl = env.ALAI_API_URL const endpoint = "/v1/auth/login" const fullUrl = alaiUrl + endpoint const data = { "username": env.ALAI_USERNAME, "password": env.ALAI_PASSWORD, "brandID": env.ALAI_BRANDID } try { const loginRes = await axios.post(fullUrl, data, { httpsAgent }) return { data: loginRes.data } } catch (e) { if (axios.isAxiosError(e)) { const error = e as AxiosError return { error: { msg: error.code + " : " + String(error.response?.statusText), stackTrace: String(error) } } } else { return { error: { msg: String(e) } } } } } /** * Los orders son la unidad que envuelve las peticiones para garantizar ideponencia. */ public async createOrder() { // POST const endpoint = "/v1/order" const data: AlaiAPI.CreateOrderDTO = { type: "RETAIL", salesChannel: "OWN_CALLCENTER", status: "CONFIRMED", packages: [{ id: env.ALAI_PACKAGE as string }], subscriber: { id: env.ALAI_SUBSCRIBER_ID as string } } const promReq = this.httpClient.post(endpoint, data) const res = await this.manageRequest(promReq) return res } public async applyOrder(orderId: string) { const endpoint = `/v1/order/${orderId}` const params = new URLSearchParams([ ["action", "APPLY"] ]) const promReq = this.httpClient.patch(endpoint, undefined, { params: params }) const res = await this.manageRequest(promReq) return res } /* * Ver estado del order para debug */ public async getOrder(orderId: string) { const endpoint = `/v1/order/${orderId}` const promReq = this.httpClient.post(endpoint, undefined) const res = await this.manageRequest(promReq) return res } /** * Antes se usaba PATCH /v1/sim/{iccid}/{orderId} pero en la docu ha pasado a POST */ public async createReserve(orderId: string, iccid: string): Promise> { const endpoint = `/v1/sim/${iccid}/order/${orderId}` // Crear la reserva no usa datos en el body const promReq = this.httpClient.post(endpoint, undefined) const res = await this.manageRequest(promReq) return res } /** * IMPORTANTE: * - En el campo subscription viene el id para los cambios de estado, no se hacen * sobre la sim, sino sobre la subscription */ public async getSimByICCID(iccid: string) { const endpoint = `/v1/sim/${iccid}` const promReq = this.httpClient.get(endpoint, undefined) const res = await this.manageRequest(promReq) return res } public async pauseSubscription(subscriptionId: string) { const endpoint = `/v1/subscription/${subscriptionId}` // En teoria ahora se usa ["action", "BLOCK"] pero no he probado const params = new URLSearchParams([ ["action", "CHANGE_STATUS"] ]) const data = { status: "BLOCKEDCORE" } const promReq = this.httpClient.patch(endpoint, data, { params: params }) const res = await this.manageRequest(promReq) return res } public async activateSubscription(subscriptionId: string) { const endpoint = `/v1/subscription/${subscriptionId}` // En teoria ahora se usa ["action", "UNBLOCK"] pero no he probado const params = new URLSearchParams([ ["action", "CHANGE_STATUS"] ]) const data = { "status": "ACTIVE" } const promReq = this.httpClient.patch(endpoint, data, { params: params }) const res = await this.manageRequest(promReq) return res } public async unPauseSubscription(subscriptionId: string) { const endpoint = `/v1/subscription/${subscriptionId}` // En teoria ahora se usa ["action", "UNBLOCK"] pero no he probado const params = new URLSearchParams([ ["action", "UNBLOCK"] ]) const rawParams = { "action": "UNBLOCK" } const data = { status: "ACTIVE" } const promReq = this.httpClient.patch(endpoint, undefined, { params: rawParams }) const res = await this.manageRequest(promReq) return res } public async terminateSubscription(subscriptionId: string) { const endpoint = `/v1/subscription/${subscriptionId}` // Esta llamada si es de acuerdo a la docu const params = new URLSearchParams([ ["action", "TERMINATE"] ]) const promReq = this.httpClient.patch(endpoint, undefined, { params: params }) const res = await this.manageRequest(promReq) return res } public async changeExternalId(subscriptionId: string, externalId: string) { const endpoint = `/v1/subscription/${subscriptionId}` // Esta llamada si es de acuerdo a la docu const params = new URLSearchParams([ ["action", "MODIFY"] ]) const data = { externalID: externalId } const promReq = this.httpClient.patch(endpoint, data, { params: params }) const res = await this.manageRequest(promReq) return res } public async getSubscriptionById(subscriptionId: string) { const endpoint = `/v1/subscription/${subscriptionId}` const promReq = this.httpClient.get(endpoint) const res = await this.manageRequest(promReq) return res } public async getImeiFromSubscription(subscriptionId: string) { const endpoint = `/v1/subscription/${subscriptionId}/imei` const promReq = this.httpClient.get(endpoint) const res = await this.manageRequest(promReq) return res } }