UNPKG

@malga-checkout/core

Version:
49 lines (48 loc) 2.1 kB
import { formatPayload as formatCustomer } from '../customers/customers.utils'; import { cookiesEnabled, getUserAgent, getHostname, getIpAddress, } from '@malga-checkout/utils'; const formatFraudAnalysisBrowser = async (browserFingerprint, email) => { if (!browserFingerprint) return {}; const ipAddress = await getIpAddress(); const hostName = getHostname(); const cookiesAccepted = cookiesEnabled(); const type = getUserAgent(); return { browser: { browserFingerprint, email, cookiesAccepted, hostName, ipAddress, type, }, }; }; export const formatFraudAnalysis = async (fraudAnalysis, customer) => { var _a; const currentCustomer = (fraudAnalysis === null || fraudAnalysis === void 0 ? void 0 : fraudAnalysis.customer) || customer; if (!fraudAnalysis || (!(currentCustomer === null || currentCustomer === void 0 ? void 0 : currentCustomer.address) && !(fraudAnalysis === null || fraudAnalysis === void 0 ? void 0 : fraudAnalysis.usePartialCustomer))) return null; const parsedCustomer = formatCustomer(currentCustomer, true); const address = parsedCustomer.address && Object.assign(Object.assign({}, parsedCustomer.address), { number: parsedCustomer.address.streetNumber }); const browser = await formatFraudAnalysisBrowser(fraudAnalysis.browserFingerprint, parsedCustomer.email); if (address) { delete address.streetNumber; } const phone = ((_a = parsedCustomer === null || parsedCustomer === void 0 ? void 0 : parsedCustomer.phoneNumber) === null || _a === void 0 ? void 0 : _a.trim()) ? { phone: parsedCustomer.phoneNumber } : {}; return { customer: Object.assign(Object.assign(Object.assign(Object.assign({ name: parsedCustomer.name, email: parsedCustomer.email }, (parsedCustomer.document && { identityType: parsedCustomer.document.type, identity: parsedCustomer.document.number, })), (currentCustomer.address && { deliveryAddress: address, billingAddress: address, })), phone), browser), cart: { items: fraudAnalysis.cart, }, }; };