Files
sf-sim/packages/sim-consumidor-alai/infrastructure/AlaiRepository.ts
Alvar San Martin f98097d11d Estructura para el token de alai
cabecera automatica de bearer para todas las requests a alai
2026-04-30 15:49:59 +02:00

96 lines
2.4 KiB
TypeScript

import { AlaiAPI } from "#domain/AlaiAPI.js";
import axios, { AxiosError, AxiosResponse } from "axios";
import { AlaiHttpClient } from "./AlaiHttpClient.js";
import { Result } from "sim-shared/domain/Result.js";
import { env } from "#config/env/env.js";
export class AlaiRepository {
constructor(
private httpClient: AlaiHttpClient
) {
}
private async manageRequest<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 {
error: error.code + " : " + String(error.response?.statusText)
}
} else {
return {
error: String(e)
}
}
}
}
public static async login(): 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)
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
}
/*
* Ver estado del order
*/
public async getOrder(orderId: string) {
const ENDPOINT = `/v1/order/${orderId}`
}
/**
*
*/
public async createReserve(order: string, iccid: string) {
const ENDPOINT = `/v1/sim/${iccid}/order/${order}`
}
}