Skip to content

Commit

Permalink
chore: introduce Paywalls and add ownerFirebaseId on Projects
Browse files Browse the repository at this point in the history
  • Loading branch information
ijemmao committed Dec 3, 2024
1 parent 3ce8b42 commit 6bfe70c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
51 changes: 51 additions & 0 deletions migrations/20241203060241-insert-owner-firebase-id-on-project.js
Original file line number Diff line number Diff line change
@@ -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' }]);
},
};
69 changes: 69 additions & 0 deletions migrations/20241203060300-connect-paywall-to-project.js
Original file line number Diff line number Diff line change
@@ -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();
},
};

0 comments on commit 6bfe70c

Please sign in to comment.