cleanup
This commit is contained in:
@@ -121,7 +121,7 @@ export namespace Printful {
|
||||
}[]
|
||||
}
|
||||
|
||||
export async function getSyncProduct(syncProductId: number): Promise<SyncProduct> {
|
||||
export async function get(syncProductId: number): Promise<SyncProduct> {
|
||||
const payload: MetaDataSingle<SyncProduct> = await (
|
||||
await fetch(`${Printful.API_URL}/store/products/${syncProductId}`, {
|
||||
method: "GET",
|
||||
@@ -135,7 +135,7 @@ export namespace Printful {
|
||||
return payload.result;
|
||||
}
|
||||
|
||||
export async function getSyncProducts(): Promise<SyncProduct[]> {
|
||||
export async function getAll(): Promise<SyncProduct[]> {
|
||||
const payload: MetaDataMulti<SyncProduct["sync_product"]> = await (
|
||||
await fetch(`${Printful.API_URL}/store/products`, {
|
||||
method: "GET",
|
||||
@@ -148,9 +148,15 @@ export namespace Printful {
|
||||
|
||||
// need to fetch variants
|
||||
const syncProducts = await Promise.all(
|
||||
payload.result.map(p => getSyncProduct(p.id))
|
||||
payload.result.map(p => get(p.id))
|
||||
);
|
||||
return syncProducts;
|
||||
}
|
||||
|
||||
|
||||
export function getVariantPreviewUrl(syncVariant: Printful.Products.SyncProduct["sync_variants"][number]): string {
|
||||
const previewFile = syncVariant.files.find(f => f.type === "preview");
|
||||
return previewFile?.preview_url ?? syncVariant.product.image;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
interface ProductRecord {
|
||||
webflowProductId: number;
|
||||
printfulProductId: number;
|
||||
}
|
||||
|
||||
class ProductRecords {
|
||||
private db: Database;
|
||||
|
||||
constructor() {
|
||||
const dbPath = Bun.env.NODE_ENV === "production" ?
|
||||
"/data/data.db" :
|
||||
"./data/data.db";
|
||||
|
||||
this.db = new Database(dbPath);
|
||||
}
|
||||
|
||||
findUsingWebflow(webflowProductId: number): ProductRecord | undefined {
|
||||
const row = this.db.query(`
|
||||
SELECT * FROM product_records WHERE webflowProductId = ?
|
||||
`).get(webflowProductId);
|
||||
|
||||
return row as ProductRecord | undefined;
|
||||
}
|
||||
|
||||
findUsingPrintful(printfulProductId: number): ProductRecord | undefined {
|
||||
const row = this.db.query(`
|
||||
SELECT * FROM product_records WHERE printfulProductId = ?
|
||||
`).get(printfulProductId);
|
||||
|
||||
return row as ProductRecord | undefined;
|
||||
}
|
||||
|
||||
deleteUsingWebflow(webflowProductId: number) {
|
||||
this.db.run(`
|
||||
DELETE FROM product_records WHERE webflowProductId = ?
|
||||
`, [webflowProductId]);
|
||||
}
|
||||
|
||||
deleteUsingPrintful(printfulProductId: number) {
|
||||
this.db.run(`
|
||||
DELETE FROM product_records WHERE printfulProductId = ?
|
||||
`, [printfulProductId]);
|
||||
}
|
||||
|
||||
add(record: ProductRecord): void {
|
||||
this.db.run(`
|
||||
INSERT OR IGNORE INTO product_records (webflowProductId, printfulProductId)
|
||||
VALUES (?, ?)
|
||||
`, [record.webflowProductId, record.printfulProductId]);
|
||||
}
|
||||
|
||||
all(): ProductRecord[] {
|
||||
return this.db.query(`SELECT * FROM product_records`).all() as ProductRecord[];
|
||||
}
|
||||
}
|
||||
|
||||
export const productRecords = new ProductRecords();
|
||||
+193
-190
@@ -1,4 +1,3 @@
|
||||
import { productRecords } from "./product-records";
|
||||
import { Printful } from "./printful"
|
||||
|
||||
export namespace Webflow {
|
||||
@@ -15,7 +14,7 @@ export namespace Webflow {
|
||||
.replace(/^([^a-z0-9_])/, '_$1'); // if it starts with an invalid char, prefix underscore
|
||||
};
|
||||
|
||||
namespace Products {
|
||||
export namespace Products {
|
||||
export interface Sku {
|
||||
fieldData: {
|
||||
name: string;
|
||||
@@ -30,12 +29,14 @@ export namespace Webflow {
|
||||
}
|
||||
}
|
||||
|
||||
export interface CreateProduct {
|
||||
export interface UpsertProduct {
|
||||
product: {
|
||||
fieldData: {
|
||||
name: string;
|
||||
slug: string;
|
||||
description: string;
|
||||
shippable?: boolean;
|
||||
"tax-category"?: string;
|
||||
"sku-properties": {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -48,13 +49,10 @@ export namespace Webflow {
|
||||
}
|
||||
};
|
||||
sku: Sku;
|
||||
publishStatus: string;
|
||||
publishStatus?: "staging" | "live";
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteProductUsingPrintful(printfulProductId: number) {
|
||||
const webflowProductId = productRecords.findUsingPrintful(printfulProductId)?.webflowProductId;
|
||||
if (webflowProductId) {
|
||||
export async function remove(webflowProductId: string) {
|
||||
const res = await fetch(`${Webflow.API_COLLECTIONS_URL}/items/${webflowProductId}`, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
@@ -63,213 +61,218 @@ export namespace Webflow {
|
||||
},
|
||||
body: JSON.stringify({})
|
||||
});
|
||||
productRecords.deleteUsingWebflow(webflowProductId);
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateProductUsingPrintful(printfulProduct: Printful.Products.SyncProduct) {
|
||||
const webflowProductId = productRecords.findUsingPrintful(printfulProduct.sync_product.id)?.webflowProductId;
|
||||
if (!webflowProductId) {
|
||||
throw new Error("Product is not recognized");
|
||||
}
|
||||
|
||||
const printfulVariants = printfulProduct.sync_variants;
|
||||
const webflowVariants: Products.Sku[] = [];
|
||||
for (const printfulVariant of printfulVariants) {
|
||||
webflowVariants.push({
|
||||
"fieldData": {
|
||||
"name": printfulVariant.name,
|
||||
"slug": formatSlug(printfulVariant.name),
|
||||
"sku-values": {
|
||||
"color": printfulVariant.color,
|
||||
"size": printfulVariant.size,
|
||||
},
|
||||
"price": {
|
||||
"value": +printfulVariant.retail_price * 100,
|
||||
"unit": printfulVariant.currency,
|
||||
"currency": printfulVariant.currency
|
||||
},
|
||||
// TODO: sync image
|
||||
"main-image": "https://www.example.com/image.jpg"
|
||||
export async function exists(webflowProductId: string): Promise<boolean> {
|
||||
let findProductResponse = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`,
|
||||
}
|
||||
});
|
||||
return findProductResponse.status === 200;
|
||||
}
|
||||
|
||||
const foundColors = Array.from(new Set(printfulVariants.map(v => v.color)));
|
||||
const foundSizes = Array.from(new Set(printfulVariants.map(v => v.size)));
|
||||
export async function updateUsingPrintful(printfulProduct: Printful.Products.SyncProduct) {
|
||||
const webflowProductId = printfulProduct.sync_product.external_id;
|
||||
|
||||
let updateProductResponse = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"product": {
|
||||
const printfulVariants = printfulProduct.sync_variants;
|
||||
const webflowVariants: Products.Sku[] = [];
|
||||
for (const printfulVariant of printfulVariants) {
|
||||
webflowVariants.push({
|
||||
"fieldData": {
|
||||
"name": printfulProduct.sync_product.name,
|
||||
"slug": formatSlug(printfulProduct.sync_product.name),
|
||||
"sku-properties": [
|
||||
{
|
||||
"id": "color",
|
||||
"name": "Color",
|
||||
"enum": foundColors.map(color => ({
|
||||
"id": color,
|
||||
"slug": formatSlug(color),
|
||||
"name": color
|
||||
}))
|
||||
},
|
||||
{
|
||||
"id": "size",
|
||||
"name": "Size",
|
||||
"enum": foundSizes.map(size => ({
|
||||
"id": size,
|
||||
"slug": formatSlug(size),
|
||||
"name": size
|
||||
}))
|
||||
},
|
||||
]
|
||||
"name": printfulVariant.name,
|
||||
"slug": formatSlug(printfulVariant.name),
|
||||
"sku-values": {
|
||||
"color": printfulVariant.color,
|
||||
"size": printfulVariant.size,
|
||||
},
|
||||
"price": {
|
||||
"value": +printfulVariant.retail_price * 100,
|
||||
"unit": printfulVariant.currency,
|
||||
"currency": printfulVariant.currency
|
||||
},
|
||||
"main-image": Printful.Products.getVariantPreviewUrl(printfulVariant)
|
||||
}
|
||||
},
|
||||
"sku": webflowVariants[0]
|
||||
})
|
||||
});
|
||||
updateProductResponse = await updateProductResponse.json();
|
||||
|
||||
const getProductResponse = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`
|
||||
});
|
||||
}
|
||||
});
|
||||
const webflowProduct = await getProductResponse.json();
|
||||
|
||||
for (let i = 0; i < webflowProduct["skus"].length; ++i) {
|
||||
const webflowSkuId = printfulVariants[i].external_id;
|
||||
const foundColors = Array.from(new Set(printfulVariants.map(v => v.color)));
|
||||
const foundSizes = Array.from(new Set(printfulVariants.map(v => v.size)));
|
||||
|
||||
const res = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}/skus/${webflowSkuId}`, {
|
||||
let updateProductResponse = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"product": {
|
||||
"fieldData": {
|
||||
"name": printfulProduct.sync_product.name,
|
||||
"slug": formatSlug(printfulProduct.sync_product.name),
|
||||
"shippable": true,
|
||||
"tax-category": "standard-taxable",
|
||||
"sku-properties": [
|
||||
{
|
||||
"id": "color",
|
||||
"name": "Color",
|
||||
"enum": foundColors.map(color => ({
|
||||
"id": color,
|
||||
"slug": formatSlug(color),
|
||||
"name": color
|
||||
}))
|
||||
},
|
||||
{
|
||||
"id": "size",
|
||||
"name": "Size",
|
||||
"enum": foundSizes.map(size => ({
|
||||
"id": size,
|
||||
"slug": formatSlug(size),
|
||||
"name": size
|
||||
}))
|
||||
},
|
||||
]
|
||||
}
|
||||
},
|
||||
"sku": webflowVariants[0],
|
||||
} as Products.UpsertProduct)
|
||||
});
|
||||
updateProductResponse = await updateProductResponse.json();
|
||||
|
||||
const getProductResponse = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`
|
||||
}
|
||||
});
|
||||
const webflowProduct = await getProductResponse.json();
|
||||
|
||||
for (let i = 0; i < webflowProduct["skus"].length; ++i) {
|
||||
const webflowSkuId = printfulVariants[i].external_id;
|
||||
|
||||
const res = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}/skus/${webflowSkuId}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"sku": webflowVariants[i]
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function createUsingPrintful(printfulProduct: Printful.Products.SyncProduct) {
|
||||
const printfulVariants = printfulProduct.sync_variants;
|
||||
|
||||
const webflowVariants: Products.Sku[] = [];
|
||||
for (const printfulVariant of printfulVariants) {
|
||||
webflowVariants.push({
|
||||
"fieldData": {
|
||||
"name": printfulVariant.name,
|
||||
"slug": formatSlug(printfulVariant.name),
|
||||
"sku-values": {
|
||||
"color": printfulVariant.color,
|
||||
"size": printfulVariant.size,
|
||||
},
|
||||
"price": {
|
||||
"value": +printfulVariant.retail_price * 100,
|
||||
"unit": printfulVariant.currency,
|
||||
"currency": printfulVariant.currency
|
||||
},
|
||||
"main-image": Printful.Products.getVariantPreviewUrl(printfulVariant)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const foundColors = Array.from(new Set(printfulVariants.map(v => v.color)));
|
||||
const foundSizes = Array.from(new Set(printfulVariants.map(v => v.size)));
|
||||
|
||||
// CREATE WEBFLOW PRODUCT
|
||||
let addProductResponse = await (await fetch(`${Webflow.API_SITES_URL}/products`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"product": {
|
||||
"fieldData": {
|
||||
"name": printfulProduct.sync_product.name,
|
||||
"slug": formatSlug(printfulProduct.sync_product.name),
|
||||
"shippable": true,
|
||||
"tax-category": "standard-taxable",
|
||||
"sku-properties": [
|
||||
{
|
||||
"id": "color",
|
||||
"name": "Color",
|
||||
"enum": foundColors.map(color => ({
|
||||
"id": color,
|
||||
"slug": formatSlug(color),
|
||||
"name": color
|
||||
}))
|
||||
},
|
||||
{
|
||||
"id": "size",
|
||||
"name": "Size",
|
||||
"enum": foundSizes.map(size => ({
|
||||
"id": size,
|
||||
"slug": formatSlug(size),
|
||||
"name": size
|
||||
}))
|
||||
},
|
||||
]
|
||||
}
|
||||
},
|
||||
"sku": webflowVariants[0],
|
||||
} as Products.UpsertProduct)
|
||||
}))?.json();
|
||||
|
||||
if (!addProductResponse?.product?.id) {
|
||||
console.error("Webflow product creation failed:", addProductResponse);
|
||||
throw new Error("Failed to create Webflow product");
|
||||
}
|
||||
|
||||
const webflowProductId = addProductResponse["product"]["id"];
|
||||
const printfulProductId = printfulProduct.sync_product.id;
|
||||
|
||||
// CREATE WEBFLOW PRODUCT SKUs
|
||||
let createSkuResponse = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}/skus`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"sku": webflowVariants[i]
|
||||
"skus": webflowVariants.slice(1)
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
createSkuResponse = await createSkuResponse.json();
|
||||
|
||||
export async function createProductUsingPrintful(printfulProduct: Printful.Products.SyncProduct) {
|
||||
const printfulVariants = printfulProduct.sync_variants;
|
||||
const responseSkus = [addProductResponse["sku"], ...createSkuResponse["skus"]];
|
||||
|
||||
const webflowVariants: Products.Sku[] = [];
|
||||
for (const printfulVariant of printfulVariants) {
|
||||
webflowVariants.push({
|
||||
"fieldData": {
|
||||
"name": printfulVariant.name,
|
||||
"slug": formatSlug(printfulVariant.name),
|
||||
"sku-values": {
|
||||
"color": printfulVariant.color,
|
||||
"size": printfulVariant.size,
|
||||
// SYNC WEBFLOW/PRINTFUL PRODUCT AND VARIANT IDs
|
||||
let modifyPrintfulProductResponse = await fetch(`${Printful.API_URL}/store/products/${printfulProductId}`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${Bun.env.PRINTFUL_AUTH}`,
|
||||
"X-PF-Store-Id": Bun.env.PRINTFUL_STORE_ID ?? ""
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"sync_product": {
|
||||
"external_id": String(webflowProductId)
|
||||
},
|
||||
"price": {
|
||||
"value": +printfulVariant.retail_price * 100,
|
||||
"unit": printfulVariant.currency,
|
||||
"currency": printfulVariant.currency
|
||||
},
|
||||
// TODO: sync image
|
||||
// "main-image": "https://www.example.com/image.jpg"
|
||||
}
|
||||
"sync_variants": responseSkus.map((sku, i) => ({
|
||||
"id": Number(printfulVariants[i].id), // printful variant id
|
||||
"external_id": String(sku.id), // webflow variant id
|
||||
}))
|
||||
})
|
||||
});
|
||||
modifyPrintfulProductResponse = await modifyPrintfulProductResponse.json();
|
||||
}
|
||||
|
||||
const foundColors = Array.from(new Set(printfulVariants.map(v => v.color)));
|
||||
const foundSizes = Array.from(new Set(printfulVariants.map(v => v.size)));
|
||||
|
||||
// CREATE WEBFLOW PRODUCT
|
||||
let addProductResponse = await (await fetch(`${Webflow.API_SITES_URL}/products`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"product": {
|
||||
"fieldData": {
|
||||
"name": printfulProduct.sync_product.name,
|
||||
"slug": formatSlug(printfulProduct.sync_product.name),
|
||||
"sku-properties": [
|
||||
{
|
||||
"id": "color",
|
||||
"name": "Color",
|
||||
"enum": foundColors.map(color => ({
|
||||
"id": color,
|
||||
"slug": formatSlug(color),
|
||||
"name": color
|
||||
}))
|
||||
},
|
||||
{
|
||||
"id": "size",
|
||||
"name": "Size",
|
||||
"enum": foundSizes.map(size => ({
|
||||
"id": size,
|
||||
"slug": formatSlug(size),
|
||||
"name": size
|
||||
}))
|
||||
},
|
||||
]
|
||||
}
|
||||
},
|
||||
"sku": webflowVariants[0],
|
||||
})
|
||||
}))?.json();
|
||||
|
||||
if (!addProductResponse?.product?.id) {
|
||||
console.error("Webflow product creation failed:", addProductResponse);
|
||||
throw new Error("Failed to create Webflow product");
|
||||
}
|
||||
|
||||
const webflowProductId = addProductResponse["product"]["id"];
|
||||
const printfulProductId = printfulProduct.sync_product.id;
|
||||
|
||||
// CREATE WEBFLOW PRODUCT SKUs
|
||||
let createSkuResponse = await fetch(`${Webflow.API_SITES_URL}/products/${webflowProductId}/skus`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"skus": webflowVariants.slice(1)
|
||||
})
|
||||
});
|
||||
createSkuResponse = await createSkuResponse.json();
|
||||
|
||||
const responseSkus = [addProductResponse["sku"], ...createSkuResponse["skus"]];
|
||||
|
||||
// SYNC WEBFLOW/PRINTFUL PRODUCT AND VARIANT IDs
|
||||
let modifyPrintfulProductResponse = await fetch(`${Printful.API_URL}/store/products/${printfulProductId}`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${Bun.env.PRINTFUL_AUTH}`,
|
||||
"X-PF-Store-Id": Bun.env.PRINTFUL_STORE_ID ?? ""
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"sync_product": {
|
||||
"external_id": String(webflowProductId)
|
||||
},
|
||||
"sync_variants": responseSkus.map((sku, i) => ({
|
||||
"id": Number(printfulVariants[i].id), // printful variant id
|
||||
"external_id": String(sku.id), // webflow variant id
|
||||
}))
|
||||
})
|
||||
});
|
||||
modifyPrintfulProductResponse = await modifyPrintfulProductResponse.json();
|
||||
|
||||
// CACHE WEBFLOW/PRINTFUL PRODUCT ASSOCIATION
|
||||
productRecords.add({ printfulProductId, webflowProductId });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user