diff --git a/package.json b/package.json index 7a8b48ff..d3fcb024 100644 --- a/package.json +++ b/package.json @@ -40,15 +40,15 @@ "iconv-lite": "^0.6.3", "jsonwebtoken": "^9.0.2", "module-alias": "^2.2.2", - "mongodb": "^4.2.2", - "mongoose": "^6.1.2", + "mongodb": "^6.8.0", + "mongoose": "^8.5.1", "multer": "^1.4.5-lts.1", "nodemailer": "^6.7.7", "pug": "^3.0.2", + "winston": "^3.13.1", + "winston-mongodb": "^5.1.1", "swagger-jsdoc": "^6.2.8", - "swagger-ui-express": "^5.0.1", - "winston": "^3.8.0", - "winston-mongodb": "^5.0.7" + "swagger-ui-express": "^5.0.1" }, "lint-staged": { "*.{js}": [ diff --git a/src/services/attachment.js b/src/services/attachment.js index 2b9f4d33..8bc28a16 100644 --- a/src/services/attachment.js +++ b/src/services/attachment.js @@ -76,7 +76,7 @@ const attachmentService = { throw createForbiddenError() } - await Attachment.findByIdAndRemove(id).exec() + await Attachment.findByIdAndDelete(id).exec() } } diff --git a/src/services/chat.js b/src/services/chat.js index fcd8307b..e1e14534 100644 --- a/src/services/chat.js +++ b/src/services/chat.js @@ -42,9 +42,9 @@ const chatService = { const { id: user, role: userRole } = currentUser return await Chat.find({ - 'members.user': mongoose.Types.ObjectId(user), + 'members.user': new mongoose.Types.ObjectId(user), 'members.role': userRole, - 'deletedFor.user': { $ne: mongoose.Types.ObjectId(user) } + 'deletedFor.user': { $ne: new mongoose.Types.ObjectId(user) } }).populate([ { path: 'latestMessage', @@ -67,7 +67,7 @@ const chatService = { throw createForbiddenError() } - await Chat.findByIdAndRemove(id).exec() + await Chat.findByIdAndDelete(id).exec() }, markAsDeletedForCurrentUser: async (id, currentUser) => { const chat = await Chat.findById(id).exec() diff --git a/src/services/course.js b/src/services/course.js index 3b301a5c..9fa37aec 100644 --- a/src/services/course.js +++ b/src/services/course.js @@ -80,7 +80,7 @@ const courseService = { throw createForbiddenError() } - await Course.findByIdAndRemove(id).exec() + await Course.findByIdAndDelete(id).exec() } } diff --git a/src/services/lesson.js b/src/services/lesson.js index 83b4fe19..707c122a 100644 --- a/src/services/lesson.js +++ b/src/services/lesson.js @@ -53,7 +53,7 @@ const lessonService = { throw createForbiddenError() } - await Lesson.findByIdAndRemove(id).exec() + await Lesson.findByIdAndDelete(id).exec() }, getLessonById: async (id) => { diff --git a/src/services/message.js b/src/services/message.js index 2247941d..7e3f0f2f 100644 --- a/src/services/message.js +++ b/src/services/message.js @@ -45,7 +45,7 @@ const messageService = { const messages = await Message.find({ chat: chat, - 'clearedFor.user': { $ne: mongoose.Types.ObjectId(user) } + 'clearedFor.user': { $ne: new mongoose.Types.ObjectId(user) } }) .populate({ path: 'author', select: '_id photo' }) .select('+isRead') diff --git a/src/services/note.js b/src/services/note.js index 4bfce465..ad44738c 100644 --- a/src/services/note.js +++ b/src/services/note.js @@ -62,7 +62,7 @@ const noteService = { if (!note || note.author.toString() !== userId) throw createForbiddenError() - await Note.findByIdAndRemove(noteId).exec() + await Note.findByIdAndDelete(noteId).exec() } } diff --git a/src/services/offer.js b/src/services/offer.js index a20b6563..ed207fc2 100644 --- a/src/services/offer.js +++ b/src/services/offer.js @@ -1,9 +1,9 @@ -const { ObjectId } = require('mongodb') const Offer = require('~/models/offer') const filterAllowedFields = require('~/utils/filterAllowedFields') const { allowedOfferFieldsForUpdate } = require('~/validation/services/offer') const { createForbiddenError } = require('~/utils/errorsHelper') +const mongoose = require('mongoose') const offerService = { getOffers: async (pipeline) => { @@ -26,12 +26,12 @@ const offerService = { const [chatLookup] = await Offer.aggregate([ { - $match: { _id: ObjectId(id) } + $match: { _id: new mongoose.Types.ObjectId(id) } }, { $lookup: { from: 'chats', - let: { authorId: '$author', userId: ObjectId(userId) }, + let: { authorId: '$author', userId: new mongoose.Types.ObjectId(userId) }, pipeline: [ { $match: { @@ -112,7 +112,7 @@ const offerService = { }, deleteOffer: async (id) => { - await Offer.findByIdAndRemove(id).exec() + await Offer.findByIdAndDelete(id).exec() } } diff --git a/src/services/question.js b/src/services/question.js index 494fd2e0..b4876bd4 100644 --- a/src/services/question.js +++ b/src/services/question.js @@ -44,7 +44,7 @@ const questionService = { throw createForbiddenError() } - await Question.findByIdAndRemove(id).exec() + await Question.findByIdAndDelete(id).exec() }, updateQuestion: async (id, currentUserId, data) => { diff --git a/src/services/quiz.js b/src/services/quiz.js index 1dc1f8bc..0f3ccf58 100644 --- a/src/services/quiz.js +++ b/src/services/quiz.js @@ -62,7 +62,7 @@ const quizService = { throw createForbiddenError() } - await Quiz.findByIdAndRemove(id).exec() + await Quiz.findByIdAndDelete(id).exec() } } diff --git a/src/services/resourcesCategory.js b/src/services/resourcesCategory.js index de2652b4..2fc6ceb3 100644 --- a/src/services/resourcesCategory.js +++ b/src/services/resourcesCategory.js @@ -50,7 +50,7 @@ const resourcesCategoryService = { throw createForbiddenError() } - await ResourcesCategory.findByIdAndRemove(id).exec() + await ResourcesCategory.findByIdAndDelete(id).exec() } } diff --git a/src/services/review.js b/src/services/review.js index 3749c77b..56a177e3 100644 --- a/src/services/review.js +++ b/src/services/review.js @@ -79,7 +79,7 @@ const reviewService = { }, deleteReview: async (id) => { - await Review.findByIdAndRemove(id).exec() + await Review.findByIdAndDelete(id).exec() } } diff --git a/src/services/subject.js b/src/services/subject.js index 555eeab5..65f18f99 100644 --- a/src/services/subject.js +++ b/src/services/subject.js @@ -42,7 +42,7 @@ const subjectService = { }, deleteSubject: async (id) => { - await Subject.findByIdAndRemove(id).exec() + await Subject.findByIdAndDelete(id).exec() } } diff --git a/src/services/user.js b/src/services/user.js index aaec4b3a..0afd1587 100644 --- a/src/services/user.js +++ b/src/services/user.js @@ -234,7 +234,7 @@ const userService = { }, deleteUser: async (id) => { - await User.findByIdAndRemove(id).exec() + await User.findByIdAndDelete(id).exec() }, _calculateDeletionMainSubject: async (userId, categoryId) => { diff --git a/src/test/integration/controllers/cooperation.spec.js b/src/test/integration/controllers/cooperation.spec.js index 18f9aba9..5670214a 100644 --- a/src/test/integration/controllers/cooperation.spec.js +++ b/src/test/integration/controllers/cooperation.spec.js @@ -19,8 +19,8 @@ const nonExistingOfferId = '648ae644aa322613ba08e69e' const validationErrorMessage = 'You can change only either the status or the price in one operation' const id = new mongoose.Types.ObjectId() +const optionsSearch = coopsAggregateOptions({ id, role: 'testRole' }, { search: 'testSearch' }) const optionsStatus = coopsAggregateOptions({}, { status: 'testStatus' }) -const optionsSearch = coopsAggregateOptions({ id: id.toString(), role: 'testRole' }, { search: 'testSearch' }) const tutorUserData = { role: ['tutor'], diff --git a/src/test/integration/controllers/finishedQuiz.spec.js b/src/test/integration/controllers/finishedQuiz.spec.js index 733e7d52..7ab463af 100644 --- a/src/test/integration/controllers/finishedQuiz.spec.js +++ b/src/test/integration/controllers/finishedQuiz.spec.js @@ -60,7 +60,7 @@ describe('Quiz controller', () => { testFinishedQuiz = await app .post(endpointUrl) - .send({ quiz: testQuiz._id, ...testFinishedQuizData }) + .send({ quiz: testQuiz._id.toString(), ...testFinishedQuizData }) .set('Cookie', [`accessToken=${accessToken}`]) }) diff --git a/src/test/integration/controllers/note.spec.js b/src/test/integration/controllers/note.spec.js index 6efc9cff..8f53f129 100644 --- a/src/test/integration/controllers/note.spec.js +++ b/src/test/integration/controllers/note.spec.js @@ -75,7 +75,7 @@ describe('Note controller', () => { expect(response.body.length).toBe(1) expect(Array.isArray(response.body)).toBe(true) expect(response.body[0]).toMatchObject({ - _id: testNote._body._id, + _id: testNote._body._id.toString(), text: expect.any(String), author: { _id: testUser.id, @@ -118,7 +118,7 @@ describe('Note controller', () => { it('should create new note', () => { expect(testNote.statusCode).toBe(201) expect(testNote._body).toMatchObject({ - _id: testNote._body._id, + _id: testNote._body._id.toString(), text: expect.any(String), author: testUser.id, cooperation: testCooperation._id.toString(), diff --git a/src/test/integration/controllers/offer.spec.js b/src/test/integration/controllers/offer.spec.js index 787a689e..8ba34930 100644 --- a/src/test/integration/controllers/offer.spec.js +++ b/src/test/integration/controllers/offer.spec.js @@ -157,29 +157,39 @@ describe('Offer controller', () => { it('should get an offer by ID', async () => { const response = await app.get(endpointUrl + testOffer._id).set('Cookie', [`accessToken=${accessToken}`]) - expect(response.body).toEqual({ - ...testOffer, - author: { - _id: testOffer.author, - firstName: 'Tart', - lastName: 'Drilling', - FAQ: [{ _id: expect.any(String), answer: 'answer1', question: 'question1' }], - totalReviews: { - student: 0, - tutor: 0 - }, - averageRating: { - student: 0, - tutor: 0 - } - }, - subject: { - _id: testOffer.subject, - name: 'TestSubject' - }, - chatId: null - }) expect(response.statusCode).toBe(200) + + const { FAQ, author, subject, chatId, ...restResponse } = response.body + const { FAQ: expectedFAQ, author: expectedAuthor, subject: expectedSubject, ...restExpected } = testOffer + + expect(restResponse).toEqual(restExpected) + + expect(author._id).toEqual(expectedAuthor) + expect(author.firstName).toBe('Tart') + expect(author.lastName).toBe('Drilling') + expect(author.FAQ).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + question: 'question1', + answer: 'answer1' + }) + ]) + ) + expect(author.totalReviews).toEqual({ + student: 0, + tutor: 0 + }) + expect(author.averageRating).toEqual({ + student: 0, + tutor: 0 + }) + + expect(subject._id).toEqual(expectedSubject) + expect(subject.name).toBe('TestSubject') + + expect(FAQ[0]._id).toBe(expectedFAQ[0]._id) + + expect(chatId).toBeNull() }) it('should throw DOCUMENT_NOT_FOUND', async () => { diff --git a/src/test/integration/controllers/user.spec.js b/src/test/integration/controllers/user.spec.js index d73e4986..5e3162ce 100644 --- a/src/test/integration/controllers/user.spec.js +++ b/src/test/integration/controllers/user.spec.js @@ -496,7 +496,7 @@ describe('User controller', () => { password: 'password', appLanguage: 'en', mainSubjects: { - student: [{ category: { _id: new mongoose.Types.ObjectId(), name: 'Cooking' }, subjects: [] }], + student: [{ category: { _id: new mongoose.Types.ObjectId().toString(), name: 'Cooking' }, subjects: [] }], tutor: [] } }) diff --git a/src/utils/categories/conditionCreator.js b/src/utils/categories/conditionCreator.js index 79f66d54..f7bb3590 100644 --- a/src/utils/categories/conditionCreator.js +++ b/src/utils/categories/conditionCreator.js @@ -4,11 +4,11 @@ const condition = (data) => { const condition = {} if (data.categoryId) { - condition.category = mongoose.Types.ObjectId(data.categoryId) + condition.category = new mongoose.Types.ObjectId(data.categoryId) } if (data.subjectId) { - condition.subject = mongoose.Types.ObjectId(data.subjectId) + condition.subject = new mongoose.Types.ObjectId(data.subjectId) } if (data.authorRole) { diff --git a/src/utils/cooperations/coopsAggregateOptions.js b/src/utils/cooperations/coopsAggregateOptions.js index ee7f6cc1..873283d1 100644 --- a/src/utils/cooperations/coopsAggregateOptions.js +++ b/src/utils/cooperations/coopsAggregateOptions.js @@ -20,8 +20,8 @@ const coopsAggregateOptions = (params = {}, query) => { match.$and = [ { $or: [ - { initiator: mongoose.Types.ObjectId(id), initiatorRole: role }, - { receiver: mongoose.Types.ObjectId(id), receiverRole: role } + { initiator: new mongoose.Types.ObjectId(id), initiatorRole: role }, + { receiver: new mongoose.Types.ObjectId(id), receiverRole: role } ] } ] @@ -44,7 +44,7 @@ const coopsAggregateOptions = (params = {}, query) => { $lookup: { from: 'users', let: { - lookUpField: { $cond: [{ $eq: ['$initiator', mongoose.Types.ObjectId(id)] }, '$receiver', '$initiator'] }, + lookUpField: { $cond: [{ $eq: ['$initiator', new mongoose.Types.ObjectId(id)] }, '$receiver', '$initiator'] }, role: { $cond: [{ $eq: ['$initiatorRole', role] }, '$receiverRole', '$initiatorRole'] } diff --git a/src/utils/offers/offerAggregateOptions.js b/src/utils/offers/offerAggregateOptions.js index 863925b7..8acbb432 100644 --- a/src/utils/offers/offerAggregateOptions.js +++ b/src/utils/offers/offerAggregateOptions.js @@ -60,7 +60,7 @@ const offerAggregateOptions = (query, params, user) => { } if (authorId) { - match['author._id'] = mongoose.Types.ObjectId(authorId) + match['author._id'] = new mongoose.Types.ObjectId(authorId) } if (authorRole) { @@ -97,15 +97,15 @@ const offerAggregateOptions = (query, params, user) => { } if (categoryId) { - match['category._id'] = mongoose.Types.ObjectId(categoryId) + match['category._id'] = new mongoose.Types.ObjectId(categoryId) } if (subjectId) { - match['subject._id'] = mongoose.Types.ObjectId(subjectId) + match['subject._id'] = new mongoose.Types.ObjectId(subjectId) } if (excludedOfferId) { - match._id = { $ne: mongoose.Types.ObjectId(excludedOfferId) } + match._id = { $ne: new mongoose.Types.ObjectId(excludedOfferId) } } let sortOption = {} @@ -185,7 +185,10 @@ const offerAggregateOptions = (query, params, user) => { { $lookup: { from: 'chats', - let: { authorId: { $arrayElemAt: ['$author._id', 0] }, userId: mongoose.Types.ObjectId(userId) }, + let: { + authorId: { $arrayElemAt: ['$author._id', 0] }, + userId: new mongoose.Types.ObjectId(userId) + }, pipeline: [ { $match: { diff --git a/src/utils/reviews/reviewStatsAggregation.js b/src/utils/reviews/reviewStatsAggregation.js index cfccfd64..42167999 100644 --- a/src/utils/reviews/reviewStatsAggregation.js +++ b/src/utils/reviews/reviewStatsAggregation.js @@ -5,7 +5,7 @@ const calculateReviewStats = async (targetUserId, targetUserRole) => { const [reviews] = await Review.aggregate([ { $match: { - targetUserId: mongoose.Types.ObjectId(targetUserId), + targetUserId: new mongoose.Types.ObjectId(`${targetUserId}`), targetUserRole } },