From 1a37b27ca0c4e16ca25857761533c6e928677556 Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Fri, 15 Oct 2021 11:33:45 +0300 Subject: [PATCH] Fix packages/medusa/src/services/customer.js eslint errors (#552) --- .eslintignore | 1 - packages/medusa/src/services/customer.js | 67 ++++++++++++------------ 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/.eslintignore b/.eslintignore index 88900efb40..efd4a28e6a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,7 +2,6 @@ /packages/medusa/src/services/cart.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/fulfillment-provider.js /packages/medusa/src/services/idempotency-key.js diff --git a/packages/medusa/src/services/customer.js b/packages/medusa/src/services/customer.js index 850ddb27f7..8955cb49a8 100644 --- a/packages/medusa/src/services/customer.js +++ b/packages/medusa/src/services/customer.js @@ -7,7 +7,7 @@ import { Brackets, ILike } from "typeorm" /** * Provides layer to manipulate customers. - * @implements BaseService + * @implements {BaseService} */ class CustomerService extends BaseService { static Events = { @@ -60,9 +60,7 @@ class CustomerService extends BaseService { * @return {string} the validated email */ validateEmail_(email) { - const schema = Validator.string() - .email() - .required() + const schema = Validator.string().email().required() const { value, error } = schema.validate(email) if (error) { 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, * which is always 15 minutes. * @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) { const customer = await this.retrieve(customerId) @@ -122,6 +120,7 @@ class CustomerService extends BaseService { /** * @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 */ async list(selector = {}, config = { relations: [], skip: 0, take: 50 }) { @@ -144,11 +143,11 @@ class CustomerService extends BaseService { delete where.first_name delete where.last_name - query.where = qb => { + query.where = (qb) => { qb.where(where) qb.andWhere( - new Brackets(qb => { + new Brackets((qb) => { qb.where({ email: ILike(`%${q}%`) }) .orWhere({ first_name: ILike(`%${q}%`) }) .orWhere({ last_name: ILike(`%${q}%`) }) @@ -183,11 +182,11 @@ class CustomerService extends BaseService { delete where.first_name delete where.last_name - query.where = qb => { + query.where = (qb) => { qb.where(where) qb.andWhere( - new Brackets(qb => { + new Brackets((qb) => { qb.where({ email: ILike(`%${q}%`) }) .orWhere({ first_name: ILike(`%${q}%`) }) .orWhere({ last_name: ILike(`%${q}%`) }) @@ -214,6 +213,7 @@ class CustomerService extends BaseService { /** * Gets a customer by id. * @param {string} customerId - the id of the customer to get. + * @param {Object} config - the config object containing query settings * @return {Promise} the customer document. */ async retrieve(customerId, config = {}) { @@ -238,6 +238,7 @@ class CustomerService extends BaseService { /** * Gets a customer by email. * @param {string} email - the email of the customer to get. + * @param {Object} config - the config object containing query settings * @return {Promise} the customer document. */ async retrieveByEmail(email, config = {}) { @@ -261,6 +262,7 @@ class CustomerService extends BaseService { /** * Gets a customer by phone. * @param {string} phone - the phone of the customer to get. + * @param {Object} config - the config object containing query settings * @return {Promise} the customer document. */ async retrieveByPhone(phone, config = {}) { @@ -284,7 +286,7 @@ class CustomerService extends BaseService { /** * Hashes a password * @param {string} password - the value to hash - * @return hashed password + * @return {string} hashed password */ async hashPassword_(password) { 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 */ async create(customer) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const customerRepository = manager.getCustomRepository( this.customerRepository_ ) @@ -312,7 +314,9 @@ class CustomerService extends BaseService { 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) { throw new MedusaError( @@ -353,13 +357,13 @@ class CustomerService extends BaseService { /** * 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 * @param {object} update - an object with the update values. * @return {Promise} resolves to the update result. */ async update(customerId, update) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const customerRepository = manager.getCustomRepository( this.customerRepository_ ) @@ -367,15 +371,7 @@ class CustomerService extends BaseService { const customer = await this.retrieve(customerId) - const { - email, - password, - password_hash, - billing_address, - billing_address_id, - metadata, - ...rest - } = update + const { email, password, metadata, ...rest } = update if (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 {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 */ async updateBillingAddress_(customer, addressOrId, addrRepo) { @@ -443,7 +440,7 @@ class CustomerService extends BaseService { } async updateAddress(customerId, addressId, address) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const addressRepo = manager.getCustomRepository(this.addressRepository_) address.country_code = address.country_code.toLowerCase() @@ -464,7 +461,7 @@ class CustomerService extends BaseService { } async removeAddress(customerId, addressId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const addressRepo = manager.getCustomRepository(this.addressRepository_) // 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 }, }) - if (!address) return Promise.resolve() + if (!address) { + return Promise.resolve() + } await addressRepo.softRemove(address) @@ -481,7 +480,7 @@ class CustomerService extends BaseService { } async addAddress(customerId, address) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const addressRepository = manager.getCustomRepository( this.addressRepository_ ) @@ -493,8 +492,8 @@ class CustomerService extends BaseService { }) this.validateBillingAddress_(address) - let shouldAdd = !customer.shipping_addresses.find( - a => + const shouldAdd = !customer.shipping_addresses.find( + (a) => a.country_code.toLowerCase() === address.country_code.toLowerCase() && a.address_1 === address.address_1 && a.address_2 === address.address_2 && @@ -526,13 +525,15 @@ class CustomerService extends BaseService { * @return {Promise} the result of the delete operation. */ async delete(customerId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const customerRepo = manager.getCustomRepository(this.customerRepository_) // Should not fail, if user does not exist, since delete is idempotent const customer = await customerRepo.findOne({ where: { id: customerId } }) - if (!customer) return Promise.resolve() + if (!customer) { + return Promise.resolve() + } await customerRepo.softRemove(customer)