Syncing improvemetns
This commit is contained in:
+49
-17
@@ -9,6 +9,15 @@ import rawStandards from "$lib/data/standards.json" assert { type: "json" }
|
|||||||
|
|
||||||
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
|
||||||
|
const syncProduct = async (printfulProduct: Printful.Products.Product) => {
|
||||||
|
let webflowProduct = await Webflow.Products.get(printfulProduct.sync_product.external_id);
|
||||||
|
if (webflowProduct)
|
||||||
|
await Webflow.Products.updateUsingPrintful(printfulProduct);
|
||||||
|
else
|
||||||
|
webflowProduct = await Webflow.Products.createUsingPrintful(printfulProduct);
|
||||||
|
await Webflow.Products.syncImages(webflowProduct);
|
||||||
|
};
|
||||||
|
|
||||||
export const app = new Elysia({ prefix: "/api" })
|
export const app = new Elysia({ prefix: "/api" })
|
||||||
.use(cors({
|
.use(cors({
|
||||||
origin: '*'
|
origin: '*'
|
||||||
@@ -18,7 +27,7 @@ export const app = new Elysia({ prefix: "/api" })
|
|||||||
|
|
||||||
// CALCULATOR
|
// CALCULATOR
|
||||||
|
|
||||||
.post("/calculate", async ({ body }) => {
|
.post("/calculate", async ({ body, store }) => {
|
||||||
const MAIN_STANDARDS = new Standards(rawStandards as ActivityStandards);
|
const MAIN_STANDARDS = new Standards(rawStandards as ActivityStandards);
|
||||||
interface CalcRequest {
|
interface CalcRequest {
|
||||||
player: Player;
|
player: Player;
|
||||||
@@ -38,14 +47,36 @@ export const app = new Elysia({ prefix: "/api" })
|
|||||||
})
|
})
|
||||||
|
|
||||||
// COMMERCE
|
// COMMERCE
|
||||||
|
.group("/products", app => app
|
||||||
|
.group("/sync", app => app
|
||||||
|
.state("job", {
|
||||||
|
isSyncing: false,
|
||||||
|
printfulProductId: undefined as undefined | number
|
||||||
|
})
|
||||||
|
.get("/", async ({ store }) => {
|
||||||
|
return store.job;
|
||||||
|
})
|
||||||
|
.post("/:printfulProductId?", async ({ params, store }) => {
|
||||||
|
if (store.job.isSyncing) return "";
|
||||||
|
store.job.isSyncing = true;
|
||||||
|
try {
|
||||||
|
if (params.printfulProductId) {
|
||||||
|
const printfulProductId = +params.printfulProductId;
|
||||||
|
|
||||||
|
store.job.printfulProductId = printfulProductId;
|
||||||
|
|
||||||
|
const printfulProduct = await Printful.Products.get(printfulProductId);
|
||||||
|
await syncProduct(printfulProduct);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log("Syncing all products...");
|
||||||
|
|
||||||
.post("/products/sync", async ({ body, set }) => {
|
|
||||||
console.log("Syncing...");
|
|
||||||
const printfulProducts = await Printful.Products.getAll();
|
const printfulProducts = await Printful.Products.getAll();
|
||||||
const webflowProducts = await Webflow.Products.getAll();
|
const webflowProducts = await Webflow.Products.getAll();
|
||||||
for (const printfulProduct of printfulProducts) {
|
for (const printfulProduct of printfulProducts) {
|
||||||
|
store.job.printfulProductId = printfulProduct.id;
|
||||||
|
|
||||||
await sleep(2000);
|
await sleep(2000);
|
||||||
console.log(printfulProduct.name, printfulProduct.id, printfulProduct.external_id);
|
|
||||||
const fullPrintfulProduct = await Printful.Products.get(printfulProduct.id);
|
const fullPrintfulProduct = await Printful.Products.get(printfulProduct.id);
|
||||||
|
|
||||||
let webflowProduct = webflowProducts
|
let webflowProduct = webflowProducts
|
||||||
@@ -56,17 +87,28 @@ export const app = new Elysia({ prefix: "/api" })
|
|||||||
webflowProduct = await Webflow.Products.createUsingPrintful(fullPrintfulProduct);
|
webflowProduct = await Webflow.Products.createUsingPrintful(fullPrintfulProduct);
|
||||||
await Webflow.Products.syncImages(webflowProduct);
|
await Webflow.Products.syncImages(webflowProduct);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
console.log("Done");
|
console.log("Done");
|
||||||
return "";
|
return "";
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
store.job.isSyncing = false;
|
||||||
|
store.job.printfulProductId = undefined;
|
||||||
|
}
|
||||||
}, {
|
}, {
|
||||||
error({ error }) {
|
error({ error }) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return {
|
return {
|
||||||
message: "Failed to sync products",
|
message: "Failed to sync product(s)",
|
||||||
error
|
error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
// WEBHOOKS
|
// WEBHOOKS
|
||||||
|
|
||||||
@@ -75,14 +117,7 @@ export const app = new Elysia({ prefix: "/api" })
|
|||||||
switch (payload.type) {
|
switch (payload.type) {
|
||||||
case Printful.Webhook.Event.ProductUpdated: {
|
case Printful.Webhook.Event.ProductUpdated: {
|
||||||
const printfulProduct = await Printful.Products.get(payload.data.sync_product.id);
|
const printfulProduct = await Printful.Products.get(payload.data.sync_product.id);
|
||||||
|
await syncProduct(printfulProduct);
|
||||||
let webflowProduct = await Webflow.Products.get(printfulProduct.sync_product.external_id);
|
|
||||||
if (webflowProduct)
|
|
||||||
await Webflow.Products.updateUsingPrintful(printfulProduct);
|
|
||||||
else
|
|
||||||
webflowProduct = await Webflow.Products.createUsingPrintful(printfulProduct);
|
|
||||||
await Webflow.Products.syncImages(webflowProduct);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Printful.Webhook.Event.ProductDeleted: {
|
case Printful.Webhook.Event.ProductDeleted: {
|
||||||
@@ -101,8 +136,5 @@ export const app = new Elysia({ prefix: "/api" })
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export type App = typeof app;
|
export const api = treaty<typeof app>("localhost:5173").api;
|
||||||
|
|
||||||
const apiWrapper = treaty<App>(app);
|
|
||||||
export const api = apiWrapper.api;
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,46 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Printful } from "$lib/services/commerce/printful";
|
import { api } from "$lib/api.js";
|
||||||
|
import type { Printful } from "$lib/services/commerce/printful";
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
|
||||||
const { data } = $props();
|
const { data } = $props();
|
||||||
|
|
||||||
let isSyncing = $state(false);
|
const synchronizer = $state({
|
||||||
|
isSyncing: false,
|
||||||
|
printfulProductId: undefined as undefined | number,
|
||||||
|
poll: async function () {
|
||||||
|
const res = await api.products.sync.get();
|
||||||
|
if (res.data) {
|
||||||
|
synchronizer.isSyncing = res.data.isSyncing;
|
||||||
|
synchronizer.printfulProductId = res.data.printfulProductId;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
startPolling: () => {
|
||||||
|
const intervalId = setInterval(async () => {
|
||||||
|
await synchronizer.poll();
|
||||||
|
if (!synchronizer.isSyncing) clearInterval(intervalId);
|
||||||
|
}, 100);
|
||||||
|
},
|
||||||
|
syncAll: async () => {
|
||||||
|
synchronizer.startPolling();
|
||||||
|
await api.products.sync.post();
|
||||||
|
},
|
||||||
|
sync: async (printfulProductId: number) => {
|
||||||
|
synchronizer.startPolling();
|
||||||
|
await api.products
|
||||||
|
.sync({ printfulProductId: String(printfulProductId) })
|
||||||
|
.post();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const isPrintfulProductSynced = async (
|
onMount(async () => {
|
||||||
|
await synchronizer.poll();
|
||||||
|
if (synchronizer.isSyncing) {
|
||||||
|
synchronizer.startPolling();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const isProductSynced = async (
|
||||||
printfulProduct: Printful.Products.SyncProduct,
|
printfulProduct: Printful.Products.SyncProduct,
|
||||||
) => {
|
) => {
|
||||||
const webflowProducts = await data.products.webflow;
|
const webflowProducts = await data.products.webflow;
|
||||||
@@ -13,29 +48,25 @@
|
|||||||
(p) => p.product.id === printfulProduct.external_id,
|
(p) => p.product.id === printfulProduct.external_id,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const syncProducts = async () => {
|
|
||||||
isSyncing = true;
|
|
||||||
await fetch(`http://localhost:5173/api/products/sync`, {
|
|
||||||
method: "POST",
|
|
||||||
});
|
|
||||||
isSyncing = false;
|
|
||||||
};
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#await data.products.printful}
|
{#await data.products.printful}
|
||||||
<p>Loading...</p>
|
<p>Loading...</p>
|
||||||
{:then printfulProducts}
|
{:then printfulProducts}
|
||||||
{#if isSyncing}
|
<button
|
||||||
<p>Syncing...</p>
|
disabled={synchronizer.isSyncing}
|
||||||
{:else}
|
onclick={() => synchronizer.syncAll()}
|
||||||
<button onclick={() => syncProducts()} class="btn btn-lg">sync</button>
|
class="btn btn-xl mb-5"
|
||||||
|
>
|
||||||
|
Sync all
|
||||||
|
</button>
|
||||||
<div class="w-full overflow-x-auto">
|
<div class="w-full overflow-x-auto">
|
||||||
<table class="table">
|
<table class="table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<th>Synced</th>
|
<th>Status</th>
|
||||||
|
<th>Sync</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -43,22 +74,37 @@
|
|||||||
<tr class="hover:bg-base-300">
|
<tr class="hover:bg-base-300">
|
||||||
<td>{printfulProduct.name}</td>
|
<td>{printfulProduct.name}</td>
|
||||||
<td>
|
<td>
|
||||||
{#await isPrintfulProductSynced(printfulProduct) then isSynced}
|
{#await isProductSynced(printfulProduct) then isSynced}
|
||||||
<div
|
{#if synchronizer.printfulProductId === printfulProduct.id}
|
||||||
class="badge {isSynced
|
<div class="badge badge-warning">
|
||||||
? 'badge-success'
|
Syncing...
|
||||||
: 'badge-error'}"
|
|
||||||
>
|
|
||||||
{isSynced ? "Synced" : "Desynced"}
|
|
||||||
</div>
|
</div>
|
||||||
|
{:else if isSynced}
|
||||||
|
<div class="badge badge-success">
|
||||||
|
Synced
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<div class="badge badge-error">
|
||||||
|
Desynced
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
{/await}
|
{/await}
|
||||||
</td>
|
</td>
|
||||||
|
<td>
|
||||||
|
<button
|
||||||
|
disabled={synchronizer.isSyncing}
|
||||||
|
onclick={() =>
|
||||||
|
synchronizer.sync(printfulProduct.id)}
|
||||||
|
class="btn"
|
||||||
|
>
|
||||||
|
Sync
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{/each}
|
{/each}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
|
||||||
{:catch error}
|
{:catch error}
|
||||||
<p>Something went wrong: {error.message}</p>
|
<p>Something went wrong: {error.message}</p>
|
||||||
{/await}
|
{/await}
|
||||||
|
|||||||
+2
-1
@@ -10,7 +10,8 @@
|
|||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"moduleResolution": "bundler"
|
"moduleResolution": "bundler",
|
||||||
|
"importsNotUsedAsValue": true
|
||||||
}
|
}
|
||||||
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
|
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
|
||||||
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files
|
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files
|
||||||
|
|||||||
Reference in New Issue
Block a user