From 4d266e38a070e5d5805e5a6c658329ab65550982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B3pez=20Guimaraes?= Date: Sat, 1 Nov 2025 13:02:44 +0000 Subject: [PATCH] fix(database): Search for no countries or languages if not specified This allows us to create files targeted to specific countries and/or languages without any issues such as returning a random language when none is specified. --- src/database.ts | 56 +++++++++++++++++++++++++++++++------------- src/services/npts.ts | 27 +++++++++++++++++---- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/database.ts b/src/database.ts index e7bf865..9cf306e 100644 --- a/src/database.ts +++ b/src/database.ts @@ -74,6 +74,10 @@ export function getCTRTaskFiles(allowDeleted: boolean, bossAppID: string, taskID { supported_countries: country } ] }); + } else { + filter.$and?.push({ + supported_countries: { $eq: [] } + }); } if (language) { @@ -83,10 +87,10 @@ export function getCTRTaskFiles(allowDeleted: boolean, bossAppID: string, taskID { supported_languages: language } ] }); - } - - if (filter.$and?.length === 0) { - delete filter.$and; + } else { + filter.$and?.push({ + supported_languages: { $eq: [] } + }); } return FileCTR.find(filter); @@ -112,6 +116,10 @@ export function getWUPTaskFiles(allowDeleted: boolean, bossAppID: string, taskID { supported_countries: country } ] }); + } else { + filter.$and?.push({ + supported_countries: { $eq: [] } + }); } if (language) { @@ -121,10 +129,10 @@ export function getWUPTaskFiles(allowDeleted: boolean, bossAppID: string, taskID { supported_languages: language } ] }); - } - - if (filter.$and?.length === 0) { - delete filter.$and; + } else { + filter.$and?.push({ + supported_languages: { $eq: [] } + }); } return FileWUP.find(filter); @@ -150,6 +158,10 @@ export function getCTRTaskFilesWithAttributes(allowDeleted: boolean, bossAppID: { supported_countries: country } ] }); + } else { + filter.$and?.push({ + supported_countries: { $eq: [] } + }); } if (language) { @@ -159,6 +171,10 @@ export function getCTRTaskFilesWithAttributes(allowDeleted: boolean, bossAppID: { supported_languages: language } ] }); + } else { + filter.$and?.push({ + supported_languages: { $eq: [] } + }); } if (attribute1) { @@ -198,6 +214,10 @@ export function getCTRTaskFile(bossAppID: string, taskID: string, name: string, { supported_countries: country } ] }); + } else { + filter.$and?.push({ + supported_countries: { $eq: [] } + }); } if (language) { @@ -207,10 +227,10 @@ export function getCTRTaskFile(bossAppID: string, taskID: string, name: string, { supported_languages: language } ] }); - } - - if (filter.$and?.length === 0) { - delete filter.$and; + } else { + filter.$and?.push({ + supported_languages: { $eq: [] } + }); } return FileCTR.findOne(filter); @@ -234,6 +254,10 @@ export function getWUPTaskFile(bossAppID: string, taskID: string, name: string, { supported_countries: country } ] }); + } else { + filter.$and?.push({ + supported_countries: { $eq: [] } + }); } if (language) { @@ -243,10 +267,10 @@ export function getWUPTaskFile(bossAppID: string, taskID: string, name: string, { supported_languages: language } ] }); - } - - if (filter.$and?.length === 0) { - delete filter.$and; + } else { + filter.$and?.push({ + supported_languages: { $eq: [] } + }); } return FileWUP.findOne(filter); diff --git a/src/services/npts.ts b/src/services/npts.ts index 83f5e9e..6e8721f 100644 --- a/src/services/npts.ts +++ b/src/services/npts.ts @@ -24,15 +24,24 @@ function buildFile(task: HydratedTaskDocument, file: HydratedFileWUPDocument): a }; } -npts.get('/p01/tasksheet/:id/:bossAppId/:taskId', async (request, response) => { +npts.get('/p01/tasksheet/:id/:bossAppId/:taskId', async (request: express.Request<{ + id: string; + bossAppId: string; + taskId: string; +}, any, any, { + c?: string; + l?: string; +}>, response) => { const { bossAppId, taskId } = request.params; + const country = request.query.c; + const language = request.query.l; const task = await getTask(bossAppId, taskId); if (!task) { return response.sendStatus(404); } - const files = await getWUPTaskFiles(false, bossAppId, taskId); + const files = await getWUPTaskFiles(false, bossAppId, taskId, country, language); const xmlContent = { TaskSheet: { @@ -49,15 +58,25 @@ npts.get('/p01/tasksheet/:id/:bossAppId/:taskId', async (request, response) => { response.send(xmlbuilder.create(xmlContent, xmlHeadSettings).end({ pretty: true })); }); -npts.get('/p01/tasksheet/:id/:bossAppId/:taskId/:fileName', async (request, response) => { +npts.get('/p01/tasksheet/:id/:bossAppId/:taskId/:fileName', async (request: express.Request<{ + id: string; + bossAppId: string; + taskId: string; + fileName: string; +}, any, any, { + c?: string; + l?: string; +}>, response) => { const { bossAppId, taskId, fileName } = request.params; + const country = request.query.c; + const language = request.query.l; const task = await getTask(bossAppId, taskId); if (!task) { return response.sendStatus(404); } - const file = await getWUPTaskFile(bossAppId, taskId, fileName); + const file = await getWUPTaskFile(bossAppId, taskId, fileName, country, language); if (!file) { return response.sendStatus(404); }