From c6fcf170be844dbe6f2ccd0a0cbcd7b4e28d1e3b Mon Sep 17 00:00:00 2001 From: "@yuri_assuncx" Date: Tue, 1 Oct 2024 13:07:34 -0300 Subject: [PATCH] feat(vnda): create addItems action (#889) --- vnda/actions/cart/addItems.ts | 46 +++++++++++++++++ vnda/manifest.gen.ts | 18 ++++--- vnda/utils/openapi/vnda.openapi.gen.ts | 26 +++++++--- vnda/utils/openapi/vnda.openapi.json | 70 +++++++++----------------- 4 files changed, 99 insertions(+), 61 deletions(-) create mode 100644 vnda/actions/cart/addItems.ts diff --git a/vnda/actions/cart/addItems.ts b/vnda/actions/cart/addItems.ts new file mode 100644 index 000000000..7cd31f58b --- /dev/null +++ b/vnda/actions/cart/addItems.ts @@ -0,0 +1,46 @@ +import { HttpError } from "../../../utils/http.ts"; +import cartLoader, { Cart } from "../../loaders/cart.ts"; +import { AppContext } from "../../mod.ts"; +import { getCartCookie } from "../../utils/cart.ts"; + +interface Item { + itemId: string; + quantity: number; + attributes?: Record; +} + +export interface Props { + items: Item[]; +} + +const action = async ( + props: Props, + req: Request, + ctx: AppContext, +): Promise => { + const { api } = ctx; + const { items } = props; + const cartId = getCartCookie(req.headers); + + if (!cartId) { + throw new HttpError(400, "Missing cart cookie"); + } + + await api["POST /api/v2/carts/:cartId/items/bulk"]({ cartId }, { + body: { + items: items.map((item) => ({ + sku: item.itemId, + quantity: item.quantity, + customizations: item.attributes + ? Object.entries(item.attributes).map(([key, value]) => ({ + [key]: value, + })) + : undefined, + })), + }, + }); + + return cartLoader({}, req, ctx); +}; + +export default action; diff --git a/vnda/manifest.gen.ts b/vnda/manifest.gen.ts index fac2ad1fb..cd5093019 100644 --- a/vnda/manifest.gen.ts +++ b/vnda/manifest.gen.ts @@ -3,10 +3,11 @@ // This file is automatically updated during development when running `dev.ts`. import * as $$$$$$$$$0 from "./actions/cart/addItem.ts"; -import * as $$$$$$$$$1 from "./actions/cart/simulation.ts"; -import * as $$$$$$$$$2 from "./actions/cart/updateCart.ts"; -import * as $$$$$$$$$3 from "./actions/cart/updateItem.ts"; -import * as $$$$$$$$$4 from "./actions/notifyme.ts"; +import * as $$$$$$$$$1 from "./actions/cart/addItems.ts"; +import * as $$$$$$$$$2 from "./actions/cart/simulation.ts"; +import * as $$$$$$$$$3 from "./actions/cart/updateCart.ts"; +import * as $$$$$$$$$4 from "./actions/cart/updateItem.ts"; +import * as $$$$$$$$$5 from "./actions/notifyme.ts"; import * as $$$$0 from "./handlers/sitemap.ts"; import * as $$$0 from "./loaders/cart.ts"; import * as $$$1 from "./loaders/extensions/price/list.ts"; @@ -33,10 +34,11 @@ const manifest = { }, "actions": { "vnda/actions/cart/addItem.ts": $$$$$$$$$0, - "vnda/actions/cart/simulation.ts": $$$$$$$$$1, - "vnda/actions/cart/updateCart.ts": $$$$$$$$$2, - "vnda/actions/cart/updateItem.ts": $$$$$$$$$3, - "vnda/actions/notifyme.ts": $$$$$$$$$4, + "vnda/actions/cart/addItems.ts": $$$$$$$$$1, + "vnda/actions/cart/simulation.ts": $$$$$$$$$2, + "vnda/actions/cart/updateCart.ts": $$$$$$$$$3, + "vnda/actions/cart/updateItem.ts": $$$$$$$$$4, + "vnda/actions/notifyme.ts": $$$$$$$$$5, }, "name": "vnda", "baseUrl": import.meta.url, diff --git a/vnda/utils/openapi/vnda.openapi.gen.ts b/vnda/utils/openapi/vnda.openapi.gen.ts index c17137bac..6bdf30999 100644 --- a/vnda/utils/openapi/vnda.openapi.gen.ts +++ b/vnda/utils/openapi/vnda.openapi.gen.ts @@ -1036,16 +1036,30 @@ store_coupon_code?: string */ "POST /api/v2/carts/:cartId/items/bulk": { body: { +/** + * Itens do carrinho + */ +items?: { +/** + * Código SKU da variante do produto + */ sku: string +/** + * Unidades do produto + */ quantity: number -extra?: { - -} -place_id?: number /** - * Itens do carrinho + * [Personalização](http://ajuda.vnda.com.br/pt-BR/articles/1763398-funcionalidades-produtos-personalizados) do produto */ -items?: any[][] +customizations?: { +/** + * Adicione a customização de acordo com a [personalização](http://ajuda.vnda.com.br/pt-BR/articles/1763398-funcionalidades-produtos-personalizados) incluídas no Admin da loja. + * Se por exemplo a customização do produto é a cor, o parâmetro para a requisição deve ser `Color` ao invés de `CUstomization`. + * Saiba mais sobre como utilizar esse parâmetro pelo exemplo de requsição localizado na seção de **Request Example** (ao lado do código da requisição). + */ +Customization?: string +}[] +}[] minItems?: 0 } response: CartItem[] diff --git a/vnda/utils/openapi/vnda.openapi.json b/vnda/utils/openapi/vnda.openapi.json index b6f698b99..cb8536cc3 100644 --- a/vnda/utils/openapi/vnda.openapi.json +++ b/vnda/utils/openapi/vnda.openapi.json @@ -5109,27 +5109,11 @@ "schema": { "type": "object", "properties": { - "sku": { - "type": "string" - }, - "quantity": { - "type": "integer", - "minimum": 0, - "exclusiveMinimum": true - }, - "extra": { - "type": "object" - }, - "place_id": { - "type": "integer", - "minimum": 0, - "exclusiveMinimum": true - }, "items": { "type": "array", "description": "Itens do carrinho", "items": { - "type": "array", + "type": "object", "properties": { "sku": { "type": "string", @@ -5156,39 +5140,31 @@ "sku", "quantity" ] - }, - "example": { - "itemcustomizado": { - "items": { - "value": [ - { - "sku": "variante.sku1", - "quantity": 1, - "customizations": [ - { - "Color": "Black" - } - ] - }, - { - "sku": "variante.sku2", - "quantity": 10, - "customizations": [ - { - "Color": "Red" - } - ] - } - ] - } - } } } }, - "required": [ - "sku", - "quantity" - ] + "example": { + "items": [ + { + "sku": "teste", + "quantity": 1, + "customizations": [ + { + "Color": "Black" + } + ] + }, + { + "sku": "variante.sku2", + "quantity": 10, + "customizations": [ + { + "Color": "Red" + } + ] + } + ] + } } } }