BillTracker/client/lib/billDrafts.js

41 lines
1.5 KiB
JavaScript

function categoryForTemplate(template, categories = []) {
const keywords = template?.categoryKeywords || [];
const match = categories.find(category => {
const name = String(category.name || '').toLowerCase();
return keywords.some(keyword => name.includes(keyword));
});
return match?.id ?? null;
}
function categoryIdOrFallback(categoryId, template, categories = []) {
if (categoryId && categories.some(category => String(category.id) === String(categoryId))) {
return categoryId;
}
return template ? categoryForTemplate(template, categories) : null;
}
export function makeBillDraft(source, { copy = false, template = null, categories = [] } = {}) {
const data = source || {};
return {
...data,
id: undefined,
source_bill_id: copy && data.id != null ? data.id : undefined,
active: 1,
name: copy
? `${data.name || 'Bill'} (Copy)`
: (data.name || template?.name || ''),
category_id: categoryIdOrFallback(data.category_id, template, categories),
due_day: data.due_day || 1,
expected_amount: data.expected_amount ?? 0,
billing_cycle: data.billing_cycle || 'monthly',
cycle_type: data.cycle_type || 'monthly',
cycle_day: String(data.cycle_day || '1'),
autopay_enabled: !!data.autopay_enabled,
autodraft_status: data.autodraft_status || (data.autopay_enabled ? 'assumed_paid' : 'none'),
auto_mark_paid: !!data.auto_mark_paid,
has_2fa: !!data.has_2fa,
snowball_include: !!data.snowball_include,
snowball_exempt: !!data.snowball_exempt,
};
}