UNPKG

@fabrix/spool-cart

Version:

Spool - eCommerce Spool for Fabrix

184 lines (183 loc) 6.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const common_1 = require("@fabrix/fabrix/dist/common"); const spool_sequelize_1 = require("@fabrix/spool-sequelize"); const errors_1 = require("@fabrix/spool-sequelize/dist/errors"); const lodash_1 = require("lodash"); const shortId = require("shortid"); class TransactionActionResolver extends spool_sequelize_1.SequelizeResolver { batch(options, batch) { const self = this; options.limit = options.limit || 100; options.offset = options.offset || 0; options.regressive = options.regressive || false; const recursiveQuery = function (options) { let count = 0; return self.findAndCountAll(options) .then(results => { count = results.count; return batch(results.rows); }) .then(batched => { if (count >= (options.regressive ? options.limit : options.offset + options.limit)) { options.offset = options.regressive ? 0 : options.offset + options.limit; return recursiveQuery(options); } else { return Promise.resolve(); } }); }; return recursiveQuery(options); } resolveByInstance(transaction, options = {}) { return Promise.resolve(transaction); } resolveById(transaction, options = {}) { return this.findById(transaction.id, options) .then(resUser => { if (!resUser && options.reject !== false) { throw new errors_1.ModelError('E_NOT_FOUND', `Transaction ${transaction.id} not found`); } return resUser; }); } resolveByToken(transaction, options = {}) { return this.findOne(this.app.services.SequelizeService.mergeOptionDefaults(options, { where: { token: transaction.token } })) .then(resUser => { if (!resUser && options.reject !== false) { throw new errors_1.ModelError('E_NOT_FOUND', `Transaction token ${transaction.token} not found`); } return resUser; }); } resolveByNumber(transaction, options = {}) { return this.findById(transaction, options) .then(resUser => { if (!resUser && options.reject !== false) { throw new errors_1.ModelError('E_NOT_FOUND', `Transaction ${transaction.token} not found`); } return resUser; }); } resolveByString(transaction, options = {}) { return this.findOne(this.app.services.SequelizeService.mergeOptionDefaults(options, { where: { code: transaction } })) .then(resUser => { if (!resUser && options.reject !== false) { throw new errors_1.ModelError('E_NOT_FOUND', `Transaction ${transaction} not found`); } return resUser; }); } resolve(transaction, options = {}) { const resolvers = { 'instance': transaction instanceof this.instance, 'id': !!(transaction && lodash_1.isObject(transaction) && transaction.id), 'token': !!(transaction && lodash_1.isObject(transaction) && transaction.token), 'number': !!(transaction && lodash_1.isNumber(transaction)), 'string': !!(transaction && lodash_1.isString(transaction)) }; const type = Object.keys(resolvers).find((key) => resolvers[key]); switch (type) { case 'instance': { return this.resolveByInstance(transaction, options); } case 'id': { return this.resolveById(transaction, options); } case 'token': { return this.resolveByToken(transaction, options); } case 'number': { return this.resolveByNumber(transaction, options); } case 'string': { return this.resolveByString(transaction, options); } default: { const err = new Error(`Unable to resolve Transaction ${transaction}`); return Promise.reject(err); } } } } exports.TransactionActionResolver = TransactionActionResolver; class TransactionAction extends common_1.FabrixModel { static get resolver() { return TransactionActionResolver; } static config(app, Sequelize) { return { options: { underscored: true, enums: {}, description: 'A Transaction Action is a representation of a transaction event.', scopes: { live: { where: { live_mode: true } }, authorized: { where: { kind: 'authorize', status: 'success' } }, captured: { where: { kind: ['capture', 'sale'], status: 'success' } }, voided: { where: { kind: 'void', status: 'success' } }, refunded: { where: { kind: 'refund', status: 'success' } } }, hooks: { beforeCreate: (transaction, options) => { if (!transaction.token) { transaction.token = `action_${shortId.generate()}`; } }, } } }; } static schema(app, Sequelize) { return { token: { type: Sequelize.STRING, unique: true }, transaction_id: { type: Sequelize.INTEGER, allowNull: true }, live_mode: { type: Sequelize.BOOLEAN, defaultValue: app.config.get('cart.live_mode') } }; } static associate(models) { } } exports.TransactionAction = TransactionAction;