Adds deleteMetadata to all services (#78)
This commit is contained in:
1 parent
2491bd6779
commit
70ba668202
13 files changed
+335
-17
No files matched your search
@@ -78,6 +78,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,
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -929,7 +929,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 +951,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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -406,7 +406,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") {
|
||||
@@ -423,6 +423,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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user