@fabrix/spool-cart
Version:
Spool - eCommerce Spool for Fabrix
210 lines (209 loc) • 7.38 kB
JavaScript
;
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");
const Account_1 = require("../utils/queryDefaults/Account");
class AccountResolver extends spool_sequelize_1.SequelizeResolver {
findByIdDefault(id, options = {}) {
options = this.app.services.SequelizeService.mergeOptionDefaults(Account_1.Account.default(this.app), options);
return this.findById(id, options);
}
resolveByInstance(account, options = {}) {
return Promise.resolve(account);
}
resolveById(account, options = {}) {
if (!(account instanceof Object)) {
return Promise.reject(new Error('resolveById requires an object with an id property'));
}
return this.findByIdDefault(account.id, options)
.then(resAccount => {
if (!resAccount && options.reject !== false) {
throw new errors_1.ModelError('E_NOT_FOUND', `Account ${account.id} not found`);
}
return resAccount;
});
}
resolveByGateway(account, options = {}) {
return this.findOne(lodash_1.defaultsDeep({
where: {
gateway: account.gateway,
customer_id: account.customer_id
}
}, options))
.then(resAccount => {
if (!resAccount) {
throw new errors_1.ModelError('E_NOT_FOUND', `Account with customer id ${account.customer_id} not found`);
}
return resAccount;
});
}
resolveByToken(account, options = {}) {
if (!(account instanceof Object)) {
return Promise.reject(new Error('resolveByToken requires an object with a token property'));
}
if (!(account.token)) {
return Promise.reject(new Error('resolveByToken requires an object with a token property'));
}
return this.findOne(lodash_1.defaultsDeep({
where: {
token: account.token
}
}, options))
.then(resAccount => {
if (!resAccount) {
throw new errors_1.ModelError('E_NOT_FOUND', `Account with customer id ${account.customer_id} not found`);
}
return resAccount;
});
}
resolveByNumber(account, options = {}) {
return this.findByIdDefault(account, options)
.then(resAccount => {
if (!resAccount && options.reject !== false) {
throw new errors_1.ModelError('E_NOT_FOUND', `Account ${account.token} not found`);
}
return resAccount;
});
}
resolveByString(account, options = {}) {
return this.findOne(lodash_1.defaultsDeep({
where: {
token: account
}
}, options))
.then(resAccount => {
if (!resAccount) {
throw new errors_1.ModelError('E_NOT_FOUND', `Account with token ${account} not found`);
}
return resAccount;
});
}
resolve(account, options = {}) {
const resolvers = {
'instance': account instanceof this.instance,
'id': !!(account && lodash_1.isObject(account) && account.id),
'token': !!(account && lodash_1.isObject(account) && account.token),
'gateway': !!(account && lodash_1.isObject(account) && account.gateway && account.customer_id),
'create': !!(account && lodash_1.isObject(account) && options.create !== false),
'number': !!(account && lodash_1.isNumber(account)),
'string': !!(account && lodash_1.isString(account))
};
const type = Object.keys(resolvers).find((key) => resolvers[key]);
switch (type) {
case 'instance': {
return this.resolveByInstance(account, options);
}
case 'id': {
return this.resolveById(account, options);
}
case 'token': {
return this.resolveByToken(account, options);
}
case 'gateway': {
return this.resolveByGateway(account, options);
}
case 'create': {
return this.create(account, options);
}
case 'number': {
return this.resolveByNumber(account, options);
}
case 'string': {
return this.resolveByString(account, options);
}
default: {
const err = new Error(`Unable to resolve Account ${account}`);
return Promise.reject(err);
}
}
}
}
exports.AccountResolver = AccountResolver;
class Account extends common_1.FabrixModel {
static get resolver() {
return AccountResolver;
}
static config(app, Sequelize) {
return {
options: {
underscored: true,
scopes: {
live: {
where: {
live_mode: true
}
},
},
hooks: {
beforeCreate: [
(values, options) => {
if (!values.token) {
values.token = `account_${shortId.generate()}`;
}
}
]
}
}
};
}
static schema(app, Sequelize) {
return {
customer_id: {
type: Sequelize.INTEGER,
allowNull: false
},
token: {
type: Sequelize.STRING,
unique: true
},
email: {
type: Sequelize.STRING
},
gateway: {
type: Sequelize.STRING,
defaultValue: 'payment_processor'
},
foreign_key: {
type: Sequelize.STRING,
allowNull: false
},
foreign_id: {
type: Sequelize.STRING,
allowNull: false
},
is_default: {
type: Sequelize.BOOLEAN,
defaultValue: false
},
data: {
type: Sequelize.JSONB,
defaultValue: {}
},
live_mode: {
type: Sequelize.BOOLEAN,
defaultValue: app.config.get('cart.live_mode')
}
};
}
static associate(models) {
models.Account.belongsTo(models.Customer, {
foreignKey: 'customer_id'
});
models.Account.belongsToMany(models.Source, {
as: 'sources',
through: {
model: models.CustomerSource,
unique: false
},
foreignKey: 'account_id'
});
models.Account.hasMany(models.AccountEvent, {
as: 'account_events',
foreignKey: 'account_id'
});
}
}
exports.Account = Account;