omg this is working

This commit is contained in:
Ben Miller
2024-11-13 23:43:32 -07:00
parent fb86c9c96d
commit a5c0a1176d
22 changed files with 2152 additions and 53 deletions

90
src/Product.ts Normal file
View File

@ -0,0 +1,90 @@
// prettier-ignore
import { Shop, ShopifyProduct, ShopifyProductsQuery, ShopifyProductsResponse } 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
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()
let productInventorySheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_inventory")
let row = getRowByColumnValue("product_inventory", "sku", this.sku)
getCellRangeByColumnName(productInventorySheet, "shopify_id", row).setValue(
this.shopify_id
)
}
}