Skip to content

Commit

Permalink
added index.js for user
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonrichardsmith committed Aug 18, 2016
1 parent 252f3eb commit 37a9f34
Showing 1 changed file with 173 additions and 0 deletions.
173 changes: 173 additions & 0 deletions api/user/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
(function (){
'use strict';

var express = require("express")
, request = require("request")
, endpoints = require("../endpoints")
, helpers = require("../../helpers")
, app = express()

app.get("/customers/:id", function (req, res, next) {
helpers.simpleHttpRequest(endpoints.customersUrl + "/" + req.params.id, res, next);
});

// Create Customer - TO BE USED FOR TESTING ONLY (for now)
app.post("/customers", function(req, res, next) {
var options = {
uri: endpoints.customersUrl,
method: 'POST',
json: true,
body: req.body
};

console.log("Posting Customer: " + JSON.stringify(req.body));

request(options, function (error, response, body) {
if (error) {
return next(error);
}
helpers.respondSuccessBody(res, JSON.stringify(body));
}.bind({res: res}));
});

// Create Address - TO BE USED FOR TESTING ONLY (for now)
app.post("/addresses", function(req, res, next) {
var options = {
uri: endpoints.addressUrl,
method: 'POST',
json: true,
body: req.body
};
console.log("Posting Address: " + JSON.stringify(req.body));
request(options, function (error, response, body) {
if (error) {
return next(error);
}
helpers.respondSuccessBody(res, JSON.stringify(body));
}.bind({res: res}));
});

// Create Card - TO BE USED FOR TESTING ONLY (for now)
app.post("/cards", function(req, res, next) {
var options = {
uri: endpoints.cardsUrl,
method: 'POST',
json: true,
body: req.body
};
console.log("Posting Card: " + JSON.stringify(req.body));
request(options, function (error, response, body) {
if (error) {
return next(error);
}
helpers.respondSuccessBody(res, JSON.stringify(body));
}.bind({res: res}));
});

// Delete Customer - TO BE USED FOR TESTING ONLY (for now)
app.delete("/customers/:id", function(req, res, next) {
console.log("Deleting Customer " + req.params.id);
var options = {
uri: endpoints.customersUrl + "/" + req.params.id,
method: 'DELETE'
};
request(options, function (error, response, body) {
if (error) {
return next(error);
}
helpers.respondSuccessBody(res, JSON.stringify(body));
}.bind({res: res}));
});

// Delete Address - TO BE USED FOR TESTING ONLY (for now)
app.delete("/addresses/:id", function(req, res, next) {
console.log("Deleting Address " + req.params.id);
var options = {
uri: endpoints.addressUrl + "/" + req.params.id,
method: 'DELETE'
};
request(options, function (error, response, body) {
if (error) {
return next(error);
}
helpers.respondSuccessBody(res, JSON.stringify(body));
}.bind({res: res}));
});

// Delete Card - TO BE USED FOR TESTING ONLY (for now)
app.delete("/cards/:id", function(req, res, next) {
console.log("Deleting Card " + req.params.id);
var options = {
uri: endpoints.cardsUrl + "/" + req.params.id,
method: 'DELETE'
};
request(options, function (error, response, body) {
if (error) {
return next(error);
}
helpers.respondSuccessBody(res, JSON.stringify(body));
}.bind({res: res}));
});

app.get("/login", function (req, res, next) {
console.log("Received login request");

async.waterfall([
function (callback) {
var options = {
headers: {
'Authorization': req.get('Authorization')
},
uri: endpoints.loginUrl
};
request(options, function (error, response, body) {
if (error) {
callback(error);
return;
}
if (response.statusCode == 200 && body != null && body != "") {
console.log(body);
var customerId = JSON.parse(body).user.id;
console.log(customerId);
callback(null, customerId);
return;
}
console.log(response.statusCode);
callback(true);
});
},
function (custId, callback) {
var sessionId = req.session.id;
console.log("Merging carts for customer id: " + custId + " and session id: " + sessionId);

var options = {
uri: endpoints.cartsUrl + "/" + custId + "/merge" + "?sessionId=" + sessionId,
method: 'GET'
};
request(options, function (error, response, body) {
if (error) {
callback(error);
return;
}
console.log('Carts merged.');
callback(null, custId);
});
}
],
function (err, custId) {
if (err) {
console.log("Error with log in: " + err);
res.status(401);
res.end();
return;
}
res.status(200);
res.cookie(cookie_name, custId, {maxAge: 3600000}).send('Cookie is set');
console.log("Sent cookies.");
res.end();
return;
});
});

module.exports = app;
}());

0 comments on commit 37a9f34

Please sign in to comment.