|
1
|
import { |
|
2
|
AuthenticatedMedusaRequest, |
|
3
|
MedusaResponse, |
|
4
|
} from "@medusajs/framework"; |
|
5
|
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"; |
|
6
|
import { createGiftCardsWorkflow } from "../../../workflows/gift-cards/workflows/create-gift-cards"; |
|
7
|
import { AdminCreateGiftCardParams, AdminGetGiftCardsParams, AdminGiftCardResponse, AdminGiftCardsResponse } from "../../../types"; |
|
8
|
|
|
9
|
export const GET = async ( |
|
10
|
req: AuthenticatedMedusaRequest<AdminGetGiftCardsParams>, |
|
11
|
res: MedusaResponse<AdminGiftCardsResponse> |
|
12
|
) => { |
|
13
|
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY); |
|
14
|
|
|
15
|
const { fields, pagination } = req.queryConfig; |
|
16
|
const { data: gift_cards, metadata } = await query.graph({ |
|
17
|
entity: "gift_cards", |
|
18
|
fields, |
|
19
|
filters: { |
|
20
|
...req.filterableFields, |
|
21
|
}, |
|
22
|
pagination: { |
|
23
|
...pagination, |
|
24
|
skip: pagination.skip!, |
|
25
|
}, |
|
26
|
}); |
|
27
|
|
|
28
|
res.json({ |
|
29
|
gift_cards, |
|
30
|
count: metadata!.count, |
|
31
|
offset: metadata!.skip, |
|
32
|
limit: metadata!.take, |
|
33
|
}); |
|
34
|
}; |
|
35
|
|
|
36
|
export const POST = async ( |
|
37
|
req: AuthenticatedMedusaRequest<AdminCreateGiftCardParams>, |
|
38
|
res: MedusaResponse<AdminGiftCardResponse> |
|
39
|
) => { |
|
40
|
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY); |
|
41
|
|
|
42
|
const { |
|
43
|
result: [giftCard], |
|
44
|
} = await createGiftCardsWorkflow.run({ |
|
45
|
input: [{ ...req.validatedBody }], |
|
46
|
container: req.scope, |
|
47
|
}); |
|
48
|
|
|
49
|
const { |
|
50
|
data: [gift_card], |
|
51
|
} = await query.graph( |
|
52
|
{ |
|
53
|
entity: "gift_cards", |
|
54
|
fields: req.queryConfig.fields, |
|
55
|
filters: { id: giftCard.id }, |
|
56
|
}, |
|
57
|
{ throwIfKeyNotFound: true } |
|
58
|
); |
|
59
|
|
|
60
|
res.json({ gift_card }); |
|
61
|
}; |