Skip to content

Commit

Permalink
Updated database dependencies (#880)
Browse files Browse the repository at this point in the history
* Updated database deps

* Changed objectId method

* Changed IDs to literals

* Changed cooperation test, aggregateOptions

* Fixed cooperation tests, test commit for Sonar

* Refactored code, fixed tests

* Resolve conflicts

* Fix package.json

---------

Co-authored-by: ut5tw <sheredan2003@gmail.com>
Co-authored-by: Illia Pasichnichenko <54638946+ut5tw@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 29, 2024
1 parent e9bb309 commit 810e219
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 61 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}": [
Expand Down
2 changes: 1 addition & 1 deletion src/services/attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const attachmentService = {
throw createForbiddenError()
}

await Attachment.findByIdAndRemove(id).exec()
await Attachment.findByIdAndDelete(id).exec()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/services/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/services/course.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const courseService = {
throw createForbiddenError()
}

await Course.findByIdAndRemove(id).exec()
await Course.findByIdAndDelete(id).exec()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const lessonService = {
throw createForbiddenError()
}

await Lesson.findByIdAndRemove(id).exec()
await Lesson.findByIdAndDelete(id).exec()
},

getLessonById: async (id) => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/services/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const noteService = {

if (!note || note.author.toString() !== userId) throw createForbiddenError()

await Note.findByIdAndRemove(noteId).exec()
await Note.findByIdAndDelete(noteId).exec()
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/services/offer.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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: {
Expand Down Expand Up @@ -112,7 +112,7 @@ const offerService = {
},

deleteOffer: async (id) => {
await Offer.findByIdAndRemove(id).exec()
await Offer.findByIdAndDelete(id).exec()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const questionService = {
throw createForbiddenError()
}

await Question.findByIdAndRemove(id).exec()
await Question.findByIdAndDelete(id).exec()
},

updateQuestion: async (id, currentUserId, data) => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const quizService = {
throw createForbiddenError()
}

await Quiz.findByIdAndRemove(id).exec()
await Quiz.findByIdAndDelete(id).exec()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/resourcesCategory.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const resourcesCategoryService = {
throw createForbiddenError()
}

await ResourcesCategory.findByIdAndRemove(id).exec()
await ResourcesCategory.findByIdAndDelete(id).exec()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const reviewService = {
},

deleteReview: async (id) => {
await Review.findByIdAndRemove(id).exec()
await Review.findByIdAndDelete(id).exec()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/subject.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const subjectService = {
},

deleteSubject: async (id) => {
await Subject.findByIdAndRemove(id).exec()
await Subject.findByIdAndDelete(id).exec()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const userService = {
},

deleteUser: async (id) => {
await User.findByIdAndRemove(id).exec()
await User.findByIdAndDelete(id).exec()
},

_calculateDeletionMainSubject: async (userId, categoryId) => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/controllers/cooperation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/controllers/finishedQuiz.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`])
})

Expand Down
4 changes: 2 additions & 2 deletions src/test/integration/controllers/note.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
54 changes: 32 additions & 22 deletions src/test/integration/controllers/offer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/controllers/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/utils/categories/conditionCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/cooperations/coopsAggregateOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
]
}
]
Expand All @@ -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']
}
Expand Down
13 changes: 8 additions & 5 deletions src/utils/offers/offerAggregateOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/reviews/reviewStatsAggregation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
Expand Down

0 comments on commit 810e219

Please sign in to comment.