UNPKG

forest-express

Version:

Official package for all Forest Express Lianas

59 lines (58 loc) 1.8 kB
"use strict"; var _ = require('lodash'); var JSONAPISerializer = require('jsonapi-serializer').Serializer; var Schemas = require('../../../generators/schemas'); function serializeInvoices(invoices, collectionName, meta) { function getCustomerAttributes() { if (!invoices.length) { return []; } var schema = Schemas.schemas[collectionName]; if (!schema) { return []; } return _.map(schema.fields, 'field'); } function format(invoice) { // jshint camelcase: false invoice.date = new Date(invoice.date * 1000); if (invoice.period_start) { invoice.period_start = new Date(invoice.period_start * 1000); } if (invoice.period_end) { invoice.period_end = new Date(invoice.period_end * 1000); } if (invoice.subtotal) { invoice.subtotal /= 100; } if (invoice.total) { invoice.total /= 100; } return invoice; } var customerAttributes = getCustomerAttributes(); if (invoices.length) { invoices = invoices.map(format); } else { invoices = format(invoices); } var type = "".concat(collectionName, "_stripe_invoices"); return new JSONAPISerializer(type, invoices, { attributes: ['amount_due', 'attempt_count', 'attempted', 'closed', 'currency', 'date', 'forgiven', 'paid', 'period_start', 'period_end', 'subtotal', 'total', 'application_fee', 'tax', 'tax_percent', 'customer'], customer: { ref: Schemas.schemas[collectionName].idField, attributes: customerAttributes }, keyForAttribute: function keyForAttribute(key) { return key; }, typeForAttribute: function typeForAttribute(attr) { if (attr === 'customer') { return collectionName; } return attr; }, meta: meta }); } module.exports = serializeInvoices;