|
1
|
import type { |
|
2
|
AuthenticatedMedusaRequest, |
|
3
|
MedusaResponse, |
|
4
|
} from "@medusajs/framework"; |
|
5
|
import { |
|
6
|
ContainerRegistrationKeys, |
|
7
|
MedusaError, |
|
8
|
} from "@medusajs/framework/utils"; |
|
9
|
import { StoreGetGiftCardParams } from "../../../../types"; |
|
10
|
|
|
11
|
export const GET = async ( |
|
12
|
req: AuthenticatedMedusaRequest<StoreGetGiftCardParams>, |
|
13
|
res: MedusaResponse |
|
14
|
) => { |
|
15
|
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY); |
|
16
|
const { idOrCode: code } = req.params; |
|
17
|
|
|
18
|
if (!code?.length) { |
|
19
|
throw new MedusaError( |
|
20
|
MedusaError.Types.INVALID_ARGUMENT, |
|
21
|
"Code is required" |
|
22
|
); |
|
23
|
} |
|
24
|
|
|
25
|
const { |
|
26
|
data: [gift_card], |
|
27
|
} = await query.graph({ |
|
28
|
entity: "gift_cards", |
|
29
|
fields: req.queryConfig.fields, |
|
30
|
filters: { |
|
31
|
code, |
|
32
|
}, |
|
33
|
}); |
|
34
|
|
|
35
|
if (!gift_card) { |
|
36
|
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Gift card not found"); |
|
37
|
} |
|
38
|
|
|
39
|
res.json({ gift_card }); |
|
40
|
}; |