Fixing Invalid Referral Status and Raising Test Coverage in the Brokers API (NestJS)
Описание и применение
Внедрена проверка enum в контроллере NestJS, исправлен SQL‑ошибка и расширены юнит‑тесты, что повысило покрытие до >80% и устранило 500‑ошибки при неверном статусе.
Задача
BrokersController threw 500 errors when referral status was not a valid enum value, and a SQL typo caused query failures. The test coverage for brokers and communications was below 3%, allowing bugs to slip through.
Результат
Implemented robust enum validation, corrected SQL typo, and expanded unit tests, raising test coverage from ~1% to >80% and eliminating 500 errors caused by invalid referral statuses.
Рабочий процесс
- Align controller with ReferralStatus enum using ParseEnumPipe
- Replace hard‑coded status guard with automatic enum validation
- Fix SQL query to use correct alias 'total' instead of 'count'
- Create brokers.test.ts covering CRUD, agent management, portal flows, and status updates
- Create communications.test.ts covering email logic with sendMail mock
- Run tests to confirm 400 on invalid status and 200 on valid status
- Verify coverage reports exceed 80%
- Commit changes and push to repository
Доказательства
The pipe throws a BadRequestException automatically if the payload isn’t a valid enum value
A snippet from brokers.test.ts that verifies enum validation
- Дата записи: 2026-09-20T02:30:18.935Z