import { createLead } from '../../infrastructure/create-lead' import { CallMePayload } from '../../domain/call-me' import { default as kaliEnvironment, Environment, } from '../../ui/utils/environment' type Parameters = CallMePayload & { isCallMeBack?: boolean } type Callbacks = { respondWithSuccess: () => void respondWithError: (error: unknown) => void } function getURL(url: string) { const devEnv: Environment = 'development' //Amazon Waf protection does not allow us to send 'localhost' in the request body // Currently the local environment is not dev. We have pending to know why. return kaliEnvironment.env === devEnv || url.includes('localhost') ? 'development' : url } export function handleCreatePhoneLead(): AsyncUseCase { return { execute } async function execute( { phone, url, isCallMeBack = false, businessUnit = 'electricity', }: Parameters, { respondWithSuccess, respondWithError }: Callbacks, ) { const prodEnv: Environment = 'production' const stagingEnv: Environment = 'staging' const parsedBusinessUnit = ['solar', 'gas', 'cloud', 'battery'].includes( businessUnit, ) ? businessUnit : businessUnit.includes('batteryPartner') ? 'battery' : 'electricity' try { if ( kaliEnvironment.env === prodEnv || kaliEnvironment.env === stagingEnv ) { await createLead({ phone, businessUnit: parsedBusinessUnit, isCallMeBack, url: getURL(url), }) return respondWithSuccess() } respondWithSuccess() } catch (error) { respondWithError(error) } } }