Files
product_inventory/src/Product.ts
2024-11-14 08:36:08 -07:00

156 lines
4.9 KiB
TypeScript

// prettier-ignore
import {
Shop,
ShopifyProduct,
ShopifyProductsQuery,
ShopifyProductsResponse,
ShopifyProductSetInput,
ShopifyVariant,
ShopifyProductSetQuery,
VariantOptionValueInput,
} from "./shopifyApi"
import { getCellRangeByColumnName, getRowByColumnValue } from "./sheetUtils"
export class Product {
shopify_id: string = ""
title: string = ""
style: string[] = []
tags: string = ""
category: string = ""
product_type: string = ""
description: string = ""
sku: string = ""
price: number = 0
shipping: number = 0
function: string = ""
type: string = ""
weight_grams: number = 0
photos: string = ""
shopify_product: ShopifyProduct
shopify_default_variant_id: string
shopify_default_option_id: string
shopify_default_option_value_id: string
constructor(sku: string = "") {
if (sku == "") {
return
}
this.sku = sku
let productRow = getRowByColumnValue("product_inventory", "sku", sku)
if (productRow == undefined) {
throw new Error(
"product sku '" + sku + "' not found in product_inventory"
)
}
this.ImportFromInventory(productRow)
}
ImportFromInventory(row: number) {
let productInventorySheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_inventory")
let headers = productInventorySheet
.getRange(1, 1, 1, productInventorySheet.getLastColumn())
.getValues()[0]
console.log("headers" + headers)
let productValues = productInventorySheet
.getRange(row, 1, 1, headers.length)
.getValues()[0]
console.log("productValues:" + productValues)
for (let i = 0; i < headers.length; i++) {
if (this.hasOwnProperty(headers[i])) {
console.log(
"setting value for '" + headers[i] + "' to '" + productValues[i] + "'"
)
this[headers[i]] = productValues[i]
} else {
console.log("skipping '" + headers[i] + "'")
}
}
}
MatchToShopifyProduct(shop: Shop) {
/* if (this.shopify_id.startsWith("gid://shopify/Product/")) {
return
} */
let query = new ShopifyProductsQuery("sku:" + this.sku, ["id", "title"])
console.log(query.JSON)
let response = shop.shopifyGraphQLAPI(query.JSON)
console.log(response)
let productsResponse = new ShopifyProductsResponse(response.content)
if (productsResponse.products.edges.length <= 0) {
console.log("no products matched")
return
}
if (productsResponse.products.edges.length > 1) {
console.log("more than one product matched")
return
}
this.shopify_product = productsResponse.products.edges[0].node
this.shopify_id = this.shopify_product.id.toString()
this.shopify_default_variant_id =
productsResponse.products.edges[0].node.variants.nodes[0].id
console.log(JSON.stringify(productsResponse, null, 2))
console.log(JSON.stringify(productsResponse.products, null, 2))
console.log(JSON.stringify(productsResponse.products.edges[0], null, 2))
console.log(
JSON.stringify(productsResponse.products.edges[0].node, null, 2)
)
console.log(
JSON.stringify(
productsResponse.products.edges[0].node.options[0],
null,
2
)
)
console.log(
JSON.stringify(
productsResponse.products.edges[0].node.options[0].id,
null,
2
)
)
this.shopify_default_option_id =
productsResponse.products.edges[0].node.options[0].id
this.shopify_default_option_value_id =
productsResponse.products.edges[0].node.options[0].optionValues[0].id
let productInventorySheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_inventory")
let row = getRowByColumnValue("product_inventory", "sku", this.sku)
getCellRangeByColumnName(productInventorySheet, "shopify_id", row).setValue(
this.shopify_id
)
}
ToShopifyProductSet() {
let sps = new ShopifyProductSetInput()
//TODO: map category IDs
//sps.category = this.category
sps.id = this.shopify_id
sps.productType = this.product_type
sps.tags = this.tags
sps.title = this.title
sps.descriptionHtml = this.description
sps.variants = []
let variant = new ShopifyVariant()
//TODO: handle multiple variants
variant.id = this.shopify_default_variant_id
variant.sku = this.sku
variant.price = this.price
sps.variants.push(variant)
let opt = new ShopifyVa
let options = new VariantOptionValueInput()
options.id = this.shopify_default_option_value_id
return sps
}
UpdateShopifyProduct(shop: Shop) {
let sps = this.ToShopifyProductSet()
console.log("sps: " + JSON.stringify(sps))
let query = new ShopifyProductSetQuery(sps)
console.log(JSON.stringify(query))
let response = shop.shopifyGraphQLAPI(query.JSON)
console.log(JSON.stringify(response, null, 2))
}
}