UNPKG

@gameroom/kit

Version:

Node kit for the Gameroom API

97 lines (95 loc) 4.82 kB
const { Dictionary } = require('../lib'), store = require('../store'), { Base } = store.models module.exports = store.define( 'Unit', { // Unit advertised: { type: Boolean, default: false }, altname: { type: String, default: null }, amount: { type: Number, default: 0 }, calculated: { type: Boolean, default: false }, depth: { type: Number, default: 0 }, ebayed: { type: Boolean, default: false }, group_id: { type: String, default: null }, height: { type: Number, default: 0 }, identifier: { type: String, default: null }, index: { type: Number, default: 0 }, info: { type: String, default: null }, locked: { type: Boolean, default: false }, name: { type: String, default: null }, offered: { type: Boolean, default: false }, offered_ebay: { type: Boolean, default: false }, options_string: { type: String, default: null }, properties: { type: Dictionary, default: {} }, quantity: { type: Number, default: 0 }, serial_number: { type: String, default: null }, shopified: { type: Boolean, default: false }, subname: { type: String, default: null }, tags: { type: Array, default: [] }, weight: { type: Number, default: 0 }, width: { type: Number, default: 0 }, // Relationships container_id: { type: String, default: null }, price_id: { type: String, default: null }, product_id: { type: String, default: null }, store_id: { type: String, default: null } }, { extends: Base, methods: { async reprice() { try { const { Option, Price, Unit_Option } = store.models if (!this.price_id || !this.calculated) return false const price = await Price.find(this.price_id) const unit_options = await Unit_Option.get({ filter: { key: 'unit_id', value: this.id } }) const or = unit_options.reduce((a, i) => (i.option_id ? [...a, { key: 'id', value: i.option_id }] : a), []) const options = or.length ? await Option.get({ filter: { or } }) : [] const amount = options.reduce((a, i) => { const percent = parseFloat(i.percent) if (percent) a += price.amount * percent return Math.round(a + i.amount) }, price.amount) if (this.amount === amount) return null this.amount = amount return amount } catch (err) { throw new Error(`Error repricing unit ${err}`) } } // getOptions: async function () { // const { Option, Option_Group, Price, Price_Option_Group, Unit_Option } = store.models // const price = await Price.find(this.price_id) // const unit_options = await Unit_Option.get({ filter: { key: 'unit_id', value: this.id }}) // const options = await Option.get({ filter: { or: unit_options.reduce((a, i) => [...a, { key: 'id', value: i.option_id }], []) }}) // const option_groups = await Option_Group.get({ filter: { or: price_option_groups.reduce((a, i) => [...a, { key: 'id', value: i.option_group_id }], []) }}) // // const price_option_groups = await Price_Option_Group.get({ filter: { key: 'price_id', value: this.price_id }}) // return { option_groups, options, price, price_option_groups, unit_options } // // }, // updateOptionsString: async function (data) { // const { Option, Option_Group, Price, Price_Option_Group, Unit_Option } = store.models // if (!data) data = await this.getOptions() // let { option_groups, options, price, price_option_groups, unit_options } = data // price = price || this.price ? await Price.find(this.price_id) : { name: this.price } // price_option_groups = price_option_groups || await Price_Option_Group.get({ filter: { key: 'price_id', value: this.price_id }}) // option_groups = option_groups || await Option_Group.get({ filter: { or: price_option_groups.reduce((a, i) => [...a, { key: 'id', value: i.option_group_id }], []) }}) // unit_options = unit_options || await Unit_Option.get({ filter: { key: 'unit_id', value: this.id }}) // options = options || await Option.get({ filter: { or: unit_options.reduce((a, i) => [...a, { key: 'id', value: i.option_id }], []) }}) // // console.log(options) // // let string = `${price.name}\n\n` // for (const option_group of option_groups) { // string += `${option_group.name}\n` // console.log(options.filter(i => i.option_group_id === option_group.id).length) // for (const option of options.filter(i => i.option_group_id === option_group.id)) string += `∙ ${option.name}\n` // string += '\n' // } // this.options_string = string // return string // } } } )