Skip to content

Commit

Permalink
implemented change password in backend and frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
zahra-saadawy committed Nov 12, 2023
2 parents f8340e1 + 3f0c0e2 commit fabea1a
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 109 deletions.
17 changes: 12 additions & 5 deletions backend/src/app/controllers/appointment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PatientModel } from '../models/patient.model'
import { DoctorModel } from '../models/doctor.model'
import { type HydratedDocument } from 'mongoose'
import { type UserDocument, UserModel } from '../models/user.model'
import { getApprovedDoctorById } from '../services/doctor.service'

export const appointmentsRouter = Router()

Expand All @@ -34,22 +35,28 @@ appointmentsRouter.get(
}

const filterAppointments = await getfilteredAppointments(query)
res.send(
new GetFilteredAppointmentsResponse(
filterAppointments.map((appointment) => ({
const appointmentResponses = await Promise.all(
filterAppointments.map(async (appointment) => {
const doctor = await getApprovedDoctorById(
appointment.doctorID.toString()
)

return {
id: appointment.id,
patientID: appointment.patientID.toString(),
doctorID: appointment.doctorID.toString(),
doctorName: doctor.name,
date: appointment.date,
familyID: appointment.familyID || '',
reservedFor: appointment.reservedFor || 'Me',
status:
new Date(appointment.date) > new Date()
? AppointmentStatus.Upcoming
: AppointmentStatus.Completed,
}))
)
}
})
)
res.send(new GetFilteredAppointmentsResponse(appointmentResponses))
})
)

Expand Down
44 changes: 19 additions & 25 deletions backend/src/app/controllers/healthPackage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,11 @@ healthPackagesRouter.post(
'/unsubscribe',
[allowAuthenticated, asyncWrapper(allowPatients)],
asyncWrapper(async (req, res) => {
const patient = await getPatientByUsername(req.username!)
if (!patient) throw new NotFoundError()
await unSubscribeToHealthPackage({
id: patient.id,
})
console.log(patient.healthPackageHistory)
res.status(200).send()
})
)
const { subscriberId, isFamilyMember } = req.body

healthPackagesRouter.post(
'/:id/unsubscribe',
[allowAuthenticated, asyncWrapper(allowPatients)],
asyncWrapper(async (req, res) => {
await unSubscribeToHealthPackage({
id: req.params.id,
id: subscriberId,
isFamilyMember,
})
res.status(200).send()
})
Expand Down Expand Up @@ -257,10 +246,17 @@ healthPackagesRouter.post(
healthPackagesRouter.post(
'/patient-cancelled',
asyncWrapper(async (req, res) => {
const patient = await getPatientByUsername(req.username!)
const { id, isFamilyMember } = req.body
const model = isFamilyMember
? await FamilyMemberModel.findById(id)
: await PatientModel.findById(id)

if (!model) {
throw new NotFoundError()
}

const cancelled: Types.ObjectId[] = []
if (!patient) throw new NotFoundError()
patient.healthPackageHistory.forEach((healthPackage) => {
model.healthPackageHistory.forEach((healthPackage) => {
cancelled.push(healthPackage.healthPackage)
})
res.send(cancelled)
Expand All @@ -270,17 +266,15 @@ healthPackagesRouter.post(
healthPackagesRouter.post(
'/cancellation-date/:healthPackageId',
asyncWrapper(async (req, res) => {
if (!req.username) throw new NotFoundError()
const patient = await getPatientByUsername(req.username)
if (!patient) throw new NotFoundError()
console.log('looking for' + req.params.healthPackageId)
patient.healthPackageHistory.forEach((healthPackage) => {
console.log('history' + healthPackage.healthPackage.toString())

const { id, isFamilyMember } = req.body
const model = isFamilyMember
? await FamilyMemberModel.findById(id)
: await PatientModel.findById(id)
if (!model) throw new NotFoundError()
model.healthPackageHistory.forEach((healthPackage) => {
if (
healthPackage.healthPackage.toString() === req.params.healthPackageId
) {
console.log(healthPackage.date.toDateString())
res.send(healthPackage.date.toDateString())
}
})
Expand Down
37 changes: 25 additions & 12 deletions backend/src/app/controllers/patient.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ import {
type Relation,
} from 'clinic-common/types/familyMember.types'
import {
AppointmentResponseBase,
AppointmentStatus,
GetFilteredAppointmentsResponse,
} from 'clinic-common/types/appointment.types'
import { getApprovedDoctorById } from '../services/doctor.service'
import { changePassowrd } from '../services/changePassword'
import { SUCCESS } from '../utils/httpStatusText'
import AppError from '../utils/appError'
Expand Down Expand Up @@ -298,21 +299,33 @@ patientRouter.get(

const { patient, appointments, prescriptions } = await getPatientByID(id)

const filteredAppointments = appointments
.filter((appointment) => appointment.doctorID.toString() === doctor?.id)
.map((appointment) => {
return new AppointmentResponseBase(
appointment.id,
appointment.patientID.toString(),
appointment.doctorID.toString(),
appointment.date,
appointment.familyID,
appointment.reservedFor
const filteredAppointments = appointments.filter(
(appointment) => appointment.doctorID.toString() === doctor?.id
)
const appointmentResponses = await Promise.all(
filteredAppointments.map(async (appointment) => {
const doctor = await getApprovedDoctorById(
appointment.doctorID.toString()
)

return {
id: appointment.id,
patientID: appointment.patientID.toString(),
doctorID: appointment.doctorID.toString(),
doctorName: doctor.name,
date: appointment.date,
familyID: appointment.familyID || '',
reservedFor: appointment.reservedFor || 'Me',
status:
new Date(appointment.date) > new Date()
? AppointmentStatus.Upcoming
: AppointmentStatus.Completed,
}
})
)

const appointmentsRefactored = new GetFilteredAppointmentsResponse(
filteredAppointments
appointmentResponses
)
res.send(
new GetAPatientResponse(
Expand Down
60 changes: 25 additions & 35 deletions backend/src/app/services/patient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,69 +201,59 @@ export async function subscribeToHealthPackage({
throw new NotFoundError()
}

if (model.healthPackage)
await unSubscribeToHealthPackage({ id: patientId, isFamilyMember })

model.healthPackage = healthPackage.id

// Set renewal date to 1 year from now
const renewalDate = new Date()
renewalDate.setFullYear(renewalDate.getFullYear() + 1)
model.healthPackageRenewalDate = renewalDate
const historyIndex = model.healthPackageHistory.findIndex(
(entry) => entry.healthPackage.toString() === healthPackage.id
)

await model.save() //removed console.log
if (historyIndex !== -1) {
model.healthPackageHistory.splice(historyIndex, 1)
}

await model.save()
}

export async function unSubscribeToHealthPackage(params: {
id: string
isFamilyMember: boolean
}): Promise<void> {
const patient = await PatientModel.findById(params.id)
const familyMember = await FamilyMemberModel.findById(params.id)
const model = params.isFamilyMember
? await FamilyMemberModel.findById(params.id)
: await PatientModel.findById(params.id)

if (!patient && !familyMember) {
if (!model) {
throw new NotFoundError()
}

if (patient && patient.healthPackage) {
if (model && model.healthPackage) {
// Check if there is existing history with the same healthPackage
const existingItemIndex = patient.healthPackageHistory.findIndex(
(item) =>
item.healthPackage.toString() === patient.healthPackage?.toString()
)

if (existingItemIndex !== -1) {
// Update the date attribute of the existing item
patient.healthPackageHistory[existingItemIndex].date = new Date()
} else {
// If no existing item, push a new item
patient.healthPackageHistory.push({
healthPackage: patient.healthPackage,
date: new Date(),
})
}

patient.healthPackage = undefined
patient.healthPackageRenewalDate = undefined
await patient.save()
}

if (familyMember && familyMember.healthPackage) {
const existingItemIndex = familyMember.healthPackageHistory.findIndex(
const existingItemIndex = model.healthPackageHistory.findIndex(
(item) =>
item.healthPackage.toString() === familyMember.healthPackage?.toString()
item.healthPackage.toString() === model.healthPackage?.toString()
)

if (existingItemIndex !== -1) {
// Update the date attribute of the existing item
familyMember.healthPackageHistory[existingItemIndex].date = new Date()
model.healthPackageHistory[existingItemIndex].date = new Date()
} else {
// If no existing item, push a new item
familyMember.healthPackageHistory.push({
healthPackage: familyMember.healthPackage,
model.healthPackageHistory.push({
healthPackage: model.healthPackage,
date: new Date(),
})
}

familyMember.healthPackage = undefined
familyMember.healthPackageRenewalDate = undefined
await familyMember.save()
model.healthPackage = undefined
model.healthPackageRenewalDate = undefined
await model.save()
}
}

Expand Down
1 change: 1 addition & 0 deletions clinic-common/types/appointment.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class AppointmentResponseBase {
public id: string,
public patientID: string,
public doctorID: string,
public doctorName: string,
public date: string,
public familyID: string,
public reservedFor: string
Expand Down
6 changes: 6 additions & 0 deletions clinic-common/types/healthPackage.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ export interface SubscribeToHealthPackageRequest {
isFamilyMember: boolean
healthPackageId: string
}

export interface UnsubscribeToHealthPackageRequest {
subscriberId: string
payerUsername: string
isFamilyMember: boolean
}
24 changes: 16 additions & 8 deletions frontend/src/api/healthPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
GetHealthPackageForPatientResponse,
GetHealthPackageResponse,
SubscribeToHealthPackageRequest,
UnsubscribeToHealthPackageRequest,
UpdateHealthPackageRequest,
UpdateHealthPackageResponse,
createHealthPackageRequest,
Expand Down Expand Up @@ -68,9 +69,11 @@ export async function subscribeCreditToHealthPackage(
.then((res) => res.data)
}

export async function unsubscribeToHealthPackage(): Promise<void> {
export async function unsubscribeToHealthPackage(
params: UnsubscribeToHealthPackageRequest
): Promise<void> {
return await api
.post<void>(`/health-packages/unsubscribe`)
.post<void>(`/health-packages/unsubscribe`, params)
.then((res) => res.data)
}

Expand All @@ -85,18 +88,23 @@ export async function getHealthPackageForPatient(
.then((res) => res.data)
}

export async function getCancelledHealthPackagesForPatient(): Promise<
string[]
> {
export async function getCancelledHealthPackagesForPatient(params: {
id: string
isFamilyMember: boolean
}): Promise<string[]> {
return await api
.post<string[]>(`/health-packages/patient-cancelled`)
.post<string[]>(`/health-packages/patient-cancelled`, params)
.then((res) => res.data)
}

export async function getCanellationDate(
healthPackageId: string
healthPackageId: string,
params: { id: string; isFamilyMember: boolean }
): Promise<string> {
return await api
.post<string>(`/health-packages/cancellation-date/${healthPackageId}`)
.post<string>(
`/health-packages/cancellation-date/${healthPackageId}`,
params
)
.then((res) => res.data)
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function Appointments() {
Doctor Name
</Typography>
<Typography variant="body1">
{appointment.doctorID}
{appointment.doctorName}
</Typography>
</Stack>
<Stack spacing={-1}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function FamilyMemberDetails() {
label: 'Related to',
value: query.data?.patient.name,
},
{
/*{
label: 'Health Package',
value:
query.data?.familyMember.healthPackage.name ||
Expand All @@ -49,7 +49,7 @@ export function FamilyMemberDetails() {
`${historyEntry.package} was cancelled on (${historyEntry.date})`
)
.join(', ') || 'No cancelled health packages yet',
},
},*/
]}
/>
</Grid>
Expand Down
Loading

0 comments on commit fabea1a

Please sign in to comment.