239 lines
6.7 KiB
JavaScript
239 lines
6.7 KiB
JavaScript
const { printfulAPIToken, serverBaseURL } = require("../config")
|
|
const { translateText } = require("./deepLRestApi")
|
|
const { statusIsValid } = require("./utils")
|
|
/**
|
|
*
|
|
* @param {number} bigCommerceOrderId
|
|
*/
|
|
function getPrintfulOrder(bigCommerceOrderId) {
|
|
const response = context.http.fetch("https://api.printful.com/v2/orders/@" + bigCommerceOrderId, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${printfulAPIToken}`,
|
|
},
|
|
})
|
|
|
|
if (!statusIsValid(response.status)) {
|
|
throw {
|
|
status: response.status,
|
|
error: response.statusText,
|
|
}
|
|
}
|
|
return response.body.json().data
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {number} bigCommerceOrderId
|
|
*/
|
|
function getPrintfulOrderShipments(bigCommerceOrderId) {
|
|
const response = context.http.fetch(`https://api.printful.com/v2/orders/@${bigCommerceOrderId}/shipments`, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${printfulAPIToken}`,
|
|
},
|
|
})
|
|
|
|
if (!statusIsValid(response.status)) {
|
|
throw {
|
|
status: response.status,
|
|
error: response.statusText,
|
|
}
|
|
}
|
|
return response.body.json().data
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {number} bigCommerceOrderId
|
|
* @param {any} data
|
|
* @returns
|
|
*/
|
|
function patchPrintfulOrder(bigCommerceOrderId, data) {
|
|
const response = context.http.fetch("https://api.printful.com/v2/orders/@" + bigCommerceOrderId, {
|
|
method: "PATCH",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${printfulAPIToken}`,
|
|
},
|
|
body: JSON.stringify(data),
|
|
})
|
|
if (!statusIsValid(response.status)) {
|
|
throw {
|
|
status: response.status,
|
|
error: response.statusText,
|
|
}
|
|
}
|
|
return response.body.json()
|
|
}
|
|
/**
|
|
*
|
|
* @param {string} type
|
|
*/
|
|
function createPrintfulWebhook(type) {
|
|
const response = context.http.fetch("https://api.printful.com/v2/webhooks/" + type, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${printfulAPIToken}`,
|
|
},
|
|
body: JSON.stringify({
|
|
type,
|
|
url: `${serverBaseURL}webhook`,
|
|
}),
|
|
})
|
|
console.log("response:", response.status)
|
|
if (!statusIsValid(response.status)) {
|
|
console.log("wtf?!")
|
|
throw {
|
|
status: response.status,
|
|
error: response.statusText,
|
|
}
|
|
}
|
|
return response.body.json()
|
|
}
|
|
|
|
/**
|
|
* @param {string} type
|
|
|
|
*/
|
|
function deletePrintfulWebhook(type) {
|
|
const response = context.http.fetch("https://api.printful.com/v2/webhooks/" + type, {
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${printfulAPIToken}`,
|
|
},
|
|
})
|
|
if (!statusIsValid(response.status)) {
|
|
throw {
|
|
status: response.status,
|
|
error: response.statusText,
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
/**
|
|
* @param {number} bigCommerceOrderId
|
|
|
|
*/
|
|
|
|
function cancelPrintfulOrder(bigCommerceOrderId) {
|
|
const response = context.http.fetch("https://api.printful.com/orders/@" + bigCommerceOrderId, {
|
|
method: "delete",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${printfulAPIToken}`,
|
|
},
|
|
})
|
|
if (!statusIsValid(response.status)) {
|
|
throw {
|
|
status: response.status,
|
|
error: response.statusText,
|
|
}
|
|
}
|
|
return response.body.json()
|
|
}
|
|
/**
|
|
* @param {string} printfulProductId
|
|
*/
|
|
function getPrintfulProductSizingChart(printfulProductId) {
|
|
const response = context.http.fetch(
|
|
`https://api.printful.com/v2/catalog-products/${printfulProductId}/sizes?unit=cm`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${printfulAPIToken}`,
|
|
},
|
|
}
|
|
)
|
|
if (!statusIsValid(response.status)) {
|
|
throw {
|
|
status: response.status,
|
|
error: response.statusText,
|
|
}
|
|
}
|
|
return response.body.json().data
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} printfulProductId
|
|
* @param {string} productId
|
|
* @returns
|
|
*/
|
|
function extractSizingChart(printfulProductId, productId) {
|
|
if (!printfulProductId) return null
|
|
|
|
const productSizingChart = getPrintfulProductSizingChart(printfulProductId)
|
|
const sizeTable = productSizingChart.size_tables.find((t) => t.type === "measure_yourself")
|
|
|
|
if (!sizeTable) {
|
|
throw {
|
|
status: 404,
|
|
message: "No sizing chart found for this product",
|
|
}
|
|
}
|
|
|
|
const productSizingImageDescriptionTranslation = translateText(sizeTable.image_description)
|
|
const generalProductSizingDescription = translateText(sizeTable.description)
|
|
|
|
return {
|
|
imageURL: sizeTable.image_url,
|
|
imageDescription: productSizingImageDescriptionTranslation,
|
|
availableSizes: productSizingChart.available_sizes,
|
|
generalDescription: generalProductSizingDescription,
|
|
columns: sizeTable.measurements.map((m) => {
|
|
let label = context.db.find("module", {
|
|
filter: {
|
|
label: m.type_label,
|
|
type: "sizeLabel",
|
|
},
|
|
})
|
|
|
|
if (label.length === 0) {
|
|
const germanLabelTranslation = translateText(m.type_label)
|
|
label[0] = context.db.create("module", {
|
|
label: m.type_label,
|
|
type: "sizeLabel",
|
|
germanLabelTranslation: germanLabelTranslation,
|
|
})
|
|
} else {
|
|
label = [...label]
|
|
}
|
|
|
|
return {
|
|
label: label[0].label,
|
|
sizes: m.values.map((v) => {
|
|
if (v.min_value && v.max_value) {
|
|
if (sizeTable.unit === "inches") {
|
|
return `${Math.round(Number(v.min_value) * 2.54)} - ${Math.round(
|
|
Number(v.max_value) * 2.54
|
|
)}`
|
|
}
|
|
return `${v.min_value} - ${v.max_value}`
|
|
}
|
|
if (sizeTable.unit === "inches") {
|
|
return String(Math.round(Number(v.value) * 2.54))
|
|
}
|
|
return v.value
|
|
}),
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
module.exports = {
|
|
getPrintfulOrder,
|
|
patchPrintfulOrder,
|
|
createPrintfulWebhook,
|
|
deletePrintfulWebhook,
|
|
cancelPrintfulOrder,
|
|
getPrintfulProductSizingChart,
|
|
getPrintfulOrderShipments,
|
|
extractSizingChart,
|
|
}
|