admin panel update
This commit is contained in:
1 parent
1804bdbb99
commit
3d2b0a5d6d
5 files changed
+315
-140
No files matched your search
@@ -11,7 +11,6 @@ import {
|
||||
ParseIntPipe,
|
||||
UseGuards,
|
||||
Request,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { AdminService } from './admin.service';
|
||||
@@ -96,12 +95,25 @@ export class AdminController {
|
||||
return this.adminService.updateDocumentStatus(+id, status);
|
||||
}
|
||||
|
||||
// async uploadDocument(
|
||||
// @UploadedFile() file: Express.Multer.File,
|
||||
// @Body('title') title: string,
|
||||
// @Request() req,
|
||||
// ) {
|
||||
// return this.adminService.uploadDocument(file, title, req.user.userId);
|
||||
// }
|
||||
async uploadDocument(
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@Body('title') title: string,
|
||||
@Body('description') description: number[],
|
||||
@Request() req,
|
||||
) {
|
||||
return this.adminService.uploadDocument(file, title, req.user.userId);
|
||||
return this.adminService.uploadDocument(
|
||||
file,
|
||||
title,
|
||||
req.user.userId,
|
||||
description,
|
||||
);
|
||||
}
|
||||
@Get('test-s3-connection')
|
||||
async testS3Connection() {
|
||||
|
||||
@@ -85,8 +85,41 @@ export class AdminService {
|
||||
});
|
||||
}
|
||||
|
||||
// async getAllDocuments() {
|
||||
// return this.prisma.document.findMany({
|
||||
// include: {
|
||||
// sharedWith: {
|
||||
// select: {
|
||||
// id: true,
|
||||
// name: true,
|
||||
// email: true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
async getAllDocuments() {
|
||||
return this.prisma.document.findMany();
|
||||
return this.prisma.document.findMany({
|
||||
include: {
|
||||
author: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
sharedWith: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async updateDocument(
|
||||
@@ -116,6 +149,41 @@ export class AdminService {
|
||||
},
|
||||
});
|
||||
}
|
||||
async uploadDocument(
|
||||
file: Express.Multer.File,
|
||||
title: string,
|
||||
userId: number,
|
||||
sharedWith: number[],
|
||||
) {
|
||||
let s3Key;
|
||||
try {
|
||||
s3Key = await this.s3Service.uploadFile(file, 'documents');
|
||||
|
||||
// Log the sharedWith array to verify the data
|
||||
console.log('Sharing document with users:', sharedWith);
|
||||
|
||||
const document = await this.prisma.document.create({
|
||||
data: {
|
||||
title,
|
||||
authorId: userId,
|
||||
s3Key,
|
||||
status: 'completed',
|
||||
sharedWith: {
|
||||
connect: sharedWith.map((id) => ({ id })),
|
||||
},
|
||||
},
|
||||
include: {
|
||||
sharedWith: true, // Include this to verify the relation was created
|
||||
},
|
||||
});
|
||||
|
||||
console.log('Created document:', document);
|
||||
return document;
|
||||
} catch (error) {
|
||||
console.error('Error in uploadDocument:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteDocument(id: number) {
|
||||
const document = await this.prisma.document.findUnique({
|
||||
@@ -221,42 +289,49 @@ export class AdminService {
|
||||
|
||||
// problem whith upload status writing to db, i will fix it later
|
||||
|
||||
async uploadDocument(
|
||||
file: Express.Multer.File,
|
||||
title: string,
|
||||
userId: number,
|
||||
) {
|
||||
let s3Key;
|
||||
try {
|
||||
// First upload to S3
|
||||
s3Key = await this.s3Service.uploadFile(file, 'documents');
|
||||
// async uploadDocument(
|
||||
// file: Express.Multer.File,
|
||||
// title: string,
|
||||
// userId: number,
|
||||
// sharedWith: number[],
|
||||
// ) {
|
||||
// let s3Key;
|
||||
// try {
|
||||
// // First upload to S3
|
||||
// s3Key = await this.s3Service.uploadFile(file, 'documents');
|
||||
|
||||
// Then create document with completed status and s3Key
|
||||
const document = await this.prisma.document.create({
|
||||
data: {
|
||||
title,
|
||||
authorId: userId,
|
||||
s3Key,
|
||||
status: 'completed', // Set status to completed immediately after successful upload
|
||||
},
|
||||
});
|
||||
// // Then create document with completed status and s3Key
|
||||
// const document = await this.prisma.document.create({
|
||||
// data: {
|
||||
// title,
|
||||
// authorId: userId,
|
||||
// s3Key,
|
||||
// status: 'completed', // Set status to completed immediately after successful upload
|
||||
// sharedWith: {
|
||||
// connect: sharedWith.map((id: number) => ({ id })),
|
||||
// },
|
||||
// },
|
||||
// });
|
||||
|
||||
return document;
|
||||
} catch (error) {
|
||||
// Create document with failed status if upload fails
|
||||
if (title && userId) {
|
||||
await this.prisma.document.create({
|
||||
data: {
|
||||
title,
|
||||
authorId: userId,
|
||||
s3Key: s3Key || '',
|
||||
status: 'failed',
|
||||
},
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
// return document;
|
||||
// } catch (error) {
|
||||
// // Create document with failed status if upload fails
|
||||
// if (title && userId) {
|
||||
// await this.prisma.document.create({
|
||||
// data: {
|
||||
// title,
|
||||
// authorId: userId,
|
||||
// s3Key: s3Key || '',
|
||||
// status: 'failed',
|
||||
// sharedWith: {
|
||||
// connect: sharedWith.map((id: number) => ({ id })),
|
||||
// },
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
// throw error;
|
||||
// }
|
||||
// }
|
||||
|
||||
async getDocumentUrl(documentId: number) {
|
||||
const document = await this.prisma.document.findUnique({
|
||||
|
||||
Reference in new issue
Block a user