31 lines
1.3 KiB
TypeScript
31 lines
1.3 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { winofyFetch } from "./winofy";
|
|
import { callApi, type ActionResult } from "./result";
|
|
import type { DeliveryType, OrderGroup, PaymentMethod } from "@/types/api";
|
|
|
|
export interface CheckoutInput {
|
|
address_uuid: string;
|
|
delivery_type: DeliveryType;
|
|
scheduled_at: string | null;
|
|
payment_method: PaymentMethod;
|
|
notes: string;
|
|
/**
|
|
* Which store-cart-groups to check out, by store uuid. Required (not optional) —
|
|
* every caller must be explicit about scope now; there's no more implicit
|
|
* "everything in the cart" default. This is what fixes the bug where paying for
|
|
* one store's order submitted every store's order: previously nothing on this
|
|
* request identified which store(s) were intended, so the backend checked out
|
|
* the whole cart every time.
|
|
*/
|
|
store_uuids: string[];
|
|
}
|
|
|
|
export async function submitCheckout(input: CheckoutInput): Promise<ActionResult<OrderGroup>> {
|
|
const result = await callApi(() => winofyFetch<OrderGroup>("/v1/checkout/", { method: "POST", body: input }));
|
|
// Only the scoped store(s)' items are removed server-side now — the cart page
|
|
// (and the sticky-cart-bar badge) needs a fresh fetch to stop showing them.
|
|
if (result.ok) revalidatePath("/cart");
|
|
return result;
|
|
}
|