feat(tags): add input validation for tag and entity operations

Added validation checks for tagId, personId, and projectId across tag-related operations. Introduced `BadRequestException` for invalid or missing inputs. Replaced generic errors with more descriptive exceptions.
This commit is contained in:
Mathis HERRIOT 2025-05-16 23:52:30 +02:00
parent d48b6fa48b
commit 5abd33e648
No known key found for this signature in database
GPG Key ID: E7EB4A211D8D4907

View File

@ -1,4 +1,4 @@
import { Injectable, NotFoundException, Inject } from '@nestjs/common'; import { Injectable, NotFoundException, Inject, BadRequestException } from '@nestjs/common';
import { eq, and } from 'drizzle-orm'; import { eq, and } from 'drizzle-orm';
import { DRIZZLE } from '../../../database/database.module'; import { DRIZZLE } from '../../../database/database.module';
import * as schema from '../../../database/schema'; import * as schema from '../../../database/schema';
@ -95,10 +95,18 @@ export class TagsService {
* Add a tag to a person * Add a tag to a person
*/ */
async addTagToPerson(tagId: string, personId: string) { async addTagToPerson(tagId: string, personId: string) {
// Validate tagId and personId
if (!tagId) {
throw new BadRequestException('Tag ID is required');
}
if (!personId) {
throw new BadRequestException('Person ID is required');
}
// Check if the tag exists and is of type PERSON // Check if the tag exists and is of type PERSON
const tag = await this.findById(tagId); const tag = await this.findById(tagId);
if (tag.type !== 'PERSON') { if (tag.type !== 'PERSON') {
throw new Error(`Tag with ID ${tagId} is not of type PERSON`); throw new BadRequestException(`Tag with ID ${tagId} is not of type PERSON`);
} }
// Check if the person exists // Check if the person exists
@ -142,6 +150,14 @@ export class TagsService {
* Remove a tag from a person * Remove a tag from a person
*/ */
async removeTagFromPerson(tagId: string, personId: string) { async removeTagFromPerson(tagId: string, personId: string) {
// Validate tagId and personId
if (!tagId) {
throw new BadRequestException('Tag ID is required');
}
if (!personId) {
throw new BadRequestException('Person ID is required');
}
const [relation] = await this.db const [relation] = await this.db
.delete(schema.personToTag) .delete(schema.personToTag)
.where( .where(
@ -163,10 +179,18 @@ export class TagsService {
* Add a tag to a project * Add a tag to a project
*/ */
async addTagToProject(tagId: string, projectId: string) { async addTagToProject(tagId: string, projectId: string) {
// Validate tagId and projectId
if (!tagId) {
throw new BadRequestException('Tag ID is required');
}
if (!projectId) {
throw new BadRequestException('Project ID is required');
}
// Check if the tag exists and is of type PROJECT // Check if the tag exists and is of type PROJECT
const tag = await this.findById(tagId); const tag = await this.findById(tagId);
if (tag.type !== 'PROJECT') { if (tag.type !== 'PROJECT') {
throw new Error(`Tag with ID ${tagId} is not of type PROJECT`); throw new BadRequestException(`Tag with ID ${tagId} is not of type PROJECT`);
} }
// Check if the project exists // Check if the project exists
@ -210,6 +234,14 @@ export class TagsService {
* Remove a tag from a project * Remove a tag from a project
*/ */
async removeTagFromProject(tagId: string, projectId: string) { async removeTagFromProject(tagId: string, projectId: string) {
// Validate tagId and projectId
if (!tagId) {
throw new BadRequestException('Tag ID is required');
}
if (!projectId) {
throw new BadRequestException('Project ID is required');
}
const [relation] = await this.db const [relation] = await this.db
.delete(schema.projectToTag) .delete(schema.projectToTag)
.where( .where(
@ -231,6 +263,11 @@ export class TagsService {
* Get all tags for a person * Get all tags for a person
*/ */
async getTagsForPerson(personId: string) { async getTagsForPerson(personId: string) {
// Validate personId
if (!personId) {
throw new BadRequestException('Person ID is required');
}
// Check if the person exists // Check if the person exists
const [person] = await this.db const [person] = await this.db
.select() .select()
@ -255,6 +292,11 @@ export class TagsService {
* Get all tags for a project * Get all tags for a project
*/ */
async getTagsForProject(projectId: string) { async getTagsForProject(projectId: string) {
// Validate projectId
if (!projectId) {
throw new BadRequestException('Project ID is required');
}
// Check if the project exists // Check if the project exists
const [project] = await this.db const [project] = await this.db
.select() .select()