Adds gift card line item generation
This commit is contained in:
@@ -33,7 +33,8 @@ describe("POST /store/carts/:id", () => {
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||
IdMap.getId("testVariant"),
|
||||
IdMap.getId("testRegion"),
|
||||
3
|
||||
3,
|
||||
undefined // no metadata
|
||||
)
|
||||
})
|
||||
|
||||
@@ -72,7 +73,8 @@ describe("POST /store/carts/:id", () => {
|
||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||
IdMap.getId("fail"),
|
||||
IdMap.getId("testRegion"),
|
||||
3
|
||||
3,
|
||||
undefined // no metdata
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ export default new mongoose.Schema({
|
||||
title: { type: String, required: true },
|
||||
description: { type: String },
|
||||
thumbnail: { type: String },
|
||||
is_giftcard: { type: Boolean, default: false },
|
||||
|
||||
// mongoose doesn't allow multi-type validation but this field allows both
|
||||
// an object containing:
|
||||
|
||||
@@ -106,6 +106,11 @@ const eur10us12 = {
|
||||
title: "EUR10US-12",
|
||||
}
|
||||
|
||||
const giftCardVar = {
|
||||
_id: IdMap.getId("giftCardVar"),
|
||||
title: "100 USD",
|
||||
}
|
||||
|
||||
export const variants = {
|
||||
one: variant1,
|
||||
two: variant2,
|
||||
@@ -115,6 +120,7 @@ export const variants = {
|
||||
empty_variant: emptyVariant,
|
||||
eur10us12: eur10us12,
|
||||
testVariant: testVariant,
|
||||
giftCard: giftCardVar,
|
||||
}
|
||||
|
||||
export const ProductVariantServiceMock = {
|
||||
@@ -129,6 +135,9 @@ export const ProductVariantServiceMock = {
|
||||
})
|
||||
}),
|
||||
retrieve: jest.fn().mockImplementation(variantId => {
|
||||
if (variantId === IdMap.getId("giftCardVar")) {
|
||||
return Promise.resolve(variants.giftCard)
|
||||
}
|
||||
if (variantId === "1") {
|
||||
return Promise.resolve(variant1)
|
||||
}
|
||||
@@ -188,6 +197,10 @@ export const ProductVariantServiceMock = {
|
||||
}
|
||||
}
|
||||
|
||||
if (variantId === IdMap.getId("giftCardVar")) {
|
||||
return Promise.resolve(100)
|
||||
}
|
||||
|
||||
return Promise.reject(new Error("Not found"))
|
||||
}),
|
||||
delete: jest.fn().mockReturnValue(Promise.resolve()),
|
||||
|
||||
@@ -96,6 +96,16 @@ export const ProductServiceMock = {
|
||||
list: jest.fn().mockImplementation(data => {
|
||||
// Used to retrieve a product based on a variant id see
|
||||
// ProductVariantService.addOptionValue
|
||||
if (data.variants === IdMap.getId("giftCardVar")) {
|
||||
return Promise.resolve([
|
||||
{
|
||||
_id: IdMap.getId("giftCardProd"),
|
||||
title: "Gift Card",
|
||||
is_giftcard: true,
|
||||
thumbnail: "1234",
|
||||
},
|
||||
])
|
||||
}
|
||||
if (data.variants === IdMap.getId("testVariant")) {
|
||||
return Promise.resolve([
|
||||
{
|
||||
|
||||
@@ -44,4 +44,51 @@ describe("LineItemService", () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("generate with giftcard", () => {
|
||||
let result
|
||||
beforeAll(async () => {
|
||||
jest.clearAllMocks()
|
||||
const lineItemService = new LineItemService({
|
||||
productVariantService: ProductVariantServiceMock,
|
||||
productService: ProductServiceMock,
|
||||
regionService: RegionServiceMock,
|
||||
})
|
||||
result = await lineItemService.generate(
|
||||
IdMap.getId("giftCardVar"),
|
||||
IdMap.getId("region-france"),
|
||||
1,
|
||||
{
|
||||
name: "Test Name",
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it("results correctly", () => {
|
||||
expect(result).toEqual({
|
||||
title: "Gift Card",
|
||||
description: "100 USD",
|
||||
thumbnail: "1234",
|
||||
is_giftcard: true,
|
||||
content: {
|
||||
unit_price: 100,
|
||||
variant: {
|
||||
_id: IdMap.getId("giftCardVar"),
|
||||
title: "100 USD",
|
||||
},
|
||||
product: {
|
||||
_id: IdMap.getId("giftCardProd"),
|
||||
title: "Gift Card",
|
||||
thumbnail: "1234",
|
||||
is_giftcard: true,
|
||||
},
|
||||
quantity: 1,
|
||||
},
|
||||
metadata: {
|
||||
name: "Test Name",
|
||||
},
|
||||
quantity: 1,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -83,8 +83,9 @@ class LineItemService extends BaseService {
|
||||
* @param {string} variantId - id of the line item variant
|
||||
* @param {*} regionId - id of the cart region
|
||||
* @param {*} quantity - number of items
|
||||
* @param {object} metadata - metadata for the line item
|
||||
*/
|
||||
async generate(variantId, regionId, quantity) {
|
||||
async generate(variantId, regionId, quantity, metadata = {}) {
|
||||
const variant = await this.productVariantService_.retrieve(variantId)
|
||||
const region = await this.regionService_.retrieve(regionId)
|
||||
|
||||
@@ -104,7 +105,7 @@ class LineItemService extends BaseService {
|
||||
region._id
|
||||
)
|
||||
|
||||
return {
|
||||
const line = {
|
||||
title: product.title,
|
||||
description: variant.title,
|
||||
quantity,
|
||||
@@ -116,6 +117,13 @@ class LineItemService extends BaseService {
|
||||
quantity: 1,
|
||||
},
|
||||
}
|
||||
|
||||
if (product.is_giftcard) {
|
||||
line.is_giftcard = true
|
||||
line.metadata = metadata
|
||||
}
|
||||
|
||||
return line
|
||||
}
|
||||
|
||||
isEqual(line, match) {
|
||||
|
||||
Reference in New Issue
Block a user