Skip to content

Commit

Permalink
latest recurring rides impl
Browse files Browse the repository at this point in the history
  • Loading branch information
namanhboi committed Nov 1, 2024
1 parent bb0e5c7 commit f5bb52b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 73 deletions.
62 changes: 35 additions & 27 deletions frontend/src/components/Modal/DeleteOrEditTypeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,52 @@ const DeleteOrEditTypeModal = ({
//Need to fix the logic for this
if (single) {
/**
* trim end date of immediate parent to before this day.
* delete all children rides of the immediate parent.
* create a new ride on the date + 1 with data similar to parent if the parent’s original endTime allow for it. Add the id of the parent ride as the id of this ride’s parentId.
* add childrenId to parent ride.
* trim the end date of the ride to before today, axios.put
* create a new recurring ride with the parent’s original end date (if that endate is > today) starting from today + 1
* link that ride back to the parent, fill in parent and children fields.
*/
const parentOriginalEndDate = new Date(ride.immediateParentRide!.endDate!);
//need to look at sourceride not ride.
const originalEndDate = new Date(ride.sourceRide!.endDate!);

let trimmedEndDateImmPar = curDate;
trimmedEndDateImmPar.setDate(trimmedEndDateImmPar.getDate() - 1);
let newEndDate = curDate;
newEndDate.setDate(newEndDate.getDate() - 1);

axios.put(`/api/rides/${ride.immediateParentRideId}`, {...ride.immediateParentRide, endDate : trimmedEndDateImmPar.toISOString()});
let currentRide = (ride.immediateParentRide)!.children;
while (currentRide !== undefined) {
axios
.delete(`/api/rides/${currentRide.id}`);
currentRide = currentRide.children;
}
axios.put(`/api/rides/${ride.id}`, {...ride.sourceRide!, endDate : newEndDate.toISOString()});

if (parentOriginalEndDate > curDate) {
let newChildRideStartTime = new Date(ride.startTime)
newChildRideStartTime.setDate(newChildRideStartTime.getDate() + 1);
let {id, ...rideNoId} = ride;
if (originalEndDate > curDate) {
//create a new recurring ride with same data as the old one but with start date = curDate + 1.
let newRideStartTime = new Date(ride.sourceRide!.startTime);
newRideStartTime.setDate(curDate.getDate() + 1);
let newRideEndTime = new Date(ride.sourceRide!.endTime);
newRideEndTime.setDate(curDate.getDate() + 1);


let {id, ...rideNoId} = ride.sourceRide!;
const newChildRide = {
...rideNoId,
startTime : newChildRideStartTime,
endDate : parentOriginalEndDate.toISOString(),
type :
'unscheduled'
startTime : newRideStartTime.toISOString(),
endTime : newRideEndTime.toISOString(),
endDate : format_date(originalEndDate),
parentRideId : ride.id,
childRideId: ride.sourceRide!.childRideId,
recurring : true,
type : 'unscheduled'
}
axios
.post('/api/rides', newChildRide)
.then((response) => response.data)
.then((rideData) =>
axios.put(`/api/rides/${ride.immediateParentRideId}`, {...ride.immediateParentRide, children : rideData, childrenId: rideData.id})
);
closeModal();
refreshRides();
.then((rideData) => {
axios.put(`/api/rides/${ride.id}`, {...ride.sourceRide!, childrenId: rideData.id});
if (ride.sourceRide!.childRideId !== undefined) {
axios.put(`/api/rides/${ride.sourceRide!.childRideId}`, {...ride.sourceRide!.childRide, parentRideId : rideData.id});
}
}
);
}
closeModal();
refreshRides();


} else {
if (allOrFollowing) {
/**
Expand Down
59 changes: 51 additions & 8 deletions frontend/src/components/RideModal/RideModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const getRideData = (ride: Ride | undefined) => {
dropoffLoc: ride.endLocation.id
? ride.endLocation.name
: ride.endLocation.address,

};
if (ride.recurring) {
let repeats;
Expand All @@ -63,6 +64,12 @@ const getRideData = (ride: Ride | undefined) => {
repeats,
days,
endDate: format_date(ride.endDate),
immediateParentRide : ride.immediateParentRide,
immediateParentRideId : ride.immediateParentRideId,
sourceRide : ride.sourceRide,
sourceRideId : ride.sourceRideId,
children: ride.children,
childrenId : ride.childrenId
};
}
return rideData;
Expand Down Expand Up @@ -211,6 +218,12 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
rider,
startLocation,
endLocation,
immediateParentRide,
immediateParentRideId,
sourceRide,
sourceRideId,
children,
childrenId,
} = formData;

const startTime = moment(`${date} ${pickupTime}`).toISOString();
Expand All @@ -225,8 +238,16 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
rider,
startLocation,
endLocation,
immediateParentRide,
immediateParentRideId,
sourceRide,
sourceRideId,
children,
childrenId,
};


//if the ride repeats
if (repeats !== RepeatValues.DoesNotRepeat) {
rideData = {
...rideData,
Expand All @@ -237,7 +258,7 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
}


//shittttt, the new form data is in form data, should use that to get latest data about ride.
//shittttt, the new form data is in formData, should use that to get latest data about ride.
if (ride) {
// scheduled ride
if (ride.type === 'active') {
Expand All @@ -260,6 +281,8 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
} else if (editAddType == EditAddRideType.THIS_AND_FOLLOWING) {
//ill finish this shit later bruh
//fuckkkk in this case we would need to lock the date.
//here I don't think we need to lock recurring to false/true because if we edit this and following and just
//set it as a non recurring ride then it ends right there.
/**
* trim the end date of the immediate parent to before today
* go down the tree of all children rides and delete all of them
Expand All @@ -272,21 +295,41 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
...ride.immediateParentRide,
endDate: trimmedEndDateImmPar.toISOString(),
});


//delete all children ride
let currentRide = ride.immediateParentRide!.children;
while (currentRide !== undefined) {
axios.delete(`/api/rides/${currentRide.id}`);
currentRide = currentRide.children;
}
//now we create a recurring ride starting from today to the en





//now we create a new ride starting from today to with the data in formData, but have it link back to the parent ride and the source ride.
// we also have to update the parentRide to have this ride as its children.
rideData = {
...rideData,
immediateParentRide : ride.immediateParentRide,
immediateParentRideId : ride.immediateParentRideId,
sourceRide : ride.sourceRide,
sourceRideId : ride.sourceRideId,
children : undefined,
childrenId : undefined
}

//adds the new ride to the database and updates the parent ride to have it as the child.
axios
.post(`/api/rides`, rideData)
.then((response) => response.data)

.then(data => axios.put(`/api/rides/${ride.immediateParentRideId}`, {
...ride.immediateParentRide,
children: data,
childrenId : data.id
}))
.then(refreshRides);
} else {
//edit single ride
//edit single ride, meaning that we should also lock the recurring to false, can't repeat.
//otherwise, you can do a thing where you add the new recurring ride and then another ride to continoue
//where that ride ended and resume with the parent ride's data. IDK yet, gotta ask desmond
/**
* Note: should also lock the date for simplicity.
* trim end date of immediate parent to before this day.
Expand Down
35 changes: 15 additions & 20 deletions frontend/src/context/RidesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,41 @@ export const RidesProvider = ({ children }: RidesProviderProps) => {
);
})
//here, i am creating repeating ride objects (that doesn't exist in the database) that link back to some ride in the database.
.map((parentRide) => {
const startTimeRecurringRide = new Date(parentRide.startTime);
.map((filteredSourceRide) => {
const startTimeRecurringRide = new Date(filteredSourceRide.startTime);
startTimeRecurringRide.setFullYear(curDate.getFullYear());
startTimeRecurringRide.setMonth(curDate.getMonth());
startTimeRecurringRide.setDate(curDate.getDate());

const endTimeRecurringRide = new Date(parentRide.endTime);
const endTimeRecurringRide = new Date(filteredSourceRide.endTime);
endTimeRecurringRide.setFullYear(curDate.getFullYear());
endTimeRecurringRide.setMonth(curDate.getMonth());
endTimeRecurringRide.setDate(curDate.getDate());

const schedule =
new Date(parentRide.startTime).getDay() == curDate.getDay() &&
new Date(parentRide.startTime).getMonth() == curDate.getMonth() &&
new Date(parentRide.startTime).getFullYear() == curDate.getFullYear()
new Date(filteredSourceRide.startTime).getDay() == curDate.getDay() &&
new Date(filteredSourceRide.startTime).getMonth() == curDate.getMonth() &&
new Date(filteredSourceRide.startTime).getFullYear() == curDate.getFullYear()
? Type.ACTIVE
: Type.UNSCHEDULED;

const immediateParentRideId = parentRide.id;
const sourceRideId = parentRide.sourceRideId;

const immediateParentRide = recurringRides.find((ride) => ride.id === immediateParentRideId);
const sourceRide = recurringRides.find((ride) => ride.id === sourceRideId);

const parentRide = recurringRides.find((ride) => ride.id === filteredSourceRide.parentRideId);
const childrenRide = recurringRides.find((ride) => ride.id === filteredSourceRide.childRideId);


return {
...parentRide,
...filteredSourceRide, // id should be the same as the sourceRide
startTime: startTimeRecurringRide.toISOString(),
endTime: endTimeRecurringRide.toISOString(),
type: schedule,
immediateParentRideId: immediateParentRideId,
sourceRideId : sourceRideId,
immediateParentRide,
sourceRide,
id: '',
parentRide: parentRide,
parentRideId: parentRide?.id,
childrenRide : childrenRide,
childrenRideId : childrenRide?.id,
sourceRide : filteredSourceRide
};
});

console.log('current date is ', curDate.getDate());
// const ridesDataTodayNoParentRec = ridesDataToday.filter((ride : Ride) => !(ride.recurring && ride.parentRide === undefined));
const combinedRidesData =
nonRecurringRidesDataToday.concat(recurringRidesToday);
if (combinedRidesData) {
Expand Down
30 changes: 12 additions & 18 deletions server/src/models/ride.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ export type RideType = {
deleted?: string[];
edits?: string[];
sourceRide? : RideType;
immediateParentRide?: RideType;
children? : RideType; // id of the children ride. Used for recurring rides.
sourceRideId? : string;
immediateParentRideId? : string;
childrenId? : string;
parentRide? : RideType;
parentRideId? : string;
childrenRide? : RideType;
childrenRideId? : string;
};

const locationSchema = {
Expand Down Expand Up @@ -128,21 +127,16 @@ const schema = new dynamoose.Schema({
required: false,
validate: /^(19|20)\d{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/,
},
immediateParentRideId : {
type: String,
required: false,
hashKey: true,
parentRideId : {
type : String,
required : false,
hashKey: true
},
sourceRideId : {
type: String,
required: false,
hashKey: true,
childrenRideId : {
type : String,
required : false,
hashKey: true
},
childrenId : {
type: String,
required: false,
hashKey: true,
}
});

export const Ride = dynamoose.model('Rides', schema, defaultModelConfig);

0 comments on commit f5bb52b

Please sign in to comment.