mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-26 06:09:41 +00:00
feat: add validation for documents
This commit is contained in:
@ -2,7 +2,7 @@ import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import moment from 'moment';
|
||||
import { FindOptionsWhere } from 'typeorm';
|
||||
import { IJwtPayload } from '~/auth/interfaces';
|
||||
import { OciService } from '~/document/services';
|
||||
import { DocumentService, OciService } from '~/document/services';
|
||||
import { CreateTaskRequestDto, TasksFilterOptions, TaskSubmissionRequestDto } from '../dtos/request';
|
||||
import { Task } from '../entities';
|
||||
import { SubmissionStatus, TaskStatus } from '../enums';
|
||||
@ -11,7 +11,11 @@ import { TaskRepository } from '../repositories';
|
||||
@Injectable()
|
||||
export class TaskService {
|
||||
private readonly logger = new Logger(TaskService.name);
|
||||
constructor(private readonly taskRepository: TaskRepository, private readonly ociService: OciService) {}
|
||||
constructor(
|
||||
private readonly taskRepository: TaskRepository,
|
||||
private readonly ociService: OciService,
|
||||
private readonly documentService: DocumentService,
|
||||
) {}
|
||||
async createTask(userId: string, body: CreateTaskRequestDto) {
|
||||
this.logger.log(`Creating task for user ${userId}`);
|
||||
if (moment(body.dueDate).isBefore(moment(body.startDate))) {
|
||||
@ -23,6 +27,8 @@ export class TaskService {
|
||||
this.logger.error(`Due date must be in the future`);
|
||||
throw new BadRequestException('TASK.DUE_DATE_IN_PAST');
|
||||
}
|
||||
|
||||
await this.validateTaskImage(userId, body.imageId);
|
||||
const task = await this.taskRepository.createTask(userId, body);
|
||||
|
||||
this.logger.log(`Task ${task.id} created successfully`);
|
||||
@ -66,6 +72,8 @@ export class TaskService {
|
||||
throw new BadRequestException('TASK.PROOF_REQUIRED');
|
||||
}
|
||||
|
||||
await this.validateTaskImage(userId, body.imageId);
|
||||
|
||||
await this.taskRepository.createSubmission(task, body);
|
||||
this.logger.log(`Task ${taskId} submitted successfully`);
|
||||
}
|
||||
@ -128,4 +136,21 @@ export class TaskService {
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
private async validateTaskImage(userId: string, imageId?: string) {
|
||||
if (!imageId) return;
|
||||
|
||||
this.logger.log(`Validating task image ${imageId}`);
|
||||
const image = await this.documentService.findDocumentById(imageId);
|
||||
|
||||
if (!image) {
|
||||
this.logger.error(`Task image ${imageId} not found`);
|
||||
throw new BadRequestException('DOCUMENT.NOT_FOUND');
|
||||
}
|
||||
|
||||
if (image.createdById && image.createdById !== userId) {
|
||||
this.logger.error(`Task image ${imageId} not created by user ${userId}`);
|
||||
throw new BadRequestException('DOCUMENT.NOT_CREATED_BY_USER');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user