@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
109 lines (108 loc) • 3.73 kB
JavaScript
import { dateStrToDate, formatDateStr, getMinutesfromDate, makeLocalDateStr, time24ToMinutes } from './datetimes';
import { makeImageMap } from './helpers';
export const makeValidDeals = (deals, orderType, serviceType, revenueCenterId) => {
if (!deals || !deals.length)
return [];
if (orderType) {
deals = deals.filter(i => !i.order_type || i.order_type === orderType);
}
if (serviceType) {
deals = deals.filter(i => !i.service_type || i.service_type === serviceType);
}
if (revenueCenterId) {
deals = deals.filter(i => {
var _a;
return !((_a = i.revenue_centers) === null || _a === void 0 ? void 0 : _a.length) ||
i.revenue_centers
.map(r => r.revenue_center_id)
.includes(revenueCenterId);
});
}
const minutes = getMinutesfromDate(new Date());
deals = deals.filter(i => {
if (!i.dayparts.length)
return true;
const validDayparts = i.dayparts.filter(d => {
const start = time24ToMinutes(d.start_time);
const end = time24ToMinutes(d.end_time);
return start <= minutes && minutes <= end;
});
return validDayparts.length > 0;
});
return deals;
};
const makeDiscountServiceType = (serviceType) => {
switch (serviceType) {
case 'WALKIN':
return 'In-store only';
case 'PICKUP':
return 'Pickup only';
case 'DELIVERY':
return 'Delivery only';
default:
return '';
}
};
const makeDiscountOrderType = (orderType) => {
switch (orderType) {
case 'OLO':
return '';
case 'CATERING':
return 'Catering only';
case 'MERCH':
return 'Merch only';
default:
return '';
}
};
const makeChannelType = (channelType) => {
switch (channelType) {
case 'APP':
return 'App Only!';
case 'ONLINE':
return 'Online Only!';
case 'POS':
return 'At POS Only!';
case 'KIOSK':
return 'Kiosk Only!';
default:
return '';
}
};
export const makeLimitations = (item) => {
const { channel_type, order_type, service_type } = item;
const serviceType = makeDiscountServiceType(service_type);
const orderType = makeDiscountOrderType(order_type);
const channelType = channel_type && makeChannelType(channel_type);
const comma = serviceType && orderType ? ', ' : '';
const dash = (serviceType || orderType) && channelType ? ' - ' : '';
if (serviceType || orderType || channelType) {
return `
${serviceType || ''}
${comma}
${orderType || ''}
${dash}
${channelType || ''}
`;
}
return 'Valid on all online & app orders';
};
export const makeDiscountEnhanced = (item) => {
const today = makeLocalDateStr(new Date(), 0, 'yyyy-MM-dd');
const imageMap = makeImageMap(item.images);
const imageUrl = imageMap.SMALL_IMAGE || imageMap.APP_IMAGE || imageMap.LARGE_IMAGE || null;
const todayOnly = item.end_date === today;
const expiration = item.end_date
? formatDateStr(item.end_date, 'MMM d')
: null;
const limitations = makeLimitations(item);
return Object.assign(Object.assign({}, item), { imageUrl, expiration, todayOnly, limitations });
};
export const getIsDealExpired = (item) => {
const { end_date } = item;
return end_date ? dateStrToDate(end_date) < new Date() : false;
};
export const getIsRewardExpired = (item) => {
const { end_at } = item;
return end_at ? dateStrToDate(end_at) < new Date() : false;
};