My Backend Journey:Overcoming Backend Challenges
Published: 2024-06-29 06:08:40

My name is Salim Imuzai. A backend developer who believes; that for every complex problem, there is an answer that is either clear, simple, or wrong I take pride in finding out. As a backend developer, the excitement of tackling complex problems fuels my passion for coding. Every challenge presents an opportunity to learn, grow, and sharpen my skills. Recently, I encountered a particularly tough issue that tested my problem-solving abilities and showed how much I would want to become an intern at [HNG Programme](https://hng.tech/premium) in order to solve such issues as they occur again.

**The Challenge: Real-time Notification System**
The problem I faced was implementing a real-time notification system for a Sexual and Gender-Based Violence (SGBV) reporting platform. The system needed to notify relevant stakeholders whenever a case was created, updated, or reached specific stages like trial, DPP advice, or judgment. Additionally, the notifications had to be sent promptly without overwhelming the server.

**Step-by-Step Solution Breakdown**

**Step 1: Understanding the Requirements**
The first step was to thoroughly understand the requirements. The system needed to:

1. Detect when a case was created or updated.
2. Identify the relevant stakeholders (e.g., admins, stakeholders).
3. Send notifications and emails promptly.
4. Mark notifications as sent to avoid duplicates.

**Step 2: Setting Up the Cron Job**
I decided to use a Cron job to periodically check for pending notifications and process them. This ensured that the notifications were sent in a controlled manner without burdening the server.

`
@Cron('0 * * * * *') // Every minute for demonstration
async handleCron() {
  // Fetch pending notifications and users
}
`
**Step 3: Fetching Pending Notifications and Users**
Next, I wrote a function to fetch pending notifications and relevant users from the database. This involved using Mongoose to interact with MongoDB.

`
const pendingNotifications = await this.notificationModel.find({ isSent: false }).exec();
const users = await this.userModel.find({
  $or: [{ role: USER_ROLE.ADMIN }, { role: USER_ROLE.STAKEHOLDER }],
}).exec();
`

**Step 4: Sending Notifications and Emails**
For each pending notification, I iterated over the users and sent the notifications and emails. To ensure reliability, I used a robust email service

`
for (const notification of pendingNotifications) {
  for (const user of users) {
    await this.notificationService.createRecipientNotification({
      user: user.id.toString(),
      message: notification.message,
      caseNumber: notification.caseNumber,
    });
    await this.sendNewCaseEmail(user.email, notification.caseNumber);
  }
  notification.isSent = true;
  await notification.save();
}
`
**Step 5: Implementing the Email Sending Logic**
Finally, I implemented the email sending logic using a hypothetical email service. This part ensured that users were promptly informed about new cases.

`
async sendNewCaseEmail(email: string, caseNumber: string) {
  await this.mailService.sendMail({
    to: email,
    subject: 'New Case Created',
    html: `<p>A new case with case number ${caseNumber} has been created.</p>`,
  });
}
`
**The Journey Ahead: HNG Internship**
Solving this problem was a rewarding experience that showcased my ability to tackle complex backend issues. However, I know there's always more to learn and achieve. This is why I am excited about the [HNG Internship](https://hng.tech/premium). The program offers a unique opportunity to collaborate with experienced professionals, work on real-world projects, and refine my skills, I am eager to contribute, learn, and grow within this vibrant community of developers.