UNPKG

forest-express

Version:

Official package for all Forest Express Lianas

163 lines (162 loc) 8.23 kB
"use strict"; var _require = require('@forestadmin/context'), inject = _require.inject; var _ = require('lodash'); var IntegrationInformationsGetter = require('../../services/integration-informations-getter'); var PaymentsGetter = require('./services/payments-getter'); var PaymentGetter = require('./services/payment-getter'); var InvoicesGetter = require('./services/invoices-getter'); var InvoiceGetter = require('./services/invoice-getter'); var SourcesGetter = require('./services/sources-getter'); var SourceGetter = require('./services/source-getter'); var SubscriptionsGetter = require('./services/subscriptions-getter'); var SubscriptionGetter = require('./services/subscription-getter'); var PaymentRefunder = require('./services/payment-refunder'); var serializePayments = require('./serializers/payments'); var serializeInvoices = require('./serializers/invoices'); var serializeCards = require('./serializers/cards'); var serializeSubscriptions = require('./serializers/subscriptions'); var serializeBankAccounts = require('./serializers/bank-accounts'); var auth = require('../../services/auth'); var path = require('../../services/path'); module.exports = function Routes(app, model, Implementation, opts) { var _inject = inject(), modelsManager = _inject.modelsManager; var modelName = Implementation.getModelName(model); var integrationInfo; if (opts.integrations && opts.integrations.stripe) { integrationInfo = new IntegrationInformationsGetter(modelName, Implementation, opts.integrations.stripe).perform(); } if (integrationInfo) { var SEPARATOR = '.'; var integrationValues = integrationInfo.split(SEPARATOR); integrationInfo = { collection: modelsManager.getModels()[integrationValues[0]], field: integrationValues[1], embeddedPath: integrationValues.slice(2).join(SEPARATOR) || null }; } var getPayments = function getPayments(request, response, next) { new PaymentsGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (results) { var count = results[0]; var payments = results[1]; return serializePayments(payments, modelName, { count: count }); }).then(function (payments) { response.send(payments); })["catch"](next); }; var getPayment = function getPayment(request, response, next) { new PaymentGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (payment) { return serializePayments(payment, modelName); }).then(function (payment) { response.send(payment); })["catch"](next); }; var refund = function refund(request, response, next) { new PaymentRefunder(request.body, opts).perform().then(function () { response.status(204).send(); })["catch"](function (err) { if (err.type === 'StripeInvalidRequestError') { response.status(400).send({ error: err.message }); } else { next(err); } }); }; var getInvoices = function getInvoices(request, response, next) { new InvoicesGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (results) { var count = results[0]; var invoices = results[1]; return serializeInvoices(invoices, modelName, { count: count }); }).then(function (invoices) { response.send(invoices); })["catch"](next); }; var getInvoice = function getInvoice(request, response, next) { new InvoiceGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (invoice) { return serializeInvoices(invoice, modelName); }).then(function (invoice) { response.send(invoice); })["catch"](next); }; var getCards = function getCards(request, response, next) { request.params.object = 'card'; new SourcesGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (results) { var count = results[0]; var cards = results[1]; return serializeCards(cards, modelName, { count: count }); }).then(function (cards) { response.send(cards); })["catch"](next); }; var getCard = function getCard(request, response, next) { new SourceGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (card) { return serializeCards(card, modelName); }).then(function (card) { response.send(card); })["catch"](next); }; var getSubscriptions = function getSubscriptions(request, response, next) { new SubscriptionsGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (results) { var count = results[0]; var subscriptions = results[1]; return serializeSubscriptions(subscriptions, modelName, { count: count }); }).then(function (subscriptions) { response.send(subscriptions); })["catch"](next); }; var getSubscription = function getSubscription(request, response, next) { new SubscriptionGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (subscription) { return serializeSubscriptions(subscription, modelName); }).then(function (subscription) { response.send(subscription); })["catch"](next); }; var getBankAccounts = function getBankAccounts(request, response, next) { request.params.object = 'bank_account'; new SourcesGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (results) { var count = results[0]; var bankAccounts = results[1]; return serializeBankAccounts(bankAccounts, modelName, { count: count }); }).then(function (bankAccounts) { response.send(bankAccounts); })["catch"](next); }; var getBankAccount = function getBankAccount(request, response, next) { new SourceGetter(Implementation, _.extend(request.query, request.params), opts, integrationInfo).perform().then(function (bankAccount) { return serializeBankAccounts(bankAccount, modelName); }).then(function (bankAccount) { response.send(bankAccount); })["catch"](next); }; this.perform = function () { if (integrationInfo) { app.get(path.generate("".concat(modelName, "_stripe_payments"), opts), auth.ensureAuthenticated, getPayments); app.get(path.generate("".concat(modelName, "_stripe_payments/:paymentId"), opts), auth.ensureAuthenticated, getPayment); app.get(path.generate("".concat(modelName, "/:recordId/stripe_payments"), opts), auth.ensureAuthenticated, getPayments); app.post(path.generate("".concat(modelName, "_stripe_payments/refunds"), opts), auth.ensureAuthenticated, refund); app.get(path.generate("".concat(modelName, "_stripe_invoices"), opts), auth.ensureAuthenticated, getInvoices); app.get(path.generate("".concat(modelName, "/:recordId/stripe_invoices"), opts), auth.ensureAuthenticated, getInvoices); app.get(path.generate("".concat(modelName, "_stripe_invoices/:invoiceId"), opts), auth.ensureAuthenticated, getInvoice); app.get(path.generate("".concat(modelName, "/:recordId/stripe_cards"), opts), auth.ensureAuthenticated, getCards); app.get(path.generate("".concat(modelName, "_stripe_cards"), opts), auth.ensureAuthenticated, getCard); app.get(path.generate("".concat(modelName, "_stripe_subscriptions"), opts), auth.ensureAuthenticated, getSubscriptions); app.get(path.generate("".concat(modelName, "_stripe_subscriptions/:subscriptionId"), opts), auth.ensureAuthenticated, getSubscription); app.get(path.generate("".concat(modelName, "/:recordId/stripe_subscriptions"), opts), auth.ensureAuthenticated, getSubscriptions); app.get(path.generate("".concat(modelName, "/:recordId/stripe_bank_accounts"), opts), auth.ensureAuthenticated, getBankAccounts); app.get(path.generate("".concat(modelName, "_stripe_bank_accounts"), opts), auth.ensureAuthenticated, getBankAccount); } }; };