till download files, everything works
This commit is contained in:
1 parent
ce893a4185
commit
ed1a580b09
6050 files changed
+768543
-177
No files matched your search
@@ -1,33 +1,170 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
// 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 { Document } from '@prisma/client';
|
||||
import { S3Service } from '../s3/s3.service';
|
||||
import { Document, User } from '@prisma/client';
|
||||
|
||||
@Injectable()
|
||||
export class DocumentsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private s3Service: S3Service,
|
||||
) {}
|
||||
|
||||
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({
|
||||
async findAllByClient(userId: number): Promise<Document[]> {
|
||||
const documents = await this.prisma.document.findMany({
|
||||
where: {
|
||||
sharedWithId: clientId,
|
||||
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<any> {
|
||||
// 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<Document[]> {
|
||||
return this.prisma.document.findMany({
|
||||
include: {
|
||||
sharedWith: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async shareDocument(documentId: number, userId: number): Promise<Document> {
|
||||
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<Document> {
|
||||
return this.prisma.document.update({
|
||||
where: { id: documentId },
|
||||
data: { status },
|
||||
include: {
|
||||
sharedWith: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user