Move backend files to backend/ subdirectory
This commit is contained in:
429
backend/models/geocache.js
Normal file
429
backend/models/geocache.js
Normal file
@@ -0,0 +1,429 @@
|
||||
const mongoose = require('mongoose');
|
||||
const GoogleMaps = require('@google/maps').createClient({
|
||||
key: 'AIzaSyCvpBGztvxtRUNigOW9f0GXVRWlukJZsps'
|
||||
});
|
||||
|
||||
var logger = require('../modules/logger');
|
||||
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const GeocacheSchema = new Schema({
|
||||
key: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
formatted: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
loc: {
|
||||
type: {
|
||||
type: String,
|
||||
default: 'Point'
|
||||
},
|
||||
coordinates: [{
|
||||
type: Number,
|
||||
default: [0, 0]
|
||||
}]
|
||||
},
|
||||
georesult: {
|
||||
type: Schema.Types.Mixed
|
||||
}
|
||||
});
|
||||
|
||||
const Conversion = {
|
||||
kilometersToMeters: (distance) => {
|
||||
return parseFloat(distance * 1000);
|
||||
},
|
||||
|
||||
kilometersToMiles: (distance) => {
|
||||
return parseFloat(distance / 1.60934);
|
||||
},
|
||||
|
||||
kilometersToNauticalMiles: (distance) => {
|
||||
return parseFloat(distance * 0.539957);
|
||||
},
|
||||
|
||||
metersToKilometers: (distance) => {
|
||||
return parseFloat(distance / 1000);
|
||||
},
|
||||
|
||||
metersToMiles: (distance) => {
|
||||
return parseFloat(distance / 1609.34);
|
||||
},
|
||||
|
||||
milesToKilometers: (distance) => {
|
||||
return parseFloat(distance * 1.60934);
|
||||
},
|
||||
|
||||
milesToMeters: (distance) => {
|
||||
return parseFloat(distance * 1609.34);
|
||||
},
|
||||
|
||||
milesToNauticalMiles: (distance) => {
|
||||
return parseFloat(distance * 0.868976);
|
||||
},
|
||||
|
||||
nauticalMilesToMeters: (distance) => {
|
||||
return parseFloat(distance / 0.868976);
|
||||
},
|
||||
|
||||
nauticalMilesToMiles: (distance) => {
|
||||
return parseFloat(distance / 0.868976);
|
||||
},
|
||||
|
||||
nauticalMilesToKilometers: (distance) => {
|
||||
return parseFloat(distance * 1.852000674128);
|
||||
}
|
||||
};
|
||||
|
||||
GeocacheSchema.index({ name: 1, loc: '2dsphere' });
|
||||
|
||||
const GeocacheModel = mongoose.model('geocache', GeocacheSchema);
|
||||
|
||||
/*function distanceBetween (geoJSON1, geoJSON2, unit = 'mi') {
|
||||
var radlat1 = Math.PI * geoJSON1.coordinates[1]/180;
|
||||
var radlat2 = Math.PI * geoJSON2.coordinates[1]/180;
|
||||
var theta = geoJSON1.coordinates[0] - geoJSON2.coordinates[0];
|
||||
var radtheta = Math.PI * theta/180;
|
||||
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
|
||||
dist = Math.acos(dist);
|
||||
dist = dist * 180/Math.PI;
|
||||
dist = dist * 60 * 1.1515; // miles between
|
||||
if (unit == "km") { dist = Conversion.metersToKilometers(Conversion.milesToMeters(dist)); }
|
||||
if (unit == "m") { dist = dist * 1.609344; }
|
||||
if (unit == "n") { dist = dist * 0.8684; }
|
||||
return dist;
|
||||
}*/
|
||||
|
||||
function queryGeodataApi (query, callback) {
|
||||
GoogleMaps.geocode({
|
||||
address: query
|
||||
}, function(err, response) {
|
||||
if (err) {
|
||||
console.error('[GeocacheModel<<getGeoData>>] Address Geocoding Error', { address: query, response: response });
|
||||
callback(null, err, null);
|
||||
}
|
||||
|
||||
if (response.json && Array.isArray(response.json.results)) {
|
||||
var data = {
|
||||
key: sanitizeNameForKey(query),
|
||||
formatted: response.json.results[0].formatted_address,
|
||||
georesult: response.json.results[0],
|
||||
loc: {
|
||||
type: 'Point',
|
||||
coordinates: [
|
||||
response.json.results[0].geometry.location.lng,
|
||||
response.json.results[0].geometry.location.lat
|
||||
]
|
||||
}
|
||||
};
|
||||
callback(null, null, data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sanitizeNameForKey (name) {
|
||||
var key = name.replace(/[.,/#!$%^&*;:{}=\-_`~()]/g,'');
|
||||
key = key.replace(/\s{2,}/g,' ');
|
||||
key = key.trim();
|
||||
key = key.toLowerCase();
|
||||
return key;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
create: (e, geodata) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
var geocacheInstance = new GeocacheModel(geodata);
|
||||
geocacheInstance.save((err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('create', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('create', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
conversion: Conversion,
|
||||
|
||||
find: (e, searchText) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
GeocacheModel.findOne({ key: sanitizeNameForKey(searchText) }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('find', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('find', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
findLike: (e, searchText) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
GeocacheModel.find({ key: new RegExp('.*' + sanitizeNameForKey(searchText) + '.*', "i") }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('findLike', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('findLike', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
findNear: (e, lng, lat, distance) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
var point = {
|
||||
type: 'Point',
|
||||
coordinates: [ lng, lat ]
|
||||
};
|
||||
|
||||
var opts = {
|
||||
spherical: true,
|
||||
maxDistance: Conversion.milesToMeters(distance)
|
||||
};
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
GeocacheModel.geoNear(point, opts, (err, results) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (results) {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('findNear', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('findNear', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
getGeo: (e, id) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
GeocacheModel.findById(id, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('getGeos', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('getGeos', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
getGeos: (e) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
GeocacheModel.find({}, (err, results) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (results) {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('getGeos', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('getGeos', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
getGeoJSON: (e, searchText) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
queryGeodataApi(searchText, (f, err, geodata) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (geodata) {
|
||||
resolve(geodata.loc);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('getGeoJSON', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('getGeoJSON', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
getGeoJSONFromCache: (e, searchText) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
GeocacheModel.findOne({ key: sanitizeNameForKey(searchText) }, (err, result) => {
|
||||
if (err || !result) {
|
||||
queryGeodataApi(searchText, (f, err, geodata) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (geodata) {
|
||||
let geocacheInstance = new GeocacheModel(geodata);
|
||||
geocacheInstance.save((err, result) => {
|
||||
if (err) {
|
||||
logger.error('[Geocache::getGeoJSON] There was an error creating the GeoJSON entry.', { err: err });
|
||||
}
|
||||
|
||||
logger.debug()
|
||||
});
|
||||
resolve(geodata.loc);
|
||||
}
|
||||
});
|
||||
} else if (result) {
|
||||
resolve(result.loc);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('getGeoJSONFromCache', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('getGeoJSONFromCache', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
populateFormatted: (e) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
GeocacheModel.find({}, (err, results) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (results) {
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
if (!results[i].formatted) {
|
||||
results[i].formatted = results[i].georesult.formatted_address;
|
||||
GeocacheModel.findByIdAndUpdate(results[i]._id, { $set: results[i] }, (err, result) => {
|
||||
if (err) logger.error('There was an error populating the geocache formatted address.');
|
||||
if (result) logger.log('The geocache entry was updated');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
resolve({ status: 'OK', message: 'The geocache entries have been updated.'});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('populateFormattedAddresses', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('populateFormattedAddresses', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
populateKeys: (e) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
GeocacheModel.find({}, (err, results) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (results) {
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
if (!results[i].key) {
|
||||
results[i].key = sanitizeNameForKey(results[i].name);
|
||||
GeocacheModel.findByIdAndUpdate(results[i]._id, { $set: results[i] }, (err, result) => {
|
||||
if (err) logger.error('There was an error populating the geocache entry key.');
|
||||
if (result) logger.log('The geocache entry was updated');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
resolve({ status: 'OK', message: 'The geocache entries have been updated.'});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('populateKeys', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('populateKeys', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
update: (e, id, geodata) => {
|
||||
var cb = typeof e === 'object' && e.emit ? e.emit.bind(e) : e;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
GeocacheModel.findByIdAndUpdate(id, { $set: geodata }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
cb('update', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
cb('update', err, null);
|
||||
});
|
||||
}
|
||||
};
|
||||
55
backend/models/message.js
Normal file
55
backend/models/message.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const Images = require('../modules/images');
|
||||
const Logger = require('../modules/logger');
|
||||
const Mongoose = require('mongoose');
|
||||
|
||||
const MessageSchema = new Mongoose.Schema({
|
||||
"text" : { type: String },
|
||||
"image" : { type: String, index: true },
|
||||
"isUser" : { type: Boolean, default: false, required: true, index: true },
|
||||
"timestamp": { type: Date, default: Date.now() }
|
||||
});
|
||||
|
||||
MessageSchema.pre('findOneAndUpdate', function (next) {
|
||||
var message = this;
|
||||
|
||||
if (message.image && typeof message.image === 'object') {
|
||||
Images.saveMessageImage(message.image, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
message.image = filename;
|
||||
next();
|
||||
}
|
||||
})
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
MessageSchema.pre('save', function (next) {
|
||||
var message = this;
|
||||
|
||||
if (message.image && typeof message.image === 'object') {
|
||||
Images.saveMessageImage(message.image, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
message.image = filename;
|
||||
next();
|
||||
}
|
||||
})
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
const MessageModel = Mongoose.model('messages', MessageSchema);
|
||||
|
||||
module.exports = {
|
||||
schema: MessageSchema,
|
||||
model: MessageModel
|
||||
};
|
||||
612
backend/models/profile.js
Normal file
612
backend/models/profile.js
Normal file
@@ -0,0 +1,612 @@
|
||||
const Images = require('../modules/images');
|
||||
const Logger = require('../modules/logger');
|
||||
const Mailer = require('../modules/mailer');
|
||||
const Messages = require('../models/message');
|
||||
const Mongoose = require('mongoose');
|
||||
|
||||
function outputMessagesAsText (messages, asHtml = false) {
|
||||
var output = '';
|
||||
for (let i = 0; i < messages.length; i++) {
|
||||
if (asHtml) {
|
||||
output += (!message.isUser ? '<p><b>Question: ' : '<b>Response</b><br>') + message.text + (!message.isUser ? '</b>' : '') + '</p>';
|
||||
} else {
|
||||
output += (!message.isUser ? 'Question: ' : 'Response\r\r') + message.text + '\r\r\r\r';
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
const ProfileSchema = new Mongoose.Schema({
|
||||
"order" : { type: Number, default: 0 },
|
||||
"details": {
|
||||
"about": { type: String },
|
||||
"age": { type: Number, index: true, default: 0 },
|
||||
"location": { type: String },
|
||||
"name": { type: String, index: true },
|
||||
"pic": {
|
||||
"detail": { type: String, default: "profile/default_detail.png" },
|
||||
"thumb": { type: String, default: "profile/default_thumbnail.png" }
|
||||
}
|
||||
},
|
||||
"messages": [ { type: Messages.schema } ],
|
||||
"submitted": { type: Boolean, default: false },
|
||||
"approved": { type: Boolean, default: false }
|
||||
});
|
||||
|
||||
ProfileSchema.pre('findOneAndUpdate', function (next) {
|
||||
var cnt = 0
|
||||
var pic;
|
||||
|
||||
if (this.details && this.details.pic) {
|
||||
pic = this.details.pic;
|
||||
cnt = cnt + (typeof pic.detail === 'object' ? 1 : 0) + (typeof pic.thumb === 'object' ? 1 : 0);
|
||||
}
|
||||
|
||||
if (cnt > 0) {
|
||||
if (typeof pic.detail === 'object') {
|
||||
Images.saveProfileDetailImage(pic.detail, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
pic.detail = filename;
|
||||
cnt -= 1;
|
||||
if (cnt === 0) next();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof pic.thumb === 'object') {
|
||||
Images.saveProfileThumbnailImage(pic.thumb, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
pic.thumb = filename;
|
||||
cnt -= 1;
|
||||
if (cnt === 0) next();
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
ProfileSchema.pre('save', function (next) {
|
||||
var cnt = 0;
|
||||
var pic;
|
||||
|
||||
if (this.details && this.details.pic) {
|
||||
pic = this.details.pic
|
||||
cnt = cnt + (typeof pic.detail === 'object' ? 1 : 0) + (typeof pic.thumb === 'object' ? 1 : 0);
|
||||
}
|
||||
|
||||
if (cnt > 0) {
|
||||
if (typeof pic.detail === 'object') {
|
||||
Images.saveProfileDetailImage(pic.detail, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
pic.detail = filename;
|
||||
cnt -= 1;
|
||||
if (cnt === 0) next();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof pic.thumb === 'object') {
|
||||
Images.saveProfileThumbnailImage(pic.thumb, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
pic.thumb = filename;
|
||||
cnt -= 1;
|
||||
if (cnt === 0) next();
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
const ProfileModel = Mongoose.model('profiles', ProfileSchema);
|
||||
|
||||
function getChatImages (profileId, match, callback) {
|
||||
callback = callback || (typeof match === 'function' ? match : function(){});
|
||||
match = typeof match === 'object' ? match : {};
|
||||
match['messages.image'] = { $exists: true };
|
||||
|
||||
ProfileModel
|
||||
.aggregate([
|
||||
{ $match: { _id: Mongoose.Types.ObjectId(profileId), 'messages.image': { $exists: true } } },
|
||||
{ $unwind: '$messages' },
|
||||
{ $match: match },
|
||||
{ $replaceRoot: { newRoot: '$messages' } }
|
||||
])
|
||||
.project('image -_id')
|
||||
.exec(callback);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
all: (e) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel
|
||||
.find({})
|
||||
.sort({ order: 1 })
|
||||
.populate({
|
||||
path: 'messages',
|
||||
select: 'text image isUser timestamp',
|
||||
options: { sort: { timestamp: 1 } }
|
||||
})
|
||||
.exec((err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('all', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('all', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
allSubmitted: (e) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel
|
||||
.find({ submitted: true, approved: true })
|
||||
.sort({ order: 1 })
|
||||
.populate({
|
||||
path: 'messages',
|
||||
select: 'text image isUser timestamp',
|
||||
options: { sort: { timestamp: 1 } }
|
||||
})
|
||||
.exec((err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('allSubmitted', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('allSubmitted', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
allVerified: (e) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel
|
||||
.find({ submitted: { $in: [ null, false ] } })
|
||||
.sort({ order: 1 })
|
||||
.populate({
|
||||
path: 'messages',
|
||||
select: 'text image isUser timestamp',
|
||||
options: { sort: { timestamp: 1 } }
|
||||
})
|
||||
.exec((err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('allVerified', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('allVerified', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
allMessages: (e, profileId) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel
|
||||
.find({ _id: profileId })
|
||||
.select('messages')
|
||||
.populate({
|
||||
path: 'messages',
|
||||
select: 'text image isUser timestamp',
|
||||
options: { sort: { timestamp: 1 } }
|
||||
})
|
||||
.exec((err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result.messages || []);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('allMessages', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('allMessages', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
allChatImages: (e, profileId) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
getChatImages(profileId, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('allChatImages', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('allChatImages', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
allChatImagesReceived: (e, profileId) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
getChatImages(profileId, { 'messages.isUser': true }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('allChatImagesReceived', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('allChatImagesReceived', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
allChatImagesSent: (e, profileId) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
getChatImages(profileId, { 'messages.isUser': false }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('allChatImagesSent', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('allChatImagesSent', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
approveSubmission: (e, profileId) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel.findOneAndUpdate(
|
||||
{ _id: profileId },
|
||||
{ $set: { approved: true } },
|
||||
{ new: true },
|
||||
(err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('approveSubmission', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('approveSubmission', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
create: (e, profiles) => {
|
||||
var count = profiles.length;
|
||||
var errors = [];
|
||||
var results = [];
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
for (let i = 0; i < profiles.length; i++) {
|
||||
var profile = profiles[i];
|
||||
var profileInstance = new ProfileModel(profile);
|
||||
|
||||
profileInstance.save((err, result) => {
|
||||
if (err) {
|
||||
count -= 1;
|
||||
errors.push({
|
||||
profile: profile,
|
||||
error: err
|
||||
});
|
||||
if (count === 0) {
|
||||
reject({ results: results, errors: errors });
|
||||
}
|
||||
}
|
||||
|
||||
if (result) {
|
||||
count -= 1;
|
||||
results.push(result);
|
||||
if (count === 0) {
|
||||
resolve({ results: results, errors: errors });
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('create', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('create', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
delete: (e, id) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel.remove({ _id: id }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('delete', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('delete', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
deleteMessage: (e, profileId, messageId) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel.findById(profileId, (err, profile) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (profile) {
|
||||
let message = profile.messages.id(messageId);
|
||||
|
||||
if (message) {
|
||||
message.remove();
|
||||
profile.save((err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
reject('The specified message does not exist');
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('deleteMessage', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('deleteMessage', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
find: (e, find) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
var query = ProfileModel
|
||||
.find(find.find)
|
||||
.skip(find.options.skip)
|
||||
.limit(find.options.limit)
|
||||
.sort(find.options.sort)
|
||||
.select(find.select || '');
|
||||
|
||||
if (!find.select || (find.select.length && find.select.indexOf('messages'))) {
|
||||
query.populate({
|
||||
path: 'messages',
|
||||
select: 'order text image isUser',
|
||||
options: { sort: { order: 1 } }
|
||||
});
|
||||
}
|
||||
|
||||
query.exec((err, results) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (results) {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('find', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('find', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
get: (e, profileId) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel.findById(profileId, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('get', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('get', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
getMessage: (e, prodileId, messageId) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel.findById(prodileId, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
let message = result.messages.id(messageId);
|
||||
resolve(message ? message : {});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('getMessage', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('getMessage', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
processSubmission: (e, profile) => {
|
||||
var profileModel = {};
|
||||
profileModel.details = profile.details;
|
||||
profileModel.details.pic = { detail: profile.image };
|
||||
profileModel.messages = profile.messages;
|
||||
profileModel.submitted = true;
|
||||
profileModel.approved = false;
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
var profileInstance = new ProfileModel(profileData);
|
||||
|
||||
profileInstance.save((err, result) => {
|
||||
if (err) {
|
||||
reject({ success: false, message: 'Failed to save user submission.', err: err });
|
||||
}
|
||||
|
||||
if (result) {
|
||||
let approveProfileLink = 'https://api.fitz.guru/urnings/profiles/approve/' + encodeURIComponent(result._id);
|
||||
|
||||
// setup email data with unicode symbols
|
||||
let mail = {
|
||||
from: '"Urnings|looking App" <system@appsby.fitz.guru>',
|
||||
to: 'npfosi@gmail.com,mike@fitz.guru',
|
||||
subject: 'New Profile Submission',
|
||||
text: 'New Profile Submission\r\r\r\rName: ' + result.details.name + '(' + result.details.email + ')\r\rLocation: ' + result.details.location + '\r\rAbout: ' + result.details.about + '\r\rImage: ' + result.details.pic.detail + '\r\rMessages:\r' + outputMessagesAsText(result.messages) + '\r\r\r\rTo approve this profile: ' + approveProfileLink + '\r\r',
|
||||
html: '<h2>New Profile Submission<h2><p><b>Name:</b> ' + result.details.name + '(<a href="mailto:' + result.details.email + '">' + result.details.email + '</a>)<br><b>Location:</b> ' + result.details.location + '<p><p>About:<br>' + result.details.about + '</p><p><b>Image:</b><br><img src="' + result.details.pic.detail + '"></p><div>Messages:<br>' + outputMessagesAsText(result.messages, true) + '</div><p>To approve this profile: <a href="' + approveProfileLink + '">click here</a>.</p>',
|
||||
};
|
||||
|
||||
Mailer.send(mail, (mailErr, mailResult) => {
|
||||
resolve({ success: true, mailer: { err: mailErr, result: mailResult } });
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('processSubmission', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('processSubmission', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
update: (e, profileId, profile) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel.findOneAndUpdate(
|
||||
{ _id: profileId },
|
||||
{ $set: profile },
|
||||
{ new: true },
|
||||
(err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('update', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('update', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
updateMessage: (e, profileId, messageId, data) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ProfileModel.findOneAndUpdate(
|
||||
{ _id: profileId, 'messages._id': messageId },
|
||||
{ $set: { 'messages.$': data } },
|
||||
{ new: true },
|
||||
(err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('updateMessage', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('updateMessage', err, null);
|
||||
});
|
||||
}
|
||||
};
|
||||
325
backend/models/reset.js
Normal file
325
backend/models/reset.js
Normal file
@@ -0,0 +1,325 @@
|
||||
const Authentication = require('../modules/authentication');
|
||||
const Crypto = require('crypto');
|
||||
const Mongoose = require('mongoose');
|
||||
const Mailer = require('nodemailer');
|
||||
const Token = require('../modules/token');
|
||||
|
||||
const secret = 'Creepily hooking the gays up since 2008!';
|
||||
|
||||
var logger = require('../modules/logger');
|
||||
|
||||
function generateHmac (userId, expires) {
|
||||
var string = String(userId) + '|' + String(expires);
|
||||
return Crypto.createHmac('sha1', secret).update(string).digest('hex');
|
||||
}
|
||||
|
||||
function sendMail (options, callback) {
|
||||
// create reusable transporter object using the default SMTP transport
|
||||
let transporter = Mailer.createTransport({
|
||||
host: 'mail.fitz.guru',
|
||||
port: 587,
|
||||
secure: false, // secure:true for port 465, secure:false for port 587
|
||||
auth: {
|
||||
user: 'support@fitz.guru',
|
||||
pass: 'NotSt@ff3d!'
|
||||
}
|
||||
});
|
||||
|
||||
callback = typeof callback === 'function' ? callback : (error, info) => {
|
||||
if (error) {
|
||||
return logger.error(error);
|
||||
}
|
||||
|
||||
logger.debug('Message %s sent: %s', info.messageId, info.response);
|
||||
};
|
||||
|
||||
// send mail with defined transport object
|
||||
transporter.sendMail(options, callback);
|
||||
}
|
||||
|
||||
const ResetSchema = new Mongoose.Schema({
|
||||
user: { type: Mongoose.Schema.Types.ObjectId, required: true },
|
||||
expires: { type: Date, default: Date.now },
|
||||
used: { type: Boolean, default: false },
|
||||
updated_at: { type: Date, default: Date.now }
|
||||
});
|
||||
|
||||
const ResetModel = Mongoose.model('resets', ResetSchema);
|
||||
|
||||
module.exports = {
|
||||
checkReset: (e, id, token, callback) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ResetModel.findOne({ _id: id }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result && (token === generateHmac(result.user, result.expires)) && (Date.now() <= result.expires)) {
|
||||
resolve({ user: String(result.user), err: null });
|
||||
} else {
|
||||
resolve({ user: false, err: 'The reset link has expired. Please request a new reset link from the login page.'});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
if (e) {
|
||||
e.emit('checkReset', null, result);
|
||||
} else if (callback) {
|
||||
callback(null, result.user);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (e) {
|
||||
e.emit('checkReset', err, null);
|
||||
} else if (callback) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
forceReset: (e, user, callback) => {
|
||||
const promise = new Promise((forceResetMailResolve, forceResetMailReject) => {
|
||||
const forceResetTokenPromise = new Promise((forceResetTokenResolve, forceResetTokenReject) => {
|
||||
var tokenInstance = new ResetModel({ user: user._id, expires: (Date.now() + (72 * 60 * 60 * 1000)) });
|
||||
|
||||
tokenInstance.save((err, result) => {
|
||||
if (err) {
|
||||
forceResetTokenReject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
forceResetTokenResolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
forceResetTokenPromise.then((result) => {
|
||||
let resetLink = 'https://timberland.bizdex.cloud/reset/' + encodeURIComponent(result._id) + '/' + encodeURIComponent(generateHmac(result.user, result.expires));
|
||||
|
||||
// setup email data with unicode symbols
|
||||
let mail = {
|
||||
from: '"GCS Vendor Database" <system@timberland.bizdex.cloud>',
|
||||
to: user.email,
|
||||
subject: 'Mandatory Password Reset',
|
||||
text: 'Mandatory Password Reset\r\r\r\r' + user.name.first + ',\r\rA Timberland GCS Vendor Database Administrator has initiated a password reset on your account.\r\rTo complete the action you will need to reset you password <<' + resetLink + '>>.\r\rIf you have any questions, please contact a system administrator.',
|
||||
html: '<h2>Mandatory Password Reset</h2><p>' + user.name.first + ',</p><p>A Timberland GCS Vendor Database Administrator has initiated a password reset on your account.</p>To complete the action you will need to <a href="' + resetLink + '">reset your password</a>.</p><p><b>If you have any questions, please contact a system administrator.</b></p>'
|
||||
};
|
||||
|
||||
sendMail(mail, (error, info) => {
|
||||
if (error) {
|
||||
logger.debug('[reset::forceReset] Message Send Error', { error: error });
|
||||
forceResetMailResolve({ success: false, message: 'There was an error sending the message', error: error });
|
||||
}
|
||||
|
||||
if (info) {
|
||||
logger.debug('[reset::forceReset] Message sent', { messageId: info.messageId, response: info.response, resetLink: resetLink });
|
||||
forceResetMailResolve({ success: true, message: 'Message ' + info.messageId + ' sent: ' + info.response + '.' });
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.debug('[reset::forceReset] There was an error creating the reset token.', { err: err });
|
||||
forceResetMailReject(err);
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
if (e) {
|
||||
e.emit('forceReset', null, result);
|
||||
} else if (callback) {
|
||||
callback(result);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (e) {
|
||||
e.emit('forceReset', err, null);
|
||||
} else if (callback) {
|
||||
callback(err);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
getResets: (e) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ResetModel.find({}, (err, results) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (results) {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((results) => {
|
||||
e.emit('getResets', null, results);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('getResets', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
markUsed: (e, id) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
ResetModel.findByIdAndUpdate(id, { $set: { used: true } }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
if (e) {
|
||||
e.emit('markUsed', null, result);
|
||||
} else {
|
||||
logger.debug('[ResetModel::markUsed] Password reset token used', { token: result });
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (e) {
|
||||
e.emit('markUsed', err, null);
|
||||
} else {
|
||||
logger.error('[ResetModel::markUsed] Error marking password reset token used', { token: err });
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
sendNewUser: (e, user) => {
|
||||
const promise = new Promise((newUserMailResolve, newUserMailReject) => {
|
||||
const newUserTokenPromise = new Promise((newUserTokenResolve, newUserTokenReject) => {
|
||||
var tokenInstance = new ResetModel({ user: user._id, expires: (Date.now() + (72 * 60 * 60 * 1000)) });
|
||||
|
||||
tokenInstance.save((err, result) => {
|
||||
if (err) {
|
||||
newUserTokenReject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
newUserTokenResolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
newUserTokenPromise.then((result) => {
|
||||
let setPasswordLink = 'https://timberland.bizdex.cloud/reset/' + encodeURIComponent(result._id) + '/' + encodeURIComponent(generateHmac(result.user, result.expires));
|
||||
|
||||
// setup email data with unicode symbols
|
||||
let mail = {
|
||||
from: '"GCS Vendor Database" <system@timberland.bizdex.cloud>',
|
||||
to: user.email,
|
||||
subject: 'New User Account Setup',
|
||||
text: 'New User Account Setup\r\r\r\r' + user.name.first + ',\r\rA new user has been created for you on the Timberland GCS Vendor Database.\r\rYour username is: <<' + user.userName + '>>.\r\r To complete the setup you will need to create a password <<' + setPasswordLink + '>>.\r\rIf you have any questions, please contact a system administrator.',
|
||||
html: '<h2>New User Account Setup</h2><p>' + user.name.first + ',</p><p>A new user has been created for you on the Timberland GCS Vendor Database.</p><p>Your username is: <b>' + user.userName + '</b>.</p>To complete the setup you will need to <a href="' + setPasswordLink + '">create a password</a>.</p><p><b>If you have any questions, please contact a system administrator.</b></p>'
|
||||
};
|
||||
|
||||
sendMail(mail, (error, info) => {
|
||||
if (error) {
|
||||
logger.error('[reset::sendNewUser] Message Send Error', { error: error });
|
||||
newUserMailResolve({ success: false, message: 'There was an error sending the message', error: error });
|
||||
}
|
||||
|
||||
if (info) {
|
||||
logger.debug('[reset::sendNewUser] Message %s sent: %s', info.messageId, info.response);
|
||||
newUserMailResolve({ success: true, message: 'Message ' + info.messageId + ' sent: ' + info.response + '.' });
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.error('[reset::sendNewUser] There was an error creating the reset token.', { err: err });
|
||||
newUserMailReject(err);
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
if (e) {
|
||||
e.emit('sendNewUser', null, result);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (e) {
|
||||
e.emit('sendNewUser', err, null);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
sendReset: (e, user) => {
|
||||
|
||||
const resetTokenPromise = new Promise((resetTokenResolve, resetTokenReject) => {
|
||||
|
||||
var tokenInstance = new ResetModel({ user: user._id, expires: (Date.now() + (30 * 60 * 1000)) });
|
||||
|
||||
tokenInstance.save((err, result) => {
|
||||
if (err) {
|
||||
resetTokenReject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resetTokenResolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
resetTokenPromise.then((data) => {
|
||||
var token = generateHmac(data.user, data.expires);
|
||||
var tokenId = data._id;
|
||||
|
||||
const sendMailPromise = new Promise((sendMailResolve, sendMailReset) => {
|
||||
|
||||
var resetLink = 'https://timberland.bizdex.cloud/reset/' + encodeURIComponent(tokenId) + '/' + encodeURIComponent(token);
|
||||
|
||||
// setup email data with unicode symbols
|
||||
var mail = {
|
||||
from: '"GCS Vendor Database" <system@timberland.bizdex.cloud>',
|
||||
to: user.email,
|
||||
subject: 'Password Reset Request',
|
||||
text: user.name.first + ',\r\rA request has been received to reset your password. If you initiated please visit <<' + resetLink + '>>.\r\rIf you did not initiate this request, you can safely ignore this email.',
|
||||
html: '<h2>Password Reset Request</h2><p>' + user.name.first + ',</p><p>A request has been received to reset your password. If you initiated this request, <a href="' + resetLink + '">click here to reset your password</a>.</p><p><b>If you did not initiate this request, you can safely ignore this email.</b></p>'
|
||||
};
|
||||
|
||||
sendMail(mail, (err, info) => {
|
||||
if (err) {
|
||||
var error = { msg: '[reset::sendReset] There was an error sending the reset email.', err: err };
|
||||
logger.error('[reset::sendReset] Message Send Error', { err: err });
|
||||
sendMailResolve({ success: false, message: 'There was an error requesting the password reset.', error: error });
|
||||
}
|
||||
|
||||
if (info) {
|
||||
var message = 'Message ' + info.messageId + ' sent: ' + info.response + '.';
|
||||
logger.debug('[reset::sendReset] ' + message);
|
||||
sendMailResolve({ success: true, message: 'The password reset request was successfully completed.', response: message });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
sendMailPromise.then((result) => {
|
||||
e.emit('sendReset', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('sendReset', err, null);
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
var error = { msg: '[reset::sendReset] There was an error creating the reset token.', err: err };
|
||||
logger.debug(error.msg, { err: err });
|
||||
e.emit('sendReset', null, { success: false, message: 'There was an error requesting the password reset.', error: error });
|
||||
});
|
||||
}
|
||||
};
|
||||
549
backend/models/user.js
Normal file
549
backend/models/user.js
Normal file
@@ -0,0 +1,549 @@
|
||||
const Authentication = require('../modules/authentication');
|
||||
const Mongoose = require('mongoose');
|
||||
const Reset = require('./reset');
|
||||
|
||||
const UserSchema = new Mongoose.Schema({
|
||||
username: { type: String, required: true, unique: true },
|
||||
password: { type: String },
|
||||
name: { first: { type: String, required: true }, last: { type: String, required: true } },
|
||||
email: { type: String, required: true, unique: true },
|
||||
can: [{ type: String, enum: ['add','edit','delete','manage','super','update','view'], default: ['view'] }],
|
||||
forceReset: { type: Boolean, default: true },
|
||||
updated_at: { type: Date, default: Date.now }
|
||||
});
|
||||
|
||||
UserSchema.pre('findOneAndUpdate', function (next) {
|
||||
var self = this;
|
||||
|
||||
this.update({}, { $set: { updated_at: Date.now() } });
|
||||
|
||||
if (this._update.$set.settings && this._update.$set.settings.length) {
|
||||
|
||||
}
|
||||
|
||||
if (this._update.$set.password && this._update.$set.confirmPassword && (this._update.$set.password == this._update.$set.confirmPassword)) {
|
||||
delete this._update.$set.confirmPassword;
|
||||
|
||||
if (this._update.$set.currentPassword) {
|
||||
confirmPassword(this._conditions.userName, this._update.$set.currentPassword, (err, valid) => {
|
||||
if (err || !valid) {
|
||||
err = new Error({ success: false, message: 'There was an error validating the current password.', err: (err || null) });
|
||||
next(err);
|
||||
}
|
||||
|
||||
if (valid) {
|
||||
delete this._update.$set.currentPassword;
|
||||
hashPassword(this._update.$set.password, (err, hashedPassword) => {
|
||||
self.update({}, { $set: { password: hashedPassword } });
|
||||
self.update({}, { $set: { forceReset: false } });
|
||||
next();
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
hashPassword(this._update.$set.password, (err, hashedPassword) => {
|
||||
self.update({}, { $set: { password: hashedPassword } });
|
||||
self.update({}, { $set: { forceReset: false } });
|
||||
next();
|
||||
});
|
||||
}
|
||||
} else if (this._update.$set.password && this._update.$set.confirmPassword && (this._update.$set.password != this._update.$set.confirmPassword)) {
|
||||
let err = new Error({ success: false, message: 'There was an error saving the updated password.'});
|
||||
next(err);
|
||||
} else if (!this._update.$set.password && !this._update.$set.confirmPassword) {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
UserSchema.post('save', function (err, res, next) {
|
||||
if (err.name === 'MongoError' && err.code === 11000) {
|
||||
next(new Error('There was a duplicate key error'));
|
||||
} else if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
const UserModel = Mongoose.model('users', UserSchema);
|
||||
|
||||
var logger = require('../modules/logger');
|
||||
|
||||
function hashPassword (password, callback) {
|
||||
callback = callback || false;
|
||||
|
||||
Authentication.hashPassword(password, (err, password) => {
|
||||
if (err !== null) {
|
||||
err = new Error({ success: false, message: 'There was an error hashing the updated password.', err: err });
|
||||
logger.error('[updateUser:hashPassword] ', err);
|
||||
|
||||
if (callback) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (password) {
|
||||
var result = password.toString('hex');
|
||||
|
||||
if (callback) {
|
||||
callback(null, result);
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function confirmPassword (username, passwordToValidate, callback) {
|
||||
callback = callback || false;
|
||||
|
||||
UserModel.findById({ userName: username }, (err, user) => {
|
||||
if (err !== null) {
|
||||
err = new Error({ success: false, message: 'There was an error locating the user.', err: (err || null) });
|
||||
logger.error('[updateUser:confirmPassword] ', err);
|
||||
|
||||
if (callback) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (user) {
|
||||
Authentication.verifyPassword(passwordToValidate, Buffer.from(storedUser.password, 'hex'), (err, valid) => {
|
||||
if (err !== null || !valid) {
|
||||
err = new Error({ success: false, message: (!err && !valid ? 'The current password was incorrect.' : 'There was an error attempting to validate the password.'), err: (err || null) });
|
||||
logger.error('[updateUser:confirmPassword] ', { err: err, valid: valid });
|
||||
|
||||
if (callback) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (valid) {
|
||||
if (callback) {
|
||||
callback(null, valid);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
adminUpdatePassword: (e, username, password) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
UserModel.findOneAndUpdate({ userName: username }, { $set: password }, { new: true }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
if (e) {
|
||||
e.emit('adminUpdatePassword', null, result);
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (e) {
|
||||
e.emit('adminUpdatePassword', err, null);
|
||||
} else {
|
||||
return err;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
authenticateUser: (e, login, headers) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
var loginObject, user;
|
||||
|
||||
UserModel
|
||||
.findOne({ userName: login.userName }, 'username name email password can forceReset')
|
||||
.exec((err, result) => {
|
||||
if (err) {
|
||||
loginObject = {
|
||||
status: 200,
|
||||
authorized: false,
|
||||
err: {
|
||||
id: '005',
|
||||
code: '[UMAU005]',
|
||||
string: 'There was an error authenticating the user.'
|
||||
}
|
||||
}
|
||||
logger.debug('[UserModel::authenticateUser] Error finding user', { err: err, username: login.userName });
|
||||
resolve(loginObject);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
user = result;
|
||||
|
||||
if (user && !user.permission.disabled) {
|
||||
if (user.forceReset) {
|
||||
loginObject = {
|
||||
status: 200,
|
||||
authorized: false,
|
||||
err: {
|
||||
id: '003',
|
||||
code: '[UMAU003]',
|
||||
string: 'A password reset has been mandated. Please check your email for a password reset link or request a new one from the login screen.'
|
||||
}
|
||||
};
|
||||
resolve(loginObject);
|
||||
}
|
||||
else {
|
||||
try {
|
||||
Authentication.verifyPassword(login.password, Buffer.from(user.password, 'hex'), (err, valid) => {
|
||||
if (err) {
|
||||
logger.debug('[UserModel::authenticateUser] Error validating password', { err: err, user: user });
|
||||
reject(err);
|
||||
}
|
||||
|
||||
loginObject = {
|
||||
status: 200,
|
||||
authorized: valid
|
||||
};
|
||||
|
||||
if (valid) {
|
||||
loginObject.user = {
|
||||
_id: user._id,
|
||||
username: user.username,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
can: user.can
|
||||
};
|
||||
|
||||
loginObject.timestamp = Date.now();
|
||||
|
||||
logger.debug('[UserModel::authenticateUser] User Validated', { user: user, loginObject: loginObject });
|
||||
resolve(loginObject);
|
||||
} else {
|
||||
loginObject.err = {
|
||||
id: '002',
|
||||
code: '[UMAU002]',
|
||||
string: 'The user id or password you entered was invalid.'
|
||||
};
|
||||
|
||||
logger.debug('[UserModel::authenticateUser] Invalid Password', { user: user, loginObject: loginObject });
|
||||
resolve(loginObject);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
loginObject = {
|
||||
status: 200,
|
||||
authorized: false,
|
||||
err: {
|
||||
id: '004',
|
||||
code: '[UMAU004]',
|
||||
string: 'There was an error authenticating the user, please contact an administrator.'
|
||||
}
|
||||
};
|
||||
|
||||
logger.error('[UserModel::authenticateUser] Error verifying password', { err: err, user: user });
|
||||
resolve(loginObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (user && user.permission.disabled) {
|
||||
loginObject = {
|
||||
status: 200,
|
||||
authorized: false,
|
||||
err: {
|
||||
id: '000',
|
||||
code: '[UMAU000]',
|
||||
string: 'The user is not authorized, please contact an administrator.'
|
||||
}
|
||||
};
|
||||
logger.debug('[UserModel::authenticateUser] The user is disabled', { err: err, user: user });
|
||||
resolve(loginObject);
|
||||
}
|
||||
else {
|
||||
loginObject = {
|
||||
status: 200,
|
||||
authorized: false,
|
||||
err: {
|
||||
id: '001',
|
||||
code: '[UMAU001]',
|
||||
string: 'The user id or password you entered was invalid.'
|
||||
}
|
||||
};
|
||||
logger.debug('[UserModel::authenticateUser] The user does not exist', { err: err, user: user });
|
||||
resolve(loginObject);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('authenticateUser', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('authenticateUser', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
createUser: (e, user) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
var userInstance = new UserModel(user);
|
||||
|
||||
userInstance.save((err, result) => {
|
||||
logger.debug('createUser', { err: err, result: result, user: userInstance });
|
||||
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
Reset.sendNewUser(null, result);
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('createUser', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('createUser', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
deleteUser: (e, id) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
UserModel.remove({ _id: id }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('deleteUser', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('deleteUser', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
findUser: (query, callback) => {
|
||||
UserModel.findOne(query, (err, result) => {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
callback(null, result);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
forcePasswordReset: (e, id) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
UserModel.findByIdAndUpdate(id, { $set: { forceReset: true } }, { new: true }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
let resetPromise = new Promise((resetResolve, resetReject) => {
|
||||
Reset.forceReset(null, result, (result) => {
|
||||
if (result.success) {
|
||||
resetResolve({ success: true, message: 'Force password reset initiated on the user.', result: result });
|
||||
} else {
|
||||
resetResolve({ success: false, message: 'There was an error initiating the forced password reset.', result: result });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
resetPromise.then((result) => {
|
||||
resolve(result);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('forcePasswordReset', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('forcePasswordReset', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
getUsers: (e, query, restricted = true) => {
|
||||
query = query || { find: {}, options: { sort: { 'name.last': 1, 'name.first': 1 }, limit: 0, skip: 0 }};
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
var projection = 'userName name' + (restricted ? '' : ' title email permission avatar settings forceReset');
|
||||
var query = UserModel.find(query.find, projection, query.options);
|
||||
|
||||
if (!restricted) {
|
||||
query.populate('permission', 'name disabled manageRoles manageUsers manageCategories manageAppPreferences deleteVendor addVendorTag addVendorSample addVendorComment editVendor addNewVendor viewPrivateDetails viewPublicDetails')
|
||||
}
|
||||
|
||||
query.exec((err, results) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (results) {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((results) => {
|
||||
if (e) {
|
||||
e.emit('getUsers', null, results);
|
||||
} else {
|
||||
return results;
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (e) {
|
||||
e.emit('getUsers', err, null);
|
||||
} else {
|
||||
return err;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
getUser: (e, id, restricted = true) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
var projection = 'userName name' + (restricted ? '' : ' title email permission avatar settings forceReset');
|
||||
var query = UserModel
|
||||
.findById(id)
|
||||
.projection(projection);
|
||||
|
||||
if (!restricted) {
|
||||
query.populate('permission', 'name disabled manageRoles manageUsers manageCategories manageAppPreferences deleteVendor addVendorTag addVendorSample addVendorComment editVendor addNewVendor viewPrivateDetails viewPublicDetails');
|
||||
}
|
||||
|
||||
query.exec((err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
if (e) {
|
||||
e.emit('getUser', null, result);
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (e) {
|
||||
e.emit('getUser', err, null);
|
||||
} else {
|
||||
return err;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
isUserNameUnique: (e, username) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
UserModel.findOne({ userName: username }, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve({ unique: false, length: true });
|
||||
} else {
|
||||
resolve({ unique: true, length: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('isUserNameUnique', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('isUserNameUnique', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
updateUser: (e, id, user) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
UserModel
|
||||
.findByIdAndUpdate(id, { $set: user }, { new: true })
|
||||
.populate('permission', 'name disabled manageRoles manageUsers manageCategories manageAppPreferences deleteVendor addVendorTag addVendorSample addVendorComment editVendor addNewVendor viewPrivateDetails viewPublicDetails')
|
||||
.exec((err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('updateUser', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('updateUser', err, null);
|
||||
});
|
||||
},
|
||||
|
||||
updatePassword: (e, id, token, data) => {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
Reset.checkReset(null, id, token, (err, validatedId) => {
|
||||
if (data.userId === validatedId) {
|
||||
UserModel.findByIdAndUpdate(data.userId, { $set: { password: data.password, confirmPassword: data.confirmPassword, forceReset: false } }, { new: true }, (err, updated) => {
|
||||
if (err) {
|
||||
if (err.success === false) {
|
||||
resolve(err);
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
if (updated) {
|
||||
Reset.markUsed(null, id);
|
||||
resolve({ success: true, updated: updated });
|
||||
}
|
||||
});
|
||||
} else {
|
||||
resolve({ success: false, message: 'The password reset link is not valid. Please request a new link.'});
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
promise.then((result) => {
|
||||
e.emit('updatePassword', null, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
e.emit('updatePassword', err, null);
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user