191 lines
6.1 KiB
TypeScript
191 lines
6.1 KiB
TypeScript
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"
|
|
|
|
export class AlaiRepository {
|
|
constructor(
|
|
private httpClient: HttpClient
|
|
) {
|
|
}
|
|
|
|
private async manageRequest<E, T>(promiseReq: Promise<AxiosResponse<T>>): Promise<Result<string, T>> {
|
|
try {
|
|
const res = await promiseReq
|
|
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
|
|
const data = {
|
|
"username": env.ALAI_USERNAME,
|
|
"password": env.ALAI_PASSWORD,
|
|
"brandID": env.ALAI_BRANDID
|
|
}
|
|
|
|
try {
|
|
const loginRes = await axios.post<AlaiAPI.LoginResponseDTO>(fullUrl, data, { httpsAgent })
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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<AlaiAPI.CreateOrderResponseDTO>(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<AlaiAPI.ApplyOrderDTO>(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<AlaiAPI.CreateOrderResponseDTO>(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<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
|
|
}
|
|
|
|
/**
|
|
* 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<AlaiAPI.Sim | undefined>(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<AlaiAPI.UpdateSubscriptionDTO | undefined>(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", "CHANGE_STATUS"]
|
|
])
|
|
const data = {
|
|
status: "ACTIVE"
|
|
}
|
|
const promReq = this.httpClient.patch<AlaiAPI.UpdateSubscriptionDTO | undefined>(endpoint, data, { params: params })
|
|
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<AlaiAPI.UpdateSubscriptionDTO | undefined>(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<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.get<AlaiAPI.Subscription | undefined>(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<AlaiAPI.GetImeiSubscriptionDTO | undefined>(endpoint)
|
|
const res = await this.manageRequest(promReq)
|
|
return res
|
|
}
|
|
}
|