diff --git a/src/api/controllers/user/get-by-id.controller.js b/src/api/controllers/user/get-by-id.controller.js index 720a9a9..097301a 100644 --- a/src/api/controllers/user/get-by-id.controller.js +++ b/src/api/controllers/user/get-by-id.controller.js @@ -4,8 +4,8 @@ module.exports = class GetUserByIdController { } async execute(req, res) { try { - const { id } = req.params; - const user = await this.user.getById(id); + const { email } = req.body; + const user = await this.user.getByEmail(email); res.status(200).send(JSON.stringify(user)); } catch (error) { const errorObj = JSON.parse(error.message); diff --git a/src/api/controllers/user/put-user-by-id.controller.js b/src/api/controllers/user/put-user-by-id.controller.js index 853d250..0ebfd92 100644 --- a/src/api/controllers/user/put-user-by-id.controller.js +++ b/src/api/controllers/user/put-user-by-id.controller.js @@ -6,7 +6,7 @@ module.exports = class PutUserByIdController { try { const { id } = req.params; const user = await this.user.update(req.body, id); - res.status(204).send(JSON.stringify(user)); + res.status(200).send(JSON.stringify(user)); } catch (error) { const errorObj = JSON.parse(error.message); if (errorObj.statusCode) { diff --git a/src/api/database/models/order_product.model.js b/src/api/database/models/order_product.model.js index 7b9a3ff..0c34764 100644 --- a/src/api/database/models/order_product.model.js +++ b/src/api/database/models/order_product.model.js @@ -30,5 +30,5 @@ const Order_product = sequelize.define('order_product', { }, }); -Order_product.sync(); +// Order_product.sync(); module.exports = Order_product; diff --git a/src/api/database/models/product.model.js b/src/api/database/models/product.model.js index ca05400..055619b 100644 --- a/src/api/database/models/product.model.js +++ b/src/api/database/models/product.model.js @@ -22,12 +22,18 @@ const Product = sequelize.define('product', { measure: { type: DataTypes.STRING, }, + quantity: { + type: DataTypes.INTEGER, + }, status: { type: DataTypes.INTEGER, }, category: { type: DataTypes.STRING, }, + url: { + type: DataTypes.STRING, + }, }); // Product.sync(); module.exports = Product; diff --git a/src/api/repositories/address.repository.js b/src/api/repositories/address.repository.js index fef6603..10f6da7 100644 --- a/src/api/repositories/address.repository.js +++ b/src/api/repositories/address.repository.js @@ -8,7 +8,19 @@ async function findSellerAddress(id) { }, }); } catch (error) { - console.log(error); + throw new Error(error); + } +} + +async function findUserAddress(id) { + try { + return await Address.findOne({ + where: { + id_users: id, + }, + }); + } catch (error) { + throw new Error(error); } } @@ -46,8 +58,34 @@ async function createUserAddress(address, id) { } } +async function updateUserAddress(address, id) { + console.log('id::', id); + try { + return await Address.update( + { + street: address.street, + number: address.number, + complement: address.complement, + city: address.city, + state: address.state, + zip_code: address.zipCode, + country: address.country, + }, + { + where: { + id_users: id, + }, + } + ); + } catch (error) { + throw new Error(error); + } +} + module.exports = { findSellerAddress, createSellerAddress, createUserAddress, + findUserAddress, + updateUserAddress, }; diff --git a/src/api/repositories/product.repository.js b/src/api/repositories/product.repository.js index deccf74..d73fd26 100644 --- a/src/api/repositories/product.repository.js +++ b/src/api/repositories/product.repository.js @@ -5,12 +5,21 @@ module.exports = class ProductRepository { constructor() {} async create(data) { try { - const { id_seller, name, price, measure, status, category } = data; + const { + id_seller, + name, + price, + measure, + status, + category, + quantity, + } = data; const product = { id_seller: id_seller, name: name, price, measure, + quantity, status, category, }; @@ -63,11 +72,13 @@ module.exports = class ProductRepository { async update(product) { try { - const { id, name, price, measure, status, category } = product; + const { id, name, price, measure, status, category, quantity } = + product; const productObj = { name: name, price: price, measure: measure, + quantity: quantity, status: status, category: category, }; diff --git a/src/api/repositories/user.repository.js b/src/api/repositories/user.repository.js index 8b63d03..c443639 100644 --- a/src/api/repositories/user.repository.js +++ b/src/api/repositories/user.repository.js @@ -1,15 +1,18 @@ const User = require('../database/models/user.model'); -const { createUserAddress } = require('../repositories/address.repository'); +const { + createUserAddress, + findUserAddress, + updateUserAddress, +} = require('../repositories/address.repository'); module.exports = class UserRepository { constructor() {} async create(user, address) { - console.log('user', user); - console.log('address', address); try { const { userId, name, phone, email, password } = user; const { street, number, complement, city, state, zipCode } = address; - + if (await this.isRegistered(email)) + throw new Error('User already registered'); return User.create({ id: userId, name, @@ -18,16 +21,17 @@ module.exports = class UserRepository { password, }) .then(async (user) => { - console.log('user', user); - const address_ = await createUserAddress({ - id_users: user.id, - street, - number, - complement, - city, - state, - zipCode, - }); + const address_ = await createUserAddress( + { + street, + number, + complement, + city, + state, + zipCode, + }, + user.id + ); return { user, address_ }; }) .catch((error) => { @@ -37,24 +41,79 @@ module.exports = class UserRepository { throw new Error(error); } } - async update() { + async update(user, address) { + // console.log('user::', user); try { - throw new Error('no implemented'); + const id = user.userId; + const addressParam = address; + return User.update( + { name: user.name, phone: user.phone, email: user.email }, + { + where: { + id: user.userId, + }, + } + ) + .then(async () => { + await updateUserAddress(addressParam, id); + const { user, address } = await this.getById(id); + return { + user: user.dataValues, + address_: address.dataValues, + }; + }) + .catch((error) => { + throw new Error(error); + }); } catch (error) { throw new Error(error); } } - async getById() { - try { - throw new Error('get seller by id'); - } catch (error) {} + async getByEmail(email) { + return User.findOne({ + where: { + email: email, + }, + }) + .then(async (user) => { + const addressRaw = await findUserAddress(user.id); + const address = { + street: addressRaw.street, + number: addressRaw.number, + complement: addressRaw.complement, + city: addressRaw.city, + state: addressRaw.state, + zipCode: addressRaw.zip_code, + }; + + return { user, address }; + }) + .catch((error) => { + throw new Error(error); + }); } async delete(id) { try { - return `delete seller by id ${id}`; + const user = await User.findByPk(id); + if (!user) throw new Error('User not found'); + await User.destroy({ + where: { + id: id, + }, + }); } catch (error) { throw new Error(error); } } + + async isRegistered(email) { + const user = await User.findOne({ + where: { + email: email, + }, + }); + if (user) return true; + return false; + } }; diff --git a/src/api/routes/user.router.js b/src/api/routes/user.router.js index 579bd8a..21ade7b 100644 --- a/src/api/routes/user.router.js +++ b/src/api/routes/user.router.js @@ -11,12 +11,12 @@ const { const { postNewProductController } = require('../composer/order.compose'); -UserRouter.get('/user/:id', async (req, res) => { - await getUserByIdController.execute(req, res); -}); UserRouter.post('/user', async (req, res) => { await postNewUserController.execute(req, res); }); +UserRouter.post('/user/login', async (req, res) => { + await getUserByIdController.execute(req, res); +}); UserRouter.put('/user/:id', async (req, res) => { await putUserByIdController.execute(req, res); }); diff --git a/src/api/services/product/create-product.service.js b/src/api/services/product/create-product.service.js index fe75c72..7d52620 100644 --- a/src/api/services/product/create-product.service.js +++ b/src/api/services/product/create-product.service.js @@ -4,12 +4,13 @@ module.exports = class CreateProductService { } async create(params, seller_id) { try { - const { name, price, measure, status, category } = params; + const { name, price, measure, status, category, quantity } = params; if (!seller_id) throw new Error('Missing seller_id'); if (!name) throw new Error('Missing name'); if (!price) throw new Error('Missing price'); if (!measure) throw new Error('Missing measure'); + if (!quantity) throw new Error('Missing quantity'); if (!status) throw new Error('Missing status'); if (!category) throw new Error('Missing category'); @@ -18,6 +19,7 @@ module.exports = class CreateProductService { name: name, price: +price, measure: measure.toLowerCase(), + quantity: +quantity, status: status == true ? 1 : 0, category: category.toLowerCase(), }; @@ -28,8 +30,10 @@ module.exports = class CreateProductService { id: product.id, seller_id: product.id_seller, name: product.name, - price: product.price.toString(), + price: +product.price, measure: product.measure, + quantity: +product.quantity, + url: product.url, status: product.status == 1 ? true : false, category: product.category, }; diff --git a/src/api/services/product/get-by-name.service.js b/src/api/services/product/get-by-name.service.js index 1c3946e..97ce34e 100644 --- a/src/api/services/product/get-by-name.service.js +++ b/src/api/services/product/get-by-name.service.js @@ -10,8 +10,10 @@ module.exports = class GetProductByNameService { id: product.id, seller_id: product.id_seller, name: product.name, - price: product.price.toString(), + price: +product.price, + quantity: +product.quantity, measure: product.measure, + url: product.url, status: product.status == 1 ? true : false, category: product.category, }; diff --git a/src/api/services/product/get-by-seller-id.service.js b/src/api/services/product/get-by-seller-id.service.js index 909c97f..cc34928 100644 --- a/src/api/services/product/get-by-seller-id.service.js +++ b/src/api/services/product/get-by-seller-id.service.js @@ -12,8 +12,10 @@ module.exports = class GetProductBySellerIdService { id: product.id, seller_id: product.id_seller, name: product.name, - price: product.price.toString(), + price: +product.price, + quantity: +product.quantity, measure: product.measure, + url: product.url, status: product.status == 1 ? true : false, category: product.category, }; diff --git a/src/api/services/product/get-by-tag.service.js b/src/api/services/product/get-by-tag.service.js index ed9c1be..8d38702 100644 --- a/src/api/services/product/get-by-tag.service.js +++ b/src/api/services/product/get-by-tag.service.js @@ -10,8 +10,10 @@ module.exports = class GetProductByTagService { id: product.id, seller_id: product.id_seller, name: product.name, - price: product.price.toString(), + price: +product.price, + quantity: +product.quantity, measure: product.measure, + url: product.url, status: product.status == 1 ? true : false, category: product.category, }; diff --git a/src/api/services/product/update-product.service.js b/src/api/services/product/update-product.service.js index 0b8e65d..f41c3ee 100644 --- a/src/api/services/product/update-product.service.js +++ b/src/api/services/product/update-product.service.js @@ -4,13 +4,15 @@ module.exports = class UpdateProductService { } async update(params) { try { - const { id, name, price, measure, status, category } = params; + const { id, name, price, measure, status, quantity, category } = + params; if (!id) throw new Error('Missing id'); if (!name) throw new Error('Missing name'); if (!price) throw new Error('Missing price'); if (!measure) throw new Error('Missing measure'); if (!status) throw new Error('Missing status'); + if (!quantity) throw new Error('Missing quantity'); if (!category) throw new Error('Missing category'); const productObj = { @@ -18,6 +20,7 @@ module.exports = class UpdateProductService { name: name, price: +price, measure: measure.toLowerCase(), + quantity: +quantity, status: status == true ? 1 : 0, category: category.toLowerCase(), }; @@ -28,8 +31,10 @@ module.exports = class UpdateProductService { id: product.id, seller_id: product.id_seller, name: product.name, - price: product.price, + price: +product.price, measure: product.measure, + quantity: +product.quantity, + url: product.url, status: product.status == 1 ? true : false, category: product.category, }; diff --git a/src/api/services/user/get-by-id.service.js b/src/api/services/user/get-by-id.service.js index 95dbe47..580be0c 100644 --- a/src/api/services/user/get-by-id.service.js +++ b/src/api/services/user/get-by-id.service.js @@ -1,12 +1,28 @@ -const isValidUUID = require('../../helpers/uuid-validator.helper'); +// const isValidUUID = require('../../helpers/uuid-validator.helper'); module.exports = class GetUserByIdService { constructor(repository) { this.repository = repository; } - async getById(id) { + async getByEmail(email) { try { - if (!isValidUUID(id)) throw new Error('Id is not a valid uuid id'); + // if (!isValidUUID(id)) throw new Error('Id is not a valid uuid id'); + const { user, address } = await this.repository.getByEmail(email); + + return await { + name: user.name, + phone: user.phone, + email: user.email, + address: { + street: address.street, + number: address.number, + complement: address.complement, + city: address.city, + state: address.state, + zipCode: address.zipCode, + }, + }; } catch (error) { + console.log(error); throw new Error( JSON.stringify({ error: error.message, statusCode: 400 }) ); diff --git a/src/api/services/user/update-user.service.js b/src/api/services/user/update-user.service.js index 0f61708..bce2f7e 100644 --- a/src/api/services/user/update-user.service.js +++ b/src/api/services/user/update-user.service.js @@ -11,20 +11,35 @@ module.exports = class UpdateUserService { if (!name) throw new Error('Missing name'); if (!phone) throw new Error('Missing phone'); if (!email) throw new Error('Missing email'); - if (!password) throw new Error('Missing password'); if (!address) throw new Error('Missing address'); - const user = await this.repository.update({ - userId: id, - contact: { + const { user, address_ } = await this.repository.update( + { + userId: id, name, phone, email, password, - address, }, - }); - return user; + address + ); + + return { + id: user.id, + contact: { + name: user.name, + phone: user.phone, + email: user.email, + }, + address: { + street: address_.street, + number: address_.number, + complement: address_.complement, + city: address_.city, + state: address_.state, + zipCode: address_.zipCode, + }, + }; } catch (error) { throw new Error( JSON.stringify({ error: error.message, statusCode: 400 }) diff --git a/src/config/app.js b/src/config/app.js index a162b35..ffb80d1 100644 --- a/src/config/app.js +++ b/src/config/app.js @@ -12,7 +12,6 @@ app.get('/', (req, res) => { const corsOptions = { origin: '*', credentials: true, - optionSuccessStatus: 200, }; app.use(cors(corsOptions)); diff --git a/src/config/database.js b/src/config/database.js index bb55eb8..dd8e1de 100644 --- a/src/config/database.js +++ b/src/config/database.js @@ -1,5 +1,11 @@ const Sequelize = require('sequelize'); -const sequelize = new Sequelize(process.env.DATABASE_URL, {}); +const sequelize = new Sequelize(process.env.DATABASE_URL, { + dialectOptions: { + ssl: { + require: 'true', + }, + }, +}); module.exports = sequelize;