From 27cb62129be58037f6a4681ac4907e21c0d61171 Mon Sep 17 00:00:00 2001 From: "https://github.com': codeplus254" Date: Sun, 29 Sep 2019 14:55:52 +0300 Subject: [PATCH] customer endpoints now working --- src/controllers/customer.controller.js | 195 +++++++++++++++++++++++-- src/routes/api/customer.route.js | 23 +-- 2 files changed, 190 insertions(+), 28 deletions(-) diff --git a/src/controllers/customer.controller.js b/src/controllers/customer.controller.js index 82fe3d3..816d8bc 100755 --- a/src/controllers/customer.controller.js +++ b/src/controllers/customer.controller.js @@ -14,6 +14,8 @@ */ import { Customer } from '../database/models'; +const jwt = require('jsonwebtoken'); +const bcrypt = require('bcrypt'); /** * * @@ -32,7 +34,33 @@ class CustomerController { */ static async create(req, res, next) { // Implement the function to create the customer account - return res.status(201).json({ message: 'this works' }); + const { name, email, password } = req.body; + + try { + const customer = await Customer.findOne({ + where: { + email, + }, + }); + if (!customer) { + // check if the customer with that email had registered before + const passwordHash = bcrypt.hashSync(password, 10); + const newCustomer = await Customer.upsert({ + name, + email, + password: passwordHash, + }); + return res.status(201).send(newCustomer); + } + return res.status(409).json({ + error: { + status: 409, + message: `User with email ${email} already exist`, + }, + }); + } catch (error) { + return next(email); + } } /** @@ -47,7 +75,72 @@ class CustomerController { */ static async login(req, res, next) { // implement function to login to user account - return res.status(200).json({ message: 'this works' }); + const { email, password } = req.body; + try { + const customer = await Customer.findOne({ + where: { + email, + password, + }, + }); + const accessToken = `Bearer ${await jwt.sign( + { customer_id: customer.customer_id }, + process.env.SECRET_KEY, + { expiresIn: '24h' } + )}`; + if (customer) { + return res.status(200).json({ + customer, + accessToken, + expiresIn: '24h', + }); + } + return res.status(404).json({ + error: { + status: 404, + message: `User does not exist`, + }, + }); + } catch (error) { + return next(error); + } + } + + /** + * log in a customer + * + * @static + * @param {object} req express request object + * @param {object} res express response object + * @param {object} next next middleware + * @returns {json} json object with status, and access token + * @memberof CustomerController + */ + static async facebookLogin(req, res, next) { + // implement function to login to user account + const { accessToken } = req.body; + try { + const customer = await Customer.findOne({ + where: { + accessToken, + }, + }); + if (customer) { + return res.status(200).json({ + customer, + accessToken: null, + expiresIn: null, + }); + } + return res.status(404).json({ + error: { + status: 404, + message: `User does not exist`, + }, + }); + } catch (error) { + return next(error); + } } /** @@ -61,13 +154,16 @@ class CustomerController { * @memberof CustomerController */ static async getCustomerProfile(req, res, next) { - // fix the bugs in this code - const { customer_id } = req; // eslint-disable-line + const accessToken = req.headers.authorization; + const decodedToken = jwt.decode(accessToken.substring(7)); // remove the Bearer tag + const customerId = decodedToken.customer_id; + try { - const customer = await Customer.findByPk(customer_id); - return res.status(400).json({ - customer, - }); + const customer = await Customer.findByPk(customerId); + if (customer) { + return res.status(200).json(customer); + } + return res.status(404).json('The user does not exist'); } catch (error) { return next(error); } @@ -85,7 +181,34 @@ class CustomerController { */ static async updateCustomerProfile(req, res, next) { // Implement function to update customer profile like name, day_phone, eve_phone and mob_phone - return res.status(200).json({ message: 'this works' }); + + // eslint-disable-next-line camelcase + const { email, name, day_phone, eve_phone, mob_phone } = req.body; + const accessToken = req.headers.authorization; + const decodedToken = jwt.decode(accessToken.substring(7)); // remove the Bearer tag + const customerId = decodedToken.customer_id; + try { + await Customer.update( + { + email, + name, + day_phone, + eve_phone, + mob_phone, + }, + { + returning: true, + where: { + // eslint-disable-next-line prettier/prettier + customer_id: customerId, + }, + } + ); + const customer = await Customer.findByPk(customerId); + return res.status(200).json(customer); + } catch (error) { + return next(error); + } } /** @@ -101,7 +224,35 @@ class CustomerController { static async updateCustomerAddress(req, res, next) { // write code to update customer address info such as address_1, address_2, city, region, postal_code, country // and shipping_region_id - return res.status(200).json({ message: 'this works' }); + // eslint-disable-next-line camelcase + const { address_1, address_2, city, region, postal_code, country, shipping_region_id } = req.body; + const accessToken = req.headers.authorization; + const decodedToken = jwt.decode(accessToken.substring(7)); // remove the Bearer tag + const customerId = decodedToken.customer_id; + try { + await Customer.update( + { + address_1, + address_2, + city, + region, + postal_code, + country, + shipping_region_id, + }, + { + returning: true, + where: { + // eslint-disable-next-line prettier/prettier + customer_id: customerId, + }, + } + ); + const customer = await Customer.findByPk(customerId); + return res.status(200).json(customer); + } catch (error) { + return next(error); + } } /** @@ -116,7 +267,29 @@ class CustomerController { */ static async updateCreditCard(req, res, next) { // write code to update customer credit card number - return res.status(200).json({ message: 'this works' }); + // eslint-disable-next-line camelcase + const { credit_card } = req.body; + const accessToken = req.headers.authorization; + const decodedToken = jwt.decode(accessToken.substring(7)); // remove the Bearer tag + const customerId = decodedToken.customer_id; + try { + await Customer.update( + { + credit_card, + }, + { + returning: true, + where: { + // eslint-disable-next-line prettier/prettier + customer_id: customerId, + }, + } + ); + const customer = await Customer.findByPk(customerId); + return res.status(200).json(customer); + } catch (error) { + return next(error); + } } } diff --git a/src/routes/api/customer.route.js b/src/routes/api/customer.route.js index 09e3ed4..bae1502 100755 --- a/src/routes/api/customer.route.js +++ b/src/routes/api/customer.route.js @@ -4,23 +4,12 @@ import CustomerController from '../../controllers/customer.controller'; // These are valid routes but they may contain a bug, please try to define and fix them const router = Router(); -router.post( - '/customers', - CustomerController.updateCreditCard -); -router.post('/customers/login', CustomerController.login); +router.post('/customers', CustomerController.create); +router.post('/customers/login', CustomerController.login); +router.post('/customers/facebook', CustomerController.facebookLogin); router.get('/customer', CustomerController.getCustomerProfile); -router.put( - '/customer', - CustomerController.apply -); -router.put( - '/customer/address', - CustomerController.updateCustomerAddress -); -router.put( - '/customer/creditCard', - CustomerController.updateCreditCard -); +router.put('/customer', CustomerController.updateCustomerProfile); +router.put('/customer/address', CustomerController.updateCustomerAddress); +router.put('/customer/creditCard', CustomerController.updateCreditCard); export default router;