Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/core/src/routes/admin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,11 +709,13 @@ async function deleteShippingZone(
if (!current) return json({ error: "Zone not found" }, 404);
// Zones own methods; fail fast if any method still references this zone
// rather than leaving orphaned methods in the store.
// Use fetch+filter instead of .where — EmDash's where clause building
// breaks on Postgres (syntax error near "=").
const methods = await storeOf<ShippingMethod>(ctx, "shipping_methods").query({
where: { zoneId: id },
limit: 1,
limit: 500,
});
if (methods.items.length > 0) {
const zoneHasMethods = methods.items.some((r) => (r.data as ShippingMethod).zoneId === id);
if (zoneHasMethods) {
return json(
{
error:
Expand Down
94 changes: 47 additions & 47 deletions packages/core/src/routes/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,29 +612,29 @@ export const cartRoutes = {
};
}
).shipping_zones;
const methodsStore = (
ctx.storage as unknown as {
shipping_methods: {
query(opts: {
where: Record<string, string | number>;
limit: number;
}): Promise<{ items: Array<{ id: string; data: ShippingMethod }> }>;
};
}
).shipping_methods;

const zones = (await zonesStore.query({ limit: 200 })).items.map((r) => ({
...r.data,
id: r.id,
}));
const zone = pickZone(cart.shippingAddress, zones);
if (!zone) return jsonResponse({ options: [] }, withSessionCookie(setCookie));
const methods = (
await methodsStore.query({
where: { zoneId: zone.id, enabled: 1 },
limit: 50,
})
).items.map((r) => ({ ...r.data, id: r.id }));
const methodsStore = (
ctx.storage as unknown as {
shipping_methods: {
query(opts: {
where?: Record<string, string | number>;
limit: number;
}): Promise<{ items: Array<{ id: string; data: ShippingMethod }> }>;
};
}
).shipping_methods;

const zones = (await zonesStore.query({ limit: 200 })).items.map((r) => ({
...r.data,
id: r.id,
}));
const zone = pickZone(cart.shippingAddress, zones);
if (!zone) return jsonResponse({ options: [] }, withSessionCookie(setCookie));
// Fetch all methods and filter in JS — EmDash's .where builds SQLite-style
// SQL that breaks on Postgres (syntax error near "="). This fetch+filter
// approach works across both databases.
const methods = (await methodsStore.query({ limit: 200 })).items
.map((r) => ({ ...r.data, id: r.id }))
.filter((m) => m.zoneId === zone.id && (m.enabled === true || (m.enabled as unknown) === 1));

const options = calculateRates({
items: cart.items,
Expand Down Expand Up @@ -669,31 +669,31 @@ export const cartRoutes = {
};
}
).shipping_zones;
const methodsStore = (
ctx.storage as unknown as {
shipping_methods: {
get(id: string): Promise<ShippingMethod | null>;
query(opts: {
where: Record<string, string | number>;
limit: number;
}): Promise<{ items: Array<{ id: string; data: ShippingMethod }> }>;
};
}
).shipping_methods;
const zones = (await zonesStore.query({ limit: 200 })).items.map((r) => ({
...r.data,
id: r.id,
}));
const zone = pickZone(cart.shippingAddress, zones);
if (!zone) {
return errorResponse("No shipping zone matches the address", 409, setCookie);
const methodsStore = (
ctx.storage as unknown as {
shipping_methods: {
get(id: string): Promise<ShippingMethod | null>;
query(opts: {
where?: Record<string, string | number>;
limit: number;
}): Promise<{ items: Array<{ id: string; data: ShippingMethod }> }>;
};
}
const methods = (
await methodsStore.query({
where: { zoneId: zone.id, enabled: 1 },
limit: 50,
})
).items.map((r) => ({ ...r.data, id: r.id }));
).shipping_methods;
const zones = (await zonesStore.query({ limit: 200 })).items.map((r) => ({
...r.data,
id: r.id,
}));
const zone = pickZone(cart.shippingAddress, zones);
if (!zone) {
return errorResponse("No shipping zone matches the address", 409, setCookie);
}
// Fetch all methods and filter in JS — EmDash's .where builds SQLite-style
// SQL that breaks on Postgres (syntax error near "="). This fetch+filter
// approach works across both databases.
const methods = (await methodsStore.query({ limit: 200 })).items
.map((r) => ({ ...r.data, id: r.id }))
.filter((m) => m.zoneId === zone.id && (m.enabled === true || (m.enabled as unknown) === 1));
const options = calculateRates({
items: cart.items,
currency: cart.currency,
Expand Down
Loading