Skip to content

Commit

Permalink
Merge pull request #1067 from ita-social-projects/feature/1058/remove…
Browse files Browse the repository at this point in the history
…-fields-from-quiz-and-update-cooperations

The resourceType field was removed from the quiz and the cooperations were updated
  • Loading branch information
PavloButynets authored Jan 2, 2025
2 parents 6994eed + 6ab4c27 commit 0c5ac39
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
30 changes: 30 additions & 0 deletions migrations/20241212093509-remove-resourceType-from-quiz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
async up(db) {
const quizCollection = db.collection('quizzes')
const cooperationsCollection = db.collection('cooperation')

const quizzes = await quizCollection.find({}).toArray()
for (const quiz of quizzes) {
if (quiz.resourceType) {
await cooperationsCollection.updateMany(
{ 'sections.resources.resource': quiz._id },
{ $set: { 'sections.$[section].resources.$[res].resourceType': quiz.resourceType } },
{
arrayFilters: [{ 'section.resources.resource': quiz._id }, { 'res.resource': quiz._id }]
}
)
}
}
await quizCollection.updateMany({}, { $unset: { resourceType: '' } })
},

async down(db) {
const quizCollection = db.collection('quizzes')
const cooperationsCollection = db.collection('cooperation')
await cooperationsCollection.updateMany(
{ 'sections.resources.resource': { $exists: true } },
{ $unset: { 'sections.$[].resources.$[].resourceType': '' } }
)
await quizCollection.updateMany({}, { $set: { resourceType: 'quiz' } })
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { MongoClient, ObjectId } = require('mongodb')
const { up, down } = require('@root/migrations/20241212093509-remove-resourceType-from-quiz')

require('~/initialization/envSetup')
const {
config: { MONGODB_URL }
} = require('~/configs/config')

const quizCollectionName = 'quizzes'
const cooperationCollectionName = 'cooperation'

const url = MONGODB_URL.slice(0, MONGODB_URL.lastIndexOf('/'))
const databaseName = MONGODB_URL.slice(MONGODB_URL.lastIndexOf('/') + 1)

describe('20241203085442-remove-resource-type-from-quizzes', () => {
let client, database

beforeAll(async () => {
client = new MongoClient(url)
database = client.db(databaseName)
await client.connect()
})

afterAll(async () => {
await up(database)
await client.close()
})

test('should remove resourceType from quizzes on migrate up', async () => {
await database.collection(quizCollectionName).insertOne({
_id: ObjectId(),
resourceType: 'quiz'
})

await up(database)

const quiz = await database.collection(quizCollectionName).findOne({ resourceType: 'quiz' })
expect(quiz).toBeNull()
})

test('should update cooperation documents with correct resourceType removal', async () => {
const quizId = (
await database.collection(quizCollectionName).insertOne({
_id: ObjectId(),
resourceType: 'quiz'
})
).insertedId

await database.collection(cooperationCollectionName).insertOne({
sections: [
{
resources: [{ resource: quizId, resourceType: 'quiz' }]
}
]
})

await up(database)
const cooperation = await database.collection(cooperationCollectionName).findOne({
'sections.resources.resource': quizId
})
const quiz = await database.collection(quizCollectionName).findOne({
_id: quizId
})

expect(quiz.resourceType).toBeUndefined()

expect(cooperation.sections[0].resources[0].resourceType).toBe('quiz')
})

test('should restore resourceType during migrate down', async () => {
const quizId = ObjectId()
await database.collection(quizCollectionName).insertOne({
_id: quizId
})

await down(database)

const quiz = await database.collection(quizCollectionName).findOne({ _id: quizId })
expect(quiz.resourceType).toBe('quiz')
})
})

0 comments on commit 0c5ac39

Please sign in to comment.