UNPKG

@gameroom/cli

Version:

A command line tool for Gameroom

107 lines (104 loc) 5.31 kB
const cosmetic = require('cosmetic'), { models: { Price } } = require('@gameroom/kit'), Image = require('./Image'), Variant = require('./Variant'), { appendHTMLBody, componentString, extractHTMLBody, extractHTMLText, generateHTML, getRequest, grGreen, timeout, tryRepeatedly } = require('../../helpers'), { conversions, shopify, spinner } = require('../../refs') module.exports = class Product { constructor(data) { if (!data) data = {} if (data.body_html) this.body_html = data.body_html if (data.created_at) this.created_at = data.created_at // read only if (data.handle) this.handle = data.handle if (data.id) this.id = data.id // read only if (data.images) this.images = data.images if (data.metafields_global_title_tag) this.metafields_global_title_tag = data.metafields_global_title_tag if (data.metafields_global_description_tag) this.metafields_global_description_tag = data.metafields_global_description_tag if (data.options) this.options = data.options if (data.product_type) this.product_type = data.product_type if (data.published_at) this.published_at = data.published_at if (data.published_scope) this.published_scope = data.published_scope if (data.tags) this.tags = data.tags if (data.template_suffix) this.template_suffix = data.template_suffix if (data.title) this.title = data.title // required if (data.updated_at) this.updated_at = data.updated_at // read only if (data.variants) this.variants = data.variants if (data.vendor) this.vendor = data.vendor } clean() { return Object.keys(this).reduce((a, i) => this[i] !== undefined ? { ...a, [i]: this[i] } : a, {}) } static async fromProductAndPrices(product, prices) { const title = componentString(product.name, '-', product.subname) const body = product.info ? extractHTMLText(product.info) : '' const body_items = [...body.split('\n') ] if (product.altname) body_items.push(`Alternative Names ∙ ${product.altname}`) const tags = product.tags const images = await Image.fromProduct(product) const variants = prices ? prices.reduce((a, i) => [...a, new Variant({ barcode: product.identifier, inventory_management: 'shopify', grams: product.weight * conversions.grams_per_ounce, options: [ i.name ], price: (i.amount * 0.01).toFixed(2), sku: componentString(product.identifier, i.name) }) ], []) : null return new Product({ body_html: generateHTML(body_items), // handle: product.id, images, metafields_global_title_tag: title, metafields_global_description_tag: body, options: [ 'Price Name' ], product_type: product.subname, tags, title: title, variants }) } static async getAll() { const limit = 250 let spinner_text = spinner ? spinner.text : null const products = [] let done while (!done) { if (spinner) spinner.text = `${spinner_text} (${products.length})` const result = await tryRepeatedly(() => shopify.get(`/products.json`, { params: { limit, since_id: products.length > 0 ? products[products.length - 1].id : 0 }, }), (err) => spinner.warn(`error getting ${cosmetic.green('shopify products')} ${err}`)) const new_products = result && result.data && result.data.products ? result.data.products : [] products.push(...new_products) done = new_products < limit } if (spinner) spinner.text = `${spinner_text} (${products.length})` return products.reduce((a, i) => [...a, new Product(i)], []) } static async getWithHandle(handle) { const result = await tryRepeatedly(() => shopify.get('/products.json', { params: { handle } }), (err) => spinner.warn(`error getting ${cosmetic.green('shopify product')} ${err}`)) return result && result.data && result.data.products && result.data.products[0] ? new Product(result.data.products[0]) : null } static async getWithId(id) { const result = await tryRepeatedly(() => shopify.get(`/products/${id}.json`), (err) => spinner.warn(`error getting ${cosmetic.green('shopify product')} with id ${id} ${err}`)) return result?.data?.product ? new Product(result?.data?.product) : null } async save() { const result = await tryRepeatedly(() => shopify.post('/products.json', { product: this.clean() }, { }), (err) => spinner.warn(`error saving ${cosmetic.green('shopify product')} ${err}`)) return result?.data?.product ? new Product(result?.data?.product) : null } async updateFromProduct(product) { const shopify_product = await Product.fromProduct(product) // delete product.images // product.variants[0].id = this.variants[0].id const result = await tryRepeatedly(() => shopify.put(`/products/${this.id}.json`, { product: shopify_product }, { }), (err) => spinner.warn(`error updating ${cosmetic.green('shopify product')} from ${grGreen(product.id)}${err}`)) return result && result.data && result.data.product ? new Product(result.data.product) : null } static async delete(id) { const result = await tryRepeatedly(() => shopify.delete(`/products/${id}.json`, { }), (err) => spinner.warn(`error deleting ${cosmetic.green('shopify product')} ${id} ${err}`)) return result } }