208 lines
4.6 KiB
JavaScript
208 lines
4.6 KiB
JavaScript
const fs = require('fs');
|
|
const Messages = require('../models/message');
|
|
const Mongoose = require('mongoose');
|
|
const ShortId = require('shortid');
|
|
|
|
const ATTACHMENT_STORE = '../images';
|
|
const ATTACHMENT_STORE_PROFILE = '/profile';
|
|
const ATTACHMENT_STORE_MESSAGE = '/message';
|
|
const ATTACHMENT_SUFFIX_DETAIL = '_detail';
|
|
const ATTACHMENT_SUFFIX_THUMBNAIL = '_thumbnail';
|
|
|
|
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 } ]
|
|
});
|
|
|
|
const ProfileModel = Mongoose.model('profiles', ProfileSchema);
|
|
|
|
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);
|
|
}
|
|
|
|
if (result) {
|
|
resolve(result);
|
|
}
|
|
});
|
|
});
|
|
|
|
promise.then((result) => {
|
|
e.emit('all', null, result);
|
|
})
|
|
.catch((err) => {
|
|
e.emit('all', 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);
|
|
});
|
|
},
|
|
|
|
find: (e, find) => {
|
|
const promise = new Promise((resolve, reject) => {
|
|
var query = ProfileModel.find(find);
|
|
|
|
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, id) => {
|
|
const promise = new Promise((resolve, reject) => {
|
|
ProfileModel.find({ _id: id }, (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);
|
|
});
|
|
},
|
|
|
|
update: (e, id, profile) => {
|
|
const promise = new Promise((resolve, reject) => {
|
|
ProfileModel.findByIdAndUpdate(id, { $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) => {}
|
|
};
|