Skip to content

Commit 9ac5b7f

Browse files
authored
Merge pull request #3 from erowe-dev/render-examples#26-user-management
render-examples#26 user management
2 parents c63cd9a + 5938594 commit 9ac5b7f

22 files changed

+442
-161
lines changed

.vscode/settings.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"workbench.colorCustomizations": {
3+
"activityBar.activeBackground": "#c89781",
4+
"activityBar.background": "#c89781",
5+
"activityBar.foreground": "#15202b",
6+
"activityBar.inactiveForeground": "#15202b99",
7+
"activityBarBadge.background": "#d3ecda",
8+
"activityBarBadge.foreground": "#15202b",
9+
"commandCenter.border": "#15202b99",
10+
"sash.hoverBorder": "#c89781",
11+
"statusBar.background": "#b87a5e",
12+
"statusBar.foreground": "#15202b",
13+
"statusBarItem.hoverBackground": "#9d6145",
14+
"statusBarItem.remoteBackground": "#b87a5e",
15+
"statusBarItem.remoteForeground": "#15202b",
16+
"titleBar.activeBackground": "#b87a5e",
17+
"titleBar.activeForeground": "#15202b",
18+
"titleBar.inactiveBackground": "#b87a5e99",
19+
"titleBar.inactiveForeground": "#15202b99"
20+
},
21+
"peacock.color": "#b87a5e"
22+
}

app.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const authRouter = require("./routes/auth.routes");
1010
const cooperativeDoctorsRouter = require("./routes/cooperative-doctors.routes");
1111
const prospectiveDoctorsRouter = require("./routes/prospective-doctors.routes");
1212
const hospitalsRouter = require("./routes/hospitals.routes");
13+
const notesRouter = require("./routes/notes.routes");
1314
const usersRouter = require("./routes/users.routes");
15+
const userManagmentRouter = require("./routes/user-management.routes");
1416

1517
const port = process.env.PORT || 3001;
1618
const dbConnectionString = process.env.MongoURI;
@@ -53,11 +55,21 @@ app.use(
5355
passport.authenticate("jwt", { session: false }),
5456
hospitalsRouter
5557
);
58+
app.use(
59+
"/notes",
60+
passport.authenticate("jwt", { session: false }),
61+
notesRouter
62+
);
5663
app.use(
5764
"/users",
5865
passport.authenticate("jwt", { session: false }),
5966
usersRouter
6067
);
68+
app.use(
69+
"/user-management",
70+
passport.authenticate("jwt", { session: false }),
71+
userManagmentRouter
72+
);
6173

6274
app.get("/", (req, res) => res.type("html").send(html));
6375

auth/auth.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ passport.use(
3030
},
3131
async (email, password, done) => {
3232
try {
33+
email = email.toLowerCase();
3334
const user = await UserModel.create({ email, password });
3435

3536
return done(null, user);
3637
} catch (error) {
38+
console.log(error);
3739
done(error);
3840
}
3941
}
@@ -49,8 +51,9 @@ passport.use(
4951
},
5052
async (email, password, done) => {
5153
try {
54+
email = email.toLowerCase();
5255
const user = await UserModel.findOne({ email });
53-
56+
5457
if (!user) {
5558
return done(null, false, { message: "User not found" });
5659
}
Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,66 @@
1-
const mongoose = require("mongoose");
2-
const CooperativeDoctor = require('../models/cooperative-doctor.model');
3-
1+
const decode = require("jwt-decode");
2+
const CooperativeDoctor = require("../models/cooperative-doctor.model");
43

54
const getCooperativeDoctors = async (req, res) => {
6-
let doctors = await CooperativeDoctor.find().populate('primaryFacility');
5+
let doctors = await CooperativeDoctor.find().populate("primaryFacility");
76

8-
if(doctors) {
9-
res.status(200).json(doctors);
10-
} else {
11-
res.status(400).json()
12-
}
13-
}
7+
if (doctors) {
8+
res.status(200).json(doctors);
9+
} else {
10+
res.status(400).json();
11+
}
12+
};
1413

1514
const getCooperativeDoctor = async (req, res) => {
16-
let doctor = await CooperativeDoctor.findById(req.params.id).populate('primaryFacility');
15+
let doctor = await CooperativeDoctor.findById(req.params.id).populate(
16+
"primaryFacility"
17+
);
1718

18-
if(doctor) {
19-
res.status(200).json(doctor);
20-
} else {
21-
res.status(400).json()
22-
}
23-
}
19+
if (doctor) {
20+
res.status(200).json(doctor);
21+
} else {
22+
res.status(400).json();
23+
}
24+
};
2425

2526
const createCooperativeDoctor = async (req, res) => {
26-
req.body._id = new mongoose.Types.ObjectId();
27-
req.body.firstname = req.body.fullname.split(" ")[0];
28-
req.body.lastname = req.body.fullname.split(" ")[1] ? req.body.fullname.split(" ")[1] : "";
29-
req.body.primaryPhone ? req.body.phoneNumbers.push(req.body.primaryPhone) : null;
30-
let doctor = await CooperativeDoctor.create(req.body);
31-
res.status(201).json(doctor);
32-
}
27+
var token = decode(req.headers.authorization);
28+
req.body.createdBy = token.user.name;
29+
30+
req.body.primaryPhone
31+
? req.body.phoneNumbers.push(req.body.primaryPhone)
32+
: null;
33+
let doctor = await CooperativeDoctor.create(req.body);
34+
res.status(201).json(doctor);
35+
};
3336

3437
const updateCooperativeDoctor = async (req, res) => {
35-
req.body.firstname = req.body.fullname.split(" ")[0];
36-
req.body.lastname = req.body.fullname.split(" ")[1] ? req.body.fullname.split(" ")[1] : "";
37-
req.body.primaryPhone ? req.body.phoneNumbers = [req.body.primaryPhone] : req.body.phoneNumbers = [];
38-
req.body.secondaryPhones ? req.body.phoneNumbers.push(req.body.secondaryPhones) : null;
39-
let doctor = await CooperativeDoctor.findByIdAndUpdate(req.params.id, req.body, { new: true });
40-
res.status(200).json(doctor);
41-
}
38+
var token = decode(req.headers.authorization);
39+
req.body.updatedBy = token.user.name;
40+
41+
req.body.primaryPhone
42+
? (req.body.phoneNumbers = [req.body.primaryPhone])
43+
: (req.body.phoneNumbers = []);
44+
req.body.secondaryPhones
45+
? req.body.phoneNumbers.push(req.body.secondaryPhones)
46+
: null;
47+
let doctor = await CooperativeDoctor.findOneAndUpdate(
48+
{ _id: req.params.id },
49+
req.body,
50+
{ new: true }
51+
);
52+
res.status(200).json(doctor);
53+
};
54+
55+
const deleteCooperativeDoctor = async (req, res) => {
56+
await CooperativeDoctor.findByIdAndDelete(req.params.id);
57+
res.status(200).json({ message: "Cooperative doctor deleted successfully" });
58+
};
4259

4360
module.exports = {
44-
getCooperativeDoctors, getCooperativeDoctor, createCooperativeDoctor, updateCooperativeDoctor
45-
}
61+
getCooperativeDoctors,
62+
getCooperativeDoctor,
63+
createCooperativeDoctor,
64+
updateCooperativeDoctor,
65+
deleteCooperativeDoctor,
66+
};
Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,59 @@
1-
const Hospital = require('../models/hospital.model');
1+
const decode = require("jwt-decode");
2+
const Hospital = require("../models/hospital.model");
23

34
const getHospitals = async (req, res) => {
4-
let hospitals = await Hospital.find();
5+
let hospitals = await Hospital.find();
56

6-
if(hospitals) {
7-
res.status(200).json(hospitals);
8-
} else {
9-
res.status(400).json()
10-
}
11-
}
7+
if (hospitals) {
8+
res.status(200).json(hospitals);
9+
} else {
10+
res.status(400).json();
11+
}
12+
};
1213

1314
const getHospital = async (req, res) => {
14-
let hospital = await Hospital.findById(req.params.id);
15+
let hospital = await Hospital.findById(req.params.id);
1516

16-
if(hospital) {
17-
res.status(200).json(hospital);
18-
} else {
19-
res.status(400).json()
20-
}
21-
}
17+
if (hospital) {
18+
res.status(200).json(hospital);
19+
} else {
20+
res.status(400).json();
21+
}
22+
};
2223

2324
const createHospital = async (req, res) => {
24-
let hospital = await Hospital.create(req.body);
25-
res.status(201).json(hospital);
26-
}
25+
var token = decode(req.headers.authorization);
26+
req.body.createdBy = token.user.name;
27+
28+
let hospital = await Hospital.create(req.body);
29+
30+
hospital.createdBy = req.user._id;
31+
32+
res.status(201).json(hospital);
33+
};
2734

2835
const updateHospital = async (req, res) => {
29-
let hosptial = await Hospital.findByIdAndUpdate(req.params.id, req.body, { new: true });
30-
res.status(200).json(hosptial);
31-
}
36+
var token = decode(req.headers.authorization);
37+
req.body.updatedBy = token.user.name;
38+
39+
let hospital = await Hospital.findByIdAndUpdate(req.params.id, req.body, {
40+
new: true,
41+
});
42+
43+
hospital.updatedBy = req.user._id;
44+
45+
res.status(200).json(hospital);
46+
};
3247

48+
const deleteHospital = async (req, res) => {
49+
await CooperativeDoctor.findByIdAndDelete(req.params.id);
50+
res.status(200).json({ message: "Hospital deleted successfully" });
51+
};
3352

3453
module.exports = {
35-
getHospitals, getHospital, createHospital, updateHospital
36-
}
54+
getHospitals,
55+
getHospital,
56+
createHospital,
57+
updateHospital,
58+
deleteHospital,
59+
};

controllers/notes.controller.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const decode = require("jwt-decode");
2+
const Note = require("../models/hospital.model");
3+
4+
const getNotes = async (req, res) => {
5+
let entityType = req.params.entity;
6+
let entityId = req.params.id;
7+
8+
if (entityType === "facility") {
9+
let notes = await Note.find({ facilityId: entityId });
10+
res.status(200).json(notes);
11+
} else if (entityType === "cooperativeDoctor") {
12+
let notes = await Note.find({ cooperativeDoctorId: entityId });
13+
res.status(200).json(notes);
14+
} else if (entityType === "prospectiveDoctor") {
15+
let notes = await Note.find({ prospectiveDoctorId: entityId });
16+
res.status(200).json(notes);
17+
} else {
18+
res.status(400).json({ message: "Invalid entity type" });
19+
}
20+
};
21+
22+
const createNote = async (req, res) => {
23+
var token = decode(req.headers.authorization);
24+
req.body.createdBy = token.user.name;
25+
26+
let note = new Note(req.body);
27+
await note.save();
28+
29+
res.status(201).json(note.id);
30+
};
31+
32+
module.exports = {
33+
getNotes,
34+
createNote,
35+
};
Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,57 @@
1-
const ProspectiveDoctor = require('../models/prospective-doctor.model');
1+
const decode = require("jwt-decode");
2+
const ProspectiveDoctor = require("../models/prospective-doctor.model");
23

34
const getProspectiveDoctors = async (req, res) => {
4-
let doctors = await ProspectiveDoctor.find().populate('primaryFacility');
5+
let doctors = await ProspectiveDoctor.find().populate("primaryFacility");
56

6-
if(doctors) {
7-
res.status(200).json(doctors);
8-
} else {
9-
res.status(400).json()
10-
}
11-
}
7+
if (doctors) {
8+
res.status(200).json(doctors);
9+
} else {
10+
res.status(400).json();
11+
}
12+
};
1213

1314
const getProspectiveDoctor = async (req, res) => {
14-
let doctor = await ProspectiveDoctor.findById(req.params.id).populate('primaryFacility');
15+
let doctor = await ProspectiveDoctor.findById(req.params.id).populate(
16+
"primaryFacility"
17+
);
1518

16-
if(doctor) {
17-
res.status(200).json(doctor);
18-
} else {
19-
res.status(400).json()
20-
}
21-
}
19+
if (doctor) {
20+
res.status(200).json(doctor);
21+
} else {
22+
res.status(400).json();
23+
}
24+
};
2225

2326
const createProspectiveDoctor = async (req, res) => {
24-
let doctor = await ProspectiveDoctor.create(req.body);
25-
res.status(201).json(doctor);
26-
}
27+
var token = decode(req.headers.authorization);
28+
req.body.createdBy = token.user.name;
29+
30+
let doctor = await ProspectiveDoctor.create(req.body);
31+
res.status(201).json(doctor);
32+
};
2733

2834
const updateProspectiveDoctor = async (req, res) => {
29-
let doctor = await ProspectiveDoctor.findByIdAndUpdate(req.params.id, req.body, { new: true });
30-
res.status(200).json(doctor);
31-
}
35+
var token = decode(req.headers.authorization);
36+
req.body.updatedBy = token.user.name;
37+
38+
let doctor = await ProspectiveDoctor.findByIdAndUpdate(
39+
req.params.id,
40+
req.body,
41+
{ new: true }
42+
);
43+
res.status(200).json(doctor);
44+
};
45+
46+
const deleteProspectiveDoctor = async (req, res) => {
47+
await ProspectiveDoctor.findByIdAndDelete(req.params.id);
48+
res.status(200).json({ message: "Prospective doctor deleted successfully" });
49+
};
3250

3351
module.exports = {
34-
getProspectiveDoctors, getProspectiveDoctor, createProspectiveDoctor, updateProspectiveDoctor
35-
}
52+
getProspectiveDoctors,
53+
getProspectiveDoctor,
54+
createProspectiveDoctor,
55+
updateProspectiveDoctor,
56+
deleteProspectiveDoctor,
57+
};

0 commit comments

Comments
 (0)