feat(services): update condition checks in model.service.ts

Updated the condition checks in the 'delete' and 'fetch' methods of the `model.service.ts`. Now, it properly checks if the model exists by examining the first item of the result array, instead of considering the result array itself. This resolves issues where fetching or deleting a model with a non-existent slug would incorrectly attempt to perform operations considering the empty array as valid input.

Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-05-03 13:18:15 +02:00
parent 438ae4b5d0
commit e8acfd7b30
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -80,12 +80,14 @@ async function deleteModel(modelSlug: string): Promise<boolean> {
}
logger.info(`Deleting model with ID: ${modelSlug}`);
const doesExist = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
if (!doesExist || !doesExist.id) {
if (!doesExist[0]) {
logger.warn(`Model with slug ${modelSlug} not found`);
return false;
}
const target = doesExist[0]
if (!target.id) return false;
try {
await MysqlService.Model.delete(DbHandler, doesExist.id);
await MysqlService.Model.delete(DbHandler, target.id);
logger.info("Deletion Successful !");
return true;
} catch (error) {
@ -104,11 +106,11 @@ async function getBySlugModel(modelSlug: string): Promise<IDbModel | null> {
logger.info(`Fetching model with slug: ${modelSlug}`);
try {
const model = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
if (!model) {
if (!model[0]) {
logger.warn(`Model with slug ${modelSlug} not found`);
return null;
}
return model;
return model[0];
} catch (error) {
logger.error(`Error fetching model by slug: ${error}`);
return null;