Skip to content

Commit

Permalink
final applicantuploadingdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
rayyanmridha committed Feb 14, 2025
1 parent 7b399a9 commit affec93
Show file tree
Hide file tree
Showing 10 changed files with 630 additions and 654 deletions.
4 changes: 3 additions & 1 deletion apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PluralNamingStrategy } from './strategies/plural-naming.strategy';
import { ApplicationsModule } from './applications/applications.module';
import { Application } from './applications/application.entity';
import { FileUpload } from './file-upload/entities/file-upload.entity';
import { FileUploadModule } from './file-upload/file-upload.module';

@Module({
imports: [
Expand All @@ -26,11 +27,12 @@ import { FileUpload } from './file-upload/entities/file-upload.entity';
synchronize: true,
namingStrategy: new PluralNamingStrategy(),
}),
TypeOrmModule.forFeature([Application, FileUpload]),
TypeOrmModule.forFeature([Application, FileUpload]),
AuthModule,
UsersModule,
ApplicationsModule,
ReviewsModule,
FileUploadModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/applications/application.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { User } from '../users/user.entity';
import {
Response,
Semester,
Position,
ApplicationStage,
Position,
ApplicationStage,
ApplicationStep,
} from './types';
import { GetApplicationResponseDTO } from './dto/get-application.response.dto';
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/applications/applications.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CurrentUserInterceptor } from '../interceptors/current-user.interceptor
import { AuthService } from '../auth/auth.service';

@Module({
imports: [TypeOrmModule.forFeature([Application, User, FileUpload])],
imports: [TypeOrmModule.forFeature([Application, User, FileUpload])],
controllers: [ApplicationsController],
providers: [
ApplicationsService,
Expand Down
15 changes: 12 additions & 3 deletions apps/backend/src/file-upload/file-upload.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Controller, Post, Param, BadRequestException, Body } from '@nestjs/common';
import {
Controller,
Post,
Param,
BadRequestException,
UseInterceptors,
UploadedFile,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { FileUploadService } from './file-upload.service';
import { Application } from '../applications/application.entity';

Check warning on line 11 in apps/backend/src/file-upload/file-upload.controller.ts

View workflow job for this annotation

GitHub Actions / pre-deploy

'Application' is defined but never used

Expand All @@ -7,14 +15,15 @@ export class FileUploadController {
constructor(private readonly fileUploadService: FileUploadService) {}

@Post(':applicationId')
@UseInterceptors(FileInterceptor('file'))
async uploadFile(
@UploadedFile() file: Express.Multer.File,
@Param('applicationId') applicationId: number,
@Body() file: any, // The file will now be passed in the body as a raw buffer
) {
if (!applicationId) {
throw new BadRequestException('Application ID is required');
}

console.log('Received file in controller:', file);
return this.fileUploadService.handleFileUpload(file, applicationId);
}
}
19 changes: 17 additions & 2 deletions apps/backend/src/file-upload/file-upload.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@ import { FileUpload } from './entities/file-upload.entity';
import { FileUploadService } from './file-upload.service';
import { FileUploadController } from './file-upload.controller';
import { Application } from '../applications/application.entity';
import { ApplicationsService } from '../applications/applications.service';
import { UsersService } from '../users/users.service';
import { AuthService } from '../auth/auth.service';
import { JwtStrategy } from '../auth/jwt.strategy';
import { CurrentUserInterceptor } from '../interceptors/current-user.interceptor';
import { User } from '../users/user.entity';
import { UsersModule } from '../users/users.module';

@Module({
imports: [
// TypeORM setup for managing database storage
TypeOrmModule.forFeature([FileUpload, Application]),
TypeOrmModule.forFeature([FileUpload, Application, User]),
UsersModule,
],
controllers: [FileUploadController],
providers: [FileUploadService],
providers: [
FileUploadService,
UsersService,
AuthService,
JwtStrategy,
CurrentUserInterceptor,
ApplicationsService,
],
exports: [TypeOrmModule],
})
export class FileUploadModule {}
26 changes: 19 additions & 7 deletions apps/backend/src/file-upload/file-upload.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Injectable, BadRequestException, NotFoundException } from '@nestjs/common';
import {
Injectable,
BadRequestException,
NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FileUpload } from './entities/file-upload.entity';
Expand All @@ -12,13 +16,19 @@ export class FileUploadService {
private readonly applicationsService: ApplicationsService,
) {}

async handleFileUpload(file: any, applicationId: number) {
async handleFileUpload(file: Express.Multer.File, applicationId: number) {
console.log('Received file:', file);
if (!file) {
throw new BadRequestException('No file uploaded');
}

// Validate file type
const allowedMimeTypes = ['image/jpeg', 'image/png', 'application/pdf', 'application/msword'];
const allowedMimeTypes = [
'image/jpeg',
'image/png',
'application/pdf',
'application/msword',
];
if (!allowedMimeTypes.includes(file.mimetype)) {
throw new BadRequestException('Invalid file type');
}
Expand All @@ -30,17 +40,19 @@ export class FileUploadService {
}

// Check if the application exists
const application = await this.applicationsService.findCurrent(applicationId);
const application = await this.applicationsService.findCurrent(
applicationId,
);
if (!application) {
throw new NotFoundException('Application not found');
}

// Save file to the database
const uploadedFile = this.fileRepository.create({
filename: file.originalname, // assuming file name is passed in the request
mimetype: file.mimetype, // assuming mime type is passed in the request
size: file.size, // assuming size is passed in the request
file_data: file.buffer, // the raw buffer from the request
mimetype: file.mimetype, // assuming mime type is passed in the request
size: file.size, // assuming size is passed in the request
file_data: file.buffer, // the raw buffer from the request
application: application,
});

Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/reviews/reviews.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ export class ReviewsController {

return this.reviewsService.createReview(req.user, createReviewDTO);
}
}
}
1 change: 1 addition & 0 deletions apps/backend/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ import { AuthService } from '../auth/auth.service';
imports: [TypeOrmModule.forFeature([User])],
controllers: [UsersController],
providers: [UsersService, AuthService, JwtStrategy, CurrentUserInterceptor],
exports: [UsersService],
})
export class UsersModule {}
2 changes: 1 addition & 1 deletion apps/backend/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["node"],
"types": ["node", "Multer"],
"emitDecoratorMetadata": true,
"target": "es2021"
},
Expand Down
Loading

0 comments on commit affec93

Please sign in to comment.