Allow Gift Card balance lookup in the Storefront API (#193)

* feat: enable balance lookup for gift-cards
This commit is contained in:
Sebastian Rindom
2021-03-08 13:26:30 +01:00
committed by GitHub
parent 60f164913d
commit 0b172b6d77
5 changed files with 129 additions and 1 deletions

View File

@@ -22,7 +22,7 @@ describe("/store/carts", () => {
afterAll(async () => {
dbConnection.close();
dropDatabase({ databaseName: "medusa-integration" });
await dropDatabase({ databaseName: "medusa-integration" });
medusaProcess.kill();
});

View File

@@ -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,
});
});
});
});

View File

@@ -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
}
}

View File

@@ -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"]

View File

@@ -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
}