feat: enforce SKU validity, use SKU as handle

This commit enforces proper SKU validation, uses the SKU as the Shopify handle, and implements ID-based product updates to allow renaming. It also extracts the IShop interface for TDD.
This commit is contained in:
Ben Miller
2025-12-25 04:54:55 -07:00
parent 2672d47203
commit 7cb469ccf9
8 changed files with 251 additions and 9 deletions

View File

@ -18,6 +18,7 @@
import { Config } from "./config"
import * as shopify from "shopify-admin-api-typings"
import gql from 'graphql-tag'
import { IShop } from "./interfaces/IShop"
const ss = SpreadsheetApp.getActive()
@ -392,7 +393,7 @@ function parseLinkHeader(header) {
return rels
}
export class Shop {
export class Shop implements IShop {
private shopifyApiKey: string
private shopifyApiSecretKey: string
private shopifyAdminApiAccessToken: string
@ -599,6 +600,34 @@ export class Shop {
return product
}
GetProductById(id: string) {
console.log("GetProductById('" + id + "')")
let gql = /* GraphQL */ `
query productById {
product(id: "${id}") {
id
title
handle
variants(first: 1) {
nodes {
id
sku
}
}
}
}
`
let query = buildGqlQuery(gql, {})
let response = this.shopifyGraphQLAPI(query)
if (!response.content.data.product) {
console.log("GetProductById: no product matched")
return null;
}
let product = response.content.data.product
console.log("Product found:\n" + JSON.stringify(product, null, 2))
return product
}
GetInventoryItemBySku(sku: string) {
console.log('GetInventoryItemBySku("' + sku + '")')
let gql = /* GraphQL */ `
@ -1132,6 +1161,7 @@ export class ShopifyProductSetQuery {
export class ShopifyProductSetInput {
category: string
descriptionHtml: string
handle: string
id?: string
productType: string
redirectNewHandle: boolean = true