feat: working on signed url for private files

This commit is contained in:
Abdalhamid Alhamad
2024-12-12 09:46:38 +03:00
parent 83fc634d25
commit 220a03cc46
20 changed files with 359 additions and 2818 deletions

View File

@ -4,6 +4,7 @@ import { Roles } from '~/auth/enums';
import { IJwtPayload } from '~/auth/interfaces';
import { AllowedRoles, AuthenticatedUser } from '~/common/decorators';
import { AccessTokenGuard, RolesGuard } from '~/common/guards';
import { ApiDataPageResponse } from '~/core/decorators';
import { CustomParseUUIDPipe } from '~/core/pipes';
import { ResponseFactory } from '~/core/utils';
import { CreateTaskRequestDto, TaskSubmissionRequestDto } from '../dtos/request';
@ -27,6 +28,7 @@ export class TaskController {
@Get()
@UseGuards(AccessTokenGuard)
@ApiDataPageResponse(TaskResponseDto)
async findTasks(@Query() query: TasksFilterOptions, @AuthenticatedUser() user: IJwtPayload) {
const [tasks, itemCount] = await this.taskService.findTasks(user, query);
return ResponseFactory.dataPage(

View File

@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { DocumentMetaResponseDto } from '~/document/dtos/response';
import { JuniorResponseDto } from '~/junior/dtos/response';
import { Task } from '~/task/entities';
import { TaskSubmission } from '~/task/entities/task-submissions.entity';
@ -28,6 +29,9 @@ export class TaskResponseDto {
@ApiProperty()
junior!: JuniorResponseDto;
@ApiProperty()
image!: DocumentMetaResponseDto;
@ApiProperty()
createdAt!: Date;
@ -42,6 +46,7 @@ export class TaskResponseDto {
this.dueDate = task.dueDate;
this.rewardAmount = task.rewardAmount;
this.submission = task.submission;
this.image = new DocumentMetaResponseDto(task.image);
this.junior = new JuniorResponseDto(task.assignedTo);
this.createdAt = task.createdAt;
this.updatedAt = task.updatedAt;

View File

@ -32,7 +32,14 @@ export class TaskRepository {
findTask(where: FindOptionsWhere<Task>) {
return this.taskRepository.findOne({
where,
relations: ['image', 'assignedTo', 'assignedTo.customer', 'assignedTo.customer.user', 'submission'],
relations: [
'image',
'assignedTo',
'assignedTo.customer',
'assignedTo.customer.user',
'assignedTo.customer.user.profilePicture',
'submission',
],
});
}
@ -44,6 +51,7 @@ export class TaskRepository {
.leftJoinAndSelect('task.assignedTo', 'assignedTo')
.leftJoinAndSelect('assignedTo.customer', 'customer')
.leftJoinAndSelect('customer.user', 'user')
.leftJoinAndSelect('user.profilePicture', 'profilePicture')
.leftJoinAndSelect('task.submission', 'submission');
if (roles.includes(Roles.GUARDIAN)) {

View File

@ -1,6 +1,7 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { FindOptionsWhere } from 'typeorm';
import { IJwtPayload } from '~/auth/interfaces';
import { OciService } from '~/document/services';
import { CreateTaskRequestDto, TasksFilterOptions, TaskSubmissionRequestDto } from '../dtos/request';
import { Task } from '../entities';
import { SubmissionStatus, TaskStatus } from '../enums';
@ -8,7 +9,7 @@ import { TaskRepository } from '../repositories';
@Injectable()
export class TaskService {
constructor(private readonly taskRepository: TaskRepository) {}
constructor(private readonly taskRepository: TaskRepository, private readonly ociService: OciService) {}
async createTask(userId: string, body: CreateTaskRequestDto) {
const task = await this.taskRepository.createTask(userId, body);
return this.findTask({ id: task.id });
@ -24,8 +25,12 @@ export class TaskService {
return task;
}
findTasks(user: IJwtPayload, query: TasksFilterOptions) {
return this.taskRepository.findTasks(user, query);
async findTasks(user: IJwtPayload, query: TasksFilterOptions): Promise<[Task[], number]> {
const [tasks, count] = await this.taskRepository.findTasks(user, query);
await this.prepareTasksPictures(tasks);
return [tasks, count];
}
async submitTask(userId: string, taskId: string, body: TaskSubmissionRequestDto) {
const task = await this.findTask({ id: taskId, assignedToId: userId });
@ -68,4 +73,26 @@ export class TaskService {
await this.taskRepository.rejectSubmission(task.submission);
}
async prepareTasksPictures(tasks: Task[]) {
await Promise.all(
tasks.map(async (task) => {
const [imageUrl, submissionUrl, profilePictureUrl] = await Promise.all([
this.ociService.generatePreSignedUrl(task.image),
this.ociService.generatePreSignedUrl(task.submission?.proofOfCompletion),
this.ociService.generatePreSignedUrl(task.assignedTo.customer.user.profilePicture),
]);
task.image.url = imageUrl;
if (task.submission) {
task.submission.proofOfCompletion.url = submissionUrl;
}
if (task.assignedTo.customer.user.profilePicture) {
task.assignedTo.customer.user.profilePicture.url = profilePictureUrl;
}
}),
);
}
}