mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-10-25 21:07:56 +00:00
feat: add validation for documents
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import { Body, Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
|
||||
import { Body, Controller, Post, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger';
|
||||
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger';
|
||||
import { memoryStorage } from 'multer';
|
||||
import { IJwtPayload } from '~/auth/interfaces';
|
||||
import { AuthenticatedUser } from '~/common/decorators';
|
||||
import { AccessTokenGuard } from '~/common/guards';
|
||||
import { ResponseFactory } from '~/core/utils';
|
||||
import { UploadDocumentRequestDto } from '../dtos/request';
|
||||
import { DocumentMetaResponseDto } from '../dtos/response';
|
||||
@@ -9,6 +12,8 @@ import { DocumentType } from '../enums';
|
||||
import { DocumentService } from '../services';
|
||||
@Controller('document')
|
||||
@ApiTags('Document')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AccessTokenGuard)
|
||||
export class DocumentController {
|
||||
constructor(private readonly documentService: DocumentService) {}
|
||||
|
||||
@@ -36,8 +41,9 @@ export class DocumentController {
|
||||
async createDocument(
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@Body() uploadedDocumentRequest: UploadDocumentRequestDto,
|
||||
@AuthenticatedUser() user: IJwtPayload,
|
||||
) {
|
||||
const document = await this.documentService.createDocument(file, uploadedDocumentRequest);
|
||||
const document = await this.documentService.createDocument(file, uploadedDocumentRequest, user.sub);
|
||||
|
||||
return ResponseFactory.data(new DocumentMetaResponseDto(document));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
import { Column, Entity, OneToMany, OneToOne, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
OneToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
import { Customer } from '~/customer/entities';
|
||||
import { Gift } from '~/gift/entities';
|
||||
import { Junior, Theme } from '~/junior/entities';
|
||||
@@ -22,6 +31,9 @@ export class Document {
|
||||
@Column({ type: 'varchar', length: 255, name: 'document_type' })
|
||||
documentType!: DocumentType;
|
||||
|
||||
@Column({ type: 'uuid', nullable: true, name: 'created_by_id' })
|
||||
createdById!: string;
|
||||
|
||||
@OneToOne(() => Customer, (customer) => customer.profilePicture, { onDelete: 'SET NULL' })
|
||||
customerPicture?: Customer;
|
||||
|
||||
@@ -46,6 +58,10 @@ export class Document {
|
||||
@OneToMany(() => Gift, (gift) => gift.image)
|
||||
gifts?: Gift[];
|
||||
|
||||
@ManyToOne(() => User, (user) => user.createdDocuments, { nullable: true, onDelete: 'SET NULL' })
|
||||
@JoinColumn({ name: 'created_by_id' })
|
||||
createdBy?: User;
|
||||
|
||||
@UpdateDateColumn({ name: 'updated_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
|
||||
updatedAt!: Date;
|
||||
|
||||
|
||||
@@ -8,12 +8,13 @@ import { Document } from '../entities';
|
||||
export class DocumentRepository {
|
||||
constructor(@InjectRepository(Document) private documentRepository: Repository<Document>) {}
|
||||
|
||||
createDocument(document: UploadResponseDto) {
|
||||
createDocument(userId: string, document: UploadResponseDto) {
|
||||
return this.documentRepository.save(
|
||||
this.documentRepository.create({
|
||||
name: document.name,
|
||||
documentType: document.documentType,
|
||||
extension: document.extension,
|
||||
createdById: userId,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -21,4 +22,8 @@ export class DocumentRepository {
|
||||
findDocuments(where: FindOptionsWhere<Document>) {
|
||||
return this.documentRepository.find({ where });
|
||||
}
|
||||
|
||||
findDocumentById(id: string) {
|
||||
return this.documentRepository.findOne({ where: { id } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,19 @@ import { OciService } from './oci.service';
|
||||
export class DocumentService {
|
||||
private readonly logger = new Logger(DocumentService.name);
|
||||
constructor(private readonly ociService: OciService, private readonly documentRepository: DocumentRepository) {}
|
||||
async createDocument(file: Express.Multer.File, uploadedDocumentRequest: UploadDocumentRequestDto) {
|
||||
async createDocument(file: Express.Multer.File, uploadedDocumentRequest: UploadDocumentRequestDto, userId: string) {
|
||||
this.logger.log(`creating document for with type ${uploadedDocumentRequest.documentType}`);
|
||||
const uploadedFile = await this.ociService.uploadFile(file, uploadedDocumentRequest);
|
||||
return this.documentRepository.createDocument(uploadedFile);
|
||||
return this.documentRepository.createDocument(userId, uploadedFile);
|
||||
}
|
||||
|
||||
findDocuments(where: FindOptionsWhere<Document>) {
|
||||
this.logger.log(`finding documents with where clause ${JSON.stringify(where)}`);
|
||||
return this.documentRepository.findDocuments(where);
|
||||
}
|
||||
|
||||
findDocumentById(id: string) {
|
||||
this.logger.log(`finding document with id ${id}`);
|
||||
return this.documentRepository.findDocumentById(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user