@gameroom/cli
Version:
A command line tool for Gameroom
111 lines (104 loc) • 4.11 kB
JavaScript
const cosmetic = require('cosmetic'),
{ join, resolve } = require('path'),
{ difference, dollarString, getBaseline, grGreen, timeout, writeCSVFile } = require('../../helpers'),
{ Price_Charting } = require('../../models'),
{ spinner } = require('../../refs'),
{ models: { Price, Product } } = require('@gameroom/kit'),
PRICE_CHARTING = cosmetic.cyan('price charting'),
TRUE = 'true',
FALSE = 'false'
module.exports = async ({ products, write }) => {
if (!process.env.PRICE_CHARTING_TOKEN) throw new Error('missing price charting env keys')
spinner.info(`updating ${grGreen('gameroom')} from ${PRICE_CHARTING} @ ${new Date().toLocaleString()}`)
const csv_data = []
let matched = 0,
unmatched = 0,
missing_id = 0,
products_updated = 0
for (const [i, product] of products.entries()) {
// instantiate csv data
const data = new Price_Charting.Update({
// product
product_name: product.name,
product_subname: product.subname,
product_id: product.id
})
if ((!product.identifier || product.identifier.includes(' ')) && !product.properties.pcid) {
missing_id++
csv_data.push(data.update({
notes: 'missing or invalid identifier'
}))
spinner.warn(`missing identifier ${grGreen('product')} ${product.id}`)
continue
}
spinner.text = `checking ${grGreen('product')} ${i}/${products.length}`
let pc_product
try {
pc_product = product.properties.pcid ?
await Price_Charting.Product.getWithId(product.properties.pcid)
:
await Price_Charting.Product.getWithUPC(product.identifier)
if (!pc_product) throw new Error(`missing data from result: ${JSON.stringify(result)}`)
} catch(err) {
spinner.warn(`error getting ${PRICE_CHARTING} product ${product.id}: ${err}`)
unmatched++
csv_data.push(data.update({
notes: `error getting ${PRICE_CHARTING} product ${product.id}: ${err}`
}))
continue
}
// info(`got ${grGreen('product')} ${product.id} ${product.name}`)
matched++
// update product properties
// const { properties } = product
const properties = Object.assign({}, product.properties)
// info
if (!properties.asin) properties.asin = pc_product.asin
if (!properties.epid) properties.epid = pc_product.epid
properties.pcid = pc_product.id
properties.release_date = pc_product['release-date']
properties.sales_volume = pc_product['sales-volume']
// update csv data
data.update({
auto_priced: properties.auto_price,
pcid: properties.pcid,
// prices
gamestop_price: pc_product['gamestop-price'],
gamestop_trade_price: properties['gamestop-trade-price']
})
// set auto_price to false if it doesnt exist
if (!Object.keys(properties).includes('auto_price')) properties.auto_price = FALSE
// get price_baseline
const baseline = getBaseline(product, pc_product)
properties.price_baseline = baseline
// look for difference in properties
let diff = false
Object.keys(properties).map(k => { if (product.properties[k] != properties[k]) diff = true })
// update product only if update option and difference
if (diff) {
let updated = false
while (!updated) {
try {
await Product.update({ id: product.id, properties })
updated = true
products_updated++
spinner.succeed(`updated ${grGreen('product')} ${product.id}`)
} catch(err) {
spinner.warn(`error updating product ${product.id}: ${err}`)
await timeout()
}
}
}
csv_data.push(data)
}
if (write) {
const path = await writeCSVFile(resolve(write), csv_data)
spinner.succeed(`wrote data to ${path}`)
}
spinner.info(`total: ${products.length}`)
spinner.warn(`missing id: ${missing_id}`)
spinner.warn(`unmatched: ${unmatched}`)
spinner.succeed(`matched: ${matched}`)
spinner.succeed(`products updated: ${products_updated}`)
spinner.succeed(`completed ${PRICE_CHARTING} update @ ${new Date().toLocaleString()}`).stop()
}