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:
parent
438ae4b5d0
commit
e8acfd7b30
@ -80,12 +80,14 @@ async function deleteModel(modelSlug: string): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
logger.info(`Deleting model with ID: ${modelSlug}`);
|
logger.info(`Deleting model with ID: ${modelSlug}`);
|
||||||
const doesExist = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
|
const doesExist = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
|
||||||
if (!doesExist || !doesExist.id) {
|
if (!doesExist[0]) {
|
||||||
logger.warn(`Model with slug ${modelSlug} not found`);
|
logger.warn(`Model with slug ${modelSlug} not found`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
const target = doesExist[0]
|
||||||
|
if (!target.id) return false;
|
||||||
try {
|
try {
|
||||||
await MysqlService.Model.delete(DbHandler, doesExist.id);
|
await MysqlService.Model.delete(DbHandler, target.id);
|
||||||
logger.info("Deletion Successful !");
|
logger.info("Deletion Successful !");
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -104,11 +106,11 @@ async function getBySlugModel(modelSlug: string): Promise<IDbModel | null> {
|
|||||||
logger.info(`Fetching model with slug: ${modelSlug}`);
|
logger.info(`Fetching model with slug: ${modelSlug}`);
|
||||||
try {
|
try {
|
||||||
const model = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
|
const model = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
|
||||||
if (!model) {
|
if (!model[0]) {
|
||||||
logger.warn(`Model with slug ${modelSlug} not found`);
|
logger.warn(`Model with slug ${modelSlug} not found`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return model;
|
return model[0];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error fetching model by slug: ${error}`);
|
logger.error(`Error fetching model by slug: ${error}`);
|
||||||
return null;
|
return null;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user