-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: introduce Paywalls and add ownerFirebaseId on Projects
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
migrations/20241203060241-insert-owner-firebase-id-on-project.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,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' }]); | ||
}, | ||
}; |
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,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(); | ||
}, | ||
}; |