UNPKG

@open-tender/utils

Version:

A library of utils for use with Open Tender applications that utilize our cloud-based Order API.

141 lines (140 loc) 5.78 kB
import { isCreditCustomerCheckoutTender, isCreditGuestCheckoutTender, isCreditTender, isGiftCardCheckoutTender, isGiftCardTender, isHouseAccountCheckoutTender, isHouseAccountTender } from '@open-tender/types'; import { checkAmountRemaining } from './cart'; export const adjustTenders = (tenders, isPaid, amountRemaining, updateForm) => { if (!tenders.length || isPaid) return; const gift = tenders.filter(i => i.tender_type === 'GIFT_CARD'); const nonGift = tenders.filter(i => i.tender_type !== 'GIFT_CARD'); if (amountRemaining > 0) { if (!nonGift.length) return; const newTenders = nonGift.map(i => { const newAmount = parseFloat(i.amount) + amountRemaining; amountRemaining = 0.0; return Object.assign(Object.assign({}, i), { amount: newAmount.toFixed(2) }); }); updateForm({ tenders: [...gift, ...newTenders] }); isPaid = Math.abs(amountRemaining).toFixed(2) === '0.00'; } else { const newTenders = nonGift.map(i => { const newAmount = Math.max(parseFloat(i.amount) + amountRemaining, 0.0); amountRemaining += parseFloat(i.amount) - newAmount; return Object.assign(Object.assign({}, i), { amount: newAmount.toFixed(2) }); }); isPaid = Math.abs(amountRemaining).toFixed(2) === '0.00'; if (isPaid) { const nonZero = newTenders.filter(i => i.amount !== '0.00'); updateForm({ tenders: [...gift, ...nonZero] }); } else { const newGift = gift.map(i => { const newAmount = Math.max(parseFloat(i.amount) + amountRemaining, 0.0); amountRemaining += parseFloat(i.amount) - newAmount; return Object.assign(Object.assign({}, i), { amount: newAmount.toFixed(2) }); }); const allNew = [...newGift, ...newTenders].filter(i => i.amount !== '0.00'); updateForm({ tenders: allNew }); isPaid = Math.abs(amountRemaining).toFixed(2) === '0.00'; } } }; export const updateTenders = (tenders, total) => { if (!total || !tenders || !tenders.length) return null; let amountRemaining = checkAmountRemaining(total, tenders); if (amountRemaining === 0) return null; const gift = tenders.filter(i => i.tender_type === 'GIFT_CARD'); const nonGift = tenders.filter(i => i.tender_type !== 'GIFT_CARD'); if (amountRemaining > 0) { if (!nonGift.length) return null; const newTenders = nonGift.map(i => { const newAmount = parseFloat(i.amount) + amountRemaining; amountRemaining = 0.0; return Object.assign(Object.assign({}, i), { amount: newAmount.toFixed(2) }); }); return [...gift, ...newTenders]; } else { const newTenders = nonGift.map(i => { const newAmount = Math.max(parseFloat(i.amount) + amountRemaining, 0.0); amountRemaining += parseFloat(i.amount) - newAmount; return Object.assign(Object.assign({}, i), { amount: newAmount.toFixed(2) }); }); const isPaid = Math.abs(amountRemaining).toFixed(2) === '0.00'; if (isPaid) { const nonZero = newTenders.filter(i => i.amount !== '0.00'); return [...gift, ...nonZero]; } else { const newGift = gift.map(i => { const newAmount = Math.max(parseFloat(i.amount) + amountRemaining, 0.0); amountRemaining += parseFloat(i.amount) - newAmount; return Object.assign(Object.assign({}, i), { amount: newAmount.toFixed(2) }); }); return [...newGift, ...newTenders].filter(i => i.amount !== '0.00'); } } }; export const makeCreditCardName = (creditCard) => { const { card_type_name, last4 } = creditCard; return last4 ? `${card_type_name} ending in ${last4}` : card_type_name; }; export const makeTenderName = (tender) => { if (isCreditTender(tender)) { return makeCreditCardName(tender.credit_card); } else if (isGiftCardTender(tender)) { return `Gift Card ${tender.card_number}`; } else if (isHouseAccountTender(tender)) { return `${tender.house_account.name} House Account`; } else { return ''; } }; export const makeCheckoutTenderName = (tender) => { if (isCreditCustomerCheckoutTender(tender)) { const { card_type_name, last4 } = tender; return last4 ? `${card_type_name} ending in ${last4}` : card_type_name; } else if (isCreditGuestCheckoutTender(tender)) { const { card_type_name, last4 } = tender; return last4 ? `${card_type_name} ending in ${last4}` : card_type_name; } else if (isGiftCardCheckoutTender(tender)) { return `Gift Card ${tender.card_number}`; } else if (isHouseAccountCheckoutTender(tender)) { return `${tender.name} House Account`; } else { return ''; } }; export const makeConfirmationOrderType = (order) => { if (!order) return null; const { order_type, service_type, order_fulfillment, revenue_center, details } = order; const { notes_internal } = details || {}; const isWalkIn = service_type === 'WALKIN'; if (isWalkIn) { if (notes_internal && notes_internal.includes('TAKE OUT')) { return 'takeOut'; } return 'dineIn'; } if (order_type === 'CATERING') return 'catering'; if (revenue_center.is_outpost) return 'outpost'; if (service_type === 'DELIVERY') return 'delivery'; if (service_type === 'PICKUP' && order_fulfillment) { return 'curbside'; } return 'pickup'; };