diff --git a/integration-tests/api/__tests__/store/cart.js b/integration-tests/api/__tests__/store/cart.js index e924f26e42..e85a31b066 100644 --- a/integration-tests/api/__tests__/store/cart.js +++ b/integration-tests/api/__tests__/store/cart.js @@ -22,7 +22,7 @@ describe("/store/carts", () => { afterAll(async () => { dbConnection.close(); - dropDatabase({ databaseName: "medusa-integration" }); + await dropDatabase({ databaseName: "medusa-integration" }); medusaProcess.kill(); }); diff --git a/integration-tests/api/__tests__/store/gift-cards.js b/integration-tests/api/__tests__/store/gift-cards.js new file mode 100644 index 0000000000..8a75021e14 --- /dev/null +++ b/integration-tests/api/__tests__/store/gift-cards.js @@ -0,0 +1,65 @@ +const { dropDatabase } = require("pg-god"); +const path = require("path"); +const { Region, GiftCard } = require("@medusajs/medusa"); + +const setupServer = require("../../../helpers/setup-server"); +const { useApi } = require("../../../helpers/use-api"); +const { initDb } = require("../../../helpers/use-db"); + +jest.setTimeout(30000); + +describe("/store/gift-cards", () => { + let medusaProcess; + let dbConnection; + + beforeAll(async () => { + const cwd = path.resolve(path.join(__dirname, "..", "..")); + dbConnection = await initDb({ cwd }); + medusaProcess = await setupServer({ cwd }); + }); + + afterAll(async () => { + dbConnection.close(); + await dropDatabase({ databaseName: "medusa-integration" }); + + medusaProcess.kill(); + }); + + describe("GET /store/gift-cards/:code", () => { + beforeEach(async () => { + const manager = dbConnection.manager; + await manager.insert(Region, { + id: "region", + name: "Test Region", + currency_code: "usd", + tax_rate: 0, + }); + await manager.insert(GiftCard, { + id: "gift_test", + code: "GC_TEST", + value: 200, + balance: 120, + region_id: "region", + }); + }); + + afterEach(async () => { + const manager = dbConnection.manager; + await manager.query(`DELETE FROM "gift_card"`); + await manager.query(`DELETE FROM "region"`); + }); + + it("retrieves a gift card", async () => { + const api = useApi(); + + const response = await api.get("/store/gift-cards/GC_TEST"); + expect(response.status).toEqual(200); + expect(response.data.gift_card).toEqual({ + id: "gift_test", + code: "GC_TEST", + value: 200, + balance: 120, + }); + }); + }); +}); diff --git a/packages/medusa/src/api/routes/store/gift-cards/get-gift-card.js b/packages/medusa/src/api/routes/store/gift-cards/get-gift-card.js new file mode 100644 index 0000000000..7a60875ab8 --- /dev/null +++ b/packages/medusa/src/api/routes/store/gift-cards/get-gift-card.js @@ -0,0 +1,41 @@ +import { defaultRelations, defaultFields } from "./" + +/** + * @oas [get] /gift-cards/{code} + * operationId: "GetGiftCardsCode" + * summary: "Retrieve Gift Card by Code" + * description: "Retrieves a Gift Card by its associated unqiue code." + * tags: + * - Region + * responses: + * 200: + * description: OK + * content: + * application/json: + * schema: + * properties: + * id: + * description: The id of the Gift Card + * code: + * description: The code of the Gift Card + * value: + * description: The original value of the Gift Card. + * balance: + * description: The current balanace of the Gift Card + */ +export default async (req, res) => { + const { code } = req.params + + try { + const giftCardService = req.scope.resolve("giftCardService") + const giftCard = await giftCardService.retrieveByCode(code, { + select: defaultFields, + relations: defaultRelations, + }) + + res.json({ gift_card: giftCard }) + } catch (error) { + console.log(error) + throw error + } +} diff --git a/packages/medusa/src/api/routes/store/gift-cards/index.js b/packages/medusa/src/api/routes/store/gift-cards/index.js new file mode 100644 index 0000000000..cbd8b6c3c4 --- /dev/null +++ b/packages/medusa/src/api/routes/store/gift-cards/index.js @@ -0,0 +1,20 @@ +import { Router } from "express" +import middlewares from "../../../middlewares" + +const route = Router() + +export default app => { + app.use("/gift-cards", route) + + route.get("/:code", middlewares.wrap(require("./get-gift-card").default)) + + return app +} + +export const defaultRelations = [] + +export const defaultFields = ["id", "code", "value", "balance"] + +export const allowedRelations = [] + +export const allowedFields = ["id", "code", "value", "balance"] diff --git a/packages/medusa/src/api/routes/store/index.js b/packages/medusa/src/api/routes/store/index.js index 24f7063c7c..72d8137ca1 100644 --- a/packages/medusa/src/api/routes/store/index.js +++ b/packages/medusa/src/api/routes/store/index.js @@ -12,6 +12,7 @@ import shippingOptionRoutes from "./shipping-options" import regionRoutes from "./regions" import swapRoutes from "./swaps" import variantRoutes from "./variants" +import giftCardRoutes from "./gift-cards" const route = Router() @@ -37,6 +38,7 @@ export default (app, container, config) => { regionRoutes(route) swapRoutes(route) variantRoutes(route) + giftCardRoutes(route) return app }