Skip to content

Commit 3284c01

Browse files
Merge branch 'main' into dev-rohit
2 parents 6ebeca5 + 7e4c9b0 commit 3284c01

6 files changed

Lines changed: 74 additions & 47 deletions

File tree

src/config/db.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import mongoose from 'mongoose';
33
const connectDB = async () => {
44
try {
55
await mongoose.connect(process.env.MONGO_URI);
6-
console.log('MongoDB connected');
76
} catch (error) {
87
console.error('DB connection failed', error);
98
process.exit(1);

src/controllers/patient.js

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,24 @@
11
import Patient from '../models/patient.js';
2-
import User from '../models/User.js';
3-
import bcrypt from 'bcrypt';
4-
import * as emailService from '../services/email.service.js';
2+
import { createPatientPortalAccount } from '../services/patientPortal.service.js';
53

64
const createPatient = async (req, res, next) => {
75
try {
86
const { name, email, phno, paymentMode } = req.body;
7+
98
console.log(`Creating patient: ${name}, Email: ${email}, PaymentMode: ${paymentMode}`);
109

11-
// Create the patient record
1210
const patient = new Patient(req.body);
1311
await patient.save();
1412

15-
// If online payment, create a patient portal account
16-
if (paymentMode === 'online') {
17-
console.log(`Online payment mode detected for ${email}. Processing portal creation...`);
18-
const existingUser = await User.findOne({ email });
19-
if (!existingUser) {
20-
// Use phone number as default password
21-
const password = phno; // Plain text for the email
22-
const hashedPassword = await bcrypt.hash(password, 10);
23-
24-
await User.create({
25-
name,
26-
email,
27-
password: hashedPassword,
28-
role: 'patient',
29-
status: 'Active',
30-
phno
31-
});
32-
console.log(`Patient portal User record created for: ${email}`);
33-
34-
// Send credentials email
35-
try {
36-
await emailService.sendPortalCredentials(email, name, password);
37-
console.log(`Portal credentials email sent to: ${email}`);
38-
} catch (mailErr) {
39-
console.error(`Failed to send portal credentials email: ${mailErr.message}`);
40-
}
41-
} else {
42-
console.log(`User account already exists for: ${email}. Skipping portal creation.`);
43-
}
44-
} else {
45-
console.log(`Payment mode is '${paymentMode}'. Skipping portal creation.`);
13+
if (paymentMode === "online" && email) {
14+
await createPatientPortalAccount(email);
4615
}
4716

48-
res.status(201).json(patient);
17+
res.status(201).json({
18+
success: true,
19+
patient
20+
});
21+
4922
} catch (err) {
5023
console.error(`Error in createPatient: ${err.message}`);
5124
next(err);

src/models/patient.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ status:{
3232
default:'active',
3333
required:true
3434
},
35+
paymentMethod: {
36+
type: String,
37+
enum: ['cash', 'online'],
38+
default: 'cash'
39+
},
3540
bg:{
3641
type: String,
3742
enum:['A+','A-','B+','B-','AB+','AB-','O+','O-']

src/services/email.service.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,30 @@ export const sendOTPEmail = async (email, otp) => {
6262
});
6363
};
6464

65-
export const sendPortalCredentials = async (email, name, password) => {
65+
export const sendCredentialsEmail = async (email, password) => {
6666
await transporter.sendMail({
6767
from: `"HMS Team" <${process.env.GMAIL_EMAIL}>`,
6868
to: email,
69-
subject: 'Your HMS Patient Portal Account Details',
69+
subject: "Your HMS Patient Portal Login Credentials",
70+
7071
html: `
71-
<div style="font-family: Arial, sans-serif; padding: 20px;">
72-
<h2>Welcome to HMS, ${name}!</h2>
73-
<p>Your patient portal account has been created successfully.</p>
74-
<p><strong>Username/Email:</strong> ${email}</p>
75-
<p><strong>Temporary Password:</strong> ${password}</p>
76-
<p style="color: #555;">Please log in to the portal and change your password for security.</p>
77-
<hr />
78-
<p style="font-size: 12px; color: #888;">© ${new Date().getFullYear()} HMS Team.</p>
72+
<div style="font-family: Arial; padding:20px;">
73+
<h2>Welcome to HMS Portal</h2>
74+
75+
<p>Your account has been created successfully.</p>
76+
77+
<p><b>Email:</b> ${email}</p>
78+
<p><b>Password:</b> ${password}</p>
79+
80+
<p>Please login and change your password immediately.</p>
81+
82+
<p>Login Link: <a href="https://hms-app/login">Login</a></p>
83+
84+
<hr/>
85+
86+
<p style="font-size:12px;color:#888;">
87+
This email was generated automatically.
88+
</p>
7989
</div>
8090
`
8191
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import bcrypt from 'bcrypt';
2+
import User from '../models/User.js';
3+
import { generateRandomPassword } from '../utils/password.utils.js';
4+
import { sendCredentialsEmail } from './email.service.js';
5+
6+
export const createPatientPortalAccount = async (email) => {
7+
8+
try {const existingUser = await User.findOne({ email });
9+
10+
if (existingUser) {
11+
return;
12+
}
13+
14+
const rawPassword = generateRandomPassword(10);
15+
16+
const hashedPassword = await bcrypt.hash(rawPassword, 10);
17+
18+
const user = new User({
19+
email,
20+
password: hashedPassword,
21+
role: "patient",
22+
status: "Active",
23+
mustChangePassword: true
24+
});
25+
26+
await user.save();
27+
28+
await sendCredentialsEmail(email, rawPassword);
29+
} catch (err) {
30+
console.error("Patient pirtal account creation failed:", err);
31+
}
32+
};

src/utils/password.utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import crypto from 'crypto';
2+
3+
export const generateRandomPassword = (length = 10) => {
4+
return crypto
5+
.randomBytes(length)
6+
.toString('base64')
7+
.slice(0, length);
8+
};

0 commit comments

Comments
 (0)