cart service: updateShippingAddress, updateBillingAdress, updateEmail; address validator in core utils

This commit is contained in:
Sebastian Rindom
2020-02-05 10:20:58 +01:00
parent eed979b9d9
commit cb727c8689
4 changed files with 231 additions and 2 deletions

View File

@@ -2,4 +2,18 @@ import Joi from "@hapi/joi"
Joi.objectId = require("joi-objectid")(Joi)
Joi.address = () => {
return Joi.object().keys({
first_name: Joi.string().required(),
last_name: Joi.string().required(),
address_1: Joi.string().required(),
address_2: Joi.string(),
city: Joi.string().required(),
country_code: Joi.string().required(),
province: Joi.string(),
postal_code: Joi.string().required(),
metadata: Joi.object()
})
}
export default Joi

View File

@@ -6,8 +6,8 @@ import mongoose from "mongoose"
export default new mongoose.Schema({
first_name: { type: String, required: true },
last_name: { type: String, required: true },
address1: { type: String, required: true },
address2: { type: String },
address_1: { type: String, required: true },
address_2: { type: String },
city: { type: String, required: true },
country_code: { type: String, required: true },
province: { type: String },

View File

@@ -256,4 +256,153 @@ describe("CartService", () => {
}
})
})
describe("updateEmail", () => {
const cartService = new CartService({
cartModel: CartModelMock,
})
beforeEach(() => {
jest.clearAllMocks()
})
it("successfully updates an email", async () => {
await cartService.updateEmail(
IdMap.getId("emptyCart"),
"test@testdom.com"
)
expect(CartModelMock.updateOne).toHaveBeenCalledTimes(1)
expect(CartModelMock.updateOne).toHaveBeenCalledWith(
{
_id: IdMap.getId("emptyCart"),
},
{
$set: { email: "test@testdom.com" },
}
)
})
it("throws on invalid email", async () => {
try {
await cartService.updateEmail(IdMap.getId("emptyCart"), "test@test")
} catch (err) {
expect(err.message).toEqual("The email is not valid")
}
expect(CartModelMock.updateOne).toHaveBeenCalledTimes(0)
})
})
describe("updateBillingAddress", () => {
const cartService = new CartService({
cartModel: CartModelMock,
})
beforeEach(() => {
jest.clearAllMocks()
})
it("successfully updates billing address", async () => {
const address = {
first_name: "LeBron",
last_name: "James",
address_1: "24 Dunks Drive",
city: "Los Angeles",
country_code: "US",
province: "CA",
postal_code: "93011",
}
await cartService.updateBillingAddress(IdMap.getId("emptyCart"), address)
expect(CartModelMock.updateOne).toHaveBeenCalledTimes(1)
expect(CartModelMock.updateOne).toHaveBeenCalledWith(
{
_id: IdMap.getId("emptyCart"),
},
{
$set: { billing_address: address },
}
)
})
it("throws on invalid address", async () => {
const address = {
last_name: "James",
address_1: "24 Dunks Drive",
city: "Los Angeles",
country_code: "US",
province: "CA",
postal_code: "93011",
}
try {
await cartService.updateBillingAddress(
IdMap.getId("emptyCart"),
address
)
} catch (err) {
expect(err.message).toEqual("The address is not valid")
}
expect(CartModelMock.updateOne).toHaveBeenCalledTimes(0)
})
})
describe("updateShippingAddress", () => {
const cartService = new CartService({
cartModel: CartModelMock,
})
beforeEach(() => {
jest.clearAllMocks()
})
it("successfully updates billing address", async () => {
const address = {
first_name: "LeBron",
last_name: "James",
address_1: "24 Dunks Drive",
city: "Los Angeles",
country_code: "US",
province: "CA",
postal_code: "93011",
}
await cartService.updateShippingAddress(IdMap.getId("emptyCart"), address)
expect(CartModelMock.updateOne).toHaveBeenCalledTimes(1)
expect(CartModelMock.updateOne).toHaveBeenCalledWith(
{
_id: IdMap.getId("emptyCart"),
},
{
$set: { shipping_address: address },
}
)
})
it("throws on invalid address", async () => {
const address = {
last_name: "James",
address_1: "24 Dunks Drive",
city: "Los Angeles",
country_code: "US",
province: "CA",
postal_code: "93011",
}
try {
await cartService.updateShippingAddress(
IdMap.getId("emptyCart"),
address
)
} catch (err) {
expect(err.message).toEqual("The address is not valid")
}
expect(CartModelMock.updateOne).toHaveBeenCalledTimes(0)
})
})
})

View File

@@ -240,6 +240,72 @@ class CartService extends BaseService {
)
}
/**
* Sets the email of a cart
* @param {string} cartId - the id of the cart to add email to
* @param {string} email - the email to add to cart
* @return {Promise} the result of the update operation
*/
updateEmail(cartId, email) {
const schema = Validator.string()
.email()
.required()
const { value, error } = schema.validate(email)
if (error) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"The email is not valid"
)
}
return this.cartModel_.updateOne(
{
_id: cartId,
},
{
$set: { email: value },
}
)
}
updateBillingAddress(cartId, address) {
const { value, error } = Validator.address().validate(address)
if (error) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"The address is not valid"
)
}
return this.cartModel_.updateOne(
{
_id: cartId,
},
{
$set: { billing_address: value },
}
)
}
updateShippingAddress(cartId, address) {
const { value, error } = Validator.address().validate(address)
if (error) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"The address is not valid"
)
}
return this.cartModel_.updateOne(
{
_id: cartId,
},
{
$set: { shipping_address: value },
}
)
}
/**
* Dedicated method to set metadata for a cart.
* To ensure that plugins does not overwrite each