Skip to content

Commit

Permalink
chore: Add pdf-lib dependency for generating certificates, Add COMPLE…
Browse files Browse the repository at this point in the history
…TE status
  • Loading branch information
mayura-andrew committed Jul 11, 2024
1 parent d0e4843 commit a9d954c
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 15 deletions.
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"passport": "^0.6.0",
"passport-google-oauth20": "^2.0.0",
"passport-jwt": "^4.0.1",
"pdf-lib": "^1.17.1",
"pg": "^8.10.0",
"reflect-metadata": "^0.1.13",
"ts-node": "^10.9.1",
Expand Down
Binary file added src/certificates/certificate_template.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion src/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export enum EmailStatusTypes {
export enum ApplicationStatus {
PENDING = 'pending',
REJECTED = 'rejected',
APPROVED = 'approved'
APPROVED = 'approved',
COMPLETED = 'completed'
}

export enum StatusUpdatedBy {
Expand Down
6 changes: 4 additions & 2 deletions src/services/admin/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions src/services/admin/generateCertificate.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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
}
}
16 changes: 9 additions & 7 deletions src/services/admin/mentee.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export const updateStatus = async (
profile: {
uuid: profileUuid
}
}
},
relations: ['mentor']
})

// Handle Approve status
Expand All @@ -129,17 +130,18 @@ export const updateStatus = async (
status_updated_date: new Date()
}
)
const content = getEmailContent(
'mentee',
state,
mentee.application.firstName as string
)

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.message,
content.attachment
)
}
return {
Expand Down
2 changes: 1 addition & 1 deletion src/services/admin/mentor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/services/mentee.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/services/mentor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import path from 'path'
import multer from 'multer'
import ejs from 'ejs'
import { ApplicationStatus } from './enums'
import { generateCertificate } from './services/admin/generateCertificate'

export const signAndSetCookie = (res: Response, uuid: string): void => {
const token = jwt.sign({ userId: uuid }, JWT_SECRET ?? '')
Expand Down Expand Up @@ -79,11 +80,18 @@ 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 }>
}
| undefined
> => {
if (type === 'mentor') {
switch (status) {
case ApplicationStatus.PENDING:
Expand Down Expand Up @@ -145,6 +153,28 @@ 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.<br /><br />
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 pdfFileName = await generateCertificate(
name,
'./src/certificates/certificate_template.pdf',
`./src/certificates/${name}_certificate.pdf`
)
return {
subject: 'Congratulations! You have completed ScholarX',
message: `Dear ${name},<br /><br />
We are delighted to inform you that you have successfully completed the ScholarX program. We extend our heartfelt congratulations to you!<br /><br />
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.<br /><br />
To commemorate your achievement, we have attached your certificate of completion. Please download and save the certificate for your records.<br /><br />
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.<br /><br />
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
}
]
}
}
default:
return undefined
}
Expand Down

0 comments on commit a9d954c

Please sign in to comment.