Added 'in' query to messages endpoint

This commit is contained in:
Halla Moore 2016-06-30 15:23:34 -07:00
parent 6225f09018
commit aec946f7de
2 changed files with 52 additions and 18 deletions

View file

@ -14,4 +14,24 @@ module.exports = {
})
})
},
findFolderOrLabel: function findFolderOrLabel({Folder, Label}, str) {
return Promise.props({
folder: Folder.find({
where: { $or: [
{ id: str },
{ name: str },
{ role: str },
]},
}),
label: Label.find({
where: { $or: [
{ id: str },
{ name: str },
{ role: str },
]},
}),
}).then(({folder, label}) =>
folder || label || null
)
},
}

View file

@ -1,6 +1,6 @@
const Joi = require('joi');
const Serialization = require('../serialization');
const {createSyncbackRequest} = require('../route-helpers');
const {createSyncbackRequest, findFolderOrLabel} = require('../route-helpers');
module.exports = (server) => {
@ -13,15 +13,16 @@ module.exports = (server) => {
tags: ['messages'],
validate: {
query: {
unread: Joi.boolean(),
starred: Joi.boolean(),
subject: Joi.string(),
thread_id: Joi.number().integer().min(0),
received_before: Joi.date(),
received_after: Joi.date(),
filename: Joi.string(),
limit: Joi.number().integer().min(1).max(2000).default(100),
offset: Joi.number().integer().min(0).default(0),
'unread': Joi.boolean(),
'starred': Joi.boolean(),
'subject': Joi.string(),
'thread_id': Joi.number().integer().min(0),
'received_before': Joi.date(),
'received_after': Joi.date(),
'filename': Joi.string(),
'in': Joi.string(),
'limit': Joi.number().integer().min(1).max(2000).default(100),
'offset': Joi.number().integer().min(0).default(0),
},
},
response: {
@ -35,7 +36,7 @@ module.exports = (server) => {
const {Message, Folder, Label, File} = db;
const query = request.query;
const where = {};
const include = [{model: Folder}, {model: Label}];
const include = [];
if (query.unread != null) {
where.unread = query.unread;
@ -66,13 +67,26 @@ module.exports = (server) => {
})
}
Message.findAll({
where: where,
limit: query.limit,
offset: query.offset,
include: include,
}).then((messages) => {
reply(Serialization.jsonStringify(messages));
let loadAssociatedModels = Promise.resolve();
if (query.in) {
loadAssociatedModels = findFolderOrLabel({Folder, Label}, query.in)
.then((container) => {
include.push({
model: container.Model,
where: {id: container.id},
})
})
}
loadAssociatedModels.then(() => {
Message.findAll({
where: where,
limit: query.limit,
offset: query.offset,
include: include,
}).then((messages) => {
reply(Serialization.jsonStringify(messages));
})
})
})
},