diff --git a/backend/src/departments/departments.controller.ts b/backend/src/departments/departments.controller.ts index 5fb1642..2801126 100644 --- a/backend/src/departments/departments.controller.ts +++ b/backend/src/departments/departments.controller.ts @@ -1,7 +1,8 @@ -import { Controller, Get, Post, Delete, Body, Param, UseGuards } from '@nestjs/common'; +import { Controller, Get, Post, Delete, Body, Param, UseGuards, Patch } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; import { DepartmentsService } from './departments.service'; import { CreateDepartmentDto } from './dto/create-department.dto'; +import { UpdateDepartmentDto } from './dto/update-department.dto'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; @ApiTags('Departments') @@ -29,6 +30,12 @@ export class DepartmentsController { return this.service.create(dto); } + @Patch(':id') + @ApiOperation({ summary: 'Update a department' }) + update(@Param('id') id: string, @Body() dto: UpdateDepartmentDto) { + return this.service.update(id, dto); + } + @Delete(':id') @ApiOperation({ summary: 'Delete a department' }) remove(@Param('id') id: string) { diff --git a/backend/src/departments/departments.service.ts b/backend/src/departments/departments.service.ts index c28343d..30cda09 100644 --- a/backend/src/departments/departments.service.ts +++ b/backend/src/departments/departments.service.ts @@ -1,8 +1,13 @@ -import { Injectable, NotFoundException, ConflictException } from '@nestjs/common'; +import { + ConflictException, + Injectable, + NotFoundException, +} from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Department } from './department.entity'; import { CreateDepartmentDto } from './dto/create-department.dto'; +import { UpdateDepartmentDto } from './dto/update-department.dto'; export interface DepartmentWithCount extends Department { assetCount: number; @@ -33,13 +38,29 @@ export class DepartmentsService { } async create(dto: CreateDepartmentDto): Promise { - const existing = await this.repo.findOne({ where: { name: dto.name } }); - if (existing) throw new ConflictException('A department with this name already exists'); + await this.ensureNameUnique(dto.name); return this.repo.save(this.repo.create(dto)); } + async update(id: string, dto: UpdateDepartmentDto): Promise { + const dept = await this.findOne(id); + if (dto.name && dto.name !== dept.name) { + await this.ensureNameUnique(dto.name); + } + + Object.assign(dept, dto); + return this.repo.save(dept); + } + async remove(id: string): Promise { const dept = await this.findOne(id); await this.repo.remove(dept); } + + private async ensureNameUnique(name: string): Promise { + const existing = await this.repo.findOne({ where: { name } }); + if (existing) { + throw new ConflictException('A department with this name already exists'); + } + } } diff --git a/backend/src/departments/dto/update-department.dto.ts b/backend/src/departments/dto/update-department.dto.ts new file mode 100644 index 0000000..144bdc9 --- /dev/null +++ b/backend/src/departments/dto/update-department.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/mapped-types'; +import { CreateDepartmentDto } from './create-department.dto'; + +export class UpdateDepartmentDto extends PartialType(CreateDepartmentDto) {}