@gameroom/cli
Version:
A command line tool for Gameroom
86 lines (84 loc) • 3.67 kB
JavaScript
const axios = require('axios'),
cosmetic = require('cosmetic'),
{ componentString, tryRepeatedly } = require('../../helpers'),
{ conversions, shopify, spinner } = require('../../refs')
module.exports = class Variant {
constructor(data) {
if (!data) data = {}
this.barcode = data.barcode
this.compare_at_price = data.compare_at_price
this.created_at = data.created_at
this.fulfillment_service = data.fulfillment_service
this.grams = data.grams
this.id = data.id
this.image_id = data.image_id
this.inventory_item_id = data.inventory_item_id
this.inventory_management = data.inventory_management
this.inventory_policy = data.inventory_policy
// inventory_quality read only
this.option1 = data.option1
this.option2 = data.option2
this.option3 = data.option3
this.presentment_prices = data.presentment_prices
// position read only
this.price = data.price
this.product_id = data.product_id
this.sku = data.sku
this.taxable = data.taxable
this.tax_code = data.tax_code
this.title = data.title
this.updated_at = data.updated_at
this.weight = data.weight
this.weight_unit = data.weight_unit
}
static async fromProductAndPrice(product, price) {
return new Variant({
barcode: product.identifier,
inventory_management: 'shopify',
grams: product.weight * conversions.grams_per_ounce,
option1: price.name,
price: (price.amount * 0.01).toFixed(2),
product_id: product.properties.shopify_id
})
}
clean() {
return Object.keys(this).reduce((a, i) => this[i] === undefined ? a : { ...a, [i]: this[i] }, {})
}
static async getWithId(id) {
const result = await tryRepeatedly(() => shopify.get(`/variants/${id}.json`), (err) => spinner.warn(`error getting ${cosmetic.green('shopify variant')} with id ${id} ${err}`))
return result?.data?.variant ? new Variant(result?.data?.variant) : null
}
async save() {
return this.id ? this.update() : this.create()
}
async create() {
const result = await tryRepeatedly(() => shopify.post(`/products/${this.product_id}/variants.json`, { variant: this.clean() }), (err) => spinner.warn(`error saving ${cosmetic.green('shopify variant')} ${err}`))
return result && result.data && result.data.variant ? new Variant(result.data.variant) : null
}
async update() {
const result = await tryRepeatedly(() => shopify.put(`/variants/${this.id}.json`, { variant: this.clean() }), (err) => spinner.warn(`error saving ${cosmetic.green('shopify variant')} ${err}`))
return result && result.data && result.data.variant ? new Variant(result.data.variant) : null
}
async updateFromPrice(price) {
const snap = Object.assign({}, this.clean())
this.option1 = price.name
this.price = (price.amount * 0.01).toFixed(2)
const updated = JSON.stringify(snap) !== JSON.stringify(this.clean())
return updated ? this.update() : this
}
async updateFromProductAndPrice(product, price) {
const snap = Object.assign({}, this)
this.barcode = product.identifier
this.inventory_management = 'shopify'
this.grams = product.weight * conversions.grams_per_ounce
this.option1 = price.name
this.price = (price.amount * 0.01).toFixed(2)
this.product_id = product.properties.shopify_id
const updated = JSON.stringify(snap) !== JSON.stringify(this)
return updated ? this.update() : this
}
static async delete(id) {
const result = await tryRepeatedly(() => shopify.delete(`/variants/${id}.json`), (err) => spinner.warn(`error deleting ${cosmetic.green('shopify variant')} ${id} ${err}`))
return result
}
}