@gameroom/cli
Version:
A command line tool for Gameroom
61 lines (59 loc) • 2.62 kB
JavaScript
const cosmetic = require('cosmetic'),
{ models: { Image, Price, Product, Unit } } = require('@gameroom/kit'),
{ grGreen } = require('../../../helpers'),
{ ShopifyV2 } = require('../../../models'),
{ config, spinner } = require('../../../refs'),
LIMIT = 500
module.exports = async (product, options) => {
// setup options
const { verbose } = options
// snapshot product properties
const { properties: { shopify_id }, updated_at } = product
// get shopify product
// let shopify_product = shopify_id ? await ShopifyV2.Product.getWithId(shopify_id) : null
let shopify_product = await product.getShopifyProduct()
// conditions
const shopifyable = product.offered
const add = !shopify_product && shopifyable
const remove = shopify_product && !shopifyable
const update = shopify_product && shopifyable
if (add) {
// create shopify product
// create variants and inventory levels too on add
// const [ images, prices, units ] = await Promise.all([
// Image.get({ filter: { key: 'imageable_id', value: product.id }, sort: [{ position: 1 }] }),
// Price.get({ filter: { key: 'product_id', value: product.id }, sort: [{ position: 1 }] }),
// Unit.getAll({ filter: { and: [
// { key: 'product_id', value: product.id },
// { key: 'quantity', comparison: '>', value: 0 },
// ] }, sort: [{ position: 1 }] })
// ])
shopify_product = await product.createShopifyProduct()
// shopify_product = await shopify_product.save()
} else if (remove) {
// delete shopify product
await product.deleteShopifyProduct()
shopify_product = null
} else if (update) {
// update shopify product
// shopify_product = await product.updateShopifyProduct()
// shopify_product = await shopify_product.updateFromProduct(product)
} else {
// not offered and not shopified, no action necessary
return
}
// update shopify id
if (shopify_product) {
product.properties.shopify_id = shopify_product.id
} else {
delete product.properties.shopify_id
}
// update product if need be
if (product.properties.shopify_id != shopify_id) {
product = await Product.update({ id: product.id, properties: product.properties })
// if (verbose) spinner.info(`updated gameroom ${grGreen('product')}`)
// updated products go to the end of the pagination line and mess up the offset...
}
// if (verbose) spinner.succeed(`synced ${grGreen('product')}`)
if (verbose) spinner.succeed(`${add ? cosmetic.green('added') : remove ? cosmetic.red('removed') : 'updated' } ${grGreen('product')} ${product.id}`)
}