diff options
Diffstat (limited to 'apps/Notes/models')
-rw-r--r-- | apps/Notes/models/Note.js | 37 | ||||
-rw-r--r-- | apps/Notes/models/NoteList.js | 68 |
2 files changed, 105 insertions, 0 deletions
diff --git a/apps/Notes/models/Note.js b/apps/Notes/models/Note.js new file mode 100644 index 0000000..0f4b3f9 --- /dev/null +++ b/apps/Notes/models/Note.js @@ -0,0 +1,37 @@ +const { encrypt, decrypt } = require('helpers/crypt') +const mongoose = require('mongoose') + +const noteSchema = new mongoose.Schema({ + content: { type: String, required: true, minlength: 1 } +}) + +noteSchema.statics.getNote = async (id) => { + const note = await Note.findById(id) + if (!note) throw new Error('Could not fetch note') + + const content = decrypt(note.content) + + return { ...note, content } +} + +noteSchema.statics.updateNote = async (id, content) => { + const note = await Note.findByIdAndUpdate(id, { content: encrypt(content) }, { new: true }) + + if (!note) throw new Error('Could not update note') + + return { ...note, content } +} + +noteSchema.pre('save', async function (next) { + const note = this + + if (note.isModified('content')) { + note.content = encrypt(note.content) + } + + next() +}) + +const Note = mongoose.models.Note || mongoose.model('Note', noteSchema) + +export default Note diff --git a/apps/Notes/models/NoteList.js b/apps/Notes/models/NoteList.js new file mode 100644 index 0000000..0af648a --- /dev/null +++ b/apps/Notes/models/NoteList.js @@ -0,0 +1,68 @@ +const { encrypt, decrypt } = require('helpers/crypt') +const mongoose = require('mongoose') + +const decryptTitles = l => ({ notes: l.notes.map(n => ({ ...n, title: decrypt(n.title) })) }) + +const noteListSchema = new mongoose.Schema({ + notes: [{ + title: { + type: String, + maxlength: 1000, + required: true + }, + noteId: { + type: mongoose.Schema.Types.ObjectId, + ref: 'Note', + required: true + }, + created_at: { type: Date, default: Date.now }, + updated_at: { type: Date, default: Date.now } + }] +}) + +noteListSchema.statics.getList = async (id) => { + const newList = await NoteList.findById(id).lean() + + return { notes: newList.notes.map(n => ({ ...n, title: decrypt(n.title) })) } +} + +noteListSchema.statics.getNoteId = async (_id, id) => { + const noteList = await NoteList.findById(_id).lean() + return noteList.notes + .find(n => n._id.toString() === id.toString()).noteId +} + +noteListSchema.statics.addNote = async (_id, noteId, title) => { + const noteList = await NoteList.findOneAndUpdate( + { _id }, { $push: { notes: { title: encrypt(title || 'no title'), noteId } } }, { new: true } + ).lean() + + return decryptTitles(noteList) +} + +noteListSchema.statics.removeNote = async (_id, id) => { + const noteList = await NoteList.findOneAndUpdate( + { _id }, { $pull: { notes: { _id: id } } } + ).lean() + + return decryptTitles(noteList) +} + +noteListSchema.statics.updateList = async (_id, noteId, title) => { + const noteList = await NoteList.findOneAndUpdate( + { _id, 'notes.noteId': noteId }, + { + $set: { + 'notes.$.title': encrypt(title || 'No title'), + 'notes.$.updated_at': Date.now() + } + }, + { new: true } + ).lean() + + return decryptTitles(noteList) +} + +const NoteList = mongoose.models.NoteList || mongoose.model('NoteList', noteListSchema) + +export default NoteList |