fix(medusa): AddressPayload typing and removes Joi validation from CustomerService (#1520)

This commit is contained in:
Kasper Fabricius Kristensen authored and GitHub committed 2022-05-16 13:43:59 +02:00
1 parent 02eab5ee86
commit 8dd27ecb7e
4 files changed
+33 -137

No files matched your search

@@ -144,27 +144,6 @@ describe("CustomerService", () => {
has_account: true, 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", () => { describe("update", () => {
@@ -328,22 +307,6 @@ describe("CustomerService", () => {
phone: "+1 (222) 333 4444", 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", () => { describe("removeAddress", () => {
+2 -45
View File
@@ -1,6 +1,6 @@
import jwt from "jsonwebtoken" import jwt from "jsonwebtoken"
import _ from "lodash" import _ from "lodash"
import { MedusaError, Validator } from "medusa-core-utils" import { MedusaError } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces" import { BaseService } from "medusa-interfaces"
import Scrypt from "scrypt-kdf" import Scrypt from "scrypt-kdf"
import { Brackets, ILike } from "typeorm" import { Brackets, ILike } from "typeorm"
@@ -55,36 +55,6 @@ class CustomerService extends BaseService {
return cloned 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 * Generate a JSON Web token, that will be sent to a customer, that wishes to
* reset password. * reset password.
@@ -323,12 +293,7 @@ class CustomerService extends BaseService {
this.customerRepository_ this.customerRepository_
) )
const { email, billing_address, password } = customer const { email, password } = customer
customer.email = this.validateEmail_(email)
if (billing_address) {
customer.billing_address = this.validateBillingAddress_(billing_address)
}
const existing = await this.retrieveByEmail(email).catch( const existing = await this.retrieveByEmail(email).catch(
(err) => undefined (err) => undefined
@@ -389,7 +354,6 @@ class CustomerService extends BaseService {
const customer = await this.retrieve(customerId) const customer = await this.retrieve(customerId)
const { const {
email,
password, password,
metadata, metadata,
billing_address, billing_address,
@@ -402,10 +366,6 @@ class CustomerService extends BaseService {
customer.metadata = this.setMetadata_(customer, metadata) customer.metadata = this.setMetadata_(customer, metadata)
} }
if (email) {
customer.email = this.validateEmail_(email)
}
if ("billing_address_id" in update || "billing_address" in update) { if ("billing_address_id" in update || "billing_address" in update) {
const address = billing_address_id || billing_address const address = billing_address_id || billing_address
if (typeof address !== "undefined") { if (typeof address !== "undefined") {
@@ -488,8 +448,6 @@ class CustomerService extends BaseService {
where: { id: addressId, customer_id: customerId }, where: { id: addressId, customer_id: customerId },
}) })
this.validateBillingAddress_(address)
for (const [key, value] of Object.entries(address)) { for (const [key, value] of Object.entries(address)) {
toUpdate[key] = value toUpdate[key] = value
} }
@@ -529,7 +487,6 @@ class CustomerService extends BaseService {
const customer = await this.retrieve(customerId, { const customer = await this.retrieve(customerId, {
relations: ["shipping_addresses"], relations: ["shipping_addresses"],
}) })
this.validateBillingAddress_(address)
const shouldAdd = !customer.shipping_addresses.find( const shouldAdd = !customer.shipping_addresses.find(
(a) => (a) =>
+1 -37
View File
@@ -1,4 +1,4 @@
import { MedusaError, Validator } from "medusa-core-utils" import { MedusaError } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces" import { BaseService } from "medusa-interfaces"
import { Brackets } from "typeorm" import { Brackets } from "typeorm"
@@ -140,42 +140,6 @@ class OrderService extends BaseService {
return rawId 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} selector - the query object for find
* @param {Object} config - the config to be used for find * @param {Object} config - the config to be used for find
+30 -18
View File
@@ -1,12 +1,13 @@
import { Transform, Type } from "class-transformer" import { Transform, Type } from "class-transformer"
import { IsDate, IsNumber, IsOptional, IsString } from "class-validator"
import "reflect-metadata"
import { import {
BaseEntity, IsDate,
FindManyOptions, IsNumber,
FindOperator, IsObject,
OrderByCondition, IsOptional,
} from "typeorm" IsString,
} from "class-validator"
import "reflect-metadata"
import { FindManyOptions, OrderByCondition } from "typeorm"
import { transformDate } from "../utils/validators/date-transform" import { transformDate } from "../utils/validators/date-transform"
export type PartialPick<T, K extends keyof T> = { export type PartialPick<T, K extends keyof T> = {
@@ -131,73 +132,84 @@ export class NumericalComparisonOperator {
export class AddressPayload { export class AddressPayload {
@IsOptional() @IsOptional()
@IsString() @IsString()
first_name: string first_name?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
last_name: string last_name?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
phone: string phone?: string
@IsOptional() @IsOptional()
metadata: object @IsObject()
metadata?: Record<string, unknown>
@IsOptional() @IsOptional()
@IsString() @IsString()
company: string company?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
address_1: string address_1?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
address_2: string address_2?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
city: string city?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
country_code: string country_code?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
province: string province?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
postal_code: string postal_code?: string
} }
export class AddressCreatePayload { export class AddressCreatePayload {
@IsString() @IsString()
first_name: string first_name: string
@IsString() @IsString()
last_name: string last_name: string
@IsOptional() @IsOptional()
@IsString() @IsString()
phone: string phone: string
@IsOptional() @IsOptional()
metadata: object metadata: object
@IsOptional() @IsOptional()
@IsString() @IsString()
company: string company: string
@IsString() @IsString()
address_1: string address_1: string
@IsOptional() @IsOptional()
@IsString() @IsString()
address_2: string address_2: string
@IsString() @IsString()
city: string city: string
@IsString() @IsString()
country_code: string country_code: string
@IsOptional() @IsOptional()
@IsString() @IsString()
province: string province: string
@IsString() @IsString()
postal_code: string postal_code: string
} }