// import { Injectable } from '@nestjs/common'; // import { PrismaService } from '../prisma/prisma.service'; // //import { Document } from '@prisma/client'; // @Injectable() // export class DocumentsService { // downloadDocument(key: string, id: any) { // throw new Error('Method not implemented.'); // } // constructor(private readonly prisma: PrismaService) {} // async getClientDocuments(clientId: number) { // // return this.prisma.document.findMany({ // // where: { // // sharedWithId: clientId, // // }, // // include: { // // sharedWith: { // // select: { // // id: true, // // name: true, // // email: true, // // }, // // }, // // }, // // }); // return this.prisma.document.findMany({ // where: { // sharedWithId: clientId, // }, // orderBy: { // createdAt: 'desc', // }, // }); // } // } import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { S3Service } from '../s3/s3.service'; import { Document, User } from '@prisma/client'; @Injectable() export class DocumentsService { constructor( private prisma: PrismaService, private s3Service: S3Service, ) {} async findAllByClient(userId: number): Promise { const documents = await this.prisma.document.findMany({ where: { sharedWithId: userId, }, include: { sharedWith: { select: { id: true, name: true, email: true, }, }, }, orderBy: { createdAt: 'desc', }, }); return documents; } async getClientDocuments(clientId: number) { // return this.prisma.document.findMany({ // where: { // sharedWithId: clientId, // }, // include: { // sharedWith: { // select: { // id: true, // name: true, // email: true, // }, // }, // }, // }); return this.prisma.document.findMany({ where: { sharedWithId: clientId, }, orderBy: { createdAt: 'desc', }, }); } async downloadDocument(s3Key: string, userId: number): Promise { // Verify document exists and user has access const document = await this.prisma.document.findFirst({ where: { s3Key: s3Key, sharedWithId: userId, }, }); if (!document) { throw new NotFoundException('Document not found or access denied'); } try { // Get document stream from S3 const fileStream = await this.s3Service.getObject(s3Key).createReadStream(); return fileStream; } catch (error) { console.error('Error downloading document:', error); throw new NotFoundException('Document file not found in storage'); } } async getAllDocuments(): Promise { return this.prisma.document.findMany({ include: { sharedWith: { select: { id: true, name: true, email: true, }, }, }, orderBy: { createdAt: 'desc', }, }); } async shareDocument(documentId: number, userId: number): Promise { const document = await this.prisma.document.update({ where: { id: documentId }, data: { sharedWithId: userId, }, include: { sharedWith: { select: { id: true, name: true, email: true, }, }, }, }); return document; } async updateStatus(documentId: number, status: string): Promise { return this.prisma.document.update({ where: { id: documentId }, data: { status }, include: { sharedWith: { select: { id: true, name: true, email: true, }, }, }, }); } }