From 0a6c2fe8be426be2404fbcbf3a1a94729c723792 Mon Sep 17 00:00:00 2001 From: Ijemma Onwuzulike Date: Sat, 5 Oct 2024 09:50:33 -0700 Subject: [PATCH] Migrate `Copora` and `Projects` + Omit `projectId` (#811) * fix(migration): add languages to corpora and types to projects * feat(examples): omit projectId from returned examples * chore: remove typo in integration file * test: fix tests * test: fix all test blocks --- .github/workflows/integration.yml | 2 +- .../20240928023848-add-languages-to-corpus.js | 25 +++++++++++++++++++ .../20240928024053-add-types-to-projecs.js | 25 +++++++++++++++++++ src/controllers/utils/buildDocs.ts | 18 ++++++------- .../__tests__/authorizeDeveloperUsage.test.ts | 20 +++++++-------- 5 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 migrations/20240928023848-add-languages-to-corpus.js create mode 100644 migrations/20240928024053-add-types-to-projecs.js diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 9ab5e2f5..40caf30b 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -27,7 +27,7 @@ jobs: with: node-version: ${{ matrix.node-version }} - name: Start MongoDB - uses: supercharge/mongodbw-github-action@1.3.0 + uses: supercharge/mongodb-github-action@1.3.0 with: mongodb-version: ${{ matrix.mongodb-version }} - name: Install Project Dependencies diff --git a/migrations/20240928023848-add-languages-to-corpus.js b/migrations/20240928023848-add-languages-to-corpus.js new file mode 100644 index 00000000..102ce882 --- /dev/null +++ b/migrations/20240928023848-add-languages-to-corpus.js @@ -0,0 +1,25 @@ +module.exports = { + async up(db) { + const collections = ['corpus', 'corpussuggestions']; + return collections.map((collection) => + db.collection(collection).updateMany( + {}, + { + $set: { languages: [] }, + } + ) + ); + }, + + async down(db) { + const collections = ['corpus', 'corpussuggestions']; + return collections.map((collection) => + db.collection(collection).updateMany( + {}, + { + $unset: { languages: null }, + } + ) + ); + }, +}; diff --git a/migrations/20240928024053-add-types-to-projecs.js b/migrations/20240928024053-add-types-to-projecs.js new file mode 100644 index 00000000..1a706329 --- /dev/null +++ b/migrations/20240928024053-add-types-to-projecs.js @@ -0,0 +1,25 @@ +module.exports = { + async up(db) { + const collections = ['projects']; + return collections.map((collection) => + db.collection(collection).updateMany( + {}, + { + $set: { types: ['TEXT_AUDIO_ANNOTATION'] }, + } + ) + ); + }, + + async down(db) { + const collections = ['projects']; + return collections.map((collection) => + db.collection(collection).updateMany( + {}, + { + $unset: { types: null }, + } + ) + ); + }, +}; diff --git a/src/controllers/utils/buildDocs.ts b/src/controllers/utils/buildDocs.ts index 3b3d2891..6e743be9 100644 --- a/src/controllers/utils/buildDocs.ts +++ b/src/controllers/utils/buildDocs.ts @@ -40,19 +40,19 @@ const removeKeysInNestedDoc = (docs: T[], nestedDocsKey: keyof T) => { const cleanExamples = ({ examples, version }: { examples: IncomingExample[], version: Version }) => examples.map((example) => { - const cleanedExample: Omit & { - igbo: string, - english: string, - pronunciation?: string, - pronunciations?: string[], - } = omit( + const cleanedExample = omit( assign({ ...example, igbo: '', english: '', }), - ['source', 'translations'] - ); + ['source', 'translations', 'projectId'] + ) as Omit & { + igbo: string, + english: string, + pronunciation?: string, + pronunciations?: string[], + }; if (version === Version.VERSION_1) { cleanedExample.pronunciation = example.source.pronunciations?.[0]?.audio || ''; } else { @@ -141,7 +141,7 @@ export const findWordsWithMatch = async ({ (word) => { const updatedWord = assign(word); // @ts-expect-error different versions - updatedWord.examples = cleanExamples({ examples: updatedWord.examples, version }); + updatedWord.examples = cleanExamples({ examples: updatedWord.examples || [], version }); return updatedWord; } ); diff --git a/src/middleware/helpers/__tests__/authorizeDeveloperUsage.test.ts b/src/middleware/helpers/__tests__/authorizeDeveloperUsage.test.ts index 83cef52b..76bcbb1d 100644 --- a/src/middleware/helpers/__tests__/authorizeDeveloperUsage.test.ts +++ b/src/middleware/helpers/__tests__/authorizeDeveloperUsage.test.ts @@ -14,7 +14,7 @@ jest.mock('../createDeveloperUsage'); describe('authorizeDeveloperUsage', () => { it("authorizes the current developer's usage", async () => { - const route = 'speech-to-text'; + const path = 'speech-to-text'; const developer = developerFixture({}); const developerUsage = developerUsageFixture({}); // @ts-expect-error mockReturnValue @@ -25,12 +25,12 @@ describe('authorizeDeveloperUsage', () => { }); // @ts-expect-error developer - const res = await authorizeDeveloperUsage({ route, developer }); + const res = await authorizeDeveloperUsage({ path, developer }); expect(res).toEqual(developerUsage); }); it("updates the current developer's usage", async () => { - const route = 'speech-to-text'; + const path = 'speech-to-text'; const developer = developerFixture({}); const developerUsage = developerUsageFixture({}); const developerUsageDocument = { @@ -42,12 +42,12 @@ describe('authorizeDeveloperUsage', () => { findDeveloperUsage.mockReturnValue(developerUsageDocument); // @ts-expect-error developer - await authorizeDeveloperUsage({ route, developer }); + await authorizeDeveloperUsage({ path, developer }); expect(developerUsageDocument.usage.count).toEqual(1); }); it('creates a fallback developer usage if none exist exclusively for Igbo API', async () => { - const route = 'igbo_api'; + const path = 'igbo_api'; const developer = developerFixture({ id: documentId }); const developerUsage = developerUsageFixture({}); const developerUsageDocument = { @@ -60,24 +60,24 @@ describe('authorizeDeveloperUsage', () => { // @ts-expect-error mockReturnValue createDeveloperUsage.mockReturnValue(developerUsageDocument); // @ts-expect-error developer - await authorizeDeveloperUsage({ route, developer }); + await authorizeDeveloperUsage({ path, developer }); expect(createDeveloperUsage).toHaveBeenCalled(); }); it('throws error unable finding developer usage', async () => { - const route = 'speech-to-text'; + const path = 'speech-to-text'; const developer = developerFixture({}); // @ts-expect-error mockReturnValue findDeveloperUsage.mockReturnValue(undefined); // @ts-expect-error developer - authorizeDeveloperUsage({ route, developer }).catch((err) => { + authorizeDeveloperUsage({ path, developer }).catch((err) => { expect(err.message).toEqual('No developer usage found'); }); }); it('throws error due to exceeding usage limit', async () => { - const route = 'speech-to-text'; + const path = 'speech-to-text'; const developer = developerFixture({}); const developerUsage = developerUsageFixture({ usage: { date: new Date(), count: ApiUsageLimit[ApiType.SPEECH_TO_TEXT] + 1 }, @@ -90,7 +90,7 @@ describe('authorizeDeveloperUsage', () => { }); // @ts-expect-error developer - authorizeDeveloperUsage({ route, developer }).catch((err) => { + authorizeDeveloperUsage({ path, developer }).catch((err) => { expect(err.message).toEqual('You have exceeded your limit for this API for the day.'); }); });