diff --git a/src/modules/details/details.controller.ts b/src/modules/details/details.controller.ts index 0acce29..07a656b 100644 --- a/src/modules/details/details.controller.ts +++ b/src/modules/details/details.controller.ts @@ -17,7 +17,8 @@ import { Delete, NotFoundException, BadRequestException, - InternalServerErrorException, + HttpException, + HttpStatus, } from '@nestjs/common'; import { DetailsService } from './details.service'; @@ -31,7 +32,7 @@ export class DetailsController { * @param {number} idRestaurant - The unique identifier of the restaurant. * @returns {Promise} - A promise that resolves to an array of details. * @throws {NotFoundException} - Throws if no details are found for the restaurant. - * @throws {InternalServerErrorException} - Throws if there is an error during retrieval. + * @throws {HttpException} - Throws if there is an error during retrieval. * @async */ @Get() @@ -39,13 +40,14 @@ export class DetailsController { try { const details = await this.detailsService.findAll(Number(idRestaurant)); if (!details || details.length === 0) { - throw new NotFoundException('No details found'); + throw new NotFoundException(); } return details.details; } catch (error) { - throw new InternalServerErrorException( - `Error fetching details: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -56,7 +58,7 @@ export class DetailsController { * @param {number} id - The unique identifier of the detail. * @returns {Promise} - A promise that resolves to the requested detail. * @throws {NotFoundException} - Throws if the detail is not found. - * @throws {InternalServerErrorException} - Throws if there is an error during retrieval. + * @throws {HttpException} - Throws if there is an error during retrieval. * @async */ @Get(':id') @@ -70,13 +72,14 @@ export class DetailsController { Number(id), ); if (!detail) { - throw new NotFoundException(`Detail with id ${id} not found`); + throw new NotFoundException(); } return detail.details[0]; } catch (error) { - throw new InternalServerErrorException( - `Error fetching detail with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -87,7 +90,7 @@ export class DetailsController { * @param {Request} request - The request object containing detail data. * @returns {Promise} - A promise that resolves to the created detail. * @throws {BadRequestException} - Throws if there is an error during creation. - * @throws {InternalServerErrorException} - Throws if there is an error during creation. + * @throws {HttpException} - Throws if there is an error during creation. * @async */ @Post() @@ -100,12 +103,18 @@ export class DetailsController { Number(idRestaurant), request.body, ); - if (!createdDetail) { - throw new BadRequestException('Error creating detail'); + if (createdDetail.modifiedCount === 0) { + throw new NotFoundException(); + } + if (createdDetail.matchedCount === 0) { + throw new NotFoundException(); } return createdDetail; } catch (error) { - throw new InternalServerErrorException(`Error creating detail: ${error}`); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -118,7 +127,7 @@ export class DetailsController { * @returns {Promise} - A promise that resolves to a success message. * @throws {NotFoundException} - Throws if the detail is not found. * @throws {BadRequestException} - Throws if no changes are made. - * @throws {InternalServerErrorException} - Throws if there is an error during the update. + * @throws {HttpException} - Throws if there is an error during the update. * @async */ @Put(':id') @@ -134,18 +143,17 @@ export class DetailsController { request.body, ); if (result.matchedCount === 0) { - throw new NotFoundException(`Detail with id ${id} not found`); + throw new NotFoundException(); } if (result.modifiedCount === 0) { - throw new BadRequestException( - `No changes made to the detail with id ${id}`, - ); + throw new BadRequestException(); } - return { message: `Detail with id ${id} updated successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error updating detail with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -156,7 +164,7 @@ export class DetailsController { * @param {number} id - The unique identifier of the detail to be deleted. * @returns {Promise} - A promise that resolves to a success message. * @throws {NotFoundException} - Throws if the detail is not found. - * @throws {InternalServerErrorException} - Throws if there is an error during deletion. + * @throws {HttpException} - Throws if there is an error during deletion. * @async */ @Delete(':id') @@ -170,13 +178,14 @@ export class DetailsController { Number(id), ); if (result.modifiedCount === 0) { - throw new NotFoundException(`Detail with id ${id} not found`); + throw new NotFoundException(); } - return { message: `Detail with id ${id} deleted successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error deleting detail with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/src/modules/food/food.controller.ts b/src/modules/food/food.controller.ts index 3052082..d153514 100644 --- a/src/modules/food/food.controller.ts +++ b/src/modules/food/food.controller.ts @@ -17,7 +17,8 @@ import { Delete, NotFoundException, BadRequestException, - InternalServerErrorException, + HttpException, + HttpStatus, } from '@nestjs/common'; import { FoodService } from './food.service'; @@ -31,7 +32,7 @@ export class FoodController { * @param {number} idRestaurant - The unique identifier for the restaurant. * @returns {Promise} An array of food items. * @throws {NotFoundException} if no food items are found. - * @throws {InternalServerErrorException} if there is an error during the operation. + * @throws {HttpException} if there is an error during the operation. * @async */ @Get() @@ -39,11 +40,14 @@ export class FoodController { try { const food = await this.foodService.findAll(Number(idRestaurant)); if (!food || food.length === 0) { - throw new NotFoundException('No food found'); + throw new NotFoundException(); } return food.foods; // Assuming food.foods is of type any[] } catch (error) { - throw new InternalServerErrorException(`Error fetching food: ${error}`); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -54,7 +58,7 @@ export class FoodController { * @param {number} id - The unique identifier for the food item. * @returns {Promise} The food item if found. * @throws {NotFoundException} if the food item is not found. - * @throws {InternalServerErrorException} if there is an error during the operation. + * @throws {HttpException} if there is an error during the operation. * @async */ @Get(':id') @@ -68,13 +72,14 @@ export class FoodController { Number(id), ); if (!food) { - throw new NotFoundException(`Food with id ${id} not found`); + throw new NotFoundException(); } return food.foods[0]; // Assuming food.foods is of type any[] } catch (error) { - throw new InternalServerErrorException( - `Error fetching food with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -85,7 +90,7 @@ export class FoodController { * @param {Request} request - The request object containing the food item data. * @returns {Promise} The created food item. * @throws {BadRequestException} if there is an error during creation. - * @throws {InternalServerErrorException} if there is an error during the operation. + * @throws {HttpException} if there is an error during the operation. * @async */ @Post() @@ -98,12 +103,18 @@ export class FoodController { Number(idRestaurant), request.body, ); - if (!createdFood) { - throw new BadRequestException('Error creating food'); + if (createdFood.modifiedCount === 0) { + throw new NotFoundException(); + } + if (createdFood.matchedCount === 0) { + throw new NotFoundException(); } return createdFood; // Assuming createdFood is of type any } catch (error) { - throw new InternalServerErrorException(`Error creating food: ${error}`); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -116,7 +127,7 @@ export class FoodController { * @returns {Promise} A success message if the food item is updated successfully. * @throws {NotFoundException} if the food item is not found. * @throws {BadRequestException} if no changes are made. - * @throws {InternalServerErrorException} if there is an error during the operation. + * @throws {HttpException} if there is an error during the operation. * @async */ @Put(':id') @@ -132,18 +143,17 @@ export class FoodController { request.body, ); if (result.matchedCount === 0) { - throw new NotFoundException(`Food with id ${id} not found`); + throw new NotFoundException(); } if (result.modifiedCount === 0) { - throw new BadRequestException( - `No changes made to the food with id ${id}`, - ); + throw new NotFoundException(); } - return { message: `Food with id ${id} updated successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error updating food with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -154,7 +164,7 @@ export class FoodController { * @param {number} id - The unique identifier for the food item. * @returns {Promise} A success message if the food item is deleted successfully. * @throws {NotFoundException} if the food item is not found. - * @throws {InternalServerErrorException} if there is an error during the operation. + * @throws {HttpException} if there is an error during the operation. * @async */ @Delete(':id') @@ -168,13 +178,14 @@ export class FoodController { Number(id), ); if (result.modifiedCount === 0) { - throw new NotFoundException(`Food with id ${id} not found`); + throw new NotFoundException(); } - return { message: `Food with id ${id} deleted successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error deleting food with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/src/modules/food_category/food_category.controller.ts b/src/modules/food_category/food_category.controller.ts index 5e5fa36..22b9c81 100644 --- a/src/modules/food_category/food_category.controller.ts +++ b/src/modules/food_category/food_category.controller.ts @@ -8,7 +8,8 @@ import { Delete, NotFoundException, BadRequestException, - InternalServerErrorException, + HttpException, + HttpStatus, } from '@nestjs/common'; import { FoodCategoryService } from './food_category.service'; @@ -29,7 +30,7 @@ export class FoodCategoryController { * @param {number} idRestaurant - The ID of the restaurant. * @returns {Promise} The list of food categories. * @throws {NotFoundException} If no food categories are found. - * @throws {InternalServerErrorException} If there's an error fetching the categories. + * @throws {HttpException} If there's an error fetching the categories. */ @Get() async getAllFoodCategory(@Param('idRestaurant') idRestaurant: number) { @@ -38,13 +39,14 @@ export class FoodCategoryController { Number(idRestaurant), ); if (!foodCategory || foodCategory.length === 0) { - throw new NotFoundException('No foodCategory found'); + throw new NotFoundException(); } return foodCategory.food_category; // Return the list of food categories } catch (error) { - throw new InternalServerErrorException( - `Error fetching foodCategory: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -55,7 +57,7 @@ export class FoodCategoryController { * @param {number} id - The ID of the food category. * @returns {Promise} The food category details. * @throws {NotFoundException} If the food category with the specified ID is not found. - * @throws {InternalServerErrorException} If there's an error fetching the category. + * @throws {HttpException} If there's an error fetching the category. */ @Get(':id') async getOneFoodCategory( @@ -68,13 +70,14 @@ export class FoodCategoryController { Number(id), ); if (!foodCategory) { - throw new NotFoundException(`FoodCategory with id ${id} not found`); + throw new NotFoundException(); } return foodCategory.food_category[0]; // Return the specific food category } catch (error) { - throw new InternalServerErrorException( - `Error fetching foodCategory with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -85,7 +88,7 @@ export class FoodCategoryController { * @param {Request} request - The HTTP request containing the food category details. * @returns {Promise} The created food category details. * @throws {BadRequestException} If there's an error creating the food category. - * @throws {InternalServerErrorException} If there's an error during the creation process. + * @throws {HttpException} If there's an error during the creation process. */ @Post() async createFoodCategory( @@ -97,14 +100,18 @@ export class FoodCategoryController { Number(idRestaurant), request.body, ); - if (!createdFoodCategory) { - throw new BadRequestException('Error creating foodCategory'); + if (createdFoodCategory.modifiedCount === 0) { + throw new NotFoundException(); + } + if (createdFoodCategory.matchedCount === 0) { + throw new NotFoundException(); } return createdFoodCategory; // Return the created food category } catch (error) { - throw new InternalServerErrorException( - `Error creating foodCategory: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -117,7 +124,7 @@ export class FoodCategoryController { * @returns {Promise} A success message. * @throws {NotFoundException} If the food category with the specified ID is not found. * @throws {BadRequestException} If no changes were made to the food category. - * @throws {InternalServerErrorException} If there's an error updating the category. + * @throws {HttpException} If there's an error updating the category. */ @Put(':id') async updateOneFoodCategory( @@ -132,18 +139,17 @@ export class FoodCategoryController { request.body, ); if (result.matchedCount === 0) { - throw new NotFoundException(`FoodCategory with id ${id} not found`); + throw new NotFoundException(); } if (result.modifiedCount === 0) { - throw new BadRequestException( - `No changes made to the foodCategory with id ${id}`, - ); + throw new BadRequestException(); } - return { message: `FoodCategory with id ${id} updated successfully` }; // Return success message + return; } catch (error) { - throw new InternalServerErrorException( - `Error updating foodCategory with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -154,7 +160,7 @@ export class FoodCategoryController { * @param {number} id - The ID of the food category to delete. * @returns {Promise} A success message. * @throws {NotFoundException} If the food category with the specified ID is not found. - * @throws {InternalServerErrorException} If there's an error deleting the category. + * @throws {HttpException} If there's an error deleting the category. */ @Delete(':id') async deleteOneFoodCategory( @@ -167,13 +173,14 @@ export class FoodCategoryController { Number(id), ); if (result.modifiedCount === 0) { - throw new NotFoundException(`FoodCategory with id ${id} not found`); + throw new NotFoundException(); } - return { message: `FoodCategory with id ${id} deleted successfully` }; // Return success message + return; } catch (error) { - throw new InternalServerErrorException( - `Error deleting foodCategory with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/src/modules/ingredient/ingredient.controller.ts b/src/modules/ingredient/ingredient.controller.ts index 5607960..9a1cb13 100644 --- a/src/modules/ingredient/ingredient.controller.ts +++ b/src/modules/ingredient/ingredient.controller.ts @@ -9,6 +9,8 @@ import { NotFoundException, BadRequestException, InternalServerErrorException, + HttpException, + HttpStatus, } from '@nestjs/common'; import { IngredientService } from './ingredient.service'; import { Request } from 'express'; @@ -28,6 +30,9 @@ export class IngredientController { * * @param {number} idRestaurant - The ID of the restaurant. * @returns {Promise} The list of ingredients. + * @throws {NotFoundException} - Throws if no ingredients are found for the restaurant. + * @throws {HttpException} - Throws if there is an error during retrieval. + * @async */ @Get() async getAllIngredient(@Param('idRestaurant') idRestaurant: number) { @@ -36,13 +41,14 @@ export class IngredientController { Number(idRestaurant), ); if (!ingredient || ingredient.length === 0) { - throw new NotFoundException('No ingredients found'); + throw new NotFoundException(); } return ingredient.ingredients; // Ensure `ingredients` is properly defined in your service's return type } catch (error) { - throw new InternalServerErrorException( - `Error fetching ingredients: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -52,6 +58,9 @@ export class IngredientController { * @param {number} idRestaurant - The ID of the restaurant. * @param {number} id - The ID of the ingredient. * @returns {Promise} The ingredient. + * @throws {NotFoundException} - Throws if the detail is not found. + * @throws {HttpException} - Throws if there is an error during retrieval. + * @async */ @Get(':id') async getOneIngredient( @@ -64,13 +73,14 @@ export class IngredientController { Number(id), ); if (!ingredient) { - throw new NotFoundException(`Ingredient with id ${id} not found`); + throw new NotFoundException(); } return ingredient.ingredients[0]; // Ensure `ingredients` is properly defined in your service's return type } catch (error) { - throw new InternalServerErrorException( - `Error fetching ingredient with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -80,6 +90,9 @@ export class IngredientController { * @param {number} idRestaurant - The ID of the restaurant. * @param {Request} request - The request containing ingredient data. * @returns {Promise} The created ingredient. + * @throws {BadRequestException} - Throws if there is an error during creation. + * @throws {HttpException} - Throws if there is an error during creation. + * @async */ @Post() async createIngredient( @@ -91,14 +104,18 @@ export class IngredientController { Number(idRestaurant), request.body, ); - if (!createdIngredient) { - throw new BadRequestException('Error creating ingredient'); + if (createdIngredient.modifiedCount === 0) { + throw new NotFoundException(); + } + if (createdIngredient.matchedCount === 0) { + throw new NotFoundException(); } return createdIngredient; } catch (error) { - throw new InternalServerErrorException( - `Error creating ingredient: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -109,6 +126,10 @@ export class IngredientController { * @param {number} id - The ID of the ingredient to update. * @param {Request} request - The request containing updated ingredient data. * @returns {Promise} The update result message. + * @throws {NotFoundException} - Throws if the detail is not found. + * @throws {BadRequestException} - Throws if no changes are made. + * @throws {HttpException} - Throws if there is an error during the update. + * @async */ @Put(':id') async updateOneIngredient( @@ -123,18 +144,17 @@ export class IngredientController { request.body, ); if (result.matchedCount === 0) { - throw new NotFoundException(`Ingredient with id ${id} not found`); + throw new NotFoundException(); } if (result.modifiedCount === 0) { - throw new BadRequestException( - `No changes made to the ingredient with id ${id}`, - ); + throw new BadRequestException(); } - return { message: `Ingredient with id ${id} updated successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error updating ingredient with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -144,6 +164,9 @@ export class IngredientController { * @param {number} idRestaurant - The ID of the restaurant. * @param {number} id - The ID of the ingredient to delete. * @returns {Promise} The delete result message. + * @throws {NotFoundException} - Throws if the detail is not found. + * @throws {HttpException} - Throws if there is an error during deletion. + * @async */ @Delete(':id') async deleteOneIngredient( @@ -156,13 +179,14 @@ export class IngredientController { Number(id), ); if (result.modifiedCount === 0) { - throw new NotFoundException(`Ingredient with id ${id} not found`); + throw new NotFoundException(); } - return { message: `Ingredient with id ${id} deleted successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error deleting ingredient with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } } \ No newline at end of file diff --git a/src/modules/orders/orders.controller.ts b/src/modules/orders/orders.controller.ts index 2cf84c1..69c3d9b 100644 --- a/src/modules/orders/orders.controller.ts +++ b/src/modules/orders/orders.controller.ts @@ -10,6 +10,8 @@ import { NotFoundException, BadRequestException, InternalServerErrorException, + HttpException, + HttpStatus, } from '@nestjs/common'; import { OrdersService } from './orders.service'; @@ -50,8 +52,11 @@ export class OrdersController { const result = await queryFunc(Number(idRestaurant)) return result; - } catch (err) { - throw new InternalServerErrorException(`Error fetching orders: ${err}`); + } catch (error) { + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -81,7 +86,7 @@ export class OrdersController { foodOrdered = foodOrdered.orders[0]; } if (!foodOrdered) { - throw new NotFoundException(`Order with id ${id} not found`); + throw new NotFoundException(); } // Grouping food ordered items based on specific attributes @@ -113,9 +118,10 @@ export class OrdersController { foodOrdered.food_ordered = await groupedFoodOrdered; return foodOrdered; } catch (error) { - throw new InternalServerErrorException( - `Error fetching order with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -132,12 +138,18 @@ export class OrdersController { async createOrder(@Req() request: Request, @Param('idRestaurant') idRestaurant: number) { try { const createdOrder = await this.ordersService.createOne(Number(idRestaurant), request.body); - if (!createdOrder) { - throw new BadRequestException('Error creating order'); + if (createdOrder.modifiedCount === 0) { + throw new NotFoundException(); + } + if (createdOrder.matchedCount === 0) { + throw new NotFoundException(); } return createdOrder; } catch (error) { - throw new InternalServerErrorException(`Error creating order: ${error}`); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -161,18 +173,17 @@ export class OrdersController { request.body, ); if (result.matchedCount === 0) { - throw new NotFoundException(`Order with id ${id} not found`); + throw new NotFoundException(); } if (result.modifiedCount === 0) { - throw new BadRequestException( - `No changes made to the order with id ${id}`, - ); + throw new BadRequestException(); } - return { message: `Order with id ${id} updated successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error updating order with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -190,13 +201,14 @@ export class OrdersController { try { const result = await this.ordersService.deleteOne(Number(idRestaurant), Number(id)); if (result.modifiedCount === 0) { - throw new NotFoundException(`Order with id ${id} not found`); + throw new NotFoundException(); } - return { message: `Order with id ${id} deleted successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error deleting order with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -214,13 +226,14 @@ export class OrdersController { try { const result = await this.ordersService.markFoodOrderedReady(Number(idRestaurant), Number(id)); if (result.modifiedCount === 0) { - throw new NotFoundException(`Order with id ${id} not found`); + throw new NotFoundException(); } - return { message: `FoodOrder with id ${id} updated successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error updating food_ordered with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/src/modules/permission/permission.controller.ts b/src/modules/permission/permission.controller.ts index 0add081..80ecd41 100644 --- a/src/modules/permission/permission.controller.ts +++ b/src/modules/permission/permission.controller.ts @@ -9,6 +9,8 @@ import { NotFoundException, BadRequestException, InternalServerErrorException, + HttpException, + HttpStatus, } from '@nestjs/common'; import { PermissionService } from './permission.service'; @@ -23,117 +25,165 @@ export class PermissionController { constructor(private readonly permissionService: PermissionService) {} /** - * Retrieves all permissions. - * - * @returns {Promise} List of permissions. + * Retrieves all permissions for a specific restaurant. + * + * @param {number} idRestaurant - The unique identifier of the restaurant. + * @returns {Promise} - A promise that resolves to an array of permissions. + * @throws {NotFoundException} - Throws if no permissions are found for the restaurant. + * @throws {HttpException} - Throws if there is an error during retrieval. + * @async */ @Get() - async getAllPermission() { + async getAllPermission(@Param('idRestaurant') idRestaurant: number) { try { - const permissions = await this.permissionService.findAll(); + const permissions = await this.permissionService.findAll(Number(idRestaurant)); if (!permissions || permissions.length === 0) { - throw new NotFoundException('No permissions found'); + throw new NotFoundException(); } - return permissions; + return permissions.permissions; } catch (error) { - throw new InternalServerErrorException( - `Error fetching permissions: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } /** - * Retrieves a specific permission by its ID. - * - * @param {number} id - The ID of the permission. - * @returns {Promise} The requested permission. + * Retrieves a specific permission by its ID for a specific restaurant. + * + * @param {number} idRestaurant - The unique identifier of the restaurant. + * @param {number} id - The unique identifier of the permission. + * @returns {Promise} - A promise that resolves to the requested permission. + * @throws {NotFoundException} - Throws if the permission is not found. + * @throws {HttpException} - Throws if there is an error during retrieval. + * @async */ @Get(':id') - async getOnePermission(@Param('id') id: number) { + async getOnePermission( + @Param('idRestaurant') idRestaurant: number, + @Param('id') id: number, + ) { try { - const permission = await this.permissionService.findById(Number(id)); + const permission = await this.permissionService.findById( + Number(idRestaurant), + Number(id), + ); if (!permission) { - throw new NotFoundException(`Permission with id ${id} not found`); + throw new NotFoundException(); } - return permission; + return permission.permissions[0]; } catch (error) { - throw new InternalServerErrorException( - `Error fetching permission with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } /** - * Creates a new permission. - * - * @param {Request} request - The incoming request containing the permission data. - * @returns {Promise} The created permission. + * Creates a new permission for a specific restaurant. + * + * @param {number} idRestaurant - The unique identifier of the restaurant. + * @param {Request} request - The request object containing permission data. + * @returns {Promise} - A promise that resolves to the created permission. + * @throws {BadRequestException} - Throws if there is an error during creation. + * @throws {HttpException} - Throws if there is an error during creation. + * @async */ @Post() - async createPermission(@Req() request: Request) { + async createPermission( + @Param('idRestaurant') idRestaurant: number, + @Req() request: Request, + ) { try { const createdPermission = await this.permissionService.createOne( + Number(idRestaurant), request.body, ); - if (!createdPermission) { - throw new BadRequestException('Error creating permission'); + if (createdPermission.modifiedCount === 0) { + throw new NotFoundException(); + } + if (createdPermission.matchedCount === 0) { + throw new NotFoundException(); } return createdPermission; } catch (error) { - throw new InternalServerErrorException( - `Error creating permission: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } /** - * Updates an existing permission by its ID. - * - * @param {number} id - The ID of the permission to update. - * @param {Request} request - The incoming request containing the updated data. - * @returns {Promise} Success message. + * Updates an existing permission for a specific restaurant. + * + * @param {number} idRestaurant - The unique identifier of the restaurant. + * @param {number} id - The unique identifier of the permission to be updated. + * @param {Request} request - The request object containing updated permission data. + * @returns {Promise} - A promise that resolves to a success message. + * @throws {NotFoundException} - Throws if the permission is not found. + * @throws {BadRequestException} - Throws if no changes are made. + * @throws {HttpException} - Throws if there is an error during the update. + * @async */ @Put(':id') - async updateOnePermission(@Param('id') id: number, @Req() request: Request) { + async updateOnePermission( + @Param('idRestaurant') idRestaurant: number, + @Param('id') id: number, + @Req() request: Request, + ) { try { const result = await this.permissionService.updateOne( + Number(idRestaurant), Number(id), request.body, ); if (result.matchedCount === 0) { - throw new NotFoundException(`Permission with id ${id} not found`); + throw new NotFoundException(); } if (result.modifiedCount === 0) { - throw new BadRequestException( - `No changes made to the permission with id ${id}`, - ); + throw new BadRequestException(); } - return { message: `Permission with id ${id} updated successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error updating permission with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } /** - * Deletes a permission by its ID. - * - * @param {number} id - The ID of the permission to delete. - * @returns {Promise} Success message. + * Deletes a specific permission for a restaurant. + * + * @param {number} idRestaurant - The unique identifier of the restaurant. + * @param {number} id - The unique identifier of the permission to be deleted. + * @returns {Promise} - A promise that resolves to a success message. + * @throws {NotFoundException} - Throws if the permission is not found. + * @throws {HttpException} - Throws if there is an error during deletion. + * @async */ @Delete(':id') - async deleteOnePermission(@Param('id') id: number) { + async deleteOnePermission( + @Param('idRestaurant') idRestaurant: number, + @Param('id') id: number, + ) { try { - const result = await this.permissionService.deleteOne(Number(id)); - if (result.deletedCount === 0) { - throw new NotFoundException(`Permission with id ${id} not found`); + const result = await this.permissionService.deleteOne( + Number(idRestaurant), + Number(id), + ); + if (result.modifiedCount === 0) { + throw new NotFoundException(); } - return { message: `Permission with id ${id} deleted successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error deleting permission with id ${id}: ${error}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/src/modules/permission/permission.service.ts b/src/modules/permission/permission.service.ts index 0862429..358229f 100644 --- a/src/modules/permission/permission.service.ts +++ b/src/modules/permission/permission.service.ts @@ -1,69 +1,122 @@ import { Injectable } from '@nestjs/common'; import mongoose from 'mongoose'; -import { UpdateResult, InsertOneResult, DeleteResult } from 'mongodb'; +import { UpdateResult, ReturnDocument } from 'mongodb'; import { DB } from 'src/db/db'; +import { Restaurant } from 'src/shared/interfaces/restaurant.interface'; +import { Counter } from 'src/shared/interfaces/counter.interface'; @Injectable() export class PermissionService extends DB { /** - * Retrieves all permissions from the database. - * - * @returns {Promise[]>} An array of permissions. + * Retrieves all permission for a specified restaurant. + * + * @param {number} idRestaurant - The ID of the restaurant. + * @returns {Promise>} + * A promise that resolves to the restaurant permission. + * @async */ - async findAll(): Promise[]> { + async findAll( + idRestaurant: number, + ): Promise> { const db = this.getDbConnection(); - return db.collection('permission').find({}).toArray(); + + return db + .collection('restaurant') + .findOne({ id: idRestaurant }, { projection: { _id: 0, permission: 1 } }); } /** - * Retrieves a specific permission by its ID. - * - * @param {number} id - The ID of the permission. - * @returns {Promise | null>} The permission if found, otherwise null. + * Retrieves a specific permission by its ID for a specified restaurant. + * + * @param {number} idRestaurant - The ID of the restaurant. + * @param {number} id - The ID of the permission to retrieve. + * @returns {Promise>} + * A promise that resolves to the specific permission. + * @async */ async findById( + idRestaurant: number, id: number, - ): Promise | null> { + ): Promise> { const db = this.getDbConnection(); - return db.collection('permission').findOne({ id: id }); + + return db + .collection('restaurant') + .findOne( + { id: idRestaurant }, + { projection: { _id: 0, permission: { $elemMatch: { id: id } } } }, + ); } /** - * Creates a new permission in the database. - * - * @param {ReadableStream} body - The data for the new permission. - * @returns {Promise>} The result of the insert operation. + * Creates a new permission for a specified restaurant. + * + * @param {number} idRestaurant - The ID of the restaurant. + * @param {ReadableStream} body - The permission data to be created. + * @returns {Promise} + * A promise that resolves to the result of the update operation. + * @async */ async createOne( + idRestaurant: number, body: ReadableStream, - ): Promise> { + ): Promise { const db = this.getDbConnection(); - return db.collection('permission').insertOne(body); + const id = await db + .collection('counter') + .findOneAndUpdate( + { _id: 'permissionId' }, + { $inc: { sequence_value: 1 } }, + { returnDocument: ReturnDocument.AFTER }, + ); + + body['id'] = id.sequence_value; // Assign a new ID to the body. + return db + .collection('restaurant') + .updateOne({ id: idRestaurant }, { $addToSet: { permission: body } }); } /** - * Updates an existing permission by its ID. - * + * Updates an existing permission for a specified restaurant. + * + * @param {number} idRestaurant - The ID of the restaurant. * @param {number} id - The ID of the permission to update. - * @param {ReadableStream} body - The updated data for the permission. - * @returns {Promise} The result of the update operation. + * @param {ReadableStream} body - The updated permission data. + * @returns {Promise} + * A promise that resolves to the result of the update operation. + * @async */ async updateOne( + idRestaurant: number, id: number, body: ReadableStream, ): Promise { const db = this.getDbConnection(); - return db.collection('permission').updateOne({ id: id }, { $set: body }); + + return db.collection('restaurant').updateOne( + { id: idRestaurant, 'permission.id': id }, + { + $set: { + + }, + }, + ); } /** - * Deletes a permission by its ID. - * + * Deletes a specific permission for a specified restaurant. + * + * @param {number} idRestaurant - The ID of the restaurant. * @param {number} id - The ID of the permission to delete. - * @returns {Promise} The result of the delete operation. + * @returns {Promise} + * A promise that resolves to the result of the update operation. + * @async */ - async deleteOne(id: number): Promise { + async deleteOne(idRestaurant: number, id: number): Promise { const db = this.getDbConnection(); - return db.collection('permission').deleteOne({ id: id }); + + return db + .collection('restaurant') + .updateOne({ id: idRestaurant }, { $pull: { permission: { id: id } } }); } } diff --git a/src/modules/pos/pos.controller.ts b/src/modules/pos/pos.controller.ts index f02a328..f0d4216 100644 --- a/src/modules/pos/pos.controller.ts +++ b/src/modules/pos/pos.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Param, NotFoundException, InternalServerErrorException } from '@nestjs/common'; +import { Controller, Get, Param, NotFoundException, InternalServerErrorException, HttpException, HttpStatus } from '@nestjs/common'; import { PosService } from './pos.service'; @Controller('api/pos') @@ -10,6 +10,9 @@ export class PosController { * * @param {number} id - The ID of the permission to delete. * @returns {Promise} Success message. + * @throws {NotFoundException} - Throws if the restaurant is not found. + * @throws {HttpException} - Throws if there is an error during deletion. + * @async */ @Get(':id') async getAllDataPOS(@Param('id') id: number) { @@ -53,10 +56,10 @@ export class PosController { return resultFood; // Returns the structured food categories and their respective foods } catch (error) { - // Throws an InternalServerErrorException if an error occurs while fetching data - throw new InternalServerErrorException( - `Error fetching data for restaurant with id ${id}: ${error.message}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/src/modules/restaurants/restaurants.controller.ts b/src/modules/restaurants/restaurants.controller.ts index f907cca..6487d5b 100644 --- a/src/modules/restaurants/restaurants.controller.ts +++ b/src/modules/restaurants/restaurants.controller.ts @@ -9,6 +9,8 @@ import { NotFoundException, BadRequestException, InternalServerErrorException, + HttpException, + HttpStatus, } from '@nestjs/common'; import { RestaurantsService } from './restaurants.service'; import { Request } from 'express'; // Ensure to import Request from express @@ -27,13 +29,14 @@ export class RestaurantsController { try { const restaurants = await this.restaurantsService.findAll(); if (!restaurants || restaurants.length === 0) { - throw new NotFoundException('No restaurants found'); + throw new NotFoundException(); } return restaurants; } catch (error) { - throw new InternalServerErrorException( - `Error fetching restaurants: ${error.message}`, // Use error.message for clearer output - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -48,13 +51,14 @@ export class RestaurantsController { try { const restaurant = await this.restaurantsService.findById(Number(id)); if (!restaurant) { - throw new NotFoundException(`Restaurant with id ${id} not found`); + throw new NotFoundException(); } return restaurant; } catch (error) { - throw new InternalServerErrorException( - `Error fetching restaurant with id ${id}: ${error.message}`, // Use error.message for clarity - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -71,13 +75,14 @@ export class RestaurantsController { request.body, ); if (!createdRestaurant) { - throw new BadRequestException('Error creating restaurant'); + throw new BadRequestException(); } return createdRestaurant; } catch (error) { - throw new InternalServerErrorException( - `Error creating restaurant: ${error.message}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -96,18 +101,17 @@ export class RestaurantsController { request.body, ); if (result.matchedCount === 0) { - throw new NotFoundException(`Restaurant with id ${id} not found`); + throw new NotFoundException(); } if (result.modifiedCount === 0) { - throw new BadRequestException( - `No changes made to the restaurant with id ${id}`, - ); + throw new BadRequestException(); } - return { message: `Restaurant with id ${id} updated successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error updating restaurant with id ${id}: ${error.message}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -122,13 +126,14 @@ export class RestaurantsController { try { const result = await this.restaurantsService.deleteOne(Number(id)); if (result.deletedCount === 0) { - throw new NotFoundException(`Restaurant with id ${id} not found`); + throw new NotFoundException(); } - return { message: `Restaurant with id ${id} deleted successfully` }; + return; } catch (error) { - throw new InternalServerErrorException( - `Error deleting restaurant with id ${id}: ${error.message}`, - ); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index 2ebf2c6..ad9f9f9 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -9,6 +9,8 @@ import { NotFoundException, BadRequestException, InternalServerErrorException, + HttpException, + HttpStatus, } from '@nestjs/common'; import { UsersService } from './users.service'; import { Request } from 'express'; // Ensure to import Request from express @@ -22,17 +24,23 @@ export class UsersController { * * @param {number} idRestaurant - The ID of the restaurant. * @returns {Promise} An array of users. + * @throws {NotFoundException} - Throws if no users are found for the restaurant. + * @throws {HttpException} - Throws if there is an error during retrieval. + * @async */ @Get() async getAllUser(@Param('idRestaurant') idRestaurant: number) { try { const users = await this.usersService.findAll(Number(idRestaurant)); if (!users || users.length === 0) { - throw new NotFoundException('No users found'); + throw new NotFoundException(); } return users; } catch (error) { - throw new InternalServerErrorException(`Error fetching users: ${error}`); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -42,17 +50,23 @@ export class UsersController { * @param {number} idRestaurant - The ID of the restaurant. * @param {number} id - The ID of the user. * @returns {Promise} The user object if found. + * @throws {NotFoundException} - Throws if the user is not found. + * @throws {HttpException} - Throws if there is an error during retrieval. + * @async */ @Get(':id') async getOneUser(@Param('idRestaurant') idRestaurant: number, @Param('id') id: number) { try { const user = await this.usersService.findById(Number(idRestaurant), Number(id)); if (!user) { - throw new NotFoundException(`User with id ${id} not found`); + throw new NotFoundException(); } return user; } catch (error) { - throw new InternalServerErrorException(`Error fetching user with id ${id}: ${error}`); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -62,17 +76,23 @@ export class UsersController { * @param {number} idRestaurant - The ID of the restaurant. * @param {Request} request - The request object containing user data. * @returns {Promise} The created user object. + * @throws {BadRequestException} - Throws if there is an error during creation. + * @throws {HttpException} - Throws if there is an error during creation. + * @async */ @Post() async createUser(@Param('idRestaurant') idRestaurant: number, @Req() request: Request) { try { const createdUser = await this.usersService.createOne(Number(idRestaurant), request.body); if (!createdUser) { - throw new BadRequestException('Error creating user'); + throw new BadRequestException(); } return createdUser; } catch (error) { - throw new InternalServerErrorException(`Error creating user: ${error}`); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -83,6 +103,10 @@ export class UsersController { * @param {number} id - The ID of the user. * @param {Request} request - The request object containing updated user data. * @returns {Promise} A success message. + * @throws {NotFoundException} - Throws if the user is not found. + * @throws {BadRequestException} - Throws if no changes are made. + * @throws {HttpException} - Throws if there is an error during the update. + * @async */ @Put(':id') async updateOneUser( @@ -97,14 +121,17 @@ export class UsersController { request.body, ); if (result.matchedCount === 0) { - throw new NotFoundException(`User with id ${id} not found`); + throw new NotFoundException(); } if (result.modifiedCount === 0) { - throw new BadRequestException(`No changes made to the user with id ${id}`); + throw new BadRequestException(); } - return { message: `User with id ${id} updated successfully` }; + return; } catch (error) { - throw new InternalServerErrorException(`Error updating user with id ${id}: ${error}`); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -114,17 +141,23 @@ export class UsersController { * @param {number} idRestaurant - The ID of the restaurant. * @param {number} id - The ID of the user. * @returns {Promise} A success message. + * @throws {NotFoundException} - Throws if the user is not found. + * @throws {HttpException} - Throws if there is an error during deletion. + * @async */ @Delete(':id') async deleteOneUser(@Param('idRestaurant') idRestaurant: number, @Param('id') id: number) { try { const result = await this.usersService.deleteOne(Number(idRestaurant), Number(id)); if (result.modifiedCount === 0) { - throw new NotFoundException(`User with id ${id} not found`); + throw new NotFoundException(); } - return { message: `User with id ${id} deleted successfully` }; + return; } catch (error) { - throw new InternalServerErrorException(`Error deleting user with id ${id}: ${error}`); + if (error instanceof HttpException) { + throw error; + } + throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR); } } }