Skip to content

Commit

Permalink
fix on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald-pro committed Sep 26, 2024
1 parent 66ff1b8 commit 9fe3bf3
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
22 changes: 22 additions & 0 deletions models/n_roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const sequelize = require("../db_config");
const Sequelize = require("sequelize");
const Joi = require("joi");

const Nroles = sequelize.sequelize.define(
"tbl_nishauri_roles", {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
role_name:Sequelize.STRING,
status:Sequelize.ENUM('Active', 'Inactive')
}, {
timestamps: true,
paranoid: true,
underscored: true,
freezeTableName: true,
tableName: "tbl_nishauri_roles"
}
);
exports.Nroles = Nroles;
93 changes: 91 additions & 2 deletions routes/processes/nishauri_new.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const { NBmiLog } = require("../../models/n_bmi_log");
const { NBloodPressure } = require("../../models/n_blood_pressure");
const { NBloodSugar } = require("../../models/n_blood_sugar");
const { NMenstrual } = require("../../models/n_menstrual");
const { Nroles } = require("../../models/n_roles");

generateOtp = function (size) {
const zeros = "0".repeat(size - 1);
Expand Down Expand Up @@ -5483,8 +5484,7 @@ router.delete(

let menstrual_cycle = await NMenstrual.findOne({
where: {
id,
user_id:base64.decode(user_id)
[Op.and]: [{ id: id }, { user_id: base64.decode(user_id) }]
}
});

Expand Down Expand Up @@ -5522,6 +5522,74 @@ router.delete(
}
);

router.delete(
"/delete_menstrual_cycles",
passport.authenticate("jwt", { session: false }),
async (req, res) => {
try {
let user_id = req.body.user_id;

let today = moment(new Date())
.tz("Africa/Nairobi")
.format("YYYY-MM-DD HH:mm:ss");

// Verify user exists
let user = await NUsers.findOne({ where: { id: base64.decode(user_id) } });

if (!user) {
return res.status(404).json({
success: false,
msg: "User not found"
});
}

// Find all menstrual cycles for the user
let menstrual_cycles = await NMenstrual.findAll({
where: {
user_id: base64.decode(user_id)
}
});

if (menstrual_cycles.length === 0) {
return res.status(404).json({
success: false,
msg: "No menstrual cycle records found"
});
}

// Update the status of all menstrual cycles to 'Deleted'
await NMenstrual.update(
{
status: "Deleted",
updated_at: today,
deleted_at: today
},
{
where: { user_id: base64.decode(user_id) }
}
);

// Log the deletion activity
await NLogs.create({
user_id,
access: "MENSTRUALCYCLE"
});

return res.status(200).json({
success: true,
msg: "All menstrual cycles deleted successfully"
});
} catch (error) {
return res.status(500).json({
success: false,
msg: "An error occurred while deleting the cycles",
error: error.message
});
}
}
);


const sendAppUpdateNotification = (registrationToken) => {
if (!registrationToken) {
return;
Expand Down Expand Up @@ -5568,6 +5636,27 @@ cron.schedule(specificDateAndTime, () => {
notifyUsersAboutAppUpdate();
});

router.get("/get_roles", async (req, res) => {
try {
let roles = await Nroles.findAll({
where: {
status: "Active"
}
});
return res.status(200).json({
success: true,
message: "Roles were successfully retrieved",
roles: roles
});
} catch (error) {
return res.status(500).json({
success: false,
message: "Failed to retrieve roles",
error: error.message
});
}
});


module.exports = router;
//module.exports = { router, users };

0 comments on commit 9fe3bf3

Please sign in to comment.