omg this is working
This commit is contained in:
90
src/Product.ts
Normal file
90
src/Product.ts
Normal 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
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user