Files
lcbp3/.windsurf/rules/06-backend-patterns.md
admin 5e4e0444ed
CI / CD Pipeline / build (push) Failing after 8m55s
CI / CD Pipeline / deploy (push) Has been skipped
690417:1707 Refactor Work flow ADR-021
2026-04-17 17:07:41 +07:00

1.4 KiB

trigger, globs
trigger globs
glob
backend/**/*.service.ts
backend/**/*.controller.ts
backend/**/*.dto.ts
backend/**/*.entity.ts

Backend Patterns (NestJS)

Architecture

  • Thin Controller — business logic in Service layer
  • DTO Validation — class-validator + class-transformer
  • RBAC — CASL for authorization
  • Error Handling — Logger + HttpException

UUID Resolution Pattern

// Controller - accept UUID in DTO
@Post()
async create(@Body() dto: CreateCorrespondenceDto) {
  // Resolve UUID to internal ID
  const contract = await this.contractService.findOneByUuid(dto.contractUuid);
  const contractId = contract.id; // Internal INT for DB queries
  
  return this.service.create(dto, contractId);
}

// Service - use internal ID for DB operations
async create(dto: CreateCorrespondenceDto, contractId: number) {
  // Use contractId (INT) for database queries
  const correspondence = this.repo.create({
    contractId,  // FK is INT
    // ... other fields
  });
  return this.repo.save(correspondence);
}

API Response Pattern

// Entity
@Entity()
class Contract extends UuidBaseEntity {
  @Column({ type: 'uuid' })
  publicId: string;
  
  @PrimaryKey()
  @Exclude()
  id: number;
}

// Response automatically includes publicId as 'id'
// { id: "019505a1-7c3e-7000-8000-abc123def456", ... }

Full Guidelines

specs/05-Engineering-Guidelines/05-02-backend-guidelines.md