Files
sf-sim/packages/sim-consumidor-alai/infrastructure/AlaiRepository.ts

191 lines
6.1 KiB
TypeScript
Raw Normal View History

import { AlaiAPI } from "#domain/AlaiAPI.js";
2026-04-23 13:18:50 +02:00
import axios, { AxiosError, AxiosResponse } from "axios";
import { Result } from "sim-shared/domain/Result.js";
import { env } from "#config/env/env.js";
2026-04-30 17:41:49 +02:00
import { HttpClient } from "sim-shared/infrastructure/HTTPClient.js";
import https from "https"
2026-04-23 13:18:50 +02:00
export class AlaiRepository {
2026-04-23 13:18:50 +02:00
constructor(
2026-04-30 17:41:49 +02:00
private httpClient: HttpClient
2026-04-23 13:18:50 +02:00
) {
}
2026-04-30 17:41:49 +02:00
private async manageRequest<E, T>(promiseReq: Promise<AxiosResponse<T>>): Promise<Result<string, T>> {
2026-04-23 13:18:50 +02:00
try {
2026-04-30 17:41:49 +02:00
const res = await promiseReq
2026-04-23 13:18:50 +02:00
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 static async login(httpsAgent: https.Agent): Promise<Result<string, AlaiAPI.LoginResponseDTO>> {
const alaiUrl = env.ALAI_API_URL
const endpoint = "/v1/auth/login"
const fullUrl = alaiUrl + endpoint
2026-04-23 13:18:50 +02:00
const data = {
"username": env.ALAI_USERNAME,
"password": env.ALAI_PASSWORD,
"brandID": env.ALAI_BRANDID
2026-04-23 13:18:50 +02:00
}
try {
const loginRes = await axios.post<AlaiAPI.LoginResponseDTO>(fullUrl, data, { httpsAgent })
2026-04-23 13:18:50 +02:00
return {
data: loginRes.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)
}
2026-04-23 13:18:50 +02:00
}
}
}
/**
* Los orders son la unidad que envuelve las peticiones para garantizar ideponencia.
2026-04-23 13:18:50 +02:00
*/
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 }
2026-04-23 13:18:50 +02:00
}
const promReq = this.httpClient.post<AlaiAPI.CreateOrderResponseDTO>(endpoint, data)
const res = await this.manageRequest(promReq)
return res
2026-04-23 13:18:50 +02:00
}
2026-05-04 09:37:06 +02:00
public async applyOrder(orderId: string) {
const endpoint = `/v1/order/${orderId}`
const params = new URLSearchParams([
["action", "APPLY"]
])
2026-05-05 14:24:24 +02:00
const promReq = this.httpClient.patch<AlaiAPI.ApplyOrderDTO>(endpoint, undefined, { params: params })
2026-05-04 09:37:06 +02:00
const res = await this.manageRequest(promReq)
return res
}
/*
2026-05-05 17:31:08 +02:00
* Ver estado del order para debug
*/
public async getOrder(orderId: string) {
2026-05-05 17:31:08 +02:00
const endpoint = `/v1/order/${orderId}`
const promReq = this.httpClient.post<AlaiAPI.CreateOrderResponseDTO>(endpoint, undefined)
const res = await this.manageRequest(promReq)
return res
}
2026-04-23 13:18:50 +02:00
/**
2026-05-04 09:37:06 +02:00
* Antes se usaba PATCH /v1/sim/{iccid}/{orderId} pero en la docu ha pasado a POST
*/
2026-05-04 09:37:06 +02:00
public async createReserve(orderId: string, iccid: string): Promise<Result<string, AlaiAPI.CreateOrderResponseDTO>> {
const endpoint = `/v1/sim/${iccid}/order/${orderId}`
// Crear la reserva no usa datos en el body
const promReq = this.httpClient.post<AlaiAPI.CreateOrderResponseDTO>(endpoint, undefined)
const res = await this.manageRequest(promReq)
return res
}
2026-04-23 13:18:50 +02:00
2026-05-05 13:12:31 +02:00
/**
* IMPORTANTE:
* - En el campo subscription viene el id para los cambios de estado, no se hacen
* sobre la sim, sino sobre la subscription
*/
2026-05-04 09:37:06 +02:00
public async getSimByICCID(iccid: string) {
const endpoint = `/v1/sim/${iccid}`
2026-05-04 15:12:53 +02:00
const promReq = this.httpClient.get<AlaiAPI.Sim | undefined>(endpoint, undefined)
2026-05-04 09:37:06 +02:00
const res = await this.manageRequest(promReq)
return res
2026-04-23 13:18:50 +02:00
}
2026-05-05 13:12:31 +02:00
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"
}
2026-05-05 14:24:24 +02:00
const promReq = this.httpClient.patch<AlaiAPI.UpdateSubscriptionDTO | undefined>(endpoint, data, { params: params })
2026-05-05 13:12:31 +02:00
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", "CHANGE_STATUS"]
])
const data = {
status: "ACTIVE"
}
2026-05-05 14:24:24 +02:00
const promReq = this.httpClient.patch<AlaiAPI.UpdateSubscriptionDTO | undefined>(endpoint, data, { params: params })
2026-05-05 13:12:31 +02:00
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"]
])
2026-05-05 14:24:24 +02:00
const promReq = this.httpClient.patch<AlaiAPI.UpdateSubscriptionDTO | undefined>(endpoint, undefined, { params: params })
2026-05-05 13:12:31 +02:00
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
}
2026-05-05 14:24:24 +02:00
const promReq = this.httpClient.patch<AlaiAPI.UpdateSubscriptionDTO | undefined>(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.patch<AlaiAPI.Subscription | undefined>(endpoint)
2026-05-05 13:12:31 +02:00
const res = await this.manageRequest(promReq)
return res
}
2026-05-07 13:53:02 +02:00
public async getImeiFromSubscription(subscriptionId: string) {
const endpoint = `/v1/subscription/${subscriptionId}/imei`
const promReq = this.httpClient.patch<AlaiAPI.GetImeiSubscriptionDTO | undefined>(endpoint)
const res = await this.manageRequest(promReq)
return res
}
2026-04-23 13:18:50 +02:00
}