From f6557f602f5124d5c90019cd90cf5257dbc00ef5 Mon Sep 17 00:00:00 2001 From: piotrruss Date: Fri, 13 Aug 2021 21:36:45 +0200 Subject: crypt notes title --- apps/Notes/components/List.js | 1 - models/NoteList.js | 42 ++++++++++++++++++++++++++++++++++++++++++ pages/api/notes.js | 31 ++++++++----------------------- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/apps/Notes/components/List.js b/apps/Notes/components/List.js index 8561d4f..19a5643 100644 --- a/apps/Notes/components/List.js +++ b/apps/Notes/components/List.js @@ -24,7 +24,6 @@ const List = () => { return

Loading...

} - return ( <> { diff --git a/models/NoteList.js b/models/NoteList.js index f07ea50..1cac218 100644 --- a/models/NoteList.js +++ b/models/NoteList.js @@ -1,5 +1,8 @@ +const {encrypt, decrypt} = require('lib/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: { @@ -16,6 +19,45 @@ const noteListSchema = new mongoose.Schema({ }] }) + +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 ? 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 ? title : 'No title') } }, + { new: true } + ).lean() + + return decryptTitles(noteList) +} + const NoteList = mongoose.models.NoteList || mongoose.model('NoteList', noteListSchema) export default NoteList diff --git a/pages/api/notes.js b/pages/api/notes.js index dd31c6f..79ab281 100644 --- a/pages/api/notes.js +++ b/pages/api/notes.js @@ -15,7 +15,7 @@ export default withSession(async (req, res) => { throw new Error('Something went wrong') } - const {notes} = await NoteList.findById(user.noteList) + const {notes} = await NoteList.getList(user.noteList) res.status(200).json(notes) } catch (error) { @@ -32,12 +32,9 @@ export default withSession(async (req, res) => { } const note = await Note.create({content}) + const {notes} = await NoteList.addNote(user.noteList, note._id, title) - const noteList = await NoteList.findById(user.noteList) - noteList.notes.push({title: title ? title : 'No title', noteId: note}) - await noteList.save() - - res.status(200).json(noteList.notes) + res.status(200).json(notes) } catch (error) { res.status(400).json([]) } @@ -51,20 +48,13 @@ export default withSession(async (req, res) => { throw new Error('Something went wrong') } - const noteList = await NoteList.findById(user.noteList) - const noteId = noteList.notes - .find(n => n._id.toString() === _id.toString()).noteId - - if (!noteList || !noteId) { - throw new Error('Something went wrong') - } + const noteId = await NoteList.getNoteId(user.noteList, _id) + if ( !noteId) throw new Error('Something went wrong') await Note.findByIdAndDelete(noteId) + const {notes} = await NoteList.removeNote(user.noteList, _id) - noteList.notes.pull(_id) - await noteList.save() - - res.status(200).json(noteList.notes) + res.status(200).json(notes) } catch (error) { res.status(400).json([]) } @@ -79,12 +69,7 @@ export default withSession(async (req, res) => { } await Note.updateNote(noteId, content) - - const notes = await NoteList.findOneAndUpdate( - { _id: user.noteList, "notes.noteId": noteId }, - { $set: { "notes.$.title": title ? title : 'No title' } }, - { new: true } - ) + const {notes} = await NoteList.updateList(user.noteList, noteId, title) res.status(200).json(notes) } catch (error) { -- cgit v1.2.3