@gameroom/kit
Version:
Node kit for the Gameroom API
67 lines (60 loc) • 2.34 kB
JavaScript
const { Dictionary } = require('../lib'),
store = require('../store'),
{ Base, Price, Price_Option_Group } = store.models
module.exports = store.define(
'Product',
{
// Product
advertised: { type: Boolean, default: false },
altname: { type: String, default: null },
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 },
info: { type: String, default: null },
name: { type: String, default: null, required: true },
offered: { type: Boolean, default: false },
offered_ebay: { type: Boolean, default: false },
properties: { type: Dictionary, default: {} },
shopified: { type: Boolean, default: false },
subname: { type: String, default: null },
tags: { type: Array, default: [] },
rank: { type: Number, default: 0 },
weight: { type: Number, default: 0 },
width: { type: Number, default: 0 }
},
{
staticMethods: {
async duplicate(productID, attributes = {}) {
const productToDuplicate = await this.find(productID)
const prices = await Price.get({ filter: { key: 'product_id', value: productID } })
const priceOptionGroups = await prices.get(Price_Option_Group)
attributes.name = attributes.name ? attributes.name : productToDuplicate.name + ' (copy)'
const duplicatedProduct = await this.create({
...productToDuplicate,
...attributes,
id: null
})
for (const price of prices) {
const filteredPriceOptionGroups = priceOptionGroups.filter((i) => price.id === i.price_id)
const duplicatedPrice = await Price.create({
product_id: duplicatedProduct.id,
name: price.name,
amount: price.amount,
position: price.position
})
for (const filteredPriceOptionGroup of filteredPriceOptionGroups) {
await Price_Option_Group.create({
price_id: duplicatedPrice.id,
option_group_id: filteredPriceOptionGroup.option_group_id,
position: filteredPriceOptionGroup.position
})
}
}
return duplicatedProduct
}
},
extends: Base
}
)