aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/Note.js37
-rw-r--r--models/NoteList.js66
-rw-r--r--models/User.js55
3 files changed, 27 insertions, 131 deletions
diff --git a/models/Note.js b/models/Note.js
deleted file mode 100644
index d19eae8..0000000
--- a/models/Note.js
+++ /dev/null
@@ -1,37 +0,0 @@
-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/models/NoteList.js b/models/NoteList.js
deleted file mode 100644
index bf7b155..0000000
--- a/models/NoteList.js
+++ /dev/null
@@ -1,66 +0,0 @@
-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 ? 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'),
- "notes.$.updated_at": Date.now(),
- }},
- {new: true}
- ).lean()
-
- return decryptTitles(noteList)
-}
-
-const NoteList = mongoose.models.NoteList || mongoose.model('NoteList', noteListSchema)
-
-export default NoteList
diff --git a/models/User.js b/models/User.js
index 44d9619..bcf2523 100644
--- a/models/User.js
+++ b/models/User.js
@@ -1,6 +1,6 @@
-const mongoose = require("mongoose")
+const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')
-const {nanoid} = require('nanoid')
+const { nanoid } = require('nanoid')
const userResponse = u => ({
_id: u._id,
@@ -20,7 +20,7 @@ const userSchema = new mongoose.Schema({
required: true,
minlength: 6,
maxlength: 255,
- validate(value) {
+ validate (value) {
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
throw new Error('Wrong email address')
}
@@ -30,29 +30,29 @@ const userSchema = new mongoose.Schema({
type: String,
required: true,
minlength: 6,
- maxlength: 1024,
+ maxlength: 1024
},
isVerified: {
type: Boolean,
- default: false,
+ default: false
},
verificationKey: {
type: String,
- default: () => nanoid(6),
+ default: () => nanoid(6)
},
noteList: {
type: mongoose.Schema.Types.ObjectId,
- ref: "NoteList",
- required: true,
+ ref: 'NoteList',
+ required: true
},
- theme: {type: String, default: () => 'green'},
- language: {type: String, default: () => 'en'},
- created_at: {type: Date, default: Date.now},
- updated_at: {type: Date, default: Date.now}
+ theme: { type: String, default: () => 'green' },
+ language: { type: String, default: () => 'en' },
+ created_at: { type: Date, default: Date.now },
+ updated_at: { type: Date, default: Date.now }
})
-userSchema.statics.findByCredentials = async function (email, password){
- const user = await User.findOne({email})
+userSchema.statics.findByCredentials = async function (email, password) {
+ const user = await User.findOne({ email })
if (!user) {
throw new Error('Unable to login')
@@ -67,8 +67,8 @@ userSchema.statics.findByCredentials = async function (email, password){
return userResponse(user)
}
-userSchema.statics.getVerificationKey = async function (email){
- const {verificationKey: key} = await User.findOne({email})
+userSchema.statics.getVerificationKey = async function (email) {
+ const { verificationKey: key } = await User.findOne({ email })
if (!key) {
throw new Error('Could not verify user')
@@ -77,8 +77,8 @@ userSchema.statics.getVerificationKey = async function (email){
return key
}
-userSchema.statics.verifyUser = async function (_id, key){
- const user = await User.findOneAndUpdate({_id, verificationKey: key}, {isVerified: true, verificationKey: nanoid(10)}, {new: true})
+userSchema.statics.verifyUser = async function (_id, key) {
+ const user = await User.findOneAndUpdate({ _id, verificationKey: key }, { isVerified: true, verificationKey: nanoid(10) }, { new: true })
if (!user) {
throw new Error('Could not verify user')
@@ -87,8 +87,8 @@ userSchema.statics.verifyUser = async function (_id, key){
return userResponse(user)
}
-userSchema.statics.saveSettings = async function ({_id, theme, language}){
- const user = await User.findOneAndUpdate({_id}, {theme, language}, {new: true})
+userSchema.statics.saveSettings = async function ({ _id, theme, language }) {
+ const user = await User.findOneAndUpdate({ _id }, { theme, language }, { new: true })
if (!user) {
throw new Error('Could not save settings')
@@ -97,24 +97,23 @@ userSchema.statics.saveSettings = async function ({_id, theme, language}){
return userResponse(user)
}
-userSchema.statics.state = async function (_id){
- const user = await User.findOne({_id})
+userSchema.statics.state = async function (_id) {
+ const user = await User.findOne({ _id })
if (!user) {
- return {isLoggedIn: false}
+ return { isLoggedIn: false }
} else if (!user.isVerified) {
- return {isVerified: false}
+ return { isVerified: false }
}
return {}
}
-
-userSchema.pre('save', async function(next){
- const user = this;
+userSchema.pre('save', async function (next) {
+ const user = this
if (user.isModified('password')) {
- user.password = await bcrypt.hash(user.password, 8);
+ user.password = await bcrypt.hash(user.password, 8)
}
user.updated_at = Date.now()