Fix packages/medusa/src/services/customer.js eslint errors (#552)

This commit is contained in:
Vladyslav authored and GitHub committed 2021-10-15 10:33:45 +02:00
1 parent 38559b5454
commit 1a37b27ca0
2 files changed
+34 -34

No files matched your search

-1
View File
@@ -2,7 +2,6 @@
/packages/medusa/src/services/cart.js /packages/medusa/src/services/cart.js
/packages/medusa/src/services/claim-item.js /packages/medusa/src/services/claim-item.js
/packages/medusa/src/services/customer.js
/packages/medusa/src/services/event-bus.js /packages/medusa/src/services/event-bus.js
/packages/medusa/src/services/fulfillment-provider.js /packages/medusa/src/services/fulfillment-provider.js
/packages/medusa/src/services/idempotency-key.js /packages/medusa/src/services/idempotency-key.js
+34 -33
View File
@@ -7,7 +7,7 @@ import { Brackets, ILike } from "typeorm"
/** /**
* Provides layer to manipulate customers. * Provides layer to manipulate customers.
* @implements BaseService * @implements {BaseService}
*/ */
class CustomerService extends BaseService { class CustomerService extends BaseService {
static Events = { static Events = {
@@ -60,9 +60,7 @@ class CustomerService extends BaseService {
* @return {string} the validated email * @return {string} the validated email
*/ */
validateEmail_(email) { validateEmail_(email) {
const schema = Validator.string() const schema = Validator.string().email().required()
.email()
.required()
const { value, error } = schema.validate(email) const { value, error } = schema.validate(email)
if (error) { if (error) {
throw new MedusaError( throw new MedusaError(
@@ -93,7 +91,7 @@ class CustomerService extends BaseService {
* secret a long side a payload with userId and the expiry time for the token, * secret a long side a payload with userId and the expiry time for the token,
* which is always 15 minutes. * which is always 15 minutes.
* @param {string} customerId - the customer to reset the password for * @param {string} customerId - the customer to reset the password for
* @returns {string} the generated JSON web token * @return {string} the generated JSON web token
*/ */
async generateResetPasswordToken(customerId) { async generateResetPasswordToken(customerId) {
const customer = await this.retrieve(customerId) const customer = await this.retrieve(customerId)
@@ -122,6 +120,7 @@ class CustomerService extends BaseService {
/** /**
* @param {Object} selector - the query object for find * @param {Object} selector - the query object for find
* @param {Object} config - the config object containing query settings
* @return {Promise} the result of the find operation * @return {Promise} the result of the find operation
*/ */
async list(selector = {}, config = { relations: [], skip: 0, take: 50 }) { async list(selector = {}, config = { relations: [], skip: 0, take: 50 }) {
@@ -144,11 +143,11 @@ class CustomerService extends BaseService {
delete where.first_name delete where.first_name
delete where.last_name delete where.last_name
query.where = qb => { query.where = (qb) => {
qb.where(where) qb.where(where)
qb.andWhere( qb.andWhere(
new Brackets(qb => { new Brackets((qb) => {
qb.where({ email: ILike(`%${q}%`) }) qb.where({ email: ILike(`%${q}%`) })
.orWhere({ first_name: ILike(`%${q}%`) }) .orWhere({ first_name: ILike(`%${q}%`) })
.orWhere({ last_name: ILike(`%${q}%`) }) .orWhere({ last_name: ILike(`%${q}%`) })
@@ -183,11 +182,11 @@ class CustomerService extends BaseService {
delete where.first_name delete where.first_name
delete where.last_name delete where.last_name
query.where = qb => { query.where = (qb) => {
qb.where(where) qb.where(where)
qb.andWhere( qb.andWhere(
new Brackets(qb => { new Brackets((qb) => {
qb.where({ email: ILike(`%${q}%`) }) qb.where({ email: ILike(`%${q}%`) })
.orWhere({ first_name: ILike(`%${q}%`) }) .orWhere({ first_name: ILike(`%${q}%`) })
.orWhere({ last_name: ILike(`%${q}%`) }) .orWhere({ last_name: ILike(`%${q}%`) })
@@ -214,6 +213,7 @@ class CustomerService extends BaseService {
/** /**
* Gets a customer by id. * Gets a customer by id.
* @param {string} customerId - the id of the customer to get. * @param {string} customerId - the id of the customer to get.
* @param {Object} config - the config object containing query settings
* @return {Promise<Customer>} the customer document. * @return {Promise<Customer>} the customer document.
*/ */
async retrieve(customerId, config = {}) { async retrieve(customerId, config = {}) {
@@ -238,6 +238,7 @@ class CustomerService extends BaseService {
/** /**
* Gets a customer by email. * Gets a customer by email.
* @param {string} email - the email of the customer to get. * @param {string} email - the email of the customer to get.
* @param {Object} config - the config object containing query settings
* @return {Promise<Customer>} the customer document. * @return {Promise<Customer>} the customer document.
*/ */
async retrieveByEmail(email, config = {}) { async retrieveByEmail(email, config = {}) {
@@ -261,6 +262,7 @@ class CustomerService extends BaseService {
/** /**
* Gets a customer by phone. * Gets a customer by phone.
* @param {string} phone - the phone of the customer to get. * @param {string} phone - the phone of the customer to get.
* @param {Object} config - the config object containing query settings
* @return {Promise<Customer>} the customer document. * @return {Promise<Customer>} the customer document.
*/ */
async retrieveByPhone(phone, config = {}) { async retrieveByPhone(phone, config = {}) {
@@ -284,7 +286,7 @@ class CustomerService extends BaseService {
/** /**
* Hashes a password * Hashes a password
* @param {string} password - the value to hash * @param {string} password - the value to hash
* @return hashed password * @return {string} hashed password
*/ */
async hashPassword_(password) { async hashPassword_(password) {
const buf = await Scrypt.kdf(password, { logN: 1, r: 1, p: 1 }) const buf = await Scrypt.kdf(password, { logN: 1, r: 1, p: 1 })
@@ -300,7 +302,7 @@ class CustomerService extends BaseService {
* @return {Promise} the result of create * @return {Promise} the result of create
*/ */
async create(customer) { async create(customer) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const customerRepository = manager.getCustomRepository( const customerRepository = manager.getCustomRepository(
this.customerRepository_ this.customerRepository_
) )
@@ -312,7 +314,9 @@ class CustomerService extends BaseService {
customer.billing_address = this.validateBillingAddress_(billing_address) customer.billing_address = this.validateBillingAddress_(billing_address)
} }
const existing = await this.retrieveByEmail(email).catch(err => undefined) const existing = await this.retrieveByEmail(email).catch(
(err) => undefined
)
if (existing && existing.has_account) { if (existing && existing.has_account) {
throw new MedusaError( throw new MedusaError(
@@ -353,13 +357,13 @@ class CustomerService extends BaseService {
/** /**
* Updates a customer. * Updates a customer.
* @param {string} variantId - the id of the variant. Must be a string that * @param {string} customerId - the id of the variant. Must be a string that
* can be casted to an ObjectId * can be casted to an ObjectId
* @param {object} update - an object with the update values. * @param {object} update - an object with the update values.
* @return {Promise} resolves to the update result. * @return {Promise} resolves to the update result.
*/ */
async update(customerId, update) { async update(customerId, update) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const customerRepository = manager.getCustomRepository( const customerRepository = manager.getCustomRepository(
this.customerRepository_ this.customerRepository_
) )
@@ -367,15 +371,7 @@ class CustomerService extends BaseService {
const customer = await this.retrieve(customerId) const customer = await this.retrieve(customerId)
const { const { email, password, metadata, ...rest } = update
email,
password,
password_hash,
billing_address,
billing_address_id,
metadata,
...rest
} = update
if (metadata) { if (metadata) {
customer.metadata = this.setMetadata_(customer, metadata) customer.metadata = this.setMetadata_(customer, metadata)
@@ -408,9 +404,10 @@ class CustomerService extends BaseService {
} }
/** /**
* Updates the customers's billing address. * Updates the customers' billing address.
* @param {Customer} customer - the Customer to update * @param {Customer} customer - the Customer to update
* @param {object} address - the value to set the billing address to * @param {Object|string} addressOrId - the value to set the billing address to
* @param {Object} addrRepo - address repository
* @return {Promise} the result of the update operation * @return {Promise} the result of the update operation
*/ */
async updateBillingAddress_(customer, addressOrId, addrRepo) { async updateBillingAddress_(customer, addressOrId, addrRepo) {
@@ -443,7 +440,7 @@ class CustomerService extends BaseService {
} }
async updateAddress(customerId, addressId, address) { async updateAddress(customerId, addressId, address) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const addressRepo = manager.getCustomRepository(this.addressRepository_) const addressRepo = manager.getCustomRepository(this.addressRepository_)
address.country_code = address.country_code.toLowerCase() address.country_code = address.country_code.toLowerCase()
@@ -464,7 +461,7 @@ class CustomerService extends BaseService {
} }
async removeAddress(customerId, addressId) { async removeAddress(customerId, addressId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const addressRepo = manager.getCustomRepository(this.addressRepository_) const addressRepo = manager.getCustomRepository(this.addressRepository_)
// Should not fail, if user does not exist, since delete is idempotent // Should not fail, if user does not exist, since delete is idempotent
@@ -472,7 +469,9 @@ class CustomerService extends BaseService {
where: { id: addressId, customer_id: customerId }, where: { id: addressId, customer_id: customerId },
}) })
if (!address) return Promise.resolve() if (!address) {
return Promise.resolve()
}
await addressRepo.softRemove(address) await addressRepo.softRemove(address)
@@ -481,7 +480,7 @@ class CustomerService extends BaseService {
} }
async addAddress(customerId, address) { async addAddress(customerId, address) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const addressRepository = manager.getCustomRepository( const addressRepository = manager.getCustomRepository(
this.addressRepository_ this.addressRepository_
) )
@@ -493,8 +492,8 @@ class CustomerService extends BaseService {
}) })
this.validateBillingAddress_(address) this.validateBillingAddress_(address)
let shouldAdd = !customer.shipping_addresses.find( const shouldAdd = !customer.shipping_addresses.find(
a => (a) =>
a.country_code.toLowerCase() === address.country_code.toLowerCase() && a.country_code.toLowerCase() === address.country_code.toLowerCase() &&
a.address_1 === address.address_1 && a.address_1 === address.address_1 &&
a.address_2 === address.address_2 && a.address_2 === address.address_2 &&
@@ -526,13 +525,15 @@ class CustomerService extends BaseService {
* @return {Promise} the result of the delete operation. * @return {Promise} the result of the delete operation.
*/ */
async delete(customerId) { async delete(customerId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async (manager) => {
const customerRepo = manager.getCustomRepository(this.customerRepository_) const customerRepo = manager.getCustomRepository(this.customerRepository_)
// Should not fail, if user does not exist, since delete is idempotent // Should not fail, if user does not exist, since delete is idempotent
const customer = await customerRepo.findOne({ where: { id: customerId } }) const customer = await customerRepo.findOne({ where: { id: customerId } })
if (!customer) return Promise.resolve() if (!customer) {
return Promise.resolve()
}
await customerRepo.softRemove(customer) await customerRepo.softRemove(customer)