Merge branch 'hotfix/gift-card-creation' into develop
This commit is contained in:
@@ -0,0 +1,79 @@
|
|||||||
|
const { dropDatabase } = require("pg-god");
|
||||||
|
const path = require("path");
|
||||||
|
const { Region } = require("@medusajs/medusa");
|
||||||
|
|
||||||
|
const setupServer = require("../../../helpers/setup-server");
|
||||||
|
const { useApi } = require("../../../helpers/use-api");
|
||||||
|
const { initDb } = require("../../../helpers/use-db");
|
||||||
|
const adminSeeder = require("../../helpers/admin-seeder");
|
||||||
|
|
||||||
|
jest.setTimeout(30000);
|
||||||
|
|
||||||
|
describe("/admin/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 () => {
|
||||||
|
await dbConnection.close();
|
||||||
|
await dropDatabase({ databaseName: "medusa-integration" });
|
||||||
|
|
||||||
|
medusaProcess.kill();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("POST /admin/gift-cards", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
const manager = dbConnection.manager;
|
||||||
|
try {
|
||||||
|
await adminSeeder(dbConnection);
|
||||||
|
await manager.insert(Region, {
|
||||||
|
id: "region",
|
||||||
|
name: "Test Region",
|
||||||
|
currency_code: "usd",
|
||||||
|
tax_rate: 0,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const manager = dbConnection.manager;
|
||||||
|
await manager.query(`DELETE FROM "gift_card"`);
|
||||||
|
await manager.query(`DELETE FROM "region"`);
|
||||||
|
await manager.query(`DELETE FROM "user"`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates a gift card", async () => {
|
||||||
|
const api = useApi();
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.post(
|
||||||
|
"/admin/gift-cards",
|
||||||
|
{
|
||||||
|
value: 1000,
|
||||||
|
region_id: "region",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200);
|
||||||
|
expect(response.data.gift_card.value).toEqual(1000);
|
||||||
|
expect(response.data.gift_card.balance).toEqual(1000);
|
||||||
|
expect(response.data.gift_card.region_id).toEqual("region");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -381,6 +381,10 @@ class SendGridService extends NotificationService {
|
|||||||
relations: ["region", "order"],
|
relations: ["region", "order"],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (!giftCard.order) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const taxRate = giftCard.region.tax_rate / 100
|
const taxRate = giftCard.region.tax_rate / 100
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -20,9 +20,12 @@ export default async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const giftCardService = req.scope.resolve("giftCardService")
|
const giftCardService = req.scope.resolve("giftCardService")
|
||||||
|
|
||||||
await giftCardService.create(value)
|
const newly = await giftCardService.create({
|
||||||
|
...value,
|
||||||
|
balance: value.value,
|
||||||
|
})
|
||||||
|
|
||||||
const giftCard = await giftCardService.retrieve(id, {
|
const giftCard = await giftCardService.retrieve(newly.id, {
|
||||||
select: defaultFields,
|
select: defaultFields,
|
||||||
relations: defaultRelations,
|
relations: defaultRelations,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -108,13 +108,6 @@ class GiftCardService extends BaseService {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!giftCard.order_id) {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.Types.NOT_FOUND,
|
|
||||||
`Gift card is missing order_id`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Will throw if region does not exist
|
// Will throw if region does not exist
|
||||||
const region = await this.regionService_.retrieve(giftCard.region_id)
|
const region = await this.regionService_.retrieve(giftCard.region_id)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user