-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vnda): create addItems action (#889)
- Loading branch information
1 parent
17cfac6
commit c6fcf17
Showing
4 changed files
with
99 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, string>; | ||
} | ||
|
||
export interface Props { | ||
items: Item[]; | ||
} | ||
|
||
const action = async ( | ||
props: Props, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<Cart> => { | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters