Files
pfosi-looking-api/models/profile.js
2018-03-02 03:00:51 -05:00

208 lines
4.5 KiB
JavaScript

const fs = require('fs');
const DetailSchema = require('../models/detail');
const MessageSchema = 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": { type: DetailSchema },
"messages" : [ { type: ObjectId } ]
});
const ProfileModel = Mongoose.model('profiles', ProfileSchema);
module.exports = {
all: (e) => {
const promise = new Promise((resolve, reject) => {
var model = VendorModel
.find({})
.sort({ order: 1 })
.populate({
path: 'details'
})
.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.length || (find.select.length && find.length.indexOf('details'))) {
query.populate({
path: 'details'
});
}
if (!find.select.length || (find.select.length && find.length.indexOf('messages'))) {
query.populate({
path: 'messages',
select: 'order text image isUser',
options: { sort: { order: 1 } }
});
}
query.exec((err, results) => {
if (err) {
reject(err);
}
if (result) {
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 })
.populate({
path: 'details'
})
.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('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) => {}
};