@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
29 lines (28 loc) • 929 B
JavaScript
import { parseISO } from 'date-fns';
import { dashesToSlashes } from './helpers';
export const profileFields = [
'customer_id',
'first_name',
'last_name',
'email',
'phone',
'company',
'birth_date',
'is_verified',
'is_notification_set'
];
export const makeCustomerProfile = (customer) => {
return profileFields.reduce((obj, field) => (Object.assign(Object.assign({}, obj), { [field]: field === 'birth_date'
? dashesToSlashes(customer[field])
: customer[field] })), {});
};
export const getLastOrder = (orders) => {
if (!orders || !orders.length)
return null;
const withCreated = orders
.filter(i => i.order_type !== 'MERCH')
.map(i => (Object.assign(Object.assign({}, i), { createdAt: parseISO(i.created_at) })))
.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())
.reverse();
return withCreated[0];
};