Skip to content

Commit

Permalink
Switched to src directory
Browse files Browse the repository at this point in the history
Added mongoose
minor import alias fixes
  • Loading branch information
ad956 committed Jul 1, 2024
1 parent aae49eb commit 17beeb8
Show file tree
Hide file tree
Showing 59 changed files with 210 additions and 86 deletions.
84 changes: 77 additions & 7 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"framer-motion": "^11.1.7",
"jose": "^5.2.3",
"mongodb": "^6.5.0",
"mongoose": "^8.4.4",
"next": "14.1.0",
"next-cloudinary": "^6.6.2",
"next-qrcode": "^2.5.1",
Expand All @@ -47,6 +48,7 @@
"@types/bcrypt": "^5.0.2",
"@types/dom-to-image": "^2.6.7",
"@types/js-cookie": "^3.0.6",
"@types/mongoose": "^5.11.97",
"@types/node": "^20",
"@types/nodemailer": "^6.4.14",
"@types/react": "^18",
Expand Down
44 changes: 27 additions & 17 deletions src/app/api/auth/login/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { sendEmail } from "@lib/email";
import { render } from "@react-email/render";
import { generateSecureOTP } from "@utils/generateOtp";
import bcrypt from "bcrypt";
import { Patient } from "@/app/models/";

type LoginBody = {
email: string;
Expand Down Expand Up @@ -37,23 +38,9 @@ export async function POST(req: Request) {
}

async function setOTP(loginBody: LoginBody) {
const db = await dbConfig();
await dbConfig();

const collection = db.collection(loginBody.role);
const email = loginBody.email;
const projection = {
_id: 0,
email: 1,
firstname: 1,
lastname: 1,
password: 1,
};
const user = await collection.findOne(
{ email },
{
projection,
}
);
const user = await getUserModel(loginBody.email, loginBody.role);

if (!user || !(await bcrypt.compare(loginBody.password, user.password))) {
return Response.json(
Expand All @@ -63,7 +50,10 @@ async function setOTP(loginBody: LoginBody) {
}

const generatedOTP = generateSecureOTP();
await collection.updateOne({ email }, { $set: { otp: generatedOTP } });
await user.updateOne(
{ email: loginBody.email },
{ $set: { otp: generatedOTP } }
);

const send = {
to: user.email,
Expand All @@ -85,3 +75,23 @@ async function setOTP(loginBody: LoginBody) {
if (!mailsent) return Response.json({ error: "Email Sending Failed" });
return Response.json({ message: "ok" }, { status: 201 });
}

export async function getUserModel(email: string, role: string) {
const projection = {
_id: 0,
email: 1,
firstname: 1,
lastname: 1,
password: 1,
};
switch (role) {
case "patient":
return await Patient.findOne({ email }, projection);
// case "hospital":
// return await Hospital.findOne({ email }, projection);
case "receptionist":
return await Receptionist.findOne({ email }, projection);
default:
return null;
}
}
27 changes: 24 additions & 3 deletions src/app/api/auth/signup/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dbConfig from "@lib/db";
import { OtpTemplate } from "@/lib/emails/templates";
import { sendEmail } from "@lib/email";
import dbConfig from "@/app/lib/db";
import { OtpTemplate } from "@/app/lib/emails/templates";
import { sendEmail } from "@/app/lib/email";
import { render } from "@react-email/render";
import { generateSecureOTP } from "@utils/generateOtp";
import {
Expand All @@ -10,6 +10,7 @@ import {
receptionistadditionalDetails,
} from "@constants/index";
import bcrypt from "bcrypt";
import { Patient } from "@/app/models/Patient";

type SignupBody = {
firstname: string;
Expand Down Expand Up @@ -145,3 +146,23 @@ async function hashPassword(password: string) {
const hashedPassword = await bcrypt.hash(password, saltRounds);
return hashedPassword;
}

export async function getUserModel(email: string, role: string) {
const projection = {
_id: 0,
email: 1,
firstname: 1,
lastname: 1,
password: 1,
};
switch (role) {
case "patient":
return await Patient.findOne({ email }, projection);
case "hospital":
return await Hospital.findOne({ email }, projection);
case "receptionist":
return await Receptionist.findOne({ email }, projection);
default:
return null;
}
}
6 changes: 3 additions & 3 deletions src/app/api/auth/verifyotp/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setSession } from "@sessions/sessionUtils";
import dbConfig from "@lib/db";
import logUserActivity from "@lib/logs";
import { setSession } from "@/app/lib/sessions/sessionUtils";
import dbConfig from "@/app/lib/db";
import logUserActivity from "@/app/lib/logs";

type bodyType = {
email: string;
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/city/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dbConfig from "@lib/db";
import dbConfig from "@/app/lib/db";

export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/doctor/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dbConfig from "@lib/db";
import dbConfig from "@/app/lib/db";

export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/gethospitals/disease/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dbConfig from "@lib/db";
import dbConfig from "@/app/lib/db";

export async function GET() {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/gethospitals/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dbConfig from "@lib/db";
import dbConfig from "@/app/lib/db";

export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/patient/appointment/pending/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dbConfig from "@lib/db";
import dbConfig from "@/app/lib/db";
import { ObjectId } from "mongodb";
import { decrypt } from "@sessions/sessionUtils";
import { decrypt } from "@/app/lib/sessions/sessionUtils";

export async function POST(req: Request) {
const session = req.headers.get("Authorization");
Expand Down
10 changes: 5 additions & 5 deletions src/app/api/patient/appointment/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import dbConfig from "@lib/db";
import dbConfig from "@/app/lib/db";
import { bookingAppointment } from "@/types";
import { decrypt } from "@sessions/sessionUtils";
import { decrypt } from "@/app/lib/sessions/sessionUtils";
import { ObjectId } from "mongodb";
import { sendEmail } from "@lib/email";
import { sendEmail } from "@/app/lib/email";
import { render } from "@react-email/render";
import { AppointmentBookedTemplate } from "@lib/emails/templates";
import { AppointmentBookedTemplate } from "@/app/lib/emails/templates";
import { getFormattedDate } from "@/app/utils/getDate";
import sendNotification from "@lib/novu";
import sendNotification from "@/app/lib/novu";

type BookingAppointmentType = bookingAppointment & {
transaction_id: string | null;
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/patient/medicalhistory/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dbConfig from "@lib/db";
import { decrypt } from "@sessions/sessionUtils";
import dbConfig from "@/app/lib/db";
import { decrypt } from "@/app/lib/sessions/sessionUtils";

export async function GET(request: Request) {
const session = request.headers.get("Authorization");
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/patient/notifications/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dbConfig from "@lib/db";
import { decrypt } from "@sessions/sessionUtils";
import dbConfig from "@/app/lib/db";
import { decrypt } from "@/app/lib/sessions/sessionUtils";

export async function GET(request: Request) {
const session = request.headers.get("Authorization");
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/patient/paymenthistory/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObjectId } from "mongodb";
import dbConfig from "@lib/db";
import { decrypt } from "@sessions/sessionUtils";
import dbConfig from "@/app/lib/db";
import { decrypt } from "@/app/lib/sessions/sessionUtils";

interface Transaction {
_id: ObjectId;
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/patient/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dbConfig from "@lib/db";
import { decrypt } from "@sessions/sessionUtils";
import dbConfig from "@/app/lib/db";
import { decrypt } from "@/app/lib/sessions/sessionUtils";

export async function GET(request: Request) {
const session = request.headers.get("Authorization");
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/patient/update-profile/profile/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dbConfig from "@lib/db";
import { decrypt } from "@sessions/sessionUtils";
import dbConfig from "@/app/lib/db";
import { decrypt } from "@/app/lib/sessions/sessionUtils";

export async function PUT(request: Request) {
// const session = request.headers.get("Authorization");
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/patient/update-profile/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { User } from "@/types";
import dbConfig from "@lib/db";
import { decrypt } from "@sessions/sessionUtils";
import dbConfig from "@/app/lib/db";
import { decrypt } from "@/app/lib/sessions/sessionUtils";

type UpdatedUserType = Omit<User, "_id">;

Expand Down
2 changes: 1 addition & 1 deletion src/app/api/payment/create-order/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest } from "next/server";
import { razorpay } from "@lib/razorpay";
import { razorpay } from "@/app/lib/razorpay";

export async function POST(request: NextRequest) {
const { amount, currency } = (await request.json()) as {
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/receptionist/appointments/approve/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dbConfig from "@lib/db";
import { decrypt } from "@sessions/sessionUtils";
import dbConfig from "@/app/lib/db";
import { decrypt } from "@/app/lib/sessions/sessionUtils";
import { ObjectId } from "mongodb";

export async function GET(request: Request) {
Expand Down
Loading

0 comments on commit 17beeb8

Please sign in to comment.