This commit is contained in:
2018-03-04 21:21:35 -05:00
parent 173c7e2d68
commit f23c329662
3 changed files with 263 additions and 253 deletions

View File

@@ -1,137 +1,140 @@
const Images = require('../modules/images');
const Logger = require('../modules/logger');
const Messages = require('../models/message');
const Mongoose = require('mongoose');
const Mongoose = require('mongoose');
const ProfileSchema = new Mongoose.Schema({
"order" : { type: Number, default: 0 },
"details": {
"about": { type: String },
"age": { type: Number, index: true, defaulrt: 0 },
"body": { type: String },
"ethnicity": { type: String },
"gender": { type: String },
"height": { type: String },
"looking": { type: String },
"name": { type: String, index: true },
"pic": {
"detail": { type: String, default: "profile/default_detail.png" },
"thumb": { type: String, default: "profile/default_thumbnail.png" }
},
"position": { type: String },
"pronouns": { type: String },
"weight": { type: Number },
"status": { type: String },
"tested": { type: Date },
"tribe": { type: String }
},
"messages" : [ { type: Messages.schema } ]
"order" : { type: Number, default: 0 },
"details": {
"about": { type: String },
"age": { type: Number, index: true, defaulrt: 0 },
"body": { type: String },
"ethnicity": { type: String },
"gender": { type: String },
"height": { type: String },
"looking": { type: String },
"name": { type: String, index: true },
"pic": {
"detail": { type: String, default: "profile/default_detail.png" },
"thumb": { type: String, default: "profile/default_thumbnail.png" }
},
"position": { type: String },
"pronouns": { type: String },
"weight": { type: Number },
"status": { type: String },
"tested": { type: Date },
"tribe": { type: String }
},
"messages" : [ { type: Messages.schema } ]
});
ProfileSchema.pre('findOneAndUpdate', function (next) {
var cnt = 0;
var cnt = 0
var pic;
if (this.details && this.details.pic) {
var pic = this.details.pic
cnt = cnt + (typeof pic.detail === 'object' ? 1 : 0) + (typeof pic.thumb === 'object' ? 1 : 0);
}
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();
}
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 cnt = 0;
var pic;
if (this.details && this.details.pic) {
var pic = this.details.pic
cnt = cnt + (typeof pic.detail === 'object' ? 1 : 0) + (typeof pic.thumb === 'object' ? 1 : 0);
}
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();
}
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);
module.exports = {
all: (e) => {
module.exports = {
all: (e) => {
const promise = new Promise((resolve, reject) => {
ProfileModel
.find({})
.sort({ order: 1 })
.populate({
path: 'messages',
select: 'order text image isUser',
options: { sort: { order: 1 } }
})
.exec((err, result) => {
if (err) {
reject(err);
}
.populate({
path: 'messages',
select: 'order text image isUser',
options: { sort: { order: 1 } }
})
.exec((err, result) => {
if (err) {
reject(err);
}
if (result) {
resolve(result);
}
});
if (result) {
resolve(result);
}
});
});
promise.then((result) => {
@@ -140,19 +143,19 @@ module.exports = {
.catch((err) => {
e.emit('all', err, null);
});
},
},
allMessages: (e, prodileId) => {
const promise = new Promise((resolve, reject) => {
const promise = new Promise((resolve, reject) => {
ProfileModel.findById(prodileId, (err, result) => {
if (err) {
reject(err);
}
if (err) {
reject(err);
}
if (result) {
resolve(result.messages || []);
}
});
if (result) {
resolve(result.messages || []);
}
});
});
promise.then((result) => {
@@ -164,19 +167,19 @@ module.exports = {
},
allMessageImages: (e, profileId) => {
const promise = new Promise((resolve, reject) => {
const promise = new Promise((resolve, reject) => {
ProfileModel
.find({ _id: prodileId, 'messages.image': { $exists: true } })
.select('image')
.exec((err, result) => {
if (err) {
reject(err);
}
.find({ _id: profileId, 'messages.image': { $exists: true } })
.select('image')
.exec((err, result) => {
if (err) {
reject(err);
}
if (result) {
resolve(result);
}
});
if (result) {
resolve(result);
}
});
});
promise.then((result) => {
@@ -188,36 +191,36 @@ module.exports = {
},
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);
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 });
}
}
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 });
}
}
});
}
if (result) {
count -= 1;
results.push(result);
if (count === 0) {
resolve({ results: results, errors: errors });
}
}
});
}
});
@@ -251,31 +254,31 @@ module.exports = {
},
deleteMessage: (e, profileId, messageId) => {
const promise = new Promise((resolve, reject) => {
ProfileModel.findById(prodileId, (err, profile) => {
if (err) {
reject(err);
}
const promise = new Promise((resolve, reject) => {
ProfileModel.findById(profileId, (err, profile) => {
if (err) {
reject(err);
}
if (result) {
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');
}
}
});
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) => {
@@ -287,31 +290,31 @@ module.exports = {
},
find: (e, find) => {
const promise = new Promise((resolve, reject) => {
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 || '');
.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);
}
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);
}
});
if (results) {
resolve(results);
}
});
});
promise.then((result) => {
@@ -323,16 +326,16 @@ module.exports = {
},
get: (e, id) => {
const promise = new Promise((resolve, reject) => {
const promise = new Promise((resolve, reject) => {
ProfileModel.findById(id, (err, result) => {
if (err) {
reject(err);
}
if (err) {
reject(err);
}
if (result) {
resolve(result);
}
});
if (result) {
resolve(result);
}
});
});
promise.then((result) => {
@@ -344,17 +347,17 @@ module.exports = {
},
getMessage: (e, prodileId, messageId) => {
const promise = new Promise((resolve, reject) => {
const promise = new Promise((resolve, reject) => {
ProfileModel.findById(prodileId, (err, result) => {
if (err) {
reject(err);
}
if (err) {
reject(err);
}
if (result) {
let message = result.messages.id(messageId);
resolve(message ? message : {});
}
});
if (result) {
let message = result.messages.id(messageId);
resolve(message ? message : {});
}
});
});
promise.then((result) => {
@@ -368,18 +371,18 @@ module.exports = {
update: (e, id, profile) => {
const promise = new Promise((resolve, reject) => {
ProfileModel.findOneAndUpdate(
{ _id: profileId },
{ $set: profile },
{ new: true },
(err, result) => {
if (err) {
reject(err);
}
{ _id: id },
{ $set: profile },
{ new: true },
(err, result) => {
if (err) {
reject(err);
}
if (result) {
resolve(result);
}
});
if (result) {
resolve(result);
}
});
});
promise.then((result) => {
@@ -393,18 +396,18 @@ module.exports = {
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);
}
{ _id: profileId, 'messages._id': messageId },
{ $set: { 'messages.$': data } },
{ new: true },
(err, result) => {
if (err) {
reject(err);
}
if (result) {
resolve(result);
}
});
if (result) {
resolve(result);
}
});
});
promise.then((result) => {