diff --git a/Message.js b/Message.js index 79f229e..7eff039 100644 --- a/Message.js +++ b/Message.js @@ -1,3 +1,5 @@ +import { workers } from 'cluster'; + 'use strict'; const sqlite3 = require('sqlite3').verbose(); @@ -105,7 +107,7 @@ let csvMessageObjs = [ }, ] //Methods for the EMAIL data -//this will not need to be in the production version as the DB will already exist! Just for my build purposes -EL +// //this will not need to be in the production version as the DB will already exist! Just for my build purposes -EL // db.serialize( () => { @@ -135,13 +137,9 @@ let csvMessageObjs = [ // relatedStudentContactId TEXT, // softBounce TEXT NOT NULL, // subjectLine TEXT - - // )`); - - // module.exports.prepEmailData = (data) => { // console.log('printData firing', data["Contact Record Type"]); // data.forEach(d => { @@ -172,31 +170,31 @@ let csvMessageObjs = [ // }; // //POST from upload, iterate over each of the items in the parsed array of objects: -// module.exports.insertEmailsIntoDB = (csvEmailObjs) => { -// csvEmailObjs.forEach( (emailObj) => { -// db.run(`INSERT INTO EmailFiles VALUES (null, -// '${emailObj.clicked}', -// '${emailObj.contact}', -// '${emailObj.contactId}', -// '${emailObj.contactRecordType}', -// '${emailObj.dateBounced}', -// '${emailObj.dateOpened}', -// '${emailObj.dateSent}', -// '${emailObj.dateUnsubscribed}', -// '${emailObj.deleted}', -// '${emailObj.email}', -// '${emailObj.emailName}', -// '${emailObj.fromAddress}', -// '${emailObj.fromName}', -// '${emailObj.hardBounce}', -// ${emailObj.numberOfTotalClicks}, -// ${emailObj.numberOfUniqueClicks}, -// '${emailObj.opened}', -// '${emailObj.relatedStudentContactId}', -// '${emailObj.softBounce}', -// '${emailObj.subjectLine}')`); -// }); -// } +module.exports.insertEmailsIntoDB = (csvEmailObjs) => { + csvEmailObjs.forEach( (emailObj) => { + db.run(`INSERT INTO EmailFiles VALUES (null, + '${emailObj.clicked}', + '${emailObj.contact}', + '${emailObj.contactId}', + '${emailObj.contactRecordType}', + '${emailObj.dateBounced}', + '${emailObj.dateOpened}', + '${emailObj.dateSent}', + '${emailObj.dateUnsubscribed}', + '${emailObj.deleted}', + '${emailObj.email}', + '${emailObj.emailName}', + '${emailObj.fromAddress}', + '${emailObj.fromName}', + '${emailObj.hardBounce}', + ${emailObj.numberOfTotalClicks}, + ${emailObj.numberOfUniqueClicks}, + '${emailObj.opened}', + '${emailObj.relatedStudentContactId}', + '${emailObj.softBounce}', + '${emailObj.subjectLine}')`); + }); +} @@ -281,9 +279,9 @@ module.exports.insertMessagesIntoDB = (csvMessageObjs) => { -//uncommenting this will call this function and input those objects above -// module.exports.insertEmailsIntoDB(csvEmailObjs); - // module.exports.insertMessagesIntoDB(csvMessageObjs); +// // //uncommenting this will call this function and input those objects above + // module.exports.insertEmailsIntoDB(csvEmailObjs); +// // // module.exports.insertMessagesIntoDB(csvMessageObjs); // }); module.exports.searchByContactId = (contactId) => { @@ -295,35 +293,57 @@ module.exports.searchByContactId = (contactId) => { }); }; - -module.exports.uploadFileToDB = (file) => { - console.log("file going in", file); +//clean up and start building out step by step +module.exports.uploadFileToDB = (fileObjsArr) => { + console.log("file going in"); + console.log("fileObjsArr, fakely 0", fileObjsArr[0]['Contact ID']); return new Promise( (resolve, reject) => { - db.all(`INSERT INTO EmailFiles VALUES (null, - '${file.contactId}', - '${file.clicked}', - '${file.contact}', - '${file.contactRecordType}', - '${file.dateBounced}', - '${file.dateOpened}', - '${file.dateSent}', - '${file.dateUnsubscribed}', - '${file.deleted}', - '${file.email}', - '${file.emailName}', - '${file.fromAddress}', - '${file.fromName}', - '${file.hardBounce}', - ${file.numberOfTotalClicks}, - ${file.numberOfUniqueClicks}, - '${file.opened}', - '${file.relatedStudentContactId}', - '${file.softBounce}', - '${file.subjectLine}')`, + console.log("fileObjsArr, fakely 1", fileObjsArr[0]['Contact ID']); + + //create empty strings/sub numbers for all values just in case + + //try checking out a new branch and then using the same front end stuff?? + + //try making a whole new back end based on what you have learned with sequelize + // then see if you can use something like bulkinsert... find out how papaparse works and + //if that can be a json file... maybe stdout write it to a file or something + //then you should be able to use that as a 'seeder' ... but is there a way to sequelize it + //straight as a json collection, like instead of importing from a seed data file let {} = require whatever + //just go ahead and straight up import and then bulkinsert that stuff? + + fileObjsArr.forEach( (file) => { + if (file['Contact ID']) { + + console.log("file file file", file); + db.run(`INSERT INTO EmailFiles (contactId, clicked) VALUES ("${file['Contact ID']}", "${file.Clicked}")`, + // '${file.Clicked}', + // '${file.Contact}', + // '${file['Contact ID']}', + // '${file["Contact Record Type"]}', + // '${file["Date Bounced"]}', + // '${file['Date Opened']}', + // '${file["Date Sent"]}', + // '${file["Date Unsubscribed"]}', + // '${file.Deleted}', + // '${file.Email}', + // '${file["Email Name"]}', + // '${file['From Address']}', + // '${file["From Name"]}', + // '${file["Hard Bounce"]}', + // ${file["Number Of Total Clicks"]}, + // ${file["Number Of Unique Clicks"]}, + // '${file.Opened}', + // '${file["Related Student Contact ID"]}', + // '${file["Soft Bounce"]}', + // '${file["Subject Line"]}' + // ), (err, successMessage) => { if (err) return reject(err);//if error, pass on to error handler resolve(successMessage); }); + //ask JUFE - or someone - how can I resolve this not inside foreach?- or what else to do here + } + }) }); }; diff --git a/app/factories/upload-factory.js b/app/factories/upload-factory.js index b5974d0..fc887fa 100644 --- a/app/factories/upload-factory.js +++ b/app/factories/upload-factory.js @@ -15,10 +15,17 @@ app.factory('uploadFactory', function ($q, $http) { }); }; - //send to API -- OPTION ONE + function uploadToApi(arrObj){ + console.log("arrObj", arrObj); return $q((resolve, reject)=>{ - $http.post(`http://localhost:3000/uploadfile/${arrObj}`) + $http({ + method: 'POST', + url: 'http://localhost:3000/uploadfile', + // data: {"fake_json": "This is a small string."}, + data: arrObj, + headers: {'Content-Type': 'application/json'} + }) .then((data) => { console.log("uploaded to Api?", data); resolve(); @@ -28,6 +35,20 @@ app.factory('uploadFactory', function ($q, $http) { }); }); } + // //send to API -- OPTION ONE + // function uploadToApi(arrObj){ + // return $q((resolve, reject)=>{ + // $http.post(`http://localhost:3000/uploadfile/${arrObj}`) + // .then((data) => { + // console.log("uploaded to Api?", data); + // resolve(); + // }) + // .catch( (err) => { + // reject(err); + // }); + // }); + // } + // //send to API -- OPTION TWO // function uploadToApi(arrObj){ diff --git a/lib/package.json b/lib/package.json index 4813e8d..51f1753 100644 --- a/lib/package.json +++ b/lib/package.json @@ -28,6 +28,7 @@ "angular": "^1.6.6", "angular-route": "^1.6.6", "angular-ui-bootstrap": "^2.5.0", + "body-parser": "^1.18.2", "bootstrap": "^4.0.0-beta", "express": "^4.16.1", "grunt": "^1.0.1", diff --git a/messageCtrl.js b/messageCtrl.js index 04ca3c8..1eb5b03 100644 --- a/messageCtrl.js +++ b/messageCtrl.js @@ -20,7 +20,7 @@ let getMessagesByContactId = (req, res, next) => { }; let uploadFileFromWeb = (req, res, next) => { - uploadFileToDB(req.params.file) + uploadFileToDB(req.body) .then( (results) => { //do something with results-- send to front end for usable form, they will just be JSON so far res.status(200).json(results); diff --git a/nodeApp.js b/nodeApp.js index a5ff59e..32b23db 100644 --- a/nodeApp.js +++ b/nodeApp.js @@ -1,8 +1,14 @@ 'use strict'; -let express = require('express'); -let app = express(); -let routes = require('./routes.js'); +const express = require('express'); +const app = express(); +const routes = require('./routes.js'); +const bodyParser = require('body-parser'); +const cors = require('cors'); + + +app.use(bodyParser.json()); +app.use(cors()); app.use('/', routes); @@ -17,7 +23,7 @@ app.use( (err, req, res, next) => { res.status(err.status||500); res.json({ message:"A problem occurred.", - err:err + err: err }) }); diff --git a/package.json b/package.json new file mode 100644 index 0000000..e425e3e --- /dev/null +++ b/package.json @@ -0,0 +1,39 @@ +{ + "name": "tnAchieves-Communication-Archive", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "devDependencies": { + "concat": "^1.0.3", + "connect": "^3.6.5", + "grunt-contrib-concat": "^1.0.1", + "grunt-contrib-connect": "^1.0.2", + "grunt-contrib-jshint": "^1.1.0", + "grunt-contrib-sass": "^1.0.0", + "grunt-contrib-watch": "^1.0.0", + "jshint": "^2.9.5", + "jshint-stylish": "^2.2.1", + "matchdep": "^2.0.0", + "nodemon": "^1.12.1", + "sass": "^1.0.0-beta.2", + "sqlite3": "^3.1.13", + "watch": "^1.0.2" + }, + "dependencies": { + "angular": "^1.6.6", + "angular-route": "^1.6.6", + "angular-ui-bootstrap": "^2.5.0", + "body-parser": "^1.18.2", + "bootstrap": "^4.0.0-beta", + "cors": "^2.8.4", + "express": "^4.16.1", + "grunt": "^1.0.1", + "jquery": "^3.2.1", + "popper.js": "^1.12.5" + }, + "description": "" +} diff --git a/routes.js b/routes.js index b032e88..4b2c118 100644 --- a/routes.js +++ b/routes.js @@ -8,7 +8,7 @@ const { sayHello } = require('./helloCtrl.js'); router.get('/messages/:id', getMessagesByContactId); //if this route, get all emails attached to a certain id -router.post('/uploadfile/:file', uploadFileFromWeb); //if this route, get all emails attached to a certain id +router.post('/uploadfile', uploadFileFromWeb); //if this route, get all emails attached to a certain id // router.post('/email/upload', postEmailData); router.get('/hello', sayHello); diff --git a/tnAchieves.sqlite b/tnAchieves.sqlite deleted file mode 100644 index e3b30ad..0000000 Binary files a/tnAchieves.sqlite and /dev/null differ