import axios from "axios"; const port = process.env.USER_EVENT_PORT; const basePath = process.env.IS_LOCAL === "true" ? `http://127.0.0.1:${port}` : "http://user-event"; const userEventOrderUrl = `${basePath}/user-event/event-order`; import { BasketOrderType, IEventOrderModel, IStatusPaymentEnum } from "@nimee/shared-types"; export class EventOrderClient { async updateWithId(params: { token: string; eventOrder: IEventOrderModel; eventOrderId: string }) { const { token, eventOrder, eventOrderId } = params; const response = await axios.put( `${userEventOrderUrl}/updateWithId`, { eventOrder, eventOrderId, }, { headers: { jwt: `${token}` }, } ); return response?.data; } async getEventOrderByJWT(params: { customPaymentJWT: string; adminJWT: string }) { const { customPaymentJWT, adminJWT } = params; const response = await axios.post( `${userEventOrderUrl}/getByJWT`, { customPaymentJWT }, { headers: { jwt: `${adminJWT}` }, } ); return response?.data; } async getEventOrderById(params: { accountId: string; eventId: string; eventOrderId: string; jwt: string; shouldPopulateUserEvents?: boolean; }) { const { accountId, eventOrderId, eventId, jwt, shouldPopulateUserEvents } = params; if (shouldPopulateUserEvents) { const response = await axios.get( `${userEventOrderUrl}/getEventOrderByIdWithPopulatedUserEvents/${accountId}/${eventId}/${eventOrderId}`, { headers: { jwt: `${jwt}` }, } ); return response?.data; } const response = await axios.get(`${userEventOrderUrl}/getEventOrderById/${accountId}/${eventId}/${eventOrderId}`, { headers: { jwt: `${jwt}` }, }); return response?.data; } async getAllEventOrders(jwt: string) { const response = await axios.get(`${userEventOrderUrl}/getAllEventOrders`, { headers: { jwt: `${jwt}` }, }); return response?.data; } async updateEventOrder(jwt: string) { const response = await axios.put(`${userEventOrderUrl}/updateEventOrder`, { headers: { jwt: `${jwt}` }, }); return response?.data; } async getEventOrderByEndUserSeasonTicketId(params: { endUserSeasonTicketId: string; jwt: string; accountId: string; eventId: string; }) { const { endUserSeasonTicketId, jwt, accountId, eventId } = params; const response = await axios.get( `${userEventOrderUrl}/getByEndUserSeasonTicketId/${accountId}/${eventId}/${endUserSeasonTicketId}`, { headers: { jwt: `${jwt}` }, } ); return response?.data; } async updateEventOrderStatusAndAddDataFromDivideOrder(params: { eventOrderId: string; status: IStatusPaymentEnum; jwt: string; groupId?: string; link?: string; divideShareLink?: string; divideIdempotent?: string; }) { const { eventOrderId, status, jwt, groupId, link, divideShareLink, divideIdempotent } = params; const response = await axios.put( `${userEventOrderUrl}/updateStatus/${eventOrderId}`, { status, groupId, link, divideShareLink, divideIdempotent }, { headers: { jwt: `${jwt}` }, } ); return response?.data; } async updateEventOrderStatusToPaidByWebhook(params: { eventOrderId: string; chargeId: string; jwt: string }) { const { eventOrderId, chargeId, jwt } = params; const response = await axios.put( `${userEventOrderUrl}/updateStatusToPaidByWebhook`, { eventOrderId, chargeId }, { headers: { jwt: `${jwt}` }, } ); return response?.data; } async createEventOrderForBasket(params: { accountId: string; eventId: string; eventOrder: any; jwt: string; language?: string; }) { const { accountId, eventId, eventOrder, jwt, language } = params; const url = `${userEventOrderUrl}/createForBasket/${accountId}/${eventId}${language ? `?language=${language}` : ""}`; const response = await axios.post(url, eventOrder, { headers: { jwt }, }); return response?.data; } async cancelEventOrderForBasket(params: { eventOrderId: string; errorMessage?: string; jwt: string; }) { const { eventOrderId, errorMessage, jwt } = params; const response = await axios.post( `${userEventOrderUrl}/cancelForBasket/${eventOrderId}`, { errorMessage }, { headers: { jwt } } ); return response?.data; } async createBasketOrder(params: { seller: string; marketplace: string; eventOrderIds?: string[]; paymentMethod: string; totalPrice: number; currency: string; status?: string; clientName?: string; clientEmail?: string; clientPhone?: string; createdBy?: string; createdByUserName?: string; type?: BasketOrderType; posDeviceId?: string; posDeviceName?: string; jwt: string; }) { const { jwt, ...body } = params; const response = await axios.post( `${basePath}/user-event/basket-order/create`, body, { headers: { jwt } } ); return response?.data; } // Internal-only (JWT_ADMIN cron): completed basket-order counts for a seller in [from, to) async getBasketZSummary(params: { jwt: string; accountId: string; from: string; to: string }) { const { jwt, accountId, from, to } = params; const response = await axios.get(`${basePath}/user-event/basket-order/z-summary/${accountId}`, { params: { from, to }, headers: { jwt }, }); return response?.data; } async updateBasketOrderStatus(params: { basketOrderId: string; status: string; eventOrderIds?: string[]; totalPrice?: number; last4Digits?: string; refundedAmount?: number; jwt: string; }) { const { basketOrderId, jwt, ...body } = params; const response = await axios.put( `${basePath}/user-event/basket-order/updateStatus/${basketOrderId}`, body, { headers: { jwt } } ); return response?.data; } async getBasketOrderById(params: { accountId: string; basketOrderId: string; jwt: string }) { const { accountId, basketOrderId, jwt } = params; const response = await axios.get( `${basePath}/user-event/basket-order/getById/${accountId}/${basketOrderId}`, { headers: { jwt } } ); return response?.data; } async pushBasketOrderEdit(params: { basketOrderId: string; edit: { date?: Date; performedBy?: { userId?: string; userName?: string }; action: "update" | "refund"; previousItems?: any[]; newItems?: any[]; previousTotal?: number; newTotal?: number; netAmount?: number; refundedTickets?: any[]; refundedAmount?: number; // 'credit-card' | 'pos' | 'cash' | 'no-payment' — required, enforced by Mongoose on write paymentMethod: string; chargeId?: string; last4Digits?: string; cardBrand?: string; }; jwt: string; }) { const { basketOrderId, edit, jwt } = params; const response = await axios.put( `${basePath}/user-event/basket-order/pushEdit/${basketOrderId}`, { edit }, { headers: { jwt } } ); return response?.data; } }