import { defineAction } from "../../../automation/actions" import type { Webflow } from "webflow-api" import * as serializers from "webflow-api/serialization" import * as z from "zod" import { getWebflowApi, webflowAction, webflowActionSchemas, webflowInputSchema, webflowObjectSchema, webflowRequest, type WebflowInputValue, } from "../lib" type SiteInput = WebflowInputValue & { siteId: string } type ProductInput = SiteInput & { productId: string } type SkuInput = ProductInput & { skuId: string } type OrderInput = SiteInput & { orderId: string } type InventoryInput = WebflowInputValue & { skuCollectionId: string skuId: string } /** Lists products and their SKUs for one Webflow ecommerce site. */ export const listWebflowProducts = webflowAction< SiteInput, Webflow.ProductAndSkUsList >( defineAction("List Webflow products") .describe("Lists ecommerce products and all of their SKUs.") .account("webflow", "ecommerce:read") .input( webflowInputSchema>( webflowActionSchemas.paginated("siteId"), ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getWebflowApi(account).products.list( input.siteId, webflowRequest(input, "siteId"), ), ), ) /** Retrieves one Webflow product and all of its SKUs. */ export const getWebflowProduct = webflowAction< ProductInput, Webflow.ProductAndSkUs >( defineAction("Get Webflow product") .describe("Retrieves one ecommerce product and all of its SKUs.") .account("webflow", "ecommerce:read") .input( webflowInputSchema( webflowActionSchemas.ids("siteId", "productId"), ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getWebflowApi(account).products.get( input.siteId, input.productId, ), ), ) /** Creates a Webflow ecommerce product and its initial SKU. */ export const createWebflowProduct = webflowAction< SiteInput, Webflow.ProductAndSkUs >( defineAction("Create Webflow product") .describe("Creates an ecommerce product and its initial SKU.") .account("webflow", "ecommerce:write") .input( webflowInputSchema>( webflowActionSchemas.id("siteId"), serializers.ProductSkuCreate, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).products.create( input.siteId, webflowRequest(input, "siteId"), ), ), ) /** Updates a Webflow ecommerce product. */ export const updateWebflowProduct = webflowAction< ProductInput, Webflow.Product >( defineAction("Update Webflow product") .describe("Updates product content, settings, variants, or draft state.") .account("webflow", "ecommerce:write") .input( webflowInputSchema>( webflowActionSchemas.ids("siteId", "productId"), serializers.ProductSkuUpdate, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).products.update( input.siteId, input.productId, webflowRequest(input, "siteId", "productId"), ), ), ) /** Adds one or more SKUs to a Webflow product. */ export const createWebflowProductSkus = webflowAction< ProductInput, Webflow.ProductsCreateSkuResponse >( defineAction("Create Webflow product SKUs") .describe("Adds one or more variant SKUs to an ecommerce product.") .account("webflow", "ecommerce:write") .input( webflowInputSchema>( webflowActionSchemas.ids("siteId", "productId"), serializers.ProductsCreateSkuRequest, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).products.createSku( input.siteId, input.productId, webflowRequest(input, "siteId", "productId"), ), ), ) /** Updates one SKU belonging to a Webflow product. */ export const updateWebflowProductSku = webflowAction< SkuInput, Webflow.Sku >( defineAction("Update Webflow product SKU") .describe("Updates pricing, variants, media, or shipping data for one SKU.") .account("webflow", "ecommerce:write") .input( webflowInputSchema>( webflowActionSchemas.ids("siteId", "productId", "skuId"), serializers.ProductsUpdateSkuRequest, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).products.updateSku( input.siteId, input.productId, input.skuId, webflowRequest(input, "siteId", "productId", "skuId"), ), ), ) /** Lists orders for one Webflow ecommerce site. */ export const listWebflowOrders = webflowAction< SiteInput, Webflow.OrderList >( defineAction("List Webflow orders") .describe("Lists ecommerce orders with status and pagination filters.") .account("webflow", "ecommerce:read") .input( webflowInputSchema>( webflowActionSchemas.parameters(["siteId"], { limit: z.number().int().min(1).max(100).optional(), offset: z.number().int().nonnegative().optional(), status: z .enum([ "pending", "unfulfilled", "fulfilled", "disputed", "dispute-lost", "refunded", ]) .optional(), }), ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getWebflowApi(account).orders.list( input.siteId, webflowRequest(input, "siteId"), ), ), ) /** Retrieves one Webflow ecommerce order. */ export const getWebflowOrder = webflowAction( defineAction("Get Webflow order") .describe( "Retrieves customer, payment, fulfillment, and line-item details.", ) .account("webflow", "ecommerce:read") .input( webflowInputSchema( webflowActionSchemas.ids("siteId", "orderId"), ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getWebflowApi(account).orders.get(input.siteId, input.orderId), ), ) /** Updates the customer-facing or internal comment on a Webflow order. */ export const updateWebflowOrder = webflowAction< OrderInput, Webflow.Order >( defineAction("Update Webflow order") .describe("Updates editable comments on an ecommerce order.") .account("webflow", "ecommerce:write") .input( webflowInputSchema>( webflowActionSchemas.ids("siteId", "orderId"), serializers.OrdersUpdateRequest, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).orders.update( input.siteId, input.orderId, webflowRequest(input, "siteId", "orderId"), ), ), ) /** Marks a Webflow order fulfilled with optional tracking details. */ export const fulfillWebflowOrder = webflowAction< OrderInput, Webflow.Order >( defineAction("Fulfill Webflow order") .describe("Marks an order fulfilled and records shipping details.") .account("webflow", "ecommerce:write") .input( webflowInputSchema>( webflowActionSchemas.ids("siteId", "orderId"), serializers.OrdersUpdateFulfillRequest, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).orders.updateFulfill( input.siteId, input.orderId, webflowRequest(input, "siteId", "orderId"), ), ), ) /** Reverts a Webflow order from fulfilled to unfulfilled. */ export const unfulfillWebflowOrder = webflowAction( defineAction("Unfulfill Webflow order") .describe("Returns a fulfilled ecommerce order to unfulfilled state.") .account("webflow", "ecommerce:write") .input( webflowInputSchema( webflowActionSchemas.ids("siteId", "orderId"), ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).orders.updateUnfulfill( input.siteId, input.orderId, ), ), ) /** Refunds a Webflow ecommerce order through its payment processor. */ export const refundWebflowOrder = webflowAction< OrderInput, Webflow.Order >( defineAction("Refund Webflow order") .describe("Refunds an ecommerce order with an optional reason.") .account("webflow", "ecommerce:write") .input( webflowInputSchema>( webflowActionSchemas.ids("siteId", "orderId"), serializers.OrdersRefundRequest, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).orders.refund( input.siteId, input.orderId, webflowRequest(input, "siteId", "orderId"), ), ), ) /** Retrieves current inventory state for one Webflow SKU. */ export const getWebflowInventory = webflowAction< InventoryInput, Webflow.InventoryItem >( defineAction("Get Webflow inventory") .describe("Retrieves finite or infinite inventory state for one SKU.") .account("webflow", "ecommerce:read") .input( webflowInputSchema( webflowActionSchemas.ids("skuCollectionId", "skuId"), ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getWebflowApi(account).inventory.list( input.skuCollectionId, input.skuId, ), ), ) /** Updates inventory quantity or tracking mode for one Webflow SKU. */ export const updateWebflowInventory = webflowAction< InventoryInput, Webflow.InventoryItem >( defineAction("Update Webflow inventory") .describe("Updates quantity or finite/infinite inventory mode for one SKU.") .account("webflow", "ecommerce:write") .input( webflowInputSchema>( webflowActionSchemas.ids("skuCollectionId", "skuId"), serializers.InventoryUpdateRequest, ), ) .output(webflowObjectSchema()) .retry({ replaySafety: "unsafe" }) .handler( async ({ account, input }) => await getWebflowApi(account).inventory.update( input.skuCollectionId, input.skuId, webflowRequest(input, "skuCollectionId", "skuId"), ), ), ) /** Retrieves currency and collection settings for a Webflow ecommerce site. */ export const getWebflowEcommerceSettings = webflowAction< SiteInput, Webflow.EcommerceSettings >( defineAction("Get Webflow ecommerce settings") .describe("Retrieves currency and ecommerce collection identifiers.") .account("webflow", "ecommerce:read") .input(webflowInputSchema(webflowActionSchemas.id("siteId"))) .output(webflowObjectSchema()) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getWebflowApi(account).ecommerce.getSettings(input.siteId), ), )