Adds gift card line item generation
This commit is contained in:
1 parent
df47da50fa
commit
4c9b876407
6 files changed
+85
-4
No files matched your search
@@ -33,7 +33,8 @@ describe("POST /store/carts/:id", () => {
|
|||||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||||
IdMap.getId("testVariant"),
|
IdMap.getId("testVariant"),
|
||||||
IdMap.getId("testRegion"),
|
IdMap.getId("testRegion"),
|
||||||
3
|
3,
|
||||||
|
undefined // no metadata
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -72,7 +73,8 @@ describe("POST /store/carts/:id", () => {
|
|||||||
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
expect(LineItemServiceMock.generate).toHaveBeenCalledWith(
|
||||||
IdMap.getId("fail"),
|
IdMap.getId("fail"),
|
||||||
IdMap.getId("testRegion"),
|
IdMap.getId("testRegion"),
|
||||||
3
|
3,
|
||||||
|
undefined // no metdata
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export default new mongoose.Schema({
|
|||||||
title: { type: String, required: true },
|
title: { type: String, required: true },
|
||||||
description: { type: String },
|
description: { type: String },
|
||||||
thumbnail: { type: String },
|
thumbnail: { type: String },
|
||||||
|
is_giftcard: { type: Boolean, default: false },
|
||||||
|
|
||||||
// mongoose doesn't allow multi-type validation but this field allows both
|
// mongoose doesn't allow multi-type validation but this field allows both
|
||||||
// an object containing:
|
// an object containing:
|
||||||
|
|||||||
@@ -106,6 +106,11 @@ const eur10us12 = {
|
|||||||
title: "EUR10US-12",
|
title: "EUR10US-12",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const giftCardVar = {
|
||||||
|
_id: IdMap.getId("giftCardVar"),
|
||||||
|
title: "100 USD",
|
||||||
|
}
|
||||||
|
|
||||||
export const variants = {
|
export const variants = {
|
||||||
one: variant1,
|
one: variant1,
|
||||||
two: variant2,
|
two: variant2,
|
||||||
@@ -115,6 +120,7 @@ export const variants = {
|
|||||||
empty_variant: emptyVariant,
|
empty_variant: emptyVariant,
|
||||||
eur10us12: eur10us12,
|
eur10us12: eur10us12,
|
||||||
testVariant: testVariant,
|
testVariant: testVariant,
|
||||||
|
giftCard: giftCardVar,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ProductVariantServiceMock = {
|
export const ProductVariantServiceMock = {
|
||||||
@@ -129,6 +135,9 @@ export const ProductVariantServiceMock = {
|
|||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
retrieve: jest.fn().mockImplementation(variantId => {
|
retrieve: jest.fn().mockImplementation(variantId => {
|
||||||
|
if (variantId === IdMap.getId("giftCardVar")) {
|
||||||
|
return Promise.resolve(variants.giftCard)
|
||||||
|
}
|
||||||
if (variantId === "1") {
|
if (variantId === "1") {
|
||||||
return Promise.resolve(variant1)
|
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"))
|
return Promise.reject(new Error("Not found"))
|
||||||
}),
|
}),
|
||||||
delete: jest.fn().mockReturnValue(Promise.resolve()),
|
delete: jest.fn().mockReturnValue(Promise.resolve()),
|
||||||
|
|||||||
@@ -96,6 +96,16 @@ export const ProductServiceMock = {
|
|||||||
list: jest.fn().mockImplementation(data => {
|
list: jest.fn().mockImplementation(data => {
|
||||||
// Used to retrieve a product based on a variant id see
|
// Used to retrieve a product based on a variant id see
|
||||||
// ProductVariantService.addOptionValue
|
// 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")) {
|
if (data.variants === IdMap.getId("testVariant")) {
|
||||||
return Promise.resolve([
|
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 {string} variantId - id of the line item variant
|
||||||
* @param {*} regionId - id of the cart region
|
* @param {*} regionId - id of the cart region
|
||||||
* @param {*} quantity - number of items
|
* @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 variant = await this.productVariantService_.retrieve(variantId)
|
||||||
const region = await this.regionService_.retrieve(regionId)
|
const region = await this.regionService_.retrieve(regionId)
|
||||||
|
|
||||||
@@ -104,7 +105,7 @@ class LineItemService extends BaseService {
|
|||||||
region._id
|
region._id
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
const line = {
|
||||||
title: product.title,
|
title: product.title,
|
||||||
description: variant.title,
|
description: variant.title,
|
||||||
quantity,
|
quantity,
|
||||||
@@ -116,6 +117,13 @@ class LineItemService extends BaseService {
|
|||||||
quantity: 1,
|
quantity: 1,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (product.is_giftcard) {
|
||||||
|
line.is_giftcard = true
|
||||||
|
line.metadata = metadata
|
||||||
|
}
|
||||||
|
|
||||||
|
return line
|
||||||
}
|
}
|
||||||
|
|
||||||
isEqual(line, match) {
|
isEqual(line, match) {
|
||||||
|
|||||||
Reference in new issue
Block a user