Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added certificate and user certificate module #23

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { MemoryStore } from 'cache-manager-memory-store';
import { ConfigModule } from '@nestjs/config';
import { TrackingAssessmentModule } from 'src/modules/tracking_assessment/tracking_assessment.module';
import { TrackingContentModule } from 'src/modules/tracking_content/tracking_content.module';
import { CertificateModule } from './modules/certificate/certificate.module';
import { UserCertificateModule } from './modules/user_certificate/user_certificate.module';

@Module({
imports: [
Expand All @@ -15,6 +17,8 @@ import { TrackingContentModule } from 'src/modules/tracking_content/tracking_con
ConfigModule.forRoot({ isGlobal: true }),
DatabaseModule,
CacheModule.register({ isGlobal: true, store: MemoryStore }),
CertificateModule,
UserCertificateModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
91 changes: 91 additions & 0 deletions src/modules/certificate/certificate.contoller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Controller, Post, Body, Res, UseGuards, Req } from '@nestjs/common';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove unused import.

The UseGuards import is not used in this file.

-import { Controller, Post, Body, Res, UseGuards, Req } from '@nestjs/common';
+import { Controller, Post, Body, Res, Req } from '@nestjs/common';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { Controller, Post, Body, Res, UseGuards, Req } from '@nestjs/common';
import { Controller, Post, Body, Res, Req } from '@nestjs/common';
🧰 Tools
🪛 ESLint

[error] 1-1: 'UseGuards' is defined but never used.

(@typescript-eslint/no-unused-vars)

import {
ApiInternalServerErrorResponse,
ApiBadRequestResponse,
ApiNotFoundResponse,
ApiOkResponse,
} from '@nestjs/swagger';
import { Response } from 'express';
import { CertificateService } from './certificate.service';
import { IssueCredentialDto } from './dto/issue-certificate-dto';

@Controller('certificate')
export class CertificateController {
constructor(private readonly certificateService: CertificateService) {}
// API to generate DID
@ApiOkResponse({ description: 'DID generated successfully' })
@ApiInternalServerErrorResponse({ description: 'Internal Server Error.' })
@ApiBadRequestResponse({ description: 'Bad Request.' })
@Post('generateDid')
async generateDid(
@Body() createCertificateDto: any,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Define proper DTOs for request validation.

Using any type loses type safety and validation capabilities. Create proper DTOs for each operation:

// generate-did.dto.ts
export class GenerateDidDto {
  @IsString()
  @IsNotEmpty()
  userId: string;
}

// create-schema.dto.ts
export class CreateSchemaDto {
  @IsObject()
  @IsNotEmpty()
  schema: Record<string, unknown>;
}

// create-template.dto.ts
export class CreateTemplateDto {
  @IsString()
  @IsNotEmpty()
  schemaId: string;

  @IsObject()
  @IsNotEmpty()
  template: Record<string, unknown>;
}

// render-certificate.dto.ts
export class RenderCertificateDto {
  @IsString()
  @IsNotEmpty()
  credentialId: string;

  @IsString()
  @IsNotEmpty()
  templateId: string;
}

Also applies to: 35-35, 50-50, 82-82

@Res() response: Response,
) {
return this.certificateService.generateDid(
createCertificateDto.userId,
response,
);
}
Comment on lines +20 to +28
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Follow NestJS best practices for response handling.

Similar to the user certificate controller, avoid using Express's Response object directly and let NestJS handle the response transformation:

Example refactor:

 async generateDid(
-  @Body() createCertificateDto: any,
+  @Body() createCertificateDto: GenerateDidDto,
-  @Res() response: Response,
 ) {
-  return this.certificateService.generateDid(
+  return await this.certificateService.generateDid(
     createCertificateDto.userId,
-    response,
   );
 }

Apply similar changes to other methods.

Also applies to: 34-42, 49-58, 65-75, 81-90

// API to create schema
@ApiOkResponse({ description: 'Credential schema created successfully' })
@ApiInternalServerErrorResponse({ description: 'Internal Server Error.' })
@ApiBadRequestResponse({ description: 'Bad Request.' })
@Post('schema')
async createCredentialSchema(
@Body() createCertificateDto: any,
@Res() response: Response,
) {
return this.certificateService.createCredentialSchema(
createCertificateDto.schema,
response,
);
}
// API to create template
@ApiOkResponse({ description: 'Template created successfully' })
@ApiInternalServerErrorResponse({ description: 'Internal Server Error.' })
@ApiBadRequestResponse({ description: 'Bad Request.' })
@ApiNotFoundResponse({ description: 'Certificate Not Found.' })
@Post('template')
async createTemplate(
@Body() createCertificateDto: any,
@Res() response: Response,
) {
return this.certificateService.createTemplate(
createCertificateDto.schemaId,
createCertificateDto.template,
response,
);
}

// // API to issue certificate
@ApiOkResponse({ description: 'Certificate issued successfully.' })
@ApiInternalServerErrorResponse({ description: 'Internal Server Error.' })
@ApiBadRequestResponse({ description: 'Bad Request.' })
@Post('issue')
async issueCertificate(
@Body() issueCertificateDto: IssueCredentialDto,
@Res() response: Response,
@Req() request: Request,
) {
return await this.certificateService.issueCertificateAfterCourseCompletion(
issueCertificateDto,
request,
response,
);
}
// API to render certificate
@ApiOkResponse({ description: 'Certificate rendered successfully.' })
@ApiInternalServerErrorResponse({ description: 'Internal Server Error.' })
@ApiBadRequestResponse({ description: 'Bad Request.' })
@Post('render')
async renderCertificate(
@Body() renderCertificateDto: any,
@Res() response: Response,
) {
return await this.certificateService.renderCredentials(
renderCertificateDto.credentialId,
renderCertificateDto.templateId,
response,
);
}
}
14 changes: 14 additions & 0 deletions src/modules/certificate/certificate.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { CertificateService } from './certificate.service';
import { CertificateController } from './certificate.contoller';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix typo in controller import path.

There's a typo in the import path: .contoller should be .controller

- import { CertificateController } from './certificate.contoller';
+ import { CertificateController } from './certificate.controller';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { CertificateController } from './certificate.contoller';
import { CertificateController } from './certificate.controller';

import { TypeOrmModule } from '@nestjs/typeorm';
import { UserCourseCertificate } from './entities/user_course_certificate';
import { LoggerService } from 'src/common/logger/logger.service';
import { AxiosRequest } from 'src/common/middleware/axios.middleware';

@Module({
imports: [TypeOrmModule.forFeature([UserCourseCertificate])],
controllers: [CertificateController],
providers: [CertificateService, LoggerService, AxiosRequest],
})
export class CertificateModule {}
Loading