-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Controller, Post, Body, Res, UseGuards, Req } from '@nestjs/common'; | ||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Define proper DTOs for request validation. Using // 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
); | ||
} | ||
} |
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'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typo in controller import path. There's a typo in the import path: - import { CertificateController } from './certificate.contoller';
+ import { CertificateController } from './certificate.controller'; 📝 Committable suggestion
Suggested change
|
||||||
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 {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused import.
The
UseGuards
import is not used in this file.📝 Committable suggestion
🧰 Tools
🪛 ESLint
[error] 1-1: 'UseGuards' is defined but never used.
(@typescript-eslint/no-unused-vars)