fix(medusa): AddressPayload typing and removes Joi validation from CustomerService (#1520)
This commit is contained in:
committed by
GitHub
parent
02eab5ee86
commit
8dd27ecb7e
@@ -144,27 +144,6 @@ describe("CustomerService", () => {
|
||||
has_account: true,
|
||||
})
|
||||
})
|
||||
|
||||
it("fails if email is in incorrect format", async () => {
|
||||
await expect(
|
||||
customerService.create({
|
||||
email: "olivermedusa.com",
|
||||
})
|
||||
).rejects.toThrow("The email is not valid")
|
||||
})
|
||||
|
||||
it("fails if billing address is in incorrect format", async () => {
|
||||
await expect(
|
||||
customerService.create({
|
||||
email: "oliver@medusa.com",
|
||||
first_name: "Oliver",
|
||||
last_name: "Juhl",
|
||||
billing_address: {
|
||||
first_name: 1234,
|
||||
},
|
||||
})
|
||||
).rejects.toThrow("The address is not valid")
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
@@ -328,22 +307,6 @@ describe("CustomerService", () => {
|
||||
phone: "+1 (222) 333 4444",
|
||||
})
|
||||
})
|
||||
|
||||
it("throws on invalid address", async () => {
|
||||
await expect(
|
||||
customerService.updateAddress(
|
||||
IdMap.getId("ironman"),
|
||||
IdMap.getId("hollywood-boulevard"),
|
||||
{
|
||||
first_name: "Tony",
|
||||
last_name: "Stark",
|
||||
country_code: "us",
|
||||
unknown: "key",
|
||||
address_1: "Hollywood",
|
||||
}
|
||||
)
|
||||
).rejects.toThrow("The address is not valid")
|
||||
})
|
||||
})
|
||||
|
||||
describe("removeAddress", () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import jwt from "jsonwebtoken"
|
||||
import _ from "lodash"
|
||||
import { MedusaError, Validator } from "medusa-core-utils"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import Scrypt from "scrypt-kdf"
|
||||
import { Brackets, ILike } from "typeorm"
|
||||
@@ -55,36 +55,6 @@ class CustomerService extends BaseService {
|
||||
return cloned
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to validate customer email.
|
||||
* @param {string} email - email to validate
|
||||
* @return {string} the validated email
|
||||
*/
|
||||
validateEmail_(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 value.toLowerCase()
|
||||
}
|
||||
|
||||
validateBillingAddress_(address) {
|
||||
const { value, error } = Validator.address().validate(address)
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"The address is not valid"
|
||||
)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a JSON Web token, that will be sent to a customer, that wishes to
|
||||
* reset password.
|
||||
@@ -323,12 +293,7 @@ class CustomerService extends BaseService {
|
||||
this.customerRepository_
|
||||
)
|
||||
|
||||
const { email, billing_address, password } = customer
|
||||
customer.email = this.validateEmail_(email)
|
||||
|
||||
if (billing_address) {
|
||||
customer.billing_address = this.validateBillingAddress_(billing_address)
|
||||
}
|
||||
const { email, password } = customer
|
||||
|
||||
const existing = await this.retrieveByEmail(email).catch(
|
||||
(err) => undefined
|
||||
@@ -389,7 +354,6 @@ class CustomerService extends BaseService {
|
||||
const customer = await this.retrieve(customerId)
|
||||
|
||||
const {
|
||||
email,
|
||||
password,
|
||||
metadata,
|
||||
billing_address,
|
||||
@@ -402,10 +366,6 @@ class CustomerService extends BaseService {
|
||||
customer.metadata = this.setMetadata_(customer, metadata)
|
||||
}
|
||||
|
||||
if (email) {
|
||||
customer.email = this.validateEmail_(email)
|
||||
}
|
||||
|
||||
if ("billing_address_id" in update || "billing_address" in update) {
|
||||
const address = billing_address_id || billing_address
|
||||
if (typeof address !== "undefined") {
|
||||
@@ -488,8 +448,6 @@ class CustomerService extends BaseService {
|
||||
where: { id: addressId, customer_id: customerId },
|
||||
})
|
||||
|
||||
this.validateBillingAddress_(address)
|
||||
|
||||
for (const [key, value] of Object.entries(address)) {
|
||||
toUpdate[key] = value
|
||||
}
|
||||
@@ -529,7 +487,6 @@ class CustomerService extends BaseService {
|
||||
const customer = await this.retrieve(customerId, {
|
||||
relations: ["shipping_addresses"],
|
||||
})
|
||||
this.validateBillingAddress_(address)
|
||||
|
||||
const shouldAdd = !customer.shipping_addresses.find(
|
||||
(a) =>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { MedusaError, Validator } from "medusa-core-utils"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import { Brackets } from "typeorm"
|
||||
|
||||
@@ -140,42 +140,6 @@ class OrderService extends BaseService {
|
||||
return rawId
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to validate order addresses. Can be used to both
|
||||
* validate shipping and billing address.
|
||||
* @param {Address} address - the address to validate
|
||||
* @return {Address} the validated address
|
||||
*/
|
||||
validateAddress_(address) {
|
||||
const { value, error } = Validator.address().validate(address)
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"The address is not valid"
|
||||
)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to validate email.
|
||||
* @param {string} email - the email to vaildate
|
||||
* @return {string} the validate email
|
||||
*/
|
||||
validateEmail_(email) {
|
||||
const schema = Validator.string().email()
|
||||
const { value, error } = schema.validate(email)
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
"The email is not valid"
|
||||
)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} selector - the query object for find
|
||||
* @param {Object} config - the config to be used for find
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { Transform, Type } from "class-transformer"
|
||||
import { IsDate, IsNumber, IsOptional, IsString } from "class-validator"
|
||||
import "reflect-metadata"
|
||||
import {
|
||||
BaseEntity,
|
||||
FindManyOptions,
|
||||
FindOperator,
|
||||
OrderByCondition,
|
||||
} from "typeorm"
|
||||
IsDate,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from "class-validator"
|
||||
import "reflect-metadata"
|
||||
import { FindManyOptions, OrderByCondition } from "typeorm"
|
||||
import { transformDate } from "../utils/validators/date-transform"
|
||||
|
||||
export type PartialPick<T, K extends keyof T> = {
|
||||
@@ -131,73 +132,84 @@ export class NumericalComparisonOperator {
|
||||
export class AddressPayload {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
first_name: string
|
||||
first_name?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
last_name: string
|
||||
last_name?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
phone: string
|
||||
phone?: string
|
||||
|
||||
@IsOptional()
|
||||
metadata: object
|
||||
@IsObject()
|
||||
metadata?: Record<string, unknown>
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
company: string
|
||||
company?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
address_1: string
|
||||
address_1?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
address_2: string
|
||||
address_2?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
city: string
|
||||
city?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
country_code: string
|
||||
country_code?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
province: string
|
||||
province?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
postal_code: string
|
||||
postal_code?: string
|
||||
}
|
||||
|
||||
export class AddressCreatePayload {
|
||||
@IsString()
|
||||
first_name: string
|
||||
|
||||
@IsString()
|
||||
last_name: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
phone: string
|
||||
|
||||
@IsOptional()
|
||||
metadata: object
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
company: string
|
||||
|
||||
@IsString()
|
||||
address_1: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
address_2: string
|
||||
|
||||
@IsString()
|
||||
city: string
|
||||
|
||||
@IsString()
|
||||
country_code: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
province: string
|
||||
|
||||
@IsString()
|
||||
postal_code: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user