@gameroom/cli
Version:
A command line tool for Gameroom
82 lines (80 loc) • 3.37 kB
JavaScript
const { models: { Image, Price, Product, Unit } } = require('@gameroom/kit'),
{ ShopifyV2 } = require('../models'),
{ componentString, extractHTMLText, generateHTML } = require('../helpers'),
{ conversions, memory, shopify, spinner } = require('../refs')
// static methods
// instance methods
Product.prototype.getShopifyProduct = async function () {
const { shopify_id } = this.properties
return shopify_id ? await ShopifyV2.Product.getWithId(shopify_id) : null
}
Product.prototype.createShopifyProduct = async function () {
const body = this.info ? extractHTMLText(this.info) : ''
const body_items = [...body.split('\n') ]
if (this.altname) body_items.push(`Alternative Names ∙ ${this.altname}`)
// get relatives
let [ images, prices, units ] = await Promise.all([
Image.get({ filter: { key: 'imageable_id', value: this.id }, sort: [{ position: 1 }] }),
Price.get({ filter: { and: [
{ key: 'product_id', value: this.id },
{ key: 'amount', comparison: '>', value: 0 },
] }, sort: [{ position: 1 }] }),
Unit.getAll({ filter: { and: [
{ key: 'product_id', value: this.id },
{ key: 'offered', value: true },
{ key: 'quantity', comparison: '>', value: 0 },
] } })
])
// create shopify product
let product = new ShopifyV2.Product({
body_html: generateHTML(body_items),
// handle: this.id,
images: images.reduce((a, i) => [ ...a, new ShopifyV2.Image({ src: i.image }) ], []),
// metafields_global_title_tag: title,
// metafields_global_description_tag: body,
options: [{ name: 'Price Name' }],
product_type: this.subname,
tags: this.tags,
title: componentString(this.name, '-', this.subname),
variants: !prices ? null : prices.reduce((a, i) => [...a, new ShopifyV2.Variant({
barcode: this.identifier,
inventory_management: 'shopify',
grams: this.weight * conversions.grams_per_ounce,
option1: i.name,
price: (i.amount * 0.01).toFixed(2),
sku: componentString(this.identifier, i.name)
}) ], [])
})
// save shopify product
product = await product.save()
// update gameroom prices
prices.map((i) => {
const variant = product.variants.find(b => b.option1 === i.name)
if (!variant) return
i.properties.shopify_id = variant.id
i.properties.shopify_inventory_id = variant.inventory_item_id
})
await prices.save()
// filter out unofferable units
units = units.filter(i => memory.containers.find(b => b.id === i.container_id)?.offered)
// filter out unofferable units
const stores = memory.stores.filter(i => i.offered === true && i.shopify_id)
// generate inventory levels
const inventories = stores.reduce((a, store) => [ ...a,
...prices.reduce((b, price) => {
const available = units.reduce((c, unit) => unit.store_id === store.id && unit.price_id === price.id ? c + unit.quantity : c, 0)
if (available) b.push(new ShopifyV2.Inventory({
available,
inventory_item_id: price.properties.shopify_inventory_id,
location_id: store.shopify_id
}))
return b
}, [])
], [])
for (const inventory of inventories) await inventory.save()
return product
}
Product.prototype.deleteShopifyProduct = async function () {
const { shopify_id } = this.properties
return shopify_id ? await ShopifyV2.Product.delete(shopify_id) : null
}