Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 114 additions & 93 deletions src/models/User.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,58 @@
import mongoose from 'mongoose';
import mongoose from "mongoose";

const timeRegex = /^([01]\d|2[0-3]):([0-5]\d)$/;

const slotSchema = new mongoose.Schema({
startTime: {
type: String,
required: true,
match: timeRegex
const slotSchema = new mongoose.Schema(
{
startTime: {
type: String,
required: true,
match: timeRegex,
},
endTime: {
type: String,
required: true,
match: timeRegex,
},
},
endTime: {
type: String,
required: true,
match: timeRegex
}
}, { _id: false });

const workingDaySchema = new mongoose.Schema({
day: {
type: String,
required: true,
enum: ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
{ _id: false },
);

const workingDaySchema = new mongoose.Schema(
{
day: {
type: String,
required: true,
enum: [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
],
},
isAvailable: {
type: Boolean,
default: true,
},
slots: {
type: [slotSchema],
default: [],
},
},
isAvailable: {
type: Boolean,
default: true
},
slots: {
type: [slotSchema],
default: []
}
}, { _id: false });

workingDaySchema.pre('validate', function() {
{ _id: false },
);

workingDaySchema.pre("validate", function () {
if (!this.isAvailable && this.slots.length > 0) {
return new Error("Slots cannot exist when doctor is unavailable");
}

const sorted = [...this.slots].sort((a, b) =>
a.startTime.localeCompare(b.startTime)
);
const sorted = [...this.slots].sort((a, b) => a.startTime.localeCompare(b.startTime));

for (let i = 0; i < sorted.length; i++) {

if (sorted[i].startTime >= sorted[i].endTime) {
return new Error("Start time must be before end time");
}
Expand All @@ -55,68 +65,80 @@ workingDaySchema.pre('validate', function() {

const DEFAULT_WORKING_HOURS = [
{ day: "Monday", isAvailable: true, slots: [{ startTime: "09:00", endTime: "17:00" }] },
{ day: "Tuesday", isAvailable: true, slots: [{ startTime: "09:00", endTime: "17:00" }] },
{ day: "Wednesday", isAvailable: true, slots: [{ startTime: "09:00", endTime: "17:00" }] },
{ day: "Thursday", isAvailable: true, slots: [{ startTime: "09:00", endTime: "17:00" }] },
{
day: "Tuesday",
isAvailable: true,
slots: [{ startTime: "09:00", endTime: "17:00" }],
},
{
day: "Wednesday",
isAvailable: true,
slots: [{ startTime: "09:00", endTime: "17:00" }],
},
{
day: "Thursday",
isAvailable: true,
slots: [{ startTime: "09:00", endTime: "17:00" }],
},
{ day: "Friday", isAvailable: true, slots: [{ startTime: "09:00", endTime: "17:00" }] },
{ day: "Saturday", isAvailable: false, slots: [] },
{ day: "Sunday", isAvailable: false, slots: [] },
];

const userSchema = new mongoose.Schema({

email: {
type: String,
required: true,
unique: true,
match: /.+@.+\..+/
},

password: {
type: String,
required: true
const userSchema = new mongoose.Schema(
{
email: {
type: String,
required: true,
unique: true,
match: /.+@.+\..+/,
},

password: {
type: String,
required: true,
},

role: {
type: String,
required: true,
enum: ["admin", "doctor", "receptionist", "billing", "patient"],
},

name: { type: String },

phno: {
type: String,
match: /^[0-9]{10}$/,
unique: true,
},

spec: { type: String },

dept: {
type: mongoose.Schema.Types.ObjectId,
ref: "Department",
},

exp: { type: String },
qual: { type: String },

status: {
type: String,
enum: ["Active", "Inactive"],
default: "Active",
},

workingHours: {
type: [workingDaySchema],
},
},
{ timestamps: true },
);

role: {
type: String,
required: true,
enum: ['admin', 'doctor','receptionist','billing', 'patient']
},

name: { type: String },

phno: {
type: String,
match: /^[0-9]{10}$/
},

spec: { type: String },

dept: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Department'
},

exp: { type: String },
qual: { type: String },

status: {
type: String,
enum: ['Active', 'Inactive'],
default: 'Active'
},

workingHours: {
type: [workingDaySchema]
}

}, { timestamps: true });


userSchema.pre('validate', function() {

if (this.role === 'doctor' && this.workingHours) {
const days = this.workingHours.map(d => d.day);
userSchema.pre("validate", function () {
if (this.role === "doctor" && this.workingHours) {
const days = this.workingHours.map((d) => d.day);
const uniqueDays = new Set(days);

if (days.length !== uniqueDays.size) {
Expand All @@ -125,12 +147,11 @@ userSchema.pre('validate', function() {
}
});

userSchema.pre('save', function() {

if (this.role === 'doctor' && (!this.workingHours || this.workingHours.length === 0)) {
userSchema.pre("save", function () {
if (this.role === "doctor" && (!this.workingHours || this.workingHours.length === 0)) {
this.workingHours = DEFAULT_WORKING_HOURS;
}
});

const User = mongoose.models.User || mongoose.model('User', userSchema);
export default User;
const User = mongoose.models.User || mongoose.model("User", userSchema);
export default User;
107 changes: 53 additions & 54 deletions src/models/patient.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
import mongoose from 'mongoose';
import mongoose from "mongoose";

const Patientschema=new mongoose.Schema({
name:{ type:String, required:true },
email:{
type: String,
required:true,
match: /.+@.+\..+/
},
phno:{
type: String,
required: true,
match:/^[0-9]{10}$/
},
age:{
type:Number,
required: true,
min: [0, "Age cannot be negative"],
max: [150, "Age cannot exceed 150"],
validate: {
const Patientschema = new mongoose.Schema(
{
name: { type: String, required: true },
email: {
type: String,
required: true,
match: /.+@.+\..+/,
},
phno: {
type: String,
required: true,
match: /^[0-9]{10}$/,
},
age: {
type: Number,
required: true,
min: [0, "Age cannot be negative"],
max: [150, "Age cannot exceed 150"],
validate: {
validator: Number.isInteger,
message: "Age must be a whole number"
}
},
gender: {
type:String,
enum:['male','female','other'],
required:true},
status:{
type:String,
enum:['active','cancelled'],
default:'active',
required:true
},
paymentMethod: {
type: String,
enum: ['cash', 'online'],
default: 'cash'
message: "Age must be a whole number",
},
},
gender: {
type: String,
enum: ["male", "female", "other"],
required: true,
},
status: {
type: String,
enum: ["active", "cancelled"],
default: "active",
required: true,
},
bg: {
type: String,
enum: ["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"],
},
address: { type: String },
emerno: {
type: String,
match: /^[0-9]{10}$/,
},
medical_history: { type: String },
paymentMode: {
type: String,
enum: ["cash", "online"],
default: "cash",
},
},
bg:{
type: String,
enum:['A+','A-','B+','B-','AB+','AB-','O+','O-']
},
address: {type:String},
emerno:{
type:String,
match:/^[0-9]{10}$/
},
medical_history: {type:String},
paymentMode: {
type: String,
enum: ['cash', 'online'],
default: 'cash'
}
},{ timestamps: true});
{ timestamps: true },
);

const Patient= mongoose.model('Patient', Patientschema);
export default Patient;
const Patient = mongoose.model("Patient", Patientschema);
export default Patient;
Loading
Loading