<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: salim imuzai</title>
    <description>The latest articles on DEV Community by salim imuzai (@salimkarbm).</description>
    <link>https://dev.to/salimkarbm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F254368%2Fbf938eb1-2015-4261-aac6-70d361d23580.jpeg</url>
      <title>DEV Community: salim imuzai</title>
      <link>https://dev.to/salimkarbm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/salimkarbm"/>
    <language>en</language>
    <item>
      <title>My Journey: Overcoming Backend Challenges</title>
      <dc:creator>salim imuzai</dc:creator>
      <pubDate>Sat, 29 Jun 2024 06:08:40 +0000</pubDate>
      <link>https://dev.to/salimkarbm/my-backend-journeyovercoming-backend-challenges-29mb</link>
      <guid>https://dev.to/salimkarbm/my-backend-journeyovercoming-backend-challenges-29mb</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Challenge: Real-time Notification System&lt;/strong&gt;&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-by-Step Solution Breakdown&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Understanding the Requirements&lt;/strong&gt;&lt;br&gt;
The first step was to thoroughly understand the requirements. The system needed to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Detect when a case was created or updated.&lt;/li&gt;
&lt;li&gt;Identify the relevant stakeholders (e.g., admins, stakeholders).&lt;/li&gt;
&lt;li&gt;Send notifications and emails promptly.&lt;/li&gt;
&lt;li&gt;Mark notifications as sent to avoid duplicates.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Setting Up the Cron Job&lt;/strong&gt;&lt;br&gt;
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.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;br&gt;
const pendingNotifications = await this.notificationModel.find({ isSent: false }).exec();&lt;br&gt;
const users = await this.userModel.find({&lt;br&gt;
  $or: [{ role: USER_ROLE.ADMIN }, { role: USER_ROLE.STAKEHOLDER }],&lt;br&gt;
}).exec();&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Sending Notifications and Emails&lt;/strong&gt;&lt;br&gt;
For each pending notification, I iterated over the users and sent the notifications and emails. To ensure reliability, I used a robust email service&lt;/p&gt;

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


&lt;p&gt;&lt;code&gt;&lt;br&gt;&lt;br&gt;
async sendNewCaseEmail(email: string, caseNumber: string) {&lt;br&gt;&lt;br&gt;
  await this.mailService.sendMail({&lt;br&gt;&lt;br&gt;
    to: email,&lt;br&gt;&lt;br&gt;
    subject: 'New Case Created',&lt;br&gt;&lt;br&gt;
    html:&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;A new case with case number ${caseNumber} has been created.&lt;/p&gt;
&lt;code&gt;,&lt;br&gt;&lt;br&gt;
  });&lt;br&gt;&lt;br&gt;
}&lt;br&gt;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;The Journey Ahead&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
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 tackling complex problems that fuel my passion for coding and exploring programs that offer 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 any vibrant community of developers.

</description>
    </item>
  </channel>
</rss>
