From 6bfe70c640c7a16dab278eb408070ae8ac93394e Mon Sep 17 00:00:00 2001 From: Ijemma Onwuzulike Date: Tue, 3 Dec 2024 09:16:18 -0800 Subject: [PATCH] chore: introduce Paywalls and add ownerFirebaseId on Projects --- ...241-insert-owner-firebase-id-on-project.js | 51 ++++++++++++++ ...241203060300-connect-paywall-to-project.js | 69 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 migrations/20241203060241-insert-owner-firebase-id-on-project.js create mode 100644 migrations/20241203060300-connect-paywall-to-project.js diff --git a/migrations/20241203060241-insert-owner-firebase-id-on-project.js b/migrations/20241203060241-insert-owner-firebase-id-on-project.js new file mode 100644 index 00000000..aa9ce6e3 --- /dev/null +++ b/migrations/20241203060241-insert-owner-firebase-id-on-project.js @@ -0,0 +1,51 @@ +const determineOwnerUserProjectPermission = (projectId) => [ + { + $match: { + projectId, + role: 'admin', + }, + }, + { + $sort: { + createdAt: 1, + }, + }, + { + $limit: 1, + }, + { + $addFields: { + isOwner: true, + }, + }, +]; + +module.exports = { + async up(db) { + const projects = await db.collection('projects').find().toArray(); + await Promise.all( + projects.map(async (project) => { + const uRawDocs = await db + .collection('userprojectpermissions') + .aggregate(determineOwnerUserProjectPermission(project._id)); + const firstUserProjectPermission = (await uRawDocs.toArray())[0]; + const ownerUserProjectPermission = + firstUserProjectPermission && firstUserProjectPermission.isOwner + ? firstUserProjectPermission + : { firebaseId: undefined }; + + db.collection('projects').updateMany( + { + _id: project._id, + }, + { $set: { ownerFirebaseId: ownerUserProjectPermission.firebaseId } } + ); + }) + ); + }, + + async down(db) { + db.collection('userprojectpermissions').updateMany({}, [{ $unset: 'isOwner' }]); + db.collection('projects').updateMany({}, [{ $unset: 'ownerFirebaseId' }]); + }, +}; diff --git a/migrations/20241203060300-connect-paywall-to-project.js b/migrations/20241203060300-connect-paywall-to-project.js new file mode 100644 index 00000000..0082a123 --- /dev/null +++ b/migrations/20241203060300-connect-paywall-to-project.js @@ -0,0 +1,69 @@ +const getAllExamples = (projectId) => [ + { + $match: { + projectId, + }, + }, +]; + +const getAllExampleSuggestions = (projectId) => [ + { + $match: { + projectId, + merged: null, + mergedBy: null, + }, + }, +]; + +const getAllAudioPronunciations = (projectId) => [ + { + $match: { + projectId, + }, + }, +]; + +module.exports = { + async up(db) { + db.createCollection('paywalls'); + const projects = db.collection('projects').find(); + return await Promise.all( + (await projects.toArray()).map(async (project) => { + const examples = await db + .collection('examples') + .aggregate(getAllExamples(project._id)) + .toArray(); + const exampleSuggestions = await db + .collection('examplesuggestions') + .aggregate(getAllExampleSuggestions(project._id)) + .toArray(); + const audioPronunciations = await db + .collection('audiopronunciations') + .aggregate(getAllAudioPronunciations(project._id)) + .toArray(); + + db.collection('paywalls').insertOne({ + projectId: project._id, + totalExampleSuggestions: exampleSuggestions.length, + totalExamples: examples.length, + totalAudioPronunciations: audioPronunciations.length, + totalBytes: audioPronunciations.reduce((totalBytes, audioPronunciation) => { + return ( + totalBytes + + (typeof audioPronunciation.size === 'string' + ? parseInt(audioPronunciation.size, 10) + : audioPronunciation.size) + ); + }, 0), + updatedAt: new Date(), + createdAt: new Date(), + }); + }) + ); + }, + + async down(db) { + return db.collection('paywalls').drop(); + }, +};