1 import type {
2 AuthenticatedMedusaRequest,
3 MedusaResponse,
4 } from "@medusajs/framework";
5 import { ContainerRegistrationKeys } from "@medusajs/framework/utils";
6 import { updateGiftCardsWorkflow } from "../../../../workflows/gift-cards/workflows/update-gift-cards";
7 import { AdminGiftCardResponse, AdminUpdateGiftCardParams } from "../../../../types";
8
9 export const GET = async (
10 req: AuthenticatedMedusaRequest,
11 res: MedusaResponse<AdminGiftCardResponse>
12 ) => {
13 const query = req.scope.resolve(ContainerRegistrationKeys.QUERY);
14 const { id } = req.params;
15
16 const {
17 data: [gift_card],
18 } = await query.graph(
19 {
20 entity: "gift_cards",
21 fields: req.queryConfig.fields,
22 filters: { id },
23 },
24 { throwIfKeyNotFound: true }
25 );
26
27 res.json({ gift_card });
28 };
29
30 export const POST = async (
31 req: AuthenticatedMedusaRequest<AdminUpdateGiftCardParams>,
32 res: MedusaResponse<AdminGiftCardResponse>
33 ) => {
34 const query = req.scope.resolve(ContainerRegistrationKeys.QUERY);
35 const { id } = req.params;
36
37 await query.graph(
38 {
39 entity: "gift_cards",
40 fields: req.queryConfig.fields,
41 filters: { id },
42 },
43 { throwIfKeyNotFound: true }
44 );
45
46 await updateGiftCardsWorkflow.run({
47 input: [
48 {
49 id,
50 ...req.body,
51 },
52 ],
53 });
54
55 const {
56 data: [gift_card],
57 } = await query.graph(
58 {
59 entity: "gift_cards",
60 fields: req.queryConfig.fields,
61 filters: { id },
62 },
63 { throwIfKeyNotFound: true }
64 );
65
66 res.json({ gift_card });
67 };