-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1067 from ita-social-projects/feature/1058/remove…
…-fields-from-quiz-and-update-cooperations The resourceType field was removed from the quiz and the cooperations were updated
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
migrations/20241212093509-remove-resourceType-from-quiz.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } }) | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/test/migrations/20241212093509-remove-resourceType-from-quiz.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |