diff --git a/package-lock.json b/package-lock.json index 90a87d8..213630d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "UNLICENSED", "dependencies": { "@nestjs/common": "^11.0.1", + "@nestjs/config": "^4.0.2", "@nestjs/core": "^11.0.1", "@nestjs/platform-express": "^11.1.6", "@nestjs/platform-socket.io": "^11.1.6", @@ -2347,6 +2348,33 @@ } } }, + "node_modules/@nestjs/config": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-4.0.2.tgz", + "integrity": "sha512-McMW6EXtpc8+CwTUwFdg6h7dYcBUpH5iUILCclAsa+MbCEvC9ZKu4dCHRlJqALuhjLw97pbQu62l4+wRwGeZqA==", + "license": "MIT", + "dependencies": { + "dotenv": "16.4.7", + "dotenv-expand": "12.0.1", + "lodash": "4.17.21" + }, + "peerDependencies": { + "@nestjs/common": "^10.0.0 || ^11.0.0", + "rxjs": "^7.1.0" + } + }, + "node_modules/@nestjs/config/node_modules/dotenv": { + "version": "16.4.7", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz", + "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/@nestjs/core": { "version": "11.1.6", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-11.1.6.tgz", @@ -5164,6 +5192,21 @@ "url": "https://dotenvx.com" } }, + "node_modules/dotenv-expand": { + "version": "12.0.1", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-12.0.1.tgz", + "integrity": "sha512-LaKRbou8gt0RNID/9RoI+J2rvXsBRPMV7p+ElHlPhcSARbCPDYcYG2s1TIzAfWv4YSgyY5taidWzzs31lNV3yQ==", + "license": "BSD-2-Clause", + "dependencies": { + "dotenv": "^16.4.5" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/dunder-proto": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", diff --git a/package.json b/package.json index 2061d81..5fa3b74 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "dependencies": { "@nestjs/common": "^11.0.1", + "@nestjs/config": "^4.0.2", "@nestjs/core": "^11.0.1", "@nestjs/platform-express": "^11.1.6", "@nestjs/platform-socket.io": "^11.1.6", diff --git a/src/app.controller.ts b/src/app.controller.ts index 30ca365..587caa1 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -4,8 +4,9 @@ import { ApiTags } from '@nestjs/swagger'; import { PrismaService } from './services/prisma.service'; import { FileInterceptor } from '@nestjs/platform-express'; import { diskStorage } from 'multer'; -import { extname } from 'path'; +import { extname, join } from 'path'; import { AppGetaway } from './app.getaway'; +import { unlink } from 'fs'; @ApiTags('App') @Controller('app') @@ -14,8 +15,8 @@ export class AppController { @Get('candidates') - getCandidateList() { - return this.prisma.candidate.findMany({ + async getCandidateList() { + const data = await this.prisma.candidate.findMany({ select: { id: true, fullName: true, @@ -24,11 +25,21 @@ export class AppController { profileImage: true, } }) + + data.forEach((candidate) => { + candidate.profileImage = `${process.env.HOST_URL as string}/uploads/${candidate.profileImage}`; + }) + return data; } @Get('candidate/:id') - getCandidateDetails(@Param('id', ParseIntPipe) id: number) { - return this.prisma.candidate.findFirst({ where: { id: id } }); + async getCandidateDetails(@Param('id', ParseIntPipe) id: number) { + const data = await this.prisma.candidate.findFirst({ where: { id: id } }); + if (!data) { + throw new NotFoundException(); + } + data.profileImage = `${process.env.HOST_URL as string}/uploads/${data.profileImage}`; + return data; } @@ -105,6 +116,15 @@ export class AppController { await this.prisma.candidate.delete({ where: { id } }); + if (candidate.profileImage) { + const filePath = join(process.cwd(), 'assets', 'uploads', candidate.profileImage); + unlink(filePath, (err) => { + if (err) { + console.error('Failed to delete file:', err); + } + }); + } + this.socketService.onDeleteCandidate(id); return candidate; diff --git a/src/app.module.ts b/src/app.module.ts index bd2fb09..38dd16e 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -6,9 +6,11 @@ import { PrismaService } from './services/prisma.service'; import { AppGetaway } from './app.getaway'; import { StatsController } from './stats.controller'; import { StatsService } from './services/stats.service'; +import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ + ConfigModule.forRoot(), ServeStaticModule.forRoot( { rootPath: join(__dirname, 'assets/client'),