<?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: pawan kumar</title>
    <description>The latest articles on DEV Community by pawan kumar (@pawan_kumar_841204).</description>
    <link>https://dev.to/pawan_kumar_841204</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%2F1679076%2F2d629418-19d3-4951-bf86-086e37cd7549.png</url>
      <title>DEV Community: pawan kumar</title>
      <link>https://dev.to/pawan_kumar_841204</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pawan_kumar_841204"/>
    <language>en</language>
    <item>
      <title>One way to shedule deletion process using Node.js</title>
      <dc:creator>pawan kumar</dc:creator>
      <pubDate>Sun, 29 Dec 2024 09:43:13 +0000</pubDate>
      <link>https://dev.to/pawan_kumar_841204/one-way-to-shedule-deletion-process-using-nodejs-22mi</link>
      <guid>https://dev.to/pawan_kumar_841204/one-way-to-shedule-deletion-process-using-nodejs-22mi</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Install the node cron package&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;npm install node-cron&lt;/p&gt;

&lt;h2&gt;
  
  
  Code to Shedule Deletion
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cron = require("node-cron");
const User = require("../models/User"); // Assume `User` is your Mongoose model

exports.deleteProfile = async (req,res) =&amp;gt; {
  try{
   const {userId} = req.body;
   //validate input
   if(!userId){
      return res.status(400).json({success:false,  message: "User ID is required" });
}
 // Find user to ensure existence

const user = await User.findById(userId);
if (!user) {
      return res.status(404).json({ success: false, message: "User not found" });
    }

 // Schedule deletion after 5 days

const deletionDate = new Date();
deletionDate.setDate(deletionDate.getDate()+5);

cron.schedule(deletionDate.toISOString(), async () =&amp;gt; {
      await User.findByIdAndDelete(userId);
      console.log(`User with ID ${userId} deleted successfully.`);
    });

    return res.status(200).json({
      success: true,
      message: `User deletion scheduled for ${deletionDate.toISOString()}`,
    });


}
catch(error){
   console.error("Error scheduling deletion:", error);
    return res.status(500).json({ success: false, message: "Internal Server Error" });
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to shedule a task in Node.js using Node Cron</title>
      <dc:creator>pawan kumar</dc:creator>
      <pubDate>Sun, 29 Dec 2024 09:18:45 +0000</pubDate>
      <link>https://dev.to/pawan_kumar_841204/how-to-shedule-a-task-in-nodejs-using-node-cron-5n6</link>
      <guid>https://dev.to/pawan_kumar_841204/how-to-shedule-a-task-in-nodejs-using-node-cron-5n6</guid>
      <description>&lt;p&gt;_&lt;a href="https://dev.tourl"&gt;&lt;/a&gt;_To handle a delayed deletion request (e.g., deleting a user profile after 5 days), you can achieve this in Node.js using a delayed task scheduling approach. Here’s a step-by-step implementation:&lt;/p&gt;

&lt;p&gt;Approach 1: Using a Background Task Scheduler (e.g., node-cron or Agenda.js)&lt;br&gt;
Example with node-cron:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install the package:
 npm install node-cron
Code to Shedule Deletion
const cron = require("node-cron");
const User = require("../models/User"); // Assume &lt;code&gt;User&lt;/code&gt; is your Mongoose model&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;exports.deleteProfile = async (req, res) =&amp;gt; {&lt;br&gt;
  try {&lt;br&gt;
    const { userId } = req.body;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Validate input
if (!userId) {
  return res.status(400).json({ success: false, message: "User ID is required" });
}

// Find user to ensure existence
const user = await User.findById(userId);
if (!user) {
  return res.status(404).json({ success: false, message: "User not found" });
}

// Schedule deletion after 5 days
const deletionDate = new Date();
deletionDate.setDate(deletionDate.getDate() + 5);

cron.schedule(deletionDate.toISOString(), async () =&amp;gt; {
  await User.findByIdAndDelete(userId);
  console.log(`User with ID ${userId} deleted successfully.`);
});

return res.status(200).json({
  success: true,
  message: `User deletion scheduled for ${deletionDate.toISOString()}`,
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;} catch (error) {&lt;br&gt;
    console.error("Error scheduling deletion:", error);&lt;br&gt;
    return res.status(500).json({ success: false, message: "Internal Server Error" });&lt;br&gt;
  }&lt;br&gt;
};&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
