From da464a3b603e7559dd4dcc67e76b77e30f54170f Mon Sep 17 00:00:00 2001 From: abo3skr2019 <68832286+abo3skr2019@users.noreply.github.com> Date: Mon, 2 Jun 2025 16:32:46 +0300 Subject: [PATCH 1/2] Fixed Expectation --- services/clubService.js | 82 +++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/services/clubService.js b/services/clubService.js index 7ff6edb..196ee22 100644 --- a/services/clubService.js +++ b/services/clubService.js @@ -13,6 +13,11 @@ const isValidUUID = (uuid) => { return uuidRegex.test(uuid); }; +// Helper function to check if string is a valid numeric ID +const isNumericId = (id) => { + return !isNaN(Number(id)) && Number.isInteger(Number(id)) && Number(id) > 0; +}; + // Helper function for email validation const isValidEmail = (email) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; @@ -138,6 +143,46 @@ const findByUUID = async (uuid, params = {}) => { return clubData; }; +/** + * Find club by ID (supports both numeric IDs and UUIDs) + * @param {Number|String} id Club ID or UUID + * @param {Object} [params] Query parameters + * @param {Array} [params.fields] Fields to select + * @returns {Promise} Club data + */ +const findClubById = async (id, params = {}) => { + try { + // Check if the ID is a UUID + if (isValidUUID(id)) { + return await findByUUID(id, params); + } + + // Check if it's a numeric ID + const numericId = Number(id); + if (!isNaN(numericId) && numericId > 0) { + const { fields = [] } = params; + const columns = buildSelectFields(fields, club); + + const clubData = await db.query.club.findFirst({ + where: eq(club.id, numericId), + columns, + }); + + if (!clubData) { + throw createError(404, 'Club not found'); + } + + return clubData; + } + + // If it's neither a UUID nor a numeric ID + throw createError(400, 'Invalid club ID format'); + } catch (error) { + console.error('Error in findClubById:', error); + throw error; + } +}; + /** * Create new club * @param {Object} data Club data @@ -257,7 +302,7 @@ const deleteClub = async (uuid, userId) => { /** * Get all memberships for a club - * @param {string} clubUuid Club UUID + * @param {String|Number} clubId Club ID or UUID * @param {Object} params Query parameters * @param {Object} params.pagination Pagination options (from QueryParser) * @param {Object} params.sort Sorting options (from QueryParser) @@ -266,25 +311,21 @@ const deleteClub = async (uuid, userId) => { * @param {Array} params.fields Fields to select (from QueryParser) * @returns {Promise} Paginated memberships with metadata */ -const getAllClubMemberships = async (clubUuid, params = {}) => { - if (!isValidUUID(clubUuid)) { - throw createError(400, 'Invalid UUID format'); - } - - const clubData = await findByUUID(clubUuid); - - // Extract pagination, sort, search, filters, and fields - QueryParser already provides defaults - const { - pagination = {}, - sort = {}, - search, - filters = {}, - fields = [], - } = params; - const { page = 1, limit = 10 } = pagination; - - // Calculate offset - const offset = (page - 1) * limit; +const getAllClubMemberships = async (clubId, params = {}) => { + const clubData = await findClubById(clubId); + + // Extract pagination, sort, search, filters, and fields - QueryParser already provides defaults + const { + pagination = {}, + sort = {}, + search, + filters = {}, + fields = [], + } = params; + const { page = 1, limit = 10 } = pagination; + + // Calculate offset + const offset = (page - 1) * limit; // Build base where conditions const whereConditions = [ @@ -825,6 +866,7 @@ const resetClubTerm = async (clubUuid, userId) => { module.exports = { getAllClubs, findByUUID, + findClubById, createClub, updateClub, deleteClub, From 47116167e2ba670298d2bc3028566f772f53cc09 Mon Sep 17 00:00:00 2001 From: abo3skr2019 <68832286+abo3skr2019@users.noreply.github.com> Date: Mon, 2 Jun 2025 23:04:19 +0300 Subject: [PATCH 2/2] Used the Helper Function & Fixed Undefined --- API-REST/Club/Club-get-info.bru | 2 +- controllers/clubController.js | 2 +- services/clubService.js | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/API-REST/Club/Club-get-info.bru b/API-REST/Club/Club-get-info.bru index be03762..b744749 100644 --- a/API-REST/Club/Club-get-info.bru +++ b/API-REST/Club/Club-get-info.bru @@ -16,7 +16,7 @@ headers { vars:pre-request { userId: 1 - clubUuid: a415f272-21a8-44c8-aa68-1fd91cac977e + clubUuid: 1 selectedFields: "uuid,name,description,logo,type,foundingDate,status" includeRelations: "memberships,events,supervisor" } diff --git a/controllers/clubController.js b/controllers/clubController.js index ac454f3..4edc9b6 100644 --- a/controllers/clubController.js +++ b/controllers/clubController.js @@ -22,7 +22,7 @@ const getAllClubs = async (req, res) => { * @param {Object} res Express response object */ const getClubByUuid = async (req, res) => { - const clubData = await clubService.findByUUID( + const clubData = await clubService.findClubById( req.params.clubUuid, req.parsedQuery, ); diff --git a/services/clubService.js b/services/clubService.js index 196ee22..d329ae3 100644 --- a/services/clubService.js +++ b/services/clubService.js @@ -157,14 +157,12 @@ const findClubById = async (id, params = {}) => { return await findByUUID(id, params); } - // Check if it's a numeric ID - const numericId = Number(id); - if (!isNaN(numericId) && numericId > 0) { + if (isNumericId(id)) { const { fields = [] } = params; const columns = buildSelectFields(fields, club); const clubData = await db.query.club.findFirst({ - where: eq(club.id, numericId), + where: eq(club.id, id), columns, });