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.
This commit is contained in:
Daniel López Guimaraes
2025-11-01 13:02:44 +00:00
parent 3a55e46fa1
commit 4d266e38a0
2 changed files with 63 additions and 20 deletions

View File

@@ -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<HydratedFileCTRDocument>(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<HydratedFileWUPDocument>(filter);

View File

@@ -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);
}