diff --git a/package-lock.json b/package-lock.json index 3905aac5..3f5305d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,7 @@ "passport-google-oauth20": "^2.0.0", "passport-jwt": "^4.0.1", "passport-linkedin-oauth2": "github:auth0/passport-linkedin-oauth2#v3.0.0", + "pdf-lib": "^1.17.1", "pg": "^8.10.0", "reflect-metadata": "^0.1.13", "ts-node": "^10.9.1", @@ -49,7 +50,6 @@ "@types/pg": "^8.10.1", "@types/prettier": "^2.7.2", "@types/supertest": "^2.0.12", - "@types/uuid": "^9.0.2", "@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.59.5", "eslint": "^8.46.0", @@ -1490,6 +1490,22 @@ "node": ">= 8" } }, + "node_modules/@pdf-lib/standard-fonts": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz", + "integrity": "sha512-hU30BK9IUN/su0Mn9VdlVKsWBS6GyhVfqjwl1FjZN4TxP6cCw0jP2w7V3Hf5uX7M0AZJ16vey9yE0ny7Sa59ZA==", + "dependencies": { + "pako": "^1.0.6" + } + }, + "node_modules/@pdf-lib/upng": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@pdf-lib/upng/-/upng-1.0.1.tgz", + "integrity": "sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ==", + "dependencies": { + "pako": "^1.0.10" + } + }, "node_modules/@pkgr/utils": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/@pkgr/utils/-/utils-2.4.2.tgz", @@ -2479,12 +2495,6 @@ "@types/superagent": "*" } }, - "node_modules/@types/uuid": { - "version": "9.0.2", - "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.2.tgz", - "integrity": "sha512-kNnC1GFBLuhImSnV7w4njQkUiJi0ZXUycu1rUaouPqiKlXkh77JKgdRnTAp1x5eBwcIwbtI+3otwzuIDEuDoxQ==", - "dev": true - }, "node_modules/@types/yargs": { "version": "17.0.24", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.24.tgz", @@ -8570,6 +8580,11 @@ "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" }, + "node_modules/pako": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==" + }, "node_modules/param-case": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/param-case/-/param-case-3.0.4.tgz", @@ -8781,6 +8796,22 @@ "resolved": "https://registry.npmjs.org/pause/-/pause-0.0.1.tgz", "integrity": "sha512-KG8UEiEVkR3wGEb4m5yZkVCzigAD+cVEJck2CzYZO37ZGJfctvVptVO192MwrtPhzONn6go8ylnOdMhKqi4nfg==" }, + "node_modules/pdf-lib": { + "version": "1.17.1", + "resolved": "https://registry.npmjs.org/pdf-lib/-/pdf-lib-1.17.1.tgz", + "integrity": "sha512-V/mpyJAoTsN4cnP31vc0wfNA1+p20evqqnap0KLoRUN0Yk/p3wN52DOEsL4oBFcLdb76hlpKPtzJIgo67j/XLw==", + "dependencies": { + "@pdf-lib/standard-fonts": "^1.0.0", + "@pdf-lib/upng": "^1.0.1", + "pako": "^1.0.11", + "tslib": "^1.11.1" + } + }, + "node_modules/pdf-lib/node_modules/tslib": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" + }, "node_modules/pg": { "version": "8.10.0", "resolved": "https://registry.npmjs.org/pg/-/pg-8.10.0.tgz", @@ -10585,6 +10616,18 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "node_modules/typeorm/node_modules/uuid": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/typescript": { "version": "5.3.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", @@ -10719,14 +10762,6 @@ "node": ">= 0.4.0" } }, - "node_modules/uuid": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", - "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==", - "bin": { - "uuid": "dist/bin/uuid" - } - }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", diff --git a/package.json b/package.json index 089c24ef..35714a15 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "passport-google-oauth20": "^2.0.0", "passport-jwt": "^4.0.1", "passport-linkedin-oauth2": "github:auth0/passport-linkedin-oauth2#v3.0.0", + "pdf-lib": "^1.17.1", "pg": "^8.10.0", "reflect-metadata": "^0.1.13", "ts-node": "^10.9.1", @@ -61,7 +62,6 @@ "@types/pg": "^8.10.1", "@types/prettier": "^2.7.2", "@types/supertest": "^2.0.12", - "@types/uuid": "^9.0.2", "@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.59.5", "eslint": "^8.46.0", diff --git a/src/certificates/certificate_template.pdf b/src/certificates/certificate_template.pdf new file mode 100644 index 00000000..2421f407 Binary files /dev/null and b/src/certificates/certificate_template.pdf differ diff --git a/src/entities/baseEntity.ts b/src/entities/baseEntity.ts index 468c78a3..aa2d3037 100644 --- a/src/entities/baseEntity.ts +++ b/src/entities/baseEntity.ts @@ -1,10 +1,10 @@ +import { randomUUID } from 'crypto' import { BeforeInsert, BeforeUpdate, Column, PrimaryGeneratedColumn } from 'typeorm' -import { v4 as uuidv4 } from 'uuid' class BaseEntity { @PrimaryGeneratedColumn('uuid') @@ -28,7 +28,7 @@ class BaseEntity { @BeforeInsert() async generateUuid(): Promise { if (!this.uuid) { - this.uuid = uuidv4() + this.uuid = randomUUID() } } } diff --git a/src/entities/mentee.entity.ts b/src/entities/mentee.entity.ts index 0773ee0e..a6b6ca15 100644 --- a/src/entities/mentee.entity.ts +++ b/src/entities/mentee.entity.ts @@ -3,6 +3,7 @@ import Mentor from './mentor.entity' import profileEntity from './profile.entity' import { ApplicationStatus, StatusUpdatedBy } from '../enums' import BaseEntity from './baseEntity' +import { UUID } from 'typeorm/driver/mongodb/bson.typings' @Entity('mentee') class Mentee extends BaseEntity { @@ -22,8 +23,8 @@ class Mentee extends BaseEntity { @Column({ type: 'json' }) application: Record - @Column({ type: 'bigint', nullable: true, default: null }) - certificate_id!: bigint + @Column({ type: 'uuid', nullable: true, default: null }) + certificate_id!: UUID @Column({ default: null, nullable: true }) journal!: string diff --git a/src/enums/index.ts b/src/enums/index.ts index 1760a198..d6095836 100644 --- a/src/enums/index.ts +++ b/src/enums/index.ts @@ -12,7 +12,8 @@ export enum EmailStatusTypes { export enum ApplicationStatus { PENDING = 'pending', REJECTED = 'rejected', - APPROVED = 'approved' + APPROVED = 'approved', + COMPLETED = 'completed' } export enum StatusUpdatedBy { diff --git a/src/services/admin/email.service.ts b/src/services/admin/email.service.ts index 5a10cadf..8bf5cca1 100644 --- a/src/services/admin/email.service.ts +++ b/src/services/admin/email.service.ts @@ -18,7 +18,8 @@ const transporter = nodemailer.createTransport({ export const sendEmail = async ( to: string, subject: string, - message: string + message: string, + attachments?: Array<{ filename: string; path: string }> ): Promise<{ statusCode: number message: string @@ -35,7 +36,8 @@ export const sendEmail = async ( from: `"Sustainable Education Foundation" <${SMTP_MAIL}>`, to, subject, - html + html, + attachments }) const email = new Email(to, subject, message, EmailStatusTypes.SENT) diff --git a/src/services/admin/generateCertificate.ts b/src/services/admin/generateCertificate.ts new file mode 100644 index 00000000..50963a74 --- /dev/null +++ b/src/services/admin/generateCertificate.ts @@ -0,0 +1,40 @@ +import { PDFDocument, rgb } from 'pdf-lib' +import fs from 'fs' + +export async function generateCertificate( + menteeName: string, + sourcePdfPath: string, + outputPath: string +): Promise { + try { + const existingPdfBytes = fs.readFileSync(sourcePdfPath) + const pdfDoc = await PDFDocument.load(existingPdfBytes) + const page = pdfDoc.getPage(0) + const fontSize = 24 + const datezFontSize = 18 + + page.drawText(menteeName, { + x: 66, + y: page.getHeight() - 220, + size: fontSize, + color: rgb(0, 0, 0) + }) + + const issueDate = new Date().toLocaleDateString() + + page.drawText(issueDate, { + x: 160, + y: page.getHeight() - 476, + size: datezFontSize, + color: rgb(0, 0, 0) + }) + + const pdfBytes = await pdfDoc.save() + + fs.writeFileSync(outputPath, pdfBytes) + return outputPath + } catch (error) { + console.error('Failed to modify the PDF:', error) + throw error + } +} diff --git a/src/services/admin/mentee.service.ts b/src/services/admin/mentee.service.ts index 264fe4dd..4f19d82c 100644 --- a/src/services/admin/mentee.service.ts +++ b/src/services/admin/mentee.service.ts @@ -111,9 +111,23 @@ export const updateStatus = async ( profile: { uuid: profileUuid } - } + }, + relations: ['mentor'] }) + const menteeName = + mentee.application.firstName + ' ' + mentee.application.lastName + + const content = await getEmailContent('mentee', state, menteeName) + + if (content) { + await sendEmail( + mentee.application.email as string, + content.subject, + content.message, + content.attachment + ) + } // Handle Approve status if (approvedApplications && state === 'approved') { return { @@ -126,22 +140,10 @@ export const updateStatus = async ( { state, status_updated_by: statusUpdatedBy, - status_updated_date: new Date() + status_updated_date: new Date(), + certificate_id: content?.uniqueId } ) - const content = getEmailContent( - 'mentee', - state, - mentee.application.firstName as string - ) - - if (content) { - await sendEmail( - mentee.application.email as string, - content.subject, - content.message - ) - } return { statusCode: 200, message: 'Mentee application state successfully updated' diff --git a/src/services/admin/mentor.service.ts b/src/services/admin/mentor.service.ts index d5364096..7db826cb 100644 --- a/src/services/admin/mentor.service.ts +++ b/src/services/admin/mentor.service.ts @@ -28,7 +28,7 @@ export const updateMentorStatus = async ( await mentorRepository.update({ uuid: mentorId }, { state: status }) - const content = getEmailContent( + const content = await getEmailContent( 'mentor', status, mentor.application.firstName as string diff --git a/src/services/mentee.service.ts b/src/services/mentee.service.ts index d5869984..7a8f2e5e 100644 --- a/src/services/mentee.service.ts +++ b/src/services/mentee.service.ts @@ -76,7 +76,7 @@ export const addMentee = async ( await menteeRepository.save(newMentee) - const content = getEmailContent( + const content = await getEmailContent( 'mentee', ApplicationStatus.PENDING, application.firstName as string diff --git a/src/services/mentor.service.ts b/src/services/mentor.service.ts index cf87297a..de568790 100644 --- a/src/services/mentor.service.ts +++ b/src/services/mentor.service.ts @@ -81,7 +81,7 @@ export const createMentor = async ( const savedMentor = await mentorRepository.save(newMentor) - const content = getEmailContent( + const content = await getEmailContent( 'mentor', ApplicationStatus.PENDING, application.firstName as string diff --git a/src/utils.ts b/src/utils.ts index 6ea0d917..b86c7b7e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,7 +6,8 @@ import path from 'path' import multer from 'multer' import ejs from 'ejs' import { ApplicationStatus } from './enums' - +import { generateCertificate } from './services/admin/generateCertificate' +import { randomUUID } from 'crypto' export const signAndSetCookie = (res: Response, uuid: string): void => { const token = jwt.sign({ userId: uuid }, JWT_SECRET ?? '') @@ -79,11 +80,19 @@ export const loadTemplate = ( }) } -export const getEmailContent = ( +export const getEmailContent = async ( type: 'mentor' | 'mentee', status: ApplicationStatus, name: string -): { subject: string; message: string } | undefined => { +): Promise< + | { + subject: string + message: string + attachment?: Array<{ filename: string; path: string }> + uniqueId?: string + } + | undefined +> => { if (type === 'mentor') { switch (status) { case ApplicationStatus.PENDING: @@ -166,6 +175,30 @@ export const getEmailContent = ( We do offer the possibility for you to apply again next time if you meet the eligibility criteria. We invite you to stay engaged with us by attending our events, reaching out to our admissions team, and taking advantage of any opportunities to connect with our current students and alumni.

Thank you again for considering our program and for the time you invested in your application. We wish you all the best in your future endeavours.` } + case ApplicationStatus.COMPLETED: { + const uniqueId = randomUUID() + const pdfFileName = await generateCertificate( + name, + './src/certificates/certificate_template.pdf', + `./src/certificates/mentee/${uniqueId}_certificate.pdf` + ) + return { + subject: 'Congratulations! You have completed ScholarX', + message: `Dear ${name},

+ We are delighted to inform you that you have successfully completed the ScholarX program. We extend our heartfelt congratulations to you!

+ We are proud of your dedication, hard work, and commitment to the program. You have demonstrated exceptional talent, and we are confident that you will go on to achieve great success in your academic and professional pursuits.

+ To commemorate your achievement, we have attached your certificate of completion. Please download and save the certificate for your records.

+ We look forward to seeing the unique perspective and insights you will bring to the program. We believe that you will flourish in this year's edition of ScholarX, and we are thrilled to be a part of your academic or professional journey.

+ Once again, congratulations on your completion! We cannot wait to see the great things you will achieve in the future.`, + attachment: [ + { + filename: `${name}_certificate.pdf`, + path: pdfFileName + } + ], + uniqueId + } + } default: return undefined }