import { isEmpty } from "lodash-es"; import { cookies, headers } from "next/headers"; import { getEnvs } from "@/libs/environment/env"; import { clientConfigurationService } from "@/libs/servers/get-client-config"; import { getToken } from "@/libs/servers/get-token"; import { getStrategyKeys } from "@/services/app-api/apis/strategy.services"; import type { IClientDomainAddonResponse } from "../services/addon.services"; import { getAddonsFromRedis, getClientDomainAddonService, setAddonsToRedis, } from "../services/addon.services"; import { ClientConfigService } from "../services/client-config.services"; import { StrategyService } from "../services/strategy.services"; import { TokenService } from "../services/token.services"; export class Preload { static async clearClientToken(domainUrl: string) { await TokenService.deleteClientToken(domainUrl); } static async getClientToken(domainUrl: string, passCookie: boolean = false) { try { if (!passCookie) { const clientTokenCookie = cookies().get("client-token"); if (clientTokenCookie && clientTokenCookie.value !== "") { return clientTokenCookie.value; } } const clientTokenRedis = await TokenService.getClientToken(domainUrl); if ( clientTokenRedis?.access_token && clientTokenRedis.access_token !== "" ) { return clientTokenRedis.access_token; } const tokenResponse = await getToken(domainUrl); if (tokenResponse?.access_token && tokenResponse.access_token !== "") { await TokenService.setClientToken(domainUrl, tokenResponse); return tokenResponse.access_token; } return null; } catch (error) { return null; } } static async getStrategyKeys(domainUrl: string, clientToken: string) { const strategyKeysRedis = await StrategyService.getStrategyKeys(domainUrl); if (strategyKeysRedis && !isEmpty(strategyKeysRedis)) { return strategyKeysRedis; } const strategyKeys = await getStrategyKeys(clientToken, domainUrl); if (strategyKeys && !isEmpty(strategyKeys)) { await StrategyService.setStrategyKeys(domainUrl, strategyKeys); return strategyKeys; } return null; } static async getClientConfigs(domainUrl: string, clientToken: string) { const clientConfigRedis = await ClientConfigService.getClientConfig(domainUrl); if (clientConfigRedis && !isEmpty(clientConfigRedis)) { return clientConfigRedis; } const clientConfigResponse = await clientConfigurationService( domainUrl, clientToken, ); if (clientConfigResponse && !isEmpty(clientConfigResponse)) { await ClientConfigService.setClientConfig( clientConfigResponse, domainUrl, ); return clientConfigResponse; } return null; } static async getDomainAddons( domainUrl: string, clientToken: string, ): Promise { const appAddonRedis = await getAddonsFromRedis(domainUrl); if (appAddonRedis && !isEmpty(appAddonRedis)) { return appAddonRedis; } const clientDomainAddonListResult = await getClientDomainAddonService( domainUrl, clientToken, ); const clientDomainAddonList = clientDomainAddonListResult?.filter( (x) => x.Status === 1, ); if (clientDomainAddonList) { await setAddonsToRedis(domainUrl, clientDomainAddonList); return clientDomainAddonList; } return null; } static async getBaseUrl() { const xUrl = headers().get("x-url"); const url = new URL(xUrl as string); return url; } static isDev() { return getEnvs().clientId === "test_client"; } }