Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion backend/src/departments/departments.controller.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 24 additions & 3 deletions backend/src/departments/departments.service.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,13 +38,29 @@ export class DepartmentsService {
}

async create(dto: CreateDepartmentDto): Promise<Department> {
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<Department> {
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<void> {
const dept = await this.findOne(id);
await this.repo.remove(dept);
}

private async ensureNameUnique(name: string): Promise<void> {
const existing = await this.repo.findOne({ where: { name } });
if (existing) {
throw new ConflictException('A department with this name already exists');
}
}
}
4 changes: 4 additions & 0 deletions backend/src/departments/dto/update-department.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateDepartmentDto } from './create-department.dto';

export class UpdateDepartmentDto extends PartialType(CreateDepartmentDto) {}
Loading