egirlskey/src/server/api/endpoints/notes/show.ts

33 lines
648 B
TypeScript
Raw Normal View History

2018-04-07 17:30:37 +00:00
/**
* Module dependencies
*/
2018-04-24 09:13:06 +00:00
import $ from 'cafy'; import ID from '../../../../cafy-id';
2018-04-07 17:30:37 +00:00
import Note, { pack } from '../../../../models/note';
/**
* Show a note
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'noteId' parameter
2018-04-27 10:12:15 +00:00
const [noteId, noteIdErr] = $(params.noteId).type(ID).get();
2018-04-07 17:30:37 +00:00
if (noteIdErr) return rej('invalid noteId param');
// Get note
const note = await Note.findOne({
_id: noteId
});
if (note === null) {
return rej('note not found');
}
// Serialize
res(await pack(note, user, {
detail: true
}));
});