-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68d53cc
commit 28dfd69
Showing
3 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
api/src/app/modules/doctorTimeSlot/doctorTimeSlot.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Request, Response } from "express"; | ||
import catchAsync from "../../../shared/catchAsync"; | ||
import sendResponse from "../../../shared/sendResponse"; | ||
import { TimeSlotService } from "./doctorTimeSlot.service"; | ||
import { GardenerTimeSlot } from "@prisma/client"; | ||
|
||
const createTimeSlot = catchAsync(async (req: Request, res: Response) => { | ||
const result = await TimeSlotService.createTimeSlot(req.user, req.body); | ||
sendResponse<GardenerTimeSlot>(res, { | ||
statusCode: 200, | ||
message: 'Successfully created Time Slot !!', | ||
success: true, | ||
data: result | ||
}) | ||
}) | ||
|
||
const getAllTimeSlot = catchAsync(async (req: Request, res: Response) => { | ||
const result = await TimeSlotService.getAllTimeSlot(); | ||
sendResponse<GardenerTimeSlot[]>(res, { | ||
statusCode: 200, | ||
message: 'Successfully get all Time Slot !!', | ||
success: true, | ||
data: result | ||
}) | ||
}) | ||
|
||
const getMyTimeSlot = catchAsync(async (req: Request, res: Response) => { | ||
const result = await TimeSlotService.getMyTimeSlot(req.user, req.query); | ||
sendResponse<GardenerTimeSlot[]>(res, { | ||
statusCode: 200, | ||
message: 'Successfully get all Time Slot !!', | ||
success: true, | ||
data: result | ||
}) | ||
}) | ||
|
||
const getTimeSlot = catchAsync(async (req: Request, res: Response) => { | ||
const result = await TimeSlotService.getTimeSlot(req.params.id); | ||
sendResponse<GardenerTimeSlot>(res, { | ||
statusCode: 200, | ||
message: 'Successfully get Time Slot !!', | ||
success: true, | ||
data: result | ||
}) | ||
}) | ||
|
||
const updateTimeSlot = catchAsync(async (req: Request, res: Response) => { | ||
await TimeSlotService.updateTimeSlot(req.user, req.params.id, req.body); | ||
sendResponse<GardenerTimeSlot>(res, { | ||
statusCode: 200, | ||
message: 'Successfully updated Time Slot !!', | ||
success: true, | ||
}) | ||
}) | ||
|
||
const deleteTimeSlot = catchAsync(async (req: Request, res: Response) => { | ||
const result = await TimeSlotService.deleteTimeSlot(req.params.id); | ||
sendResponse<GardenerTimeSlot>(res, { | ||
statusCode: 200, | ||
message: 'Successfully deleted Time Slot !!', | ||
success: true, | ||
data: result | ||
}) | ||
}) | ||
const getAppointmentTimeOfEachDoctor = catchAsync(async (req: Request, res: Response) => { | ||
const result = await TimeSlotService.getAppointmentTimeOfEachDoctor(req.params.id, req.query); | ||
sendResponse<GardenerTimeSlot>(res, { | ||
statusCode: 200, | ||
message: 'Successfully deleted Time Slot !!', | ||
success: true, | ||
data: result | ||
}) | ||
}) | ||
|
||
|
||
export const doctorTimeSlotController = { | ||
getAllTimeSlot, | ||
getTimeSlot, | ||
updateTimeSlot, | ||
createTimeSlot, | ||
deleteTimeSlot, | ||
getMyTimeSlot, | ||
getAppointmentTimeOfEachDoctor | ||
} |
16 changes: 16 additions & 0 deletions
16
api/src/app/modules/doctorTimeSlot/doctorTimeSlot.route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import express from 'express'; | ||
import { auth } from '../../middlewares/auth'; | ||
import { AuthUser } from '../../../enums'; | ||
import { doctorTimeSlotController } from './doctorTimeSlot.controller'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/my-slot', auth(AuthUser.GARDENER), doctorTimeSlotController.getMyTimeSlot); | ||
router.get('/:id', auth(AuthUser.GARDENER), doctorTimeSlotController.getTimeSlot); | ||
router.get('/appointment-time/:id', doctorTimeSlotController.getAppointmentTimeOfEachDoctor); | ||
router.post('/create', auth(AuthUser.GARDENER), doctorTimeSlotController.createTimeSlot); | ||
router.get('/', doctorTimeSlotController.getAllTimeSlot); | ||
router.patch('/', auth(AuthUser.GARDENER), doctorTimeSlotController.updateTimeSlot); | ||
router.delete('/', auth(AuthUser.GARDENER), doctorTimeSlotController.deleteTimeSlot); | ||
|
||
export const DoctorTimeSlotRouter = router; |
235 changes: 235 additions & 0 deletions
235
api/src/app/modules/doctorTimeSlot/doctorTimeSlot.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
import httpStatus from "http-status"; | ||
import ApiError from "../../../errors/apiError"; | ||
import prisma from "../../../shared/prisma"; | ||
import { GardenerTimeSlot, ScheduleDay } from "@prisma/client"; | ||
import moment from "moment"; | ||
|
||
const createTimeSlot = async (user: any, payload: any): Promise<GardenerTimeSlot | null> => { | ||
const { userId } = user; | ||
const isDoctor = await prisma.gardener.findUnique({ | ||
where: { | ||
id: userId | ||
} | ||
}) | ||
if (!isDoctor) { | ||
throw new ApiError(httpStatus.NOT_FOUND, 'Gardener Account is not found !!') | ||
} | ||
|
||
const result = await prisma.$transaction(async (tx) => { | ||
const isAlreadyExist = await tx.gardenerTimeSlot.findFirst({ | ||
where:{ | ||
gardenerId: isDoctor.id, | ||
day: payload.day | ||
} | ||
}) | ||
if(isAlreadyExist){ | ||
throw new ApiError(404, 'Time Slot Already Exist Please update or try another day') | ||
} | ||
|
||
const createTimeSlot = await tx.gardenerTimeSlot.create({ | ||
data: { | ||
day: payload.day, | ||
gardenerId: isDoctor.id, | ||
maximumPatient: payload.maximumPatient, | ||
weekDay: payload.weekDay, | ||
timeSlot: { | ||
create: payload.timeSlot.map((item: any) => ({ | ||
startTime: item.startTime, | ||
endTime: item.endTime | ||
})) | ||
} | ||
} | ||
}); | ||
|
||
return createTimeSlot; | ||
}) | ||
// const tx = await prisma.$transaction(async() =>()) | ||
|
||
// const result = await prisma.gardenerTimeSlot.create({ | ||
// data: { | ||
// day: payload.day, | ||
// gardenerId: isDoctor.id, | ||
// maximumPatient: payload.maximumPatient, | ||
// weekDay: payload.weekDay, | ||
// timeSlot: { | ||
// create: payload.timeSlot.map((item: any) => ({ | ||
// startTime: item.startTime, | ||
// endTime: item.endTime | ||
// })) | ||
// } | ||
// } | ||
// }) | ||
return result; | ||
} | ||
|
||
const deleteTimeSlot = async (id: string): Promise<GardenerTimeSlot | null> => { | ||
const result = await prisma.gardenerTimeSlot.delete({ | ||
where: { | ||
id: id | ||
} | ||
}) | ||
return result; | ||
} | ||
|
||
const getTimeSlot = async (id: string): Promise<GardenerTimeSlot | null> => { | ||
const result = await prisma.gardenerTimeSlot.findFirst({ | ||
where: { | ||
id: id | ||
} | ||
}) | ||
return result; | ||
} | ||
|
||
const getMyTimeSlot = async (user: any, filter: any): Promise<GardenerTimeSlot[] | null> => { | ||
const { userId } = user; | ||
const isDoctor = await prisma.gardener.findUnique({ | ||
where: { | ||
id: userId | ||
} | ||
}) | ||
if (!isDoctor) { | ||
throw new ApiError(httpStatus.NOT_FOUND, 'Gardener Account is not found !!') | ||
} | ||
let andCondition: any = { gardenerId: isDoctor.id }; | ||
if (filter.day) { | ||
andCondition.day = filter.day | ||
} | ||
|
||
const whereCondition = andCondition ? andCondition : {} | ||
const result = await prisma.gardenerTimeSlot.findMany({ | ||
where: whereCondition, | ||
include: { | ||
timeSlot: true | ||
} | ||
}) | ||
return result; | ||
} | ||
|
||
const getAllTimeSlot = async (): Promise<GardenerTimeSlot[] | null> => { | ||
const result = await prisma.gardenerTimeSlot.findMany({ | ||
include: { | ||
timeSlot: true, | ||
gardener: { | ||
select: { | ||
firstName: true, | ||
lastName: true | ||
} | ||
} | ||
} | ||
}) | ||
return result; | ||
} | ||
const updateTimeSlot = async (user: any, id: string, payload: any): Promise<{ message: string }> => { | ||
const { userId } = user; | ||
const isDoctor = await prisma.gardener.findUnique({ | ||
where: { | ||
id: userId | ||
} | ||
}) | ||
if (!isDoctor) { | ||
throw new ApiError(httpStatus.NOT_FOUND, 'Gardener Account is not found !!') | ||
} | ||
const { timeSlot, create } = payload; | ||
|
||
if (create && create.length > 0) { | ||
const gardenerTimeSlot = await prisma.gardenerTimeSlot.findFirst({ | ||
where: { | ||
day: create[0].day | ||
} | ||
}) | ||
if (!gardenerTimeSlot) { | ||
throw new ApiError(httpStatus.NOT_FOUND, 'Time Slot is not found !!') | ||
} | ||
await Promise.all(create.map(async (item: ScheduleDay) => { | ||
try { | ||
await prisma.scheduleDay.create({ | ||
data: { | ||
startTime: item.startTime, | ||
endTime: item.endTime, | ||
gardenerTimeSlotId: gardenerTimeSlot?.id | ||
} | ||
}) | ||
} catch (error) { | ||
throw new ApiError(httpStatus.EXPECTATION_FAILED, 'Failed to create') | ||
} | ||
})) | ||
} | ||
|
||
if (timeSlot && timeSlot.length > 0) { | ||
await Promise.all(timeSlot.map(async (item: ScheduleDay) => { | ||
const { gardenerTimeSlotId, ...others } = item; | ||
try { | ||
await prisma.scheduleDay.updateMany({ | ||
where: { id: others.id }, | ||
data: { | ||
startTime: others.startTime, | ||
endTime: others.endTime | ||
} | ||
}) | ||
} catch (error) { | ||
throw new ApiError(httpStatus.EXPECTATION_FAILED, 'Failed to Update') | ||
} | ||
})) | ||
} | ||
return { | ||
message: 'Successfully Updated' | ||
} | ||
} | ||
|
||
const getAppointmentTimeOfEachDoctor = async (id: string, filter: any): Promise<any> => { | ||
const doctorTimSlot = await prisma.gardenerTimeSlot.findMany({ | ||
where: { | ||
gardenerId: id | ||
}, | ||
include: { | ||
timeSlot: true | ||
}, | ||
}) | ||
|
||
const allSlots = doctorTimSlot.map((item) => { | ||
const { day, timeSlot, ...others } = item; | ||
return { day, timeSlot } | ||
}) | ||
|
||
const generateTimeSlot = (timeSlot: any) => { | ||
const selectedTime: any[] = []; | ||
timeSlot.forEach((item: any) => { | ||
const interval = 30; | ||
const newTimeSlots: any[] = []; | ||
const day: string = item?.day; | ||
|
||
item?.timeSlot.map((slot: ScheduleDay) => { | ||
|
||
const { startTime, endTime } = slot; | ||
const startDate = moment(startTime, 'hh:mm a'); | ||
const endDate = moment(endTime, 'hh:mm a'); | ||
|
||
while (startDate < endDate) { | ||
const selectableTime = { | ||
id: newTimeSlots.length + 1, | ||
time: startDate.format('hh:mm a') | ||
} | ||
newTimeSlots.push({ day: day, slot: selectableTime }); | ||
startDate.add(interval, 'minutes'); | ||
} | ||
}) | ||
if (filter.day) { | ||
const newTime = newTimeSlots.filter((item) => item.day === filter.day); | ||
selectedTime.push(newTime); | ||
} | ||
}) | ||
return selectedTime.flat(); | ||
} | ||
const result = generateTimeSlot(allSlots) | ||
return result | ||
} | ||
|
||
export const TimeSlotService = { | ||
updateTimeSlot, | ||
getAllTimeSlot, | ||
getTimeSlot, | ||
createTimeSlot, | ||
deleteTimeSlot, | ||
getMyTimeSlot, | ||
getAppointmentTimeOfEachDoctor | ||
} |