import { IAccountModel, IEndUserModel, IUserEventModel } from "@nimee/shared-types"; import axios from "axios"; const port = process.env.USER_PORT; const basePath = process.env.IS_LOCAL === "true" ? `http://127.0.0.1:${port}` : "http://user"; const endUserUrl = `${basePath}/user/enduser`; export class EndUserClient { async createEndUserIfNeededAndUpdateUserEvent(params: { account: IAccountModel; userEvent: IUserEventModel; jwt: string; marketplaceId: string; shouldUpdateUserEvent?: boolean; }) { const { account, userEvent, jwt, marketplaceId, shouldUpdateUserEvent } = params; const response = await axios.post( `${endUserUrl}/createEndUserIfNeededAndUpdateUserEvent/${marketplaceId}/${account._id.toString()}`, { account, userEvent, shouldUpdateUserEvent: shouldUpdateUserEvent, }, { headers: { jwt }, } ); return response?.data; } async getById(params: { endUserId: string; jwt: string; marketplaceId: string; accountId: string }) { const { endUserId, jwt, marketplaceId, accountId } = params; const response = await axios.get(`${endUserUrl}/getById/${marketplaceId}/${accountId}/${endUserId}`, { headers: { jwt }, }); return response?.data; } async getByIds(params: { ids: string[]; jwt: string; marketplaceId: string; sellerId: string }) { const { ids, jwt, marketplaceId, sellerId } = params; const response = await axios.post( `${endUserUrl}/getByIds/${marketplaceId}/${sellerId}`, { ids }, { headers: { jwt }, } ); return response?.data; } async getByPhoneOrMailAndSellerId(params: { phone?: string; email?: string; jwt: string; sellerId: string }) { const { phone, email, jwt, sellerId } = params; if (!phone && !email) { throw new Error("Phone or Email are required"); } // encodeURI component const response = await axios.get(`${endUserUrl}/getByPhoneOrMailAndSellerId/${sellerId}`, { headers: { jwt }, params: { phone, email, }, }); return response?.data; } async createEndUser(params: { endUser: IEndUserModel; jwt: string; marketplaceId: string; sellerId: string }) { const { endUser, jwt, marketplaceId, sellerId } = params; const response = await axios.post(`${endUserUrl}/create/${marketplaceId}/${sellerId}`, endUser, { headers: { jwt }, }); return response?.data; } async updateEndUser(params: { endUser: IEndUserModel; jwt: string; marketplaceId: string; sellerId: string }) { const { endUser, jwt, marketplaceId, sellerId } = params; // updateEndUser: "/user/enduser/update/:marketplaceId/:accountId/:endUserId", const response = await axios.put(`${endUserUrl}/update/${marketplaceId}/${sellerId}/${endUser._id.toString()}`, endUser, { headers: { jwt }, }); return response?.data; } async updateExternalPurchaseToken(params: { endUserId: string; token: string; jwt: string; marketplaceId: string; sellerId: string; customerId?: string; }) { const { endUserId, token, jwt, marketplaceId, sellerId, customerId } = params; return axios.put( `${endUserUrl}/updateExternalPurchaseToken/${marketplaceId}/${sellerId}/${endUserId}`, { token, customerId }, { headers: { jwt } } ); } // Stores the card as the customer's card on file. Writes the display fields AND the token in one update, // so the card shown to the customer can never drift from the one a SAVED_CARD checkout charges — which is // what happens if only the token is written (see updateExternalPurchaseToken). async saveSavedCard(params: { endUserId: string; jwt: string; marketplaceId: string; sellerId: string; buyerKey: string; buyerCardMask?: string; buyerCardExp?: string; buyerName?: string; }) { const { endUserId, jwt, marketplaceId, sellerId, buyerKey, buyerCardMask, buyerCardExp, buyerName } = params; const response = await axios.post( `${endUserUrl}/saved-card/${marketplaceId}/${sellerId}/${endUserId}`, { buyerKey, buyerCardMask, buyerCardExp, buyerName }, { headers: { jwt } } ); return response?.data; } // Clears the card-on-file DISPLAY slot (endUser.savedCard) only — externalPurchaseToken is deliberately // untouched (live season-ticket billing charges that slot). mask/exp narrow the clear to that exact card, // so the user service atomically no-ops when the slot already holds a different (newer) card. async clearSavedCard(params: { endUserId: string; jwt: string; marketplaceId: string; sellerId: string; buyerCardMask?: string; buyerCardExp?: string; }) { const { endUserId, jwt, marketplaceId, sellerId, buyerCardMask, buyerCardExp } = params; const response = await axios.delete(`${endUserUrl}/saved-card/${marketplaceId}/${sellerId}/${endUserId}`, { headers: { jwt }, params: { buyerCardMask, buyerCardExp }, }); return response?.data; } }