UNPKG

@fabrix/spool-cart

Version:

Spool - eCommerce Spool for Fabrix

188 lines (187 loc) 5.96 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"); class TagResolver extends spool_sequelize_1.SequelizeResolver { resolve(tag, options = {}) { const TagModel = this; if (tag instanceof TagModel.instance) { return Promise.resolve(tag); } else if (tag && lodash_1.isObject(tag) && tag.id) { return TagModel.findById(tag.id, options) .then(foundTag => { if (!foundTag) { throw new errors_1.ModelError('E_NOT_FOUND', `Tag with ${tag.id} not found`); } return foundTag; }); } else if (tag && lodash_1.isObject(tag) && tag.name) { return TagModel.findOne(Object.assign({ where: { name: tag.name } }, options)) .then(resTag => { if (resTag) { return resTag; } return TagModel.create(tag, options); }); } else if (tag && lodash_1.isNumber(tag)) { return TagModel.findById(tag, options) .then(foundTag => { if (!foundTag) { throw new errors_1.ModelError('E_NOT_FOUND', `Tag with ${tag} not found`); } return foundTag; }); } else if (tag && lodash_1.isString(tag)) { return TagModel.findOne(lodash_1.defaultsDeep({ where: { name: tag, } }, options)) .then(resTag => { if (resTag) { return resTag; } return TagModel.create({ name: tag }); }); } else { const err = new Error(`Not able to resolve tag ${tag}`); return Promise.reject(err); } } transformTags(tags = [], options = {}) { const TagModel = this; const Sequelize = TagModel.sequelize; tags = tags.map(tag => { if (tag && lodash_1.isNumber(tag)) { return { id: tag }; } else if (tag && lodash_1.isString(tag)) { tag = { name: this.app.services.ProxyCartService.name(tag) }; return tag; } else if (tag && lodash_1.isObject(tag) && tag.name) { tag.name = this.app.services.ProxyCartService.name(tag.name); return tag; } }); tags = tags.filter(tag => tag); return Sequelize.Promise.mapSeries(tags, tag => { const newTag = tag; return TagModel.findOne({ where: lodash_1.pick(tag, ['id', 'name']), attributes: ['id', 'name'], transaction: options.transaction || null }) .then(_tag => { if (_tag) { return _tag; } else { return TagModel.create(newTag, { transaction: options.transaction || null }); } }); }); } reverseTransformTags(tags) { tags = lodash_1.map(tags, tag => { if (tag && lodash_1.isString(tag)) { return tag; } else if (tag && tag.name) { return tag.name; } }); return tags; } } exports.TagResolver = TagResolver; class Tag extends common_1.FabrixModel { static get resolver() { return TagResolver; } static config(app, Sequelize) { return { options: { underscored: true, scopes: { live: { where: { live_mode: true } } } } }; } static schema(app, Sequelize) { return { name: { type: Sequelize.STRING, unique: true, notNull: true, set: function (val) { this.setDataValue('name', app.services.ProxyCartService.name(val)); } }, live_mode: { type: Sequelize.BOOLEAN, defaultValue: app.config.get('cart.live_mode') } }; } static associate(models) { models.Tag.belongsToMany(models.Product, { as: 'products', through: { model: models.ItemTag, unique: false, scope: { model: 'product' } }, foreignKey: 'tag_id', otherKey: 'model_id', constraints: false }); models.Tag.belongsToMany(models.Customer, { as: 'customers', through: { model: models.ItemTag, unique: false, scope: { model: 'customer' } }, foreignKey: 'tag_id', otherKey: 'model_id', constraints: false }); models.Tag.belongsToMany(models.Collection, { as: 'collections', through: { model: models.ItemTag, unique: false, scope: { model: 'collection' } }, foreignKey: 'tag_id', otherKey: 'model_id', constraints: false }); } } exports.Tag = Tag;