Merge branch 'integration/dummy-project' of github.com:medusajs/medusa into integration/dummy-project

This commit is contained in:
Sebastian Rindom committed 2020-07-06 11:52:25 +02:00
commit 9c91ae77d2
14 files changed
+478 -17

No files matched your search

@@ -143,6 +143,23 @@ export const carts = {
},
quantity: 10,
},
{
_id: IdMap.getId("itemToRemove"),
title: "merge line",
description: "This is a new line",
thumbnail: "test-img-yeah.com/thumb",
content: {
unit_price: 123,
variant: {
_id: IdMap.getId("can-cover"),
},
product: {
_id: IdMap.getId("product"),
},
quantity: 1,
},
quantity: 1,
},
],
shipping_address: {},
billing_address: {},
@@ -14,6 +14,7 @@ import { CartModelMock, carts } from "../../models/__mocks__/cart"
import { LineItemServiceMock } from "../__mocks__/line-item"
import { DiscountModelMock, discounts } from "../../models/__mocks__/discount"
import { DiscountServiceMock } from "../__mocks__/discount"
import idMap from "medusa-test-utils/dist/id-map"
describe("CartService", () => {
describe("retrieve", () => {
@@ -78,6 +79,46 @@ describe("CartService", () => {
})
})
describe("deleteMetadata", () => {
const cartService = new CartService({
cartModel: CartModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
jest.clearAllMocks()
})
it("calls updateOne with correct params", async () => {
const id = mongoose.Types.ObjectId()
await cartService.deleteMetadata(`${id}`, "metadata")
expect(EventBusServiceMock.emit).toHaveBeenCalledTimes(1)
expect(EventBusServiceMock.emit).toHaveBeenCalledWith(
"cart.updated",
expect.any(Object)
)
expect(CartModelMock.updateOne).toBeCalledTimes(1)
expect(CartModelMock.updateOne).toBeCalledWith(
{ _id: `${id}` },
{ $unset: { "metadata.metadata": "" } }
)
})
it("throw error on invalid key type", async () => {
const id = mongoose.Types.ObjectId()
try {
await cartService.deleteMetadata(`${id}`, 1234)
} catch (err) {
expect(err.message).toEqual(
"Key type is invalid. Metadata keys must be strings"
)
}
})
})
describe("create", () => {
const cartService = new CartService({
cartModel: CartModelMock,
@@ -297,6 +338,75 @@ describe("CartService", () => {
})
})
describe("removeLineItem", () => {
const cartService = new CartService({
cartModel: CartModelMock,
productVariantService: ProductVariantServiceMock,
lineItemService: LineItemServiceMock,
eventBusService: EventBusServiceMock,
})
beforeEach(() => {
jest.clearAllMocks()
})
it("successfully removes a line item", async () => {
await cartService.removeLineItem(
IdMap.getId("cartWithLine"),
IdMap.getId("itemToRemove")
)
expect(EventBusServiceMock.emit).toHaveBeenCalledTimes(1)
expect(EventBusServiceMock.emit).toHaveBeenCalledWith(
"cart.updated",
expect.any(Object)
)
expect(CartModelMock.updateOne).toHaveBeenCalledTimes(1)
expect(CartModelMock.updateOne).toHaveBeenCalledWith(
{
_id: IdMap.getId("cartWithLine"),
},
{
$pull: { items: { _id: IdMap.getId("itemToRemove") } },
}
)
})
it("successfully decrements quantity if more than 1", async () => {
await cartService.removeLineItem(
IdMap.getId("cartWithLine"),
IdMap.getId("existingLine")
)
expect(EventBusServiceMock.emit).toHaveBeenCalledTimes(1)
expect(EventBusServiceMock.emit).toHaveBeenCalledWith(
"cart.updated",
expect.any(Object)
)
expect(CartModelMock.updateOne).toHaveBeenCalledTimes(1)
expect(CartModelMock.updateOne).toHaveBeenCalledWith(
{
_id: IdMap.getId("cartWithLine"),
"items._id": IdMap.getId("existingLine"),
},
{
$set: { "items.$.quantity": 9 },
}
)
})
it("resolves if line item is not in cart", async () => {
await cartService.removeLineItem(
IdMap.getId("cartWithLine"),
IdMap.getId("nonExisting")
)
expect(CartModelMock.updateOne).toHaveBeenCalledTimes(0)
})
})
describe("updateLineItem", () => {
const cartService = new CartService({
cartModel: CartModelMock,
@@ -87,17 +87,23 @@ describe("ShippingOptionService", () => {
})
it("throw error on invalid key type", async () => {
const id = mongoose.Types.ObjectId()
expect(() => optionService.setMetadata(`${id}`, 1234, "nono")).toThrow(
"Key type is invalid. Metadata keys must be strings"
)
try {
optionService.setMetadata(IdMap.getId("test"), 1234, "nono")
} catch (error) {
expect(error.message).toEqual(
"Key type is invalid. Metadata keys must be strings"
)
}
})
it("throws error on invalid optionId type", async () => {
expect(() =>
try {
optionService.setMetadata("fakeProfileId", 1234, "nono")
).toThrow("The shippingOptionId could not be casted to an ObjectId")
} catch (error) {
expect(error.message).toEqual(
"The shippingOptionId could not be casted to an ObjectId"
)
}
})
})
@@ -146,7 +146,7 @@ describe("ShippingProfileService", () => {
it("throws error on invalid profileId type", async () => {
try {
await profileService.setMetadata("fakeProfileId", 1234, "nono")
await profileService.setMetadata("fakeProfileId", "1234", "nono")
} catch (err) {
expect(err.message).toEqual(
"The profileId could not be casted to an ObjectId"
+86 -1
View File
@@ -230,6 +230,62 @@ class CartService extends BaseService {
return c
}
/**
* Removes a line item from the cart.
* @param {string} cartId - the id of the cart that we will remove from
* @param {LineItem} lineItemId - the line item to remove.
* @retur {Promise} the result of the update operation
*/
async removeLineItem(cartId, lineItemId) {
const cart = await this.retrieve(cartId)
const itemToRemove = cart.items.find(line => line._id === lineItemId)
if (!itemToRemove) {
return Promise.resolve()
}
// If cart has more than one of those line items, we update the quantity
// instead of removing it
if (itemToRemove.quantity > 1) {
const newQuantity = itemToRemove.quantity - 1
return this.cartModel_
.updateOne(
{
_id: cartId,
"items._id": itemToRemove._id,
},
{
$set: {
"items.$.quantity": newQuantity,
},
}
)
.then(result => {
// Notify subscribers
this.eventBus_.emit(CartService.Events.UPDATED, result)
return result
})
}
return this.cartModel_
.updateOne(
{
_id: cartId,
},
{
$pull: {
items: { _id: itemToRemove._id },
},
}
)
.then(result => {
// Notify subscribers
this.eventBus_.emit(CartService.Events.UPDATED, result)
return result
})
}
/**
* Adds a line item to the cart.
* @param {string} cartId - the id of the cart that we will add to
@@ -929,7 +985,7 @@ class CartService extends BaseService {
* @param {string} value - value for metadata field.
* @return {Promise} resolves to the updated result.
*/
setMetadata(cartId, key, value) {
async setMetadata(cartId, key, value) {
const validatedId = this.validateId_(cartId)
if (typeof key !== "string") {
@@ -951,6 +1007,35 @@ class CartService extends BaseService {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
* Dedicated method to delete metadata for a cart.
* @param {string} cartId - the cart to delete metadata from.
* @param {string} key - key for metadata field
* @return {Promise} resolves to the updated result.
*/
async deleteMetadata(cartId, key) {
const validatedId = this.validateId_(cartId)
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
const keyPath = `metadata.${key}`
return this.cartModel_
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
.then(result => {
// Notify subscribers
this.eventBus_.emit(CartService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
}
export default CartService
+25 -1
View File
@@ -270,7 +270,7 @@ class CustomerService extends BaseService {
* @param {string} value - value for metadata field.
* @return {Promise} resolves to the updated result.
*/
setMetadata(customerId, key, value) {
async setMetadata(customerId, key, value) {
const validatedId = this.validateId_(customerId)
if (typeof key !== "string") {
@@ -287,6 +287,30 @@ class CustomerService extends BaseService {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
* Dedicated method to delete metadata for a customer.
* @param {string} customerId - the customer to delete metadata from.
* @param {string} key - key for metadata field
* @return {Promise} resolves to the updated result.
*/
async deleteMetadata(customerId, key) {
const validatedId = this.validateId_(customerId)
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
const keyPath = `metadata.${key}`
return this.customerModel_
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
}
export default CustomerService
+25 -1
View File
@@ -286,7 +286,7 @@ class DiscountService extends BaseService {
* @param {string} value - value for metadata field.
* @return {Promise} resolves to the updated result.
*/
setMetadata(discountId, key, value) {
async setMetadata(discountId, key, value) {
const validatedId = this.validateId_(discountId)
if (typeof key !== "string") {
@@ -303,6 +303,30 @@ class DiscountService extends BaseService {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
* Dedicated method to delete metadata for a discount.
* @param {string} discountId - the discount to delete metadata from.
* @param {string} key - key for metadata field
* @return {Promise} resolves to the updated result.
*/
async deleteMetadata(discountId, key) {
const validatedId = this.validateId_(discountId)
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
const keyPath = `metadata.${key}`
return this.discountModel_
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
}
export default DiscountService
+25 -1
View File
@@ -496,7 +496,7 @@ class OrderService extends BaseService {
* @param {string} value - value for metadata field.
* @return {Promise} resolves to the updated result.
*/
setMetadata(orderId, key, value) {
async setMetadata(orderId, key, value) {
const validatedId = this.validateId_(orderId)
if (typeof key !== "string") {
@@ -513,6 +513,30 @@ class OrderService extends BaseService {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
* Dedicated method to delete metadata for an order.
* @param {string} orderId - the order to delete metadata from.
* @param {string} key - key for metadata field
* @return {Promise} resolves to the updated result.
*/
async deleteMetadata(orderId, key) {
const validatedId = this.validateId_(orderId)
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
const keyPath = `metadata.${key}`
return this.orderModel_
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
}
export default OrderService
@@ -505,7 +505,7 @@ class ProductVariantService extends BaseService {
* @param {string} value - value for metadata field.
* @return {Promise} resolves to the updated result.
*/
setMetadata(variantId, key, value) {
async setMetadata(variantId, key, value) {
const validatedId = this.validateId_(variantId)
if (typeof key !== "string") {
@@ -522,6 +522,30 @@ class ProductVariantService extends BaseService {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
* Dedicated method to delete metadata for a product variant.
* @param {string} variantId - the product variant to delete metadata from.
* @param {string} key - key for metadata field
* @return {Promise} resolves to the updated result.
*/
async deleteMetadata(variantId, key) {
const validatedId = this.validateId_(variantId)
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
const keyPath = `metadata.${key}`
return this.productVariantModel_
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
}
export default ProductVariantService
+25 -1
View File
@@ -692,7 +692,7 @@ class ProductService extends BaseService {
* @param {string} value - value for metadata field.
* @return {Promise} resolves to the updated result.
*/
setMetadata(productId, key, value) {
async setMetadata(productId, key, value) {
const validatedId = this.validateId_(productId)
if (typeof key !== "string") {
@@ -713,6 +713,30 @@ class ProductService extends BaseService {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
* Dedicated method to delete metadata for a product.
* @param {string} productId - the product to delete metadata from.
* @param {string} key - key for metadata field
* @return {Promise} resolves to the updated result.
*/
async deleteMetadata(productId, key) {
const validatedId = this.validateId_(productId)
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
const keyPath = `metadata.${key}`
return this.productModel_
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
}
export default ProductService
@@ -412,7 +412,7 @@ class ShippingOptionService extends BaseService {
* @param {string} value - value for metadata field.
* @return {Promise} resolves to the updated result.
*/
setMetadata(optionId, key, value) {
async setMetadata(optionId, key, value) {
const validatedId = this.validateId_(optionId)
if (typeof key !== "string") {
@@ -429,6 +429,30 @@ class ShippingOptionService extends BaseService {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
* Dedicated method to delete metadata for a shipping option.
* @param {string} optionId - the shipping option to delete metadata from.
* @param {string} key - key for metadata field
* @return {Promise} resolves to the updated result.
*/
async deleteMetadata(optionId, key) {
const validatedId = this.validateId_(optionId)
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
const keyPath = `metadata.${key}`
return this.optionModel_
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
}
export default ShippingOptionService
@@ -382,7 +382,7 @@ class ShippingProfileService extends BaseService {
* @param {string} value - value for metadata field.
* @return {Promise} resolves to the updated result.
*/
setMetadata(profileId, key, value) {
async setMetadata(profileId, key, value) {
const validatedId = this.validateId_(profileId)
if (typeof key !== "string") {
@@ -399,6 +399,30 @@ class ShippingProfileService extends BaseService {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
* Dedicated method to delete metadata for a shipping profile.
* @param {string} profileId - the shipping profile to delete metadata from.
* @param {string} key - key for metadata field
* @return {Promise} resolves to the updated result.
*/
async deleteMetadata(profileId, key) {
const validatedId = this.validateId_(profileId)
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
const keyPath = `metadata.${key}`
return this.profileModel_
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
}
export default ShippingProfileService
+25 -1
View File
@@ -279,7 +279,7 @@ class UserService extends BaseService {
* @param {string} value - value for metadata field.
* @return {Promise} resolves to the updated result.
*/
setMetadata(userId, key, value) {
async setMetadata(userId, key, value) {
const validatedId = this.validateId_(userId)
if (typeof key !== "string") {
@@ -296,6 +296,30 @@ class UserService extends BaseService {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
* Dedicated method to delete metadata for a user.
* @param {string} userId - the user to delete metadata from.
* @param {string} key - key for metadata field
* @return {Promise} resolves to the updated result.
*/
async deleteMetadata(userId, key) {
const validatedId = this.validateId_(userId)
if (typeof key !== "string") {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Key type is invalid. Metadata keys must be strings"
)
}
const keyPath = `metadata.${key}`
return this.userModel_
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
}
export default UserService