/* eslint-disable no-console */ /* eslint-disable no-await-in-loop */ import { getEnvs } from "@/libs/environment/env"; import { StrategyService } from "@/server/services/strategy.services"; import type { StrategyResponse } from "./strategy-services.types"; export const URLS_TYPES = { hotelList: "hotelList", hotelDetail: "hotelDetail", addToCart: "addToCart", createPost: "createPost", selectList: "selectList", requestChangeStatus: "requestChangeStatus", hotelValidate: "hotelValidate", flightList: "flightList", flightDetail: "flightDetail", flightAddToCart: "flightAddToCart", flightValidate: "flightValidate", createPostFlight: "createPostFlight", travelReason: "travelReason", transferList: "transferList", tourList: "tourList", tourDetail: "tourDetail", tourAddToCart: "tourAddToCart", tourValidate: "tourValidata", createPostTour: "createPostTour", transferDetail: "transferDetail", transferAddToCart: "transferAddToCart", transferValidate: "transferValidate", transferRetrieve: "transferRetrieve", createPostTransfer: "createPostTransfer", bookStrategy: "bookStrategy", retrieveHotel: "retrieveHotel", retrieveTour: "retrieveTour", flightRetrieve: "flightRetrieve", retrieveRentACar: "retrieveRentACar", rentACarList: "rentACarList", rentACarDetail: "rentACarDetail", rentACarAddToCart: "rentACarAddToCart", rentACarRequestCreate: "rentACarRequestCreate", rentACarValidate: "rentACarValidate", bookRequestDetail: "bookRequestDetail", bookCancelReasonList: "bookCancelReasonList", bookCore: "bookCore", hotelRoomSearch: "hotelRoomSearch", } as const; export type StrategyKeysTypes = Record; export const getStrategyKeys = async (token: string, baseUrl: string) => { const envs = getEnvs(baseUrl); const URLS = { hotelList: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-hotel-search&LanguageCode=tr`, hotelDetail: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-hotel-search-detail&LanguageCode=tr`, addToCart: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-hotel-addtocart&LanguageCode=tr`, createPost: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/Corporate-Request-Create-Hotel&LanguageCode=tr`, selectList: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/Corporate-Request-SelectList&LanguageCode=tr`, requestChangeStatus: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/Corporate-Request-Change-Status&LanguageCode=tr`, hotelValidate: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-hotel-validation&LanguageCode=tr&Culture=TR`, flightList: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-flight-search-new&LanguageCode=tr&Culture=TR&RequestIdType=1`, flightDetail: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-flight-search-detail&LanguageCode=tr&Culture=TR`, flightAddToCart: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-flight-addtocart&LanguageCode=tr&Culture=TR`, flightValidate: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-flight-validation&LanguageCode=tr&Culture=TR`, flightRetrieve: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-flight-retrieve&LanguageCode=tr&Culture=TR`, createPostFlight: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/Corporate-Request-Create-Flight&LanguageCode=tr&Culture=TR`, travelReason: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/TravelReason&LanguageCode=tr&Culture=TR&V=2`, transferList: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-transfer-search&LanguageCode=tr&Culture=TR&RequestIdType=1`, tourList: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-tour-search&LanguageCode=tr&Culture=TR&RequestIdType=1`, tourDetail: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-tour-detail&LanguageCode=tr&Culture=TR`, tourAddToCart: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-tour-addtocart&LanguageCode=tr&Culture=TR`, tourValidate: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-tour-validate&LanguageCode=tr&Culture=TR`, transferDetail: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-transfer-detail&LanguageCode=tr&Culture=TR`, transferAddToCart: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-transfer-addtocart&LanguageCode=tr&Culture=TR`, transferValidate: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-transfer-validation&LanguageCode=tr&Culture=TR`, transferRetrieve: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-transfer-retrieve&LanguageCode=tr&Culture=TR`, createPostTransfer: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/Corporate-Request-Create-Transfer&LanguageCode=tr&Culture=TR`, bookStrategy: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-booking&LanguageCode=tr&Culture=TR`, retrieveHotel: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-hotel-retrieve&LanguageCode=tr&Culture=TR`, retrieveTour: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-tour-retrieve&LanguageCode=tr&Culture=TR`, retrieveRentACar: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-carRental-retrieve&LanguageCode=tr&Culture=TR`, rentACarList: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-carRental-search&LanguageCode=tr&Culture=TR&RequestIdType=1`, rentACarDetail: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-carRental-detail&LanguageCode=tr&Culture=TR`, rentACarAddToCart: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-carRental-addcart&LanguageCode=tr&Culture=TR`, rentACarRequestCreate: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/Corporate-Request-Create-CarRental&LanguageCode=tr&Culture=TR`, rentACarValidate: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-carRental-validation&LanguageCode=tr&Culture=TR`, bookRequestDetail: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-request-detail&LanguageCode=tr&Culture=TR`, bookCancelReasonList: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/TravelReason&LanguageCode=tr&Culture=TR&V=2`, bookCore: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/corporate-booking&LanguageCode=tr&Culture=TR`, hotelRoomSearch: `${envs.wlDisplayUrl}/api/v1/strategy/get?path=/hotel-room-search&LanguageCode=tr&Culture=TR`, }; const inMemoryKeys = await StrategyService.getStrategyKeys(baseUrl); if (inMemoryKeys) { console.log("🚀", "Get Strategy Keys from Redis"); return JSON.parse(inMemoryKeys); } function updateOptions(options: RequestInit) { const update = { ...options }; if (token) { update.headers = { ...update.headers, Authorization: `Bearer ${token}`, }; update.credentials = "include"; update.referrer = baseUrl; } return update; } function fetcher(url: string, options: RequestInit) { return fetch(url, updateOptions(options)).then((response) => { return response.json(); }); } async function getKeys() { const responses: Record = {}; const keysAndUrls = Object.entries(URLS); for (const [key, url] of keysAndUrls) { try { const response: StrategyResponse = await fetcher(url, { method: "GET", }); if (key === "travelReason" || key === "bookCancelReasonList") { responses[key] = (response.result as any).strategyCode; } else { responses[key] = response.result.data.strategyCode; } } catch (error: any) { responses[key] = "err"; } } return responses; } const keys = await getKeys(); if (!Object.values(keys).includes("err")) { await StrategyService.setStrategyKeys(JSON.stringify(keys), baseUrl); return keys; } return null; };