CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl-labs/nestjs-best-practices

NestJS patterns -- modules, DI, exception filters, validation pipes, guards, interceptors, testing, config

98

1.36x
Quality

89%

Does it follow best practices?

Impact

100%

1.36x

Average score across 12 eval scenarios

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

task.mdevals/scenario-3/

User Service Refactoring with Tests

Problem/Feature Description

A SaaS company has a UsersService for their NestJS backend that handles user registration and look-up. The service is currently untestable because it reads process.env directly and its dependency on an EmailService is hard-coded with new.

The team lead has asked you to:

  1. Refactor the UsersService to use proper NestJS dependency injection and configuration
  2. Add a UsersController with POST /users/register and GET /users/:id endpoints
  3. Write comprehensive unit tests for the refactored service
  4. Set up proper NestJS bootstrap configuration (validation, error handling)

Follow NestJS best practices throughout.

Output Specification

Produce files in users-api/src/:

  • main.ts — bootstrap with proper global configuration
  • app.module.ts — root module
  • users/users.controller.ts — controller with endpoints
  • users/users.service.ts — the refactored service
  • users/users.service.spec.ts — unit tests
  • users/users.module.ts — feature module
  • users/dto/register-user.dto.ts — DTO for registration (email: non-empty string, name: non-empty string)
  • common/filters/ — error handling

Input Files

The following files are provided as inputs. Extract them before beginning.

=============== FILE: inputs/users.service.original.ts =============== import { Injectable } from '@nestjs/common'; import { EmailService } from './email.service';

@Injectable() export class UsersService { private emailService = new EmailService();

async register(email: string, name: string) { const maxAttempts = parseInt(process.env.MAX_LOGIN_ATTEMPTS || '5'); const templateId = process.env.WELCOME_EMAIL_TEMPLATE_ID || 'default-template';

const user = { id: Date.now().toString(), email, name, loginAttempts: 0, maxAttempts };
await this.emailService.send(templateId, email, { name });
return user;

}

async findById(id: string) { // Stub: return a placeholder user return { id, email: 'user@example.com', name: 'Test User' }; } }

=============== FILE: inputs/email.service.ts =============== import { Injectable } from '@nestjs/common';

@Injectable() export class EmailService { async send(templateId: string, to: string, variables: Record<string, string>): Promise<void> { // Stub: pretend email was sent console.log(Sending template ${templateId} to ${to}, variables); } }

evals

tile.json