96 lines
2.4 KiB
TypeScript
96 lines
2.4 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";
|
|
|
|
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(): 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): Promise<Result<string, string>> {
|
|
const ENDPOINT = `/v1/sim/${iccid}/order/${order}`
|
|
|
|
|
|
}
|
|
}
|