feat: add pagination in appointment
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import prisma from "../prisma/client.js";
|
||||
import {sendEmail} from "../utils/sendEmail.js";
|
||||
import {getEmailsByType} from "../utils/getEmailByTypes.js";
|
||||
import { sendEmail } from "../utils/sendEmail.js";
|
||||
import { getEmailsByType } from "../utils/getEmailByTypes.js";
|
||||
|
||||
export const createAppointment = async (req, res) => {
|
||||
try {
|
||||
const {name, mobileNumber, email, message, date, doctorId, departmentId} =
|
||||
const { name, mobileNumber, email, message, date, doctorId, departmentId } =
|
||||
req.body;
|
||||
|
||||
if (!name || !mobileNumber || !doctorId || !departmentId || !date) {
|
||||
@@ -71,19 +71,98 @@ export const createAppointment = async (req, res) => {
|
||||
|
||||
export const getAppointments = async (req, res) => {
|
||||
try {
|
||||
const appointments = await prisma.appointment.findMany({
|
||||
include: {
|
||||
doctor: true,
|
||||
department: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
});
|
||||
const page = parseInt(req.query.page);
|
||||
const limit = parseInt(req.query.limit);
|
||||
|
||||
res.status(200).json({
|
||||
const search = req.query.search || "";
|
||||
const doctor = req.query.doctor || "";
|
||||
const department = req.query.department || "";
|
||||
const date = req.query.date || "";
|
||||
|
||||
if (!page && !limit) {
|
||||
const appointments = await prisma.appointment.findMany({
|
||||
include: {
|
||||
doctor: true,
|
||||
department: true,
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
data: appointments,
|
||||
meta: null,
|
||||
});
|
||||
}
|
||||
|
||||
const currentPage = page || 1;
|
||||
const currentLimit = limit || 10;
|
||||
const skip = (currentPage - 1) * currentLimit;
|
||||
|
||||
const where = {
|
||||
AND: [
|
||||
search
|
||||
? {
|
||||
OR: [
|
||||
{ name: { contains: search, mode: "insensitive" } },
|
||||
{ mobileNumber: { contains: search } },
|
||||
{ email: { contains: search, mode: "insensitive" } },
|
||||
],
|
||||
}
|
||||
: {},
|
||||
|
||||
doctor
|
||||
? {
|
||||
doctor: {
|
||||
name: { contains: doctor, mode: "insensitive" },
|
||||
},
|
||||
}
|
||||
: {},
|
||||
|
||||
department
|
||||
? {
|
||||
department: {
|
||||
name: { contains: department, mode: "insensitive" },
|
||||
},
|
||||
}
|
||||
: {},
|
||||
|
||||
date
|
||||
? {
|
||||
date: {
|
||||
gte: new Date(date),
|
||||
lt: new Date(
|
||||
new Date(date).setDate(new Date(date).getDate() + 1),
|
||||
),
|
||||
},
|
||||
}
|
||||
: {},
|
||||
],
|
||||
};
|
||||
|
||||
const [appointments, total] = await Promise.all([
|
||||
prisma.appointment.findMany({
|
||||
where,
|
||||
include: {
|
||||
doctor: true,
|
||||
department: true,
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
skip,
|
||||
take: currentLimit,
|
||||
}),
|
||||
prisma.appointment.count({ where }),
|
||||
]);
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
data: appointments,
|
||||
meta: {
|
||||
total,
|
||||
page: currentPage,
|
||||
limit: currentLimit,
|
||||
totalPages: Math.ceil(total / currentLimit),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
@@ -98,7 +177,7 @@ export const getAppointments = async (req, res) => {
|
||||
|
||||
export const getAppointment = async (req, res) => {
|
||||
try {
|
||||
const {id} = req.params;
|
||||
const { id } = req.params;
|
||||
|
||||
const appointment = await prisma.appointment.findUnique({
|
||||
where: {
|
||||
@@ -134,7 +213,7 @@ export const getAppointment = async (req, res) => {
|
||||
|
||||
export const getAppointmentsByDoctor = async (req, res) => {
|
||||
try {
|
||||
const {doctorId} = req.params;
|
||||
const { doctorId } = req.params;
|
||||
|
||||
const appointments = await prisma.appointment.findMany({
|
||||
where: {
|
||||
@@ -166,7 +245,7 @@ export const getAppointmentsByDoctor = async (req, res) => {
|
||||
|
||||
export const getAppointmentsByDepartment = async (req, res) => {
|
||||
try {
|
||||
const {departmentId} = req.params;
|
||||
const { departmentId } = req.params;
|
||||
|
||||
const appointments = await prisma.appointment.findMany({
|
||||
where: {
|
||||
@@ -195,7 +274,7 @@ export const getAppointmentsByDepartment = async (req, res) => {
|
||||
|
||||
export const updateAppointment = async (req, res) => {
|
||||
try {
|
||||
const {id} = req.params;
|
||||
const { id } = req.params;
|
||||
|
||||
const appointment = await prisma.appointment.update({
|
||||
where: {
|
||||
@@ -226,7 +305,7 @@ export const updateAppointment = async (req, res) => {
|
||||
|
||||
export const deleteAppointment = async (req, res) => {
|
||||
try {
|
||||
const {id} = req.params;
|
||||
const { id } = req.params;
|
||||
|
||||
await prisma.appointment.delete({
|
||||
where: {
|
||||
|
||||
Reference in New Issue
Block a user