Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Router} from 'express';
- import models from '../models/index';
- import Promise from 'bluebird';
- import Helper from './helpers';
- import uuid from 'uuid';
- import reactCookie from 'react-cookie';
- import email from './emails';
- // Initial verison of error handler.
- // Helper.errorHandler is slightly modified!
- function errorHandler(err, req, res, next) {
- console.log('error object:', err);
- if (err) { res.send({message: 'errors found!', error: err}) }
- }
- var app = Router();
- //////////////////////////
- // CREATE
- //////////////////////////
- // Create a new Account
- app.post('/', function (req, res, next) {
- // res.send({foo: 'bar'});
- var userDetails = {};
- models.Account
- .create({
- firstName: req.body.firstName,
- lastName: req.body.lastName,
- // email: req.body.email,
- phoneNumber: req.body.phoneNumber,
- username: req.body.username,
- password: req.body.password
- })
- .catch(function (err) {
- Helper.handleCatchError(err, res);
- })
- .then(function (account) {
- models.Email
- .create({
- emailAddress: req.body.email,
- isPrimary: true
- })
- .catch(function (err) {
- Helper.handleCatchError(err, res);
- })
- .then(function (email) {
- if (account) {
- userDetails.userId = account.dataValues.id // : userDetails.Err = {err: account};
- // userDetails.username = account.dataValues.username
- userDetails.firstName = account.dataValues.firstName
- userDetails.lastName = account.dataValues.lastName
- userDetails.email = email.dataValues.email
- // ========================================
- // Remember to include assocId in .create!
- // ========================================
- models.Address
- .create({
- // addressLine1: req.body.addressLine1,
- // addressLine2: req.body.addressLine2,
- city: req.body.city,
- province: req.body.province,
- region: req.body.region,
- postalCode: req.body.postalCode,
- country: req.body.country,
- AccountId: account.dataValues.id
- })
- .catch(function (err) {
- Helper.handleCatchError(err, res);
- })
- .then(function (address) {
- // OPTIONAL RESPONSE -- used for checking if reg is successful
- res.send(userDetails);
- })
- }
- })
- })
- // .catch(function (err) {
- // // res.status(500).send({message: 'At end of outer then statement... Error: ', err: err})
- // console.log('in outer catch...',err)
- // errorHandler(err, req, res, next);
- // })
- })
- /////////////////////////////////////////////
- // Internal use only!
- /////////////////////////////////////////////
- // Create fields in interests table by user id
- app.post('/interests/create', function (req, res, next) {
- Helper.createInterests(req, res, next);
- })
- //////////////////////////
- // READ / FIND
- //////////////////////////
- // Read/Find one user via :id
- app.get('/user/:email/:username', function (req, res, next) {
- models.Account
- .find({
- where: {
- id: Number(req.params.id),
- username: req.params.username
- }
- }) // convert req.params.id to integer
- .catch(function (err) {
- Helper.handleCatchError(err, res);
- })
- .then(function (account) {
- var results = {}
- if (account) {
- results.account = account.dataValues
- models.Address
- .find({
- where: {
- id: Number(req.params.id),
- username: req.params.username
- }
- })
- .catch(function (err) {
- errorHandler(err.errors, req, res, next);
- })
- .then(function (address) {
- if (address) {
- results.address = address.dataValues
- res.send(results);
- }
- })
- }
- })
- })
- // find one user by email
- app.get('/user/:email', function (req, res, next) {
- console.log("attempting to find an email: ");
- console.log(req.params);
- models.Account
- .find({
- where: {
- email: req.params.email
- }
- }) // convert req.params.id to integer
- .catch(function (err) {
- Helper.handleCatchError(err, res);
- })
- .then(function (account) {
- console.log('# # # REQ SESSION # # #');
- // Create UUID for sessions
- // add to req.session.id
- // send to client cookie
- // check if UUID already exists from Redis
- // req.session.userSessionId = uuid.v4();
- req.session.userId = account.dataValues.id;
- req.session.email = account.dataValues.email
- req.session.save();
- reactCookie.setRawCookie(req.headers.cookie);
- console.log(req.session);
- console.log(req.session.id)
- console.log(' ');
- console.log('-== ReQ headers ==-');
- console.log(' ');
- console.log(req.headers)
- console.log(' ');console.log(' ');
- console.log('--- res cookie ---');
- console.log(' ');console.log(' ');
- // // console.log (req.session.blah);
- // res.setHeader('SetCookie', req.session.id)
- // res.setHeader('Set-Cookie', req.session.id.toString())
- res.cookie('sessIdResCookie', req.session.id.toString());
- console.log(res.cookie);
- // console.log(res);
- res.send({
- sessionId: req.session.id,
- userId: account.dataValues.id,
- email: account.dataValues.email
- })
- })
- })
- // LOGIN : Read/Find one user by id and username or email then check password
- app.post('/login/', function (req, res, next) {
- models.Account
- .find({
- where: {
- id: req.body.id,
- username: req.body.username,
- password: req.body.password
- }
- })
- .catch(function (err) {
- Helper.handleCatchError(err, res);
- })
- .then(function (account) {
- res.send({
- userDetails: {
- userId: account.dataValues.id,
- username: account.dataValues.username,
- firstName: account.dataValues.firstName,
- lastName: account.dataValues.lastName,
- email: account.dataValues.email
- }
- })
- })
- })
- //////////////////////////
- // UPDATE
- //////////////////////////
- // Update one account's email, phone number, or password all at once
- // ### ? ? ? How to update multiple columns at once if one or more inputs are null ? ? ? ###
- // The solution below is not optimized
- app.post('/update/:id/:username', function (req, res, next) {
- console.log({message: 'attempting to update one account with:', body: req.body, params: req.params, trimmedSpace: req.body.email.replace(/\s+/g, '')});
- var data = {},
- response = {};
- for (var [key, val] of Object.entries(req.body)) {
- if (req.body.hasOwnProperty(key)) {
- if (val === null || val.replace(/\s+/g, '') === '') {
- data[key] = null;
- }
- else {
- data[key] = val;
- }
- }
- console.log('here is the updated data object: ', data);
- }
- // console.log(typeof Helper);
- // console.log(typeof Helper.updateEmail);
- if (data.email !== null) {
- Helper.updateEmail(res, Number(req.params.id), req.params.username, data)
- }
- if (data.alias !== null) {
- Helper.updateAlias(res, Number(req.params.id), req.params.username, data)
- }
- if (data.phoneNumber !== null) {
- Helper.updatePhoneNumber(res, Number(req.params.id), req.params.username, data)
- }
- })
- // update blog boost status
- //Update all
- //////////////////////////
- // DELETE
- //////////////////////////
- // Delete One
- // retrieve a single Account
- // router.get('/:id', function(req,res){
- // models.Account.find({
- // where: {
- // id: req.params.id
- // }
- // }).then(function(account){
- // res.json(account);
- // });
- // });
- // // applying middleware to all routes defined in this router
- // router.use(function(req,res,next){
- // // do something
- // });
- export default app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement