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