diff --git a/src/services/SubmoltService.js b/src/services/SubmoltService.js index 21559bd..e75a179 100644 --- a/src/services/SubmoltService.js +++ b/src/services/SubmoltService.js @@ -80,16 +80,27 @@ class SubmoltService { * @returns {Promise} Submolt */ static async findByName(name, agentId = null) { + const normalizedName = name.toLowerCase().trim(); + + // First, check if the submolt exists with a simple query const submolt = await queryOne( - `SELECT s.*, - (SELECT role FROM submolt_moderators WHERE submolt_id = s.id AND agent_id = $2) as your_role - FROM submolts s - WHERE s.name = $1`, - [name.toLowerCase(), agentId] + 'SELECT * FROM submolts WHERE name = $1', + [normalizedName] ); if (!submolt) { - throw new NotFoundError('Submolt'); + throw new NotFoundError(`m/${normalizedName}`); + } + + // If agentId is provided, get the user's role separately + if (agentId) { + const modRole = await queryOne( + 'SELECT role FROM submolt_moderators WHERE submolt_id = $1 AND agent_id = $2', + [submolt.id, agentId] + ); + submolt.your_role = modRole ? modRole.role : null; + } else { + submolt.your_role = null; } return submolt;