-
Notifications
You must be signed in to change notification settings - Fork 9
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
Showing
9 changed files
with
140 additions
and
99 deletions.
There are no files selected for viewing
Empty file.
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
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 |
---|---|---|
@@ -1,74 +1,103 @@ | ||
import { formatSchedule } from "./formatter"; | ||
import { Schedule, ScheduleInput, CustomEventInput, SelectedCourseInput } from "../../generated-types/graphql"; | ||
import { | ||
Schedule, | ||
ScheduleInput, | ||
CustomEventInput, | ||
SelectedCourseInput, | ||
} from "../../generated-types/graphql"; | ||
import { ScheduleModel } from "../../models/schedule"; | ||
import { omitBy } from "lodash"; | ||
|
||
|
||
// get the schedules for a user | ||
export async function getSchedulesByUser(userID: string): Promise<Schedule[]> { | ||
const userSchedules = await ScheduleModel.find({created_by: userID}) | ||
export async function getSchedulesByUser( | ||
context: any | ||
): Promise<Schedule[] | null> { | ||
if (!context.user._id) throw new Error("User is not authenticated"); | ||
|
||
const userSchedules = await ScheduleModel.find({ | ||
created_by: context.user._id, | ||
}); | ||
if (userSchedules.length == 0) { | ||
throw new Error("No schedules found for this user") | ||
throw new Error("No schedules found for this user"); | ||
} | ||
return userSchedules.map(formatSchedule) | ||
return userSchedules.map(formatSchedule); | ||
} | ||
|
||
// get the schedule for a user and a specific term | ||
export async function getScheduleByID(id: string): Promise<Schedule> { | ||
const scheduleFromID = await ScheduleModel.findById({_id: id}) | ||
const scheduleFromID = await ScheduleModel.findById({ _id: id }); | ||
if (!scheduleFromID) { | ||
throw new Error("No schedules found with this ID") | ||
throw new Error("No schedules found with this ID"); | ||
} | ||
return formatSchedule(scheduleFromID) | ||
return formatSchedule(scheduleFromID); | ||
} | ||
|
||
// delete a schedule specified by ObjectID | ||
export async function removeSchedule(scheduleID: string): Promise<string> { | ||
const deletedSchedule = await ScheduleModel.findByIdAndDelete(scheduleID) | ||
const deletedSchedule = await ScheduleModel.findByIdAndDelete(scheduleID); | ||
if (!deletedSchedule) { | ||
throw new Error("Schedule deletion failed") | ||
throw new Error("Schedule deletion failed"); | ||
} | ||
return scheduleID | ||
return scheduleID; | ||
} | ||
|
||
function removeNullEventVals(custom_event: CustomEventInput) { | ||
for (const key in custom_event) { | ||
if (custom_event[key as keyof CustomEventInput] === null) { | ||
delete custom_event[key as keyof CustomEventInput]; | ||
} | ||
if (custom_event[key as keyof CustomEventInput] === null) { | ||
delete custom_event[key as keyof CustomEventInput]; | ||
} | ||
} | ||
} | ||
|
||
// create a new schedule | ||
//export async function createSchedule(created_by: string, term: TermInput, schedule_name: string | undefined | null, is_public: boolean | null | undefined, class_IDs: string[] | undefined | null, primary_section_IDs: string[] | undefined | null, secondary_section_IDs: string[] | undefined | null): Promise<Schedule> { | ||
export async function createSchedule(main_schedule: ScheduleInput): Promise<Schedule> { | ||
export async function createSchedule( | ||
main_schedule: ScheduleInput, | ||
context: any | ||
): Promise<Schedule> { | ||
if (!context.user._id) throw new Error("User is not authenticated"); | ||
if (main_schedule.custom_events) { | ||
main_schedule.custom_events.forEach(removeNullEventVals) | ||
main_schedule.custom_events.forEach(removeNullEventVals); | ||
} | ||
const non_null_schedule = omitBy(main_schedule, (value) => value == null) | ||
const newSchedule = await ScheduleModel.create(non_null_schedule) | ||
return formatSchedule(newSchedule) | ||
const non_null_schedule = omitBy(main_schedule, (value) => value == null); | ||
const newSchedule = await ScheduleModel.create({ | ||
...non_null_schedule, | ||
created_by: context.user._id, | ||
}); | ||
return formatSchedule(newSchedule); | ||
} | ||
|
||
|
||
// update an existing schedule | ||
export async function editSchedule(schedule_ID: string, main_schedule: ScheduleInput): Promise<Schedule> { | ||
export async function editSchedule( | ||
schedule_ID: string, | ||
main_schedule: ScheduleInput | ||
): Promise<Schedule> { | ||
if (main_schedule.custom_events) { | ||
main_schedule.custom_events.forEach(removeNullEventVals) | ||
main_schedule.custom_events.forEach(removeNullEventVals); | ||
} | ||
const non_null_schedule = omitBy(main_schedule, (value) => value == null) | ||
const updatedSchedule = await ScheduleModel.findByIdAndUpdate(schedule_ID, non_null_schedule, {returnDocument: 'after'}) | ||
const non_null_schedule = omitBy(main_schedule, (value) => value == null); | ||
const updatedSchedule = await ScheduleModel.findByIdAndUpdate( | ||
schedule_ID, | ||
non_null_schedule, | ||
{ returnDocument: "after" } | ||
); | ||
if (!updatedSchedule) { | ||
throw new Error("Unable to update existing schedule") | ||
throw new Error("Unable to update existing schedule"); | ||
} | ||
return formatSchedule(updatedSchedule) | ||
return formatSchedule(updatedSchedule); | ||
} | ||
|
||
// update class selection in an existing schedule | ||
export async function setClasses(scheduleID: string, courses: SelectedCourseInput[]): Promise<Schedule> { | ||
const existingSchedule = await ScheduleModel.findByIdAndUpdate(scheduleID, {courses: courses}, {returnDocument: 'after'}) | ||
export async function setClasses( | ||
scheduleID: string, | ||
courses: SelectedCourseInput[] | ||
): Promise<Schedule> { | ||
const existingSchedule = await ScheduleModel.findByIdAndUpdate( | ||
scheduleID, | ||
{ courses: courses }, | ||
{ returnDocument: "after" } | ||
); | ||
if (!existingSchedule) { | ||
throw new Error("Unable to update existing schedule's class selection") | ||
throw new Error("Unable to update existing schedule's class selection"); | ||
} | ||
return formatSchedule(existingSchedule) | ||
return formatSchedule(existingSchedule); | ||
} |
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
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 |
---|---|---|
@@ -1,30 +1,50 @@ | ||
import { getSchedulesByUser, getScheduleByID, removeSchedule, createSchedule, setClasses, editSchedule } from "./controller"; | ||
import { | ||
getSchedulesByUser, | ||
getScheduleByID, | ||
removeSchedule, | ||
createSchedule, | ||
setClasses, | ||
editSchedule, | ||
} from "./controller"; | ||
import { ScheduleModule } from "./generated-types/module-types"; | ||
import { ScheduleInput, SelectedCourseInput } from "../../generated-types/graphql"; | ||
import { | ||
ScheduleInput, | ||
SelectedCourseInput, | ||
} from "../../generated-types/graphql"; | ||
|
||
const resolvers: ScheduleModule.Resolvers = { | ||
Query: { | ||
schedulesByUser(_parent, args: { created_by: string }) { | ||
return getSchedulesByUser(args.created_by); | ||
schedulesByUser(_parent, _args, context) { | ||
return getSchedulesByUser(context); | ||
}, | ||
scheduleByID(_parent, args: { id: string }) { | ||
return getScheduleByID(args.id); | ||
} | ||
}, | ||
}, | ||
Mutation: { | ||
removeScheduleByID(_parent, args: {id: string}) { | ||
removeScheduleByID(_parent, args: { id: string }) { | ||
return removeSchedule(args.id); | ||
}, | ||
createNewSchedule(_parent, args: {main_schedule: ScheduleInput}) { | ||
return createSchedule(args.main_schedule) | ||
createNewSchedule( | ||
_parent, | ||
args: { main_schedule: ScheduleInput }, | ||
context | ||
) { | ||
return createSchedule(args.main_schedule, context); | ||
}, | ||
editExistingSchedule( | ||
_parent, | ||
args: { id: string; main_schedule: ScheduleInput } | ||
) { | ||
return editSchedule(args.id, args.main_schedule); | ||
}, | ||
editExistingSchedule(_parent, args: {id: string, main_schedule: ScheduleInput}) { | ||
return editSchedule(args.id, args.main_schedule) | ||
setSelectedClasses( | ||
_parent, | ||
args: { id: string; courses: SelectedCourseInput[] } | ||
) { | ||
return setClasses(args.id, args.courses); | ||
}, | ||
setSelectedClasses(_parent, args: {id: string, courses: SelectedCourseInput[]}) { | ||
return setClasses(args.id, args.courses) | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
export default resolvers; |
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
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
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
Oops, something went wrong.