Allow Gift Card balance lookup in the Storefront API (#193)
* feat: enable balance lookup for gift-cards
This commit is contained in:
@@ -22,7 +22,7 @@ describe("/store/carts", () => {
|
|||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
dbConnection.close();
|
dbConnection.close();
|
||||||
dropDatabase({ databaseName: "medusa-integration" });
|
await dropDatabase({ databaseName: "medusa-integration" });
|
||||||
|
|
||||||
medusaProcess.kill();
|
medusaProcess.kill();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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"]
|
||||||
@@ -12,6 +12,7 @@ import shippingOptionRoutes from "./shipping-options"
|
|||||||
import regionRoutes from "./regions"
|
import regionRoutes from "./regions"
|
||||||
import swapRoutes from "./swaps"
|
import swapRoutes from "./swaps"
|
||||||
import variantRoutes from "./variants"
|
import variantRoutes from "./variants"
|
||||||
|
import giftCardRoutes from "./gift-cards"
|
||||||
|
|
||||||
const route = Router()
|
const route = Router()
|
||||||
|
|
||||||
@@ -37,6 +38,7 @@ export default (app, container, config) => {
|
|||||||
regionRoutes(route)
|
regionRoutes(route)
|
||||||
swapRoutes(route)
|
swapRoutes(route)
|
||||||
variantRoutes(route)
|
variantRoutes(route)
|
||||||
|
giftCardRoutes(route)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user