diff --git a/migrations/20241216212401-remove-documents-with-non-existent-users.js b/migrations/20241216212401-remove-documents-with-non-existent-users.js new file mode 100644 index 00000000..701b1bcf --- /dev/null +++ b/migrations/20241216212401-remove-documents-with-non-existent-users.js @@ -0,0 +1,20 @@ +module.exports = { + async up(db) { + const allUsers = await db + .collection('users') + .find({}, { projection: { _id: 1 } }) + .toArray() + const allUserIds = allUsers.map((user) => user._id.toString()) + const collections = ['attachments', 'courses', 'lessons', 'notes', 'questions', 'quizzes', 'resourcescategories'] + for (const collectionName of collections) { + const authorsFromCollection = await db.collection(collectionName).distinct('author') + const nonExistingUsers = await authorsFromCollection.filter((userId) => !allUserIds.includes(userId.toString())) + + if (nonExistingUsers.length > 0) { + await db.collection(collectionName).deleteMany({ author: { $in: nonExistingUsers } }) + } + } + }, + + async down() {} +} diff --git a/src/test/migrations/20241216212401-remove-documents-with-non-existent-users.spec.js b/src/test/migrations/20241216212401-remove-documents-with-non-existent-users.spec.js new file mode 100644 index 00000000..b7c0e186 --- /dev/null +++ b/src/test/migrations/20241216212401-remove-documents-with-non-existent-users.spec.js @@ -0,0 +1,62 @@ +const { MongoClient, ObjectId } = require('mongodb') + +const { up } = require('@root/migrations/20241216212401-remove-documents-with-non-existent-users') + +require('~/initialization/envSetup') +const { + config: { MONGODB_URL } +} = require('~/configs/config') + +const collections = ['attachments', 'courses', 'lessons', 'notes', 'questions', 'quizzes', 'resourcescategories'] +const existingUser = { _id: new ObjectId(), firstName: 'Test', lastName: 'User' } +const fakeUser = new ObjectId() + +const url = MONGODB_URL.slice(0, MONGODB_URL.lastIndexOf('/')) +const databaseName = MONGODB_URL.slice(MONGODB_URL.lastIndexOf('/') + 1) + +describe('20241216212401-remove-documents-with-non-existent-users', () => { + let client, database + + beforeAll(() => { + client = new MongoClient(url) + database = client.db(databaseName) + }) + + afterAll(() => { + client.close() + }) + + it('should remove documents with non-existing users from all collections', async () => { + for (const collectionName of collections) { + await database.collection(collectionName).insertOne({ author: fakeUser.toString() }) + } + + await up(database) + + for (const collectionName of collections) { + const docsWithNonExistingUsers = await database + .collection(collectionName) + .find({ author: fakeUser.toString() }) + .toArray() + expect(docsWithNonExistingUsers).toHaveLength(0) + } + }) + + it('should not remove documents with existing users from all collections', async () => { + await database.collection('users').insertOne(existingUser) + + for (const collectionName of collections) { + await database.collection(collectionName).insertOne({ author: existingUser._id.toString() }) + } + + await up(database) + + for (const collectionName of collections) { + const docsWithNonExistingUsers = await database + .collection(collectionName) + .find({ author: existingUser._id.toString() }) + .toArray() + expect(docsWithNonExistingUsers).toHaveLength(1) + } + }) +})