From c6ea380538c8d2ecf6c4476d917dbefbde3c430f Mon Sep 17 00:00:00 2001 From: mrjvs Date: Sun, 14 Sep 2025 15:04:54 +0200 Subject: [PATCH] feat: Add support for taskfiles that have all countries or languages --- src/database.ts | 75 +++++++++++++++++++-------- src/services/grpc/boss/upload-file.ts | 8 --- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/database.ts b/src/database.ts index da89672..391d1e5 100644 --- a/src/database.ts +++ b/src/database.ts @@ -57,7 +57,8 @@ export function getTaskFiles(allowDeleted: boolean, bossAppID: string, taskID: s const filter: mongoose.FilterQuery = { task_id: taskID.slice(0, 7), - boss_app_id: bossAppID + boss_app_id: bossAppID, + $and: [] }; if (!allowDeleted) { @@ -65,15 +66,25 @@ export function getTaskFiles(allowDeleted: boolean, bossAppID: string, taskID: s } if (country) { - filter.supported_countries = { - $in: [country] - }; + filter.$and?.push({ + $or: [ + { supported_countries: { $eq: [] } }, + { supported_countries: { $in: [country] } } + ] + }); } if (language) { - filter.supported_languages = { - $in: [language] - }; + filter.$and?.push({ + $or: [ + { supported_languages: { $eq: [] } }, + { supported_languages: { $in: [language] } } + ] + }); + } + + if (filter.$and?.length === 0) { + delete filter.$and; } return File.find(filter); @@ -84,7 +95,8 @@ export function getTaskFilesWithAttributes(allowDeleted: boolean, bossAppID: str const filter: mongoose.FilterQuery = { task_id: taskID.slice(0, 7), - boss_app_id: bossAppID + boss_app_id: bossAppID, + $and: [] }; if (!allowDeleted) { @@ -92,15 +104,21 @@ export function getTaskFilesWithAttributes(allowDeleted: boolean, bossAppID: str } if (country) { - filter.supported_countries = { - $in: [country] - }; + filter.$and?.push({ + $or: [ + { supported_countries: { $eq: [] } }, + { supported_countries: { $in: [country] } } + ] + }); } if (language) { - filter.supported_languages = { - $in: [language] - }; + filter.$and?.push({ + $or: [ + { supported_languages: { $eq: [] } }, + { supported_languages: { $in: [language] } } + ] + }); } if (attribute1) { @@ -115,6 +133,10 @@ export function getTaskFilesWithAttributes(allowDeleted: boolean, bossAppID: str filter.attribute3 = attribute3; } + if (filter.$and?.length === 0) { + delete filter.$and; + } + return File.find(filter); } @@ -125,19 +147,30 @@ export function getTaskFile(bossAppID: string, taskID: string, name: string, cou deleted: false, boss_app_id: bossAppID, task_id: taskID.slice(0, 7), - name: name + name: name, + $and: [] }; if (country) { - filter.supported_countries = { - $in: [country] - }; + filter.$and?.push({ + $or: [ + { supported_countries: { $eq: [] } }, + { supported_countries: { $in: [country] } } + ] + }); } if (language) { - filter.supported_languages = { - $in: [language] - }; + filter.$and?.push({ + $or: [ + { supported_languages: { $eq: [] } }, + { supported_languages: { $in: [language] } } + ] + }); + } + + if (filter.$and?.length === 0) { + delete filter.$and; } return File.findOne(filter); diff --git a/src/services/grpc/boss/upload-file.ts b/src/services/grpc/boss/upload-file.ts index c9f7f37..ee86bd1 100644 --- a/src/services/grpc/boss/upload-file.ts +++ b/src/services/grpc/boss/upload-file.ts @@ -48,20 +48,12 @@ export async function uploadFile(request: UploadFileRequest, context: CallContex throw new ServerError(Status.NOT_FOUND, `Task ${taskID} does not exist for BOSS app ${bossAppID}`); } - if (supportedCountries.length === 0) { - throw new ServerError(Status.INVALID_ARGUMENT, 'Must provide at least 1 supported country'); - } - for (const country of supportedCountries) { if (!isValidCountryCode(country)) { throw new ServerError(Status.INVALID_ARGUMENT, `${country} is not a valid country`); } } - if (supportedLanguages.length === 0) { - throw new ServerError(Status.INVALID_ARGUMENT, 'Must provide at least 1 supported language'); - } - for (const language of supportedLanguages) { if (!isValidLanguage(language)) { throw new ServerError(Status.INVALID_ARGUMENT, `${language} is not a valid language`);