Skip to content

Commit

Permalink
Bug fixed while patient registration
Browse files Browse the repository at this point in the history
  • Loading branch information
sujal1256 committed Oct 20, 2024
1 parent 6c671ef commit dbb1e37
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DB_Url=
PORT=
JWT_SECRET=
JWT_SECRET=
NODE_ENV=development
28 changes: 14 additions & 14 deletions Backend/api/controllers/PatientController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import { Patient } from "../models/Patient.js";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import { v4 as uuidv4 } from "uuid";

// Register a new patient
export const registerPatient = async (req, res) => {
try {
const { name, email, gender, mobile, password, age} = req.body;
const { name, email, gender, mobile, age } = req.body;

console.log(req.body);

Expand All @@ -16,20 +17,19 @@ export const registerPatient = async (req, res) => {
return res.status(400).json({ message: "Patient already exists" });
}

// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);

// Create a new patient
const newPatient = await Patient.create({
name,
email,
gender,
mobile,
age,
password: hashedPassword,
patientId: uuidv4(),
});

res.status(201).json({ message: "Patient registered successfully" });
res
.status(201)
.json({ message: "Patient registered successfully", newPatient });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Server error", error });
Expand Down Expand Up @@ -70,18 +70,18 @@ export const loginPatient = async (req, res) => {
};

export const deletePatient = async (req, res) => {
const {patientId} = req.body;
const { patientId } = req.body;

const patient = await Patient.deleteOne({_id: patientId});
const patient = await Patient.deleteOne({ _id: patientId });

console.log(patient);
if(!patient){

if (!patient) {
return res.status(400).json({
success: false,
message: "Patient not found"
})
message: "Patient not found",
});
}

return res.status()
}
return res.status();
};
2 changes: 1 addition & 1 deletion Backend/api/models/Patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const patientSchema = mongoose.Schema({
type: String,
required: true,
},
password: {
patientId: {
type: String,
required: true,
},
Expand Down
16 changes: 15 additions & 1 deletion Backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"jsonwebtoken": "^9.0.2",
"mongodb": "^6.9.0",
"mongoose": "^8.7.1",
"nodemon": "^3.1.7"
"nodemon": "^3.1.7",
"uuid": "^10.0.0"
}
}
2 changes: 1 addition & 1 deletion Frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BASE_URL =
BASE_URL=
28 changes: 16 additions & 12 deletions Frontend/src/components/Patients.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const Patients = () => {
gender: "",
mobile: "",
email: "",
password: "",
});

const handleInputChange = (e) => {
Expand All @@ -38,13 +37,27 @@ const Patients = () => {
const handleSubmit = async (e) => {
e.preventDefault();
try {
if (newPatient.name.trim().length < 4) {
alert(
"Invalid name. Name should start with a letter and be at least 4 characters long."
);
return;
}
const contactRegex = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;

if (newPatient.mobile && !contactRegex.test(newPatient.mobile)) {
alert(
"Invalid contact number. It should follow the format XXX-XXX-XXXX"
);
return;
}

const response = await axios.post("/api/patients/register", {
name: newPatient.name,
age: newPatient.age,
gender: newPatient.gender,
mobile: newPatient.mobile,
email: newPatient.email,
password: newPatient.password,
});
setShowForm(false);
setNewPatient({
Expand All @@ -62,7 +75,7 @@ const Patients = () => {

const deletePatient = async (id) => {
try {
const response = await axios.post("/api/patients//delete-patient", {
const response = await axios.post("/api/patients/delete-patient", {
patientId: id,
});
setShowForm(false);
Expand Down Expand Up @@ -139,15 +152,6 @@ const Patients = () => {
onChange={handleInputChange}
required
/>
<input
type="text"
name="password"
placeholder="Password"
className="p-2 border border-gray-300 rounded"
value={newPatient.password}
onChange={handleInputChange}
required
/>
<input
type="number"
name="age"
Expand Down

0 comments on commit dbb1e37

Please sign in to comment.