diff --git a/src/domain/booking.ts b/src/domain/booking.ts index d58bac2..f0ecf13 100644 --- a/src/domain/booking.ts +++ b/src/domain/booking.ts @@ -7,6 +7,7 @@ export const CLOSING_HOUR = 22; // a las 22:00 se apagan las luces export const MAX_HOURS_PER_BOOKING = 3; export const BOOKING_WINDOW_DAYS = 14; // se reserva con hasta dos semanas de anticipación export const CANCELLATION_NOTICE_HOURS = 3; +export const TEAM_NAME_MIN_LENGTH = 3; export const TEAM_NAME_MAX_LENGTH = 40; export const BOOKING_STATUSES = ['confirmed', 'cancelled'] as const; diff --git a/src/http/handlers.ts b/src/http/handlers.ts index 4f1354a..3df3088 100644 --- a/src/http/handlers.ts +++ b/src/http/handlers.ts @@ -2,6 +2,7 @@ import { CLOSING_HOUR, OPENING_HOUR, TEAM_NAME_MAX_LENGTH, + TEAM_NAME_MIN_LENGTH, checkBookingRules, isCancellableAt, overlaps, @@ -178,11 +179,11 @@ function parseBookingRequest(body: unknown): BookingRequest { throw new HttpError(400, 'VALIDATION_ERROR', 'Falta el campo date (AAAA-MM-DD).'); } const teamName = typeof fields.teamName === 'string' ? fields.teamName.trim() : ''; - if (teamName.length === 0 || teamName.length > TEAM_NAME_MAX_LENGTH) { + if (teamName.length < TEAM_NAME_MIN_LENGTH || teamName.length > TEAM_NAME_MAX_LENGTH) { throw new HttpError( 400, 'VALIDATION_ERROR', - `El nombre del equipo es obligatorio y tiene como máximo ${TEAM_NAME_MAX_LENGTH} caracteres.`, + `El nombre del equipo tiene entre ${TEAM_NAME_MIN_LENGTH} y ${TEAM_NAME_MAX_LENGTH} caracteres.`, ); } return { courtId, memberId, date: fields.date, startHour, hours, teamName }; diff --git a/test/nombre-equipo.test.ts b/test/nombre-equipo.test.ts new file mode 100644 index 0000000..83b38e2 --- /dev/null +++ b/test/nombre-equipo.test.ts @@ -0,0 +1,14 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { postJson, startTestServer } from './helpers.ts'; + +test('un nombre de equipo de 2 caracteres se rechaza y uno de 3 se acepta', async () => { + const server = await startTestServer(); + const request = { courtId: 1, memberId: 1, date: '2026-03-10', startHour: 9 }; + const short = await postJson(`${server.url}/api/bookings`, { ...request, teamName: 'AB' }); + assert.equal(short.status, 400); + assert.equal(short.body.error.code, 'VALIDATION_ERROR'); + const ok = await postJson(`${server.url}/api/bookings`, { ...request, teamName: 'ABC' }); + assert.equal(ok.status, 200); + await server.close(); +});