new product defaults are set correctly

This commit is contained in:
Ben Miller
2024-11-17 06:57:03 -07:00
parent 475eee70ea
commit 5d0ae653fa
6 changed files with 253 additions and 75 deletions

View File

@ -517,6 +517,160 @@ export class Shop {
} while (!done)
}
GetProductBySku(sku: string) {
console.log("GetProductBySku('" + sku + "')")
let gql = /* GraphQL */ `
query productBySku {
products(first: 1, query: "sku:${sku}") {
edges {
node {
id
title
handle
variants(first: 1) {
nodes {
id
sku
}
}
options {
id
name
optionValues {
id
name
}
}
}
}
}
}
`
let query = buildGqlQuery(gql, {})
let response = this.shopifyGraphQLAPI(query)
if (response.content.data.products.edges.length <= 0) {
console.log("GetProductBySku: no product matched")
return
}
let product = response.content.data.products.edges[0].node
console.log("Product found:\n" + JSON.stringify(product, null, 2))
return product
}
GetInventoryItemBySku(sku: string) {
console.log('GetInventoryItemBySku("' + sku + '")')
let gql = /* GraphQL */ `
query inventoryItems {
inventoryItems(first:1, query:"sku:${sku}") {
edges {
node {
id
tracked
sku
}
}
}
}
`
let query = buildGqlQuery(gql, {})
let response = this.shopifyGraphQLAPI(query)
let item: shopify.InventoryItem =
response.content.data.inventoryItems.edges[0].node
console.log(
"GetInventoryItemBySku: found item:\n" + JSON.stringify(item, null, 2)
)
return item
}
UpdateInventoryItemQuantity(
item: shopify.InventoryItem,
delta: number = 1,
config: Config
) {
console.log("UpdateInventoryItemQuantity(" + JSON.stringify(item) + ")")
let gql = /* GraphQL */ `
mutation inventoryAdjustQuantities(
$input: InventoryAdjustQuantitiesInput!
) {
inventoryAdjustQuantities(input: $input) {
userErrors {
field
message
}
inventoryAdjustmentGroup {
createdAt
reason
referenceDocumentUri
changes {
name
delta
}
}
}
}
`
let variables = {
input: {
reason: "correction",
name: "available",
changes: [
{
delta: delta,
inventoryItemId: item.id,
locationId: config.shopifyLocationId,
},
],
},
}
let query = buildGqlQuery(gql, variables)
let response = this.shopifyGraphQLAPI(query)
let newItem: shopify.InventoryItem = response.content
console.log("new item:\n" + JSON.stringify(newItem, null, 2))
return newItem
}
SetInventoryItemDefaults(item: shopify.InventoryItem, config: Config) {
let gql = /* GraphQL */ `
mutation inventoryItemUpdate($id: ID!, $input: InventoryItemInput!) {
inventoryItemUpdate(id: $id, input: $input) {
inventoryItem {
id
unitCost {
amount
}
tracked
countryCodeOfOrigin
provinceCodeOfOrigin
harmonizedSystemCode
countryHarmonizedSystemCodes(first: 1) {
edges {
node {
harmonizedSystemCode
countryCode
}
}
}
}
userErrors {
message
}
}
}
`
let variables = {
id: item.id,
input: {
tracked: true,
countryCodeOfOrigin: config.shopifyCountryCodeOfOrigin,
provinceCodeOfOrigin: config.shopifyProvinceCodeOfOrigin,
},
}
let query = buildGqlQuery(gql, variables)
let response = this.shopifyGraphQLAPI(query)
let newItem: shopify.InventoryItem = response.content
return newItem
}
shopifyAPI(endpoint: string, query: {}, next = "") {
var options: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions = {
method: "get",
@ -541,6 +695,7 @@ export class Shop {
}
shopifyGraphQLAPI(query: {}, next = "") {
console.log("shopifyGraphQLAPI:query: " + JSON.stringify(query))
let endpoint = Shop.endpoints.graphql
let options: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions = {
method: "post",
@ -552,10 +707,13 @@ export class Shop {
muteHttpExceptions: true,
}
var url = this.buildURL(endpoint)
console.log(UrlFetchApp.getRequest(url, options))
console.log(
"shopifyGraphQLAPI sending request:\n" +
JSON.stringify(UrlFetchApp.getRequest(url, options), null, 2)
)
var resp = UrlFetchApp.fetch(url, options)
let content = resp.getContentText()
console.log(content)
console.log("shopifyGraphQLAPI got response:\n" + content)
let content_json = JSON.parse(content)
console.log(JSON.stringify(content_json, null, 2))
@ -737,8 +895,8 @@ export class ShopifyVariant {
optionValues: [{}] = [
{
optionName: "Title",
name: "Default Title"
}
name: "Default Title",
},
]
}
@ -756,7 +914,7 @@ export class ShopifyProductsQuery {
query: string = "",
fields: string[] = ["id", "title", "handle"],
cursor: string = "",
pageSize: number = 10,
pageSize: number = 10
) {
let cursorText: string
if (cursor == "") {
@ -838,7 +996,8 @@ export class ShopifyProductSetQuery {
let j = `{
"query": ${formatGqlForJSON(String(this.GQL))},
"variables": {
"productSet": ${JSON.stringify(product)}
"productSet": ${JSON.stringify(product)},
"synchronous": ${synchronous}
}
}`
console.log(j)
@ -862,9 +1021,9 @@ export class ShopifyProductSetInput {
{
name: "Title",
values: {
name: "Default Title"
}
}
name: "Default Title",
},
},
]
}
@ -904,4 +1063,11 @@ export function getShopifyProducts() {
shop.GetProducts()
}
(global as any)
export function buildGqlQuery(gql: string, variables: {}) {
let query = `{
"query": ${formatGqlForJSON(String(gql))},
"variables": ${JSON.stringify(variables)}
}`
console.log("buildGqlQuery:\n" + query)
return JSON.parse(query)
}