Gift cards are automatically added to gift card profile

This commit is contained in:
Sebastian Rindom
2020-07-20 15:54:14 +02:00
parent 0b8f1a5c31
commit df47da50fa
10 changed files with 124 additions and 4 deletions
@@ -1,6 +1,7 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
import { ShippingProfileServiceMock } from "../../../../../services/__mocks__/shipping-profile"
describe("POST /admin/products", () => {
describe("successful creation", () => {
@@ -38,8 +39,80 @@ describe("POST /admin/products", () => {
description: "Test Description",
tags: "hi,med,dig",
handle: "test-product",
is_giftcard: false,
})
})
it("calls shipping profile default", () => {
expect(ShippingProfileServiceMock.retrieveDefault).toHaveBeenCalledTimes(
1
)
expect(ShippingProfileServiceMock.retrieveDefault).toHaveBeenCalledWith()
})
})
describe("successful creation of gift card product", () => {
let subject
beforeAll(async () => {
jest.clearAllMocks()
subject = await request("POST", "/admin/products", {
payload: {
title: "Gift Card",
description: "make someone happy",
handle: "test-gift-card",
is_giftcard: true,
options: [{ title: "Denominations" }],
variants: [
{
title: "100 USD",
prices: [
{
currency_code: "USD",
amount: 100,
},
],
options: [
{
value: "100",
},
],
},
],
},
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
})
})
it("calls service createDraft", () => {
expect(ProductServiceMock.createDraft).toHaveBeenCalledTimes(1)
expect(ProductServiceMock.createDraft).toHaveBeenCalledWith({
title: "Gift Card",
description: "make someone happy",
options: [{ title: "Denominations" }],
handle: "test-gift-card",
is_giftcard: true,
})
})
it("calls profile service", () => {
expect(
ShippingProfileServiceMock.retrieveGiftCardDefault
).toHaveBeenCalledTimes(1)
expect(
ShippingProfileServiceMock.retrieveGiftCardDefault
).toHaveBeenCalledWith()
expect(ShippingProfileServiceMock.addProduct).toHaveBeenCalledTimes(1)
expect(ShippingProfileServiceMock.addProduct).toHaveBeenCalledWith(
IdMap.getId("giftCardProfile"),
undefined
)
})
})
describe("invalid data returns error details", () => {
@@ -5,6 +5,7 @@ export default async (req, res) => {
title: Validator.string().required(),
description: Validator.string(),
tags: Validator.string(),
is_giftcard: Validator.boolean().default(false),
options: Validator.array().items({
title: Validator.string().required(),
}),
@@ -71,8 +72,13 @@ export default async (req, res) => {
}
// Add to default shipping profile
const { _id } = await shippingProfileService.retrieveDefault()
await shippingProfileService.addProduct(_id, newProduct._id)
if (value.is_giftcard) {
const { _id } = await shippingProfileService.retrieveGiftCardDefault()
await shippingProfileService.addProduct(_id, newProduct._id)
} else {
const { _id } = await shippingProfileService.retrieveDefault()
await shippingProfileService.addProduct(_id, newProduct._id)
}
newProduct = await productService.decorate(
newProduct,
@@ -90,6 +96,7 @@ export default async (req, res) => {
)
res.json({ product: newProduct })
} catch (err) {
console.log(err)
throw err
}
}
@@ -6,6 +6,7 @@ export default async (req, res) => {
const schema = Validator.object().keys({
variant_id: Validator.string().required(),
quantity: Validator.number().required(),
metadata: Validator.object().optional(),
})
const { value, error } = schema.validate(req.body)
@@ -21,7 +22,8 @@ export default async (req, res) => {
const lineItem = await lineItemService.generate(
value.variant_id,
cart.region_id,
value.quantity
value.quantity,
value.metadata
)
await cartService.addLineItem(cart._id, lineItem)