Skip to content

Commit

Permalink
Introduce Paywalls and add ownerFirebaseId on Projects (#823)
Browse files Browse the repository at this point in the history
* chore: introduce Paywalls and add ownerFirebaseId on Projects

* test: fix STT tests
  • Loading branch information
ijemmao authored Dec 6, 2024
1 parent 41ab55c commit c88d0d4
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 7 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();
},
};
15 changes: 8 additions & 7 deletions src/controllers/__tests__/speechToText.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import axios from 'axios';
import {
nextFunctionFixture,
requestFixture,
responseFixture,
nextFunctionFixture,
} from '../../__tests__/shared/fixtures';
import { IGBO_STT_API } from '../../config';
import { getTranscription } from '../speechToText';
import { fetchBase64Data } from '../utils/fetchBase64Data';

Expand Down Expand Up @@ -38,12 +39,12 @@ describe('speechToText', () => {
// @ts-expect-error non-existing value
expect(axios.request.mock.calls[1][0]).toMatchObject({
method: 'POST',
url: 'http://localhost:3333/predict',
url: IGBO_STT_API,
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'main_key',
},
data: { id: 'audioId', url: 'https://igboapi.com' },
data: { audioUrl: 'https://igboapi.com' },
});
});

Expand Down Expand Up @@ -71,12 +72,12 @@ describe('speechToText', () => {
// @ts-expect-error non-existing value
expect(axios.request.mock.calls[1][0]).toMatchObject({
method: 'POST',
url: 'http://localhost:3333/predict',
url: IGBO_STT_API,
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'main_key',
},
data: { id: 'audioId', url: 'https://igboapi.com' },
data: { audioUrl: 'https://igboapi.com' },
});
});

Expand Down Expand Up @@ -104,12 +105,12 @@ describe('speechToText', () => {
});
expect(axios.request).toHaveBeenCalledWith({
method: 'POST',
url: 'http://localhost:3333/predict',
url: IGBO_STT_API,
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'main_key',
},
data: { id: 'audioId', url: audioUrl },
data: { audioUrl },
});
});

Expand Down

0 comments on commit c88d0d4

Please sign in to comment.