Skip to content

customer endpoints now working #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 184 additions & 11 deletions src/controllers/customer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
import { Customer } from '../database/models';

const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
/**
*
*
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}
}

Expand Down
23 changes: 6 additions & 17 deletions src/routes/api/customer.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;