Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added resolvers and modified typeDfs #61

Merged
merged 3 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/models/Appointment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import mongoose from "mongoose";

const AppointmentSchema = new mongoose.Schema(
{
AppointmantDate: Date,
CustomerID: Number,
BranchID: Number,
VehicleID: Number,
ServiceID: Number,
AppointmentDate: Date,
CustomerID: String,
BranchID: String,
VehicleID: String,
ServiceID: String,
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
AppointmentStatus: String,
},
{
Expand Down
102 changes: 102 additions & 0 deletions src/resolvers/Appointment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { ApolloError, UserInputError } from "apollo-server-express";
import Appointment from "../models/Appointment";
import User from "../models/User";
import Branch from "../models/Branch";
import Service from "../models/Service";

const AppointmentStatus = {
"PENDING": "PENDING",
"REJECTED": "REJECTED",
"ACCEPTED": "ACCEPTED",
"COMPLETED": "COMPLETED",
"CANCELLED": "CANCELLED"
}
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved

const AppointmentResolver = {
Query: {
appointments: async(root, { filter } , context, info) => {
if (!filter){
throw new UserInputError("No filter provided");
}
try{
const filteredAppointments = await Appointment.find(JSON.parse(filter));
return filteredAppointments;
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
} catch(err){
throw new ApolloError(err.message,500);
}

},
appointment: async (root, { id }, context, info) => {
const invalid_input = id.length === 0;
if (invalid_input) {
throw new UserInputError("Invalid ID number provided");
}
const appointment = await Appointment.findById(id);
return appointment;
}
},
Mutation: {
async createAppointment(_, {appointmentInput}) {
if (appointmentInput === null) {
throw new ApolloError("Invalid input for new Appointment");
}
const appointment = await Appointment.create({...appointmentInput});
return appointment;
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
},
async updateAppointment(_, {appointmentInput}) {
if(!appointmentInput.id) {
throw new UserInputError("Unable to update Appointment with invalid id");
}
try{
let oldAppointment = await Appointment.findById(appointmentInput.id);
if(oldAppointment){
const CustomerID = await User.findById(appointmentInput.CustomerID);
if(!CustomerID){
throw new UserInputError("Customer ID does not exist")
}
const BranchID = await Branch.findById(appointmentInput.BranchID);
if(!BranchID){
throw new UserInputError("Branch ID does not exist")
}
// TO DO: Add service data
// const ServiceID = await Service.findById(appointmentInput.ServiceID);
// if(!ServiceID){
// throw new UserInputError("Service ID does not exist")
// }
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
if (appointmentInput.AppointmentStatus === AppointmentStatus.CANCELLED || appointmentInput.AppointmentStatus === AppointmentStatus.REJECTED){
throw new UserInputError("Cancelled/Rejected Appointment cannot be updated.")
}
// Unable to check if VehicleID exist or not (NO)
if(appointmentInput.VehicleID === null){
throw new UserInputError("Vehicle ID is null")
}
} else{
throw new UserInputError("Appointment is not found with id")
}
let appointment = await Appointment.findByIdAndUpdate(appointmentInput.id, {
...appointmentInput
}, {new: true});
return appointment;
} catch (err) {
throw new ApolloError(err.message,500);
}

},
async deleteAppointment(_, { id } ) {
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
if(!id){
throw new UserInputError ("No id is provided");
} else{
try{
const appointment = await Appointment.findById(id);
if (appointment){
let deleted = await Appointment.findByIdAndRemove(id);
return deleted;
}} catch(err) {
throw new ApolloError (err.message,500);
}
}
}
}

};
export default AppointmentResolver;
77 changes: 77 additions & 0 deletions src/resolvers/AudioStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { ApolloError, UserInputError } from "apollo-server-express";
import AudioStorage from "../models/AudioStorage";

const AudioStorageResolver = {
Query: {
audioUploads:(_,args) => {},
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
audioStorages: async (root, { filter } , context, info) => {
if (!filter){
throw new UserInputError("No filter provided");
}
try{
const filteredAudioStorages =await AudioStorage.find(JSON.parse(filter))
return filteredAudioStorages;
}catch(err){
throw new ApolloError(err.message,500)
}

},
audioStorage: async (root, { id }, context, info) => {
const invalid_input = id.length === 0;
if (invalid_input) {
throw new UserInputError("Invalid ID number provided");
}
const audioStorage = await AudioStorage.findById(id);
return audioStorage;
}
},
Mutation: {
async createAudioStorage(_, {audioStorageInput}) {
if (audioStorageInput === null) {
throw new ApolloError("Invalid input for new AudioStorage");
}
const newAudioStorage = new AudioStorage({
...audioStorageInput,
});
const audioStorage = await newAudioStorage.save();
return audioStorage;
},
async updateAudioStorage(_, {audioStorageInput}) {
if(audioStorageInput.id === null) {
throw new UserInputError("Unable to update AudioStorage with invalid id");
}
try{
return await AudioStorage.findByIdAndUpdate(audioStorageInput.id, {
audioContent: audioStorageInput.audioContent,
audioURL: audioStorageInput.audioURL,
audioType: audioStorageInput.audioType,
audioLength: audioStorageInput.audioLength,
AudioStorageStatus: audioStorageInput.AudioStorageStatus
}, {new: true})
} catch (err) {
throw new ApolloError(err.message,500);
}

},
uploadAudioStorage: (_,args) => {
return args.file.then(file =>{
//Contents of Upload scalar: https://github.com/jaydenseric/graphql-upload#class-graphqlupload
//file.createReadStream() is a readable node stream that contains the contents of the uploaded file
return file;
})
},
async deleteAudioStorage(_, { id } ) {
if(!id){
throw new UserInputError ("Unable to delete AudioStorage");
}
const audioStorage = AudioStorage.findById(id);
if (audioStorage){
let deleted = await AudioStorage.findByIdAndRemove(id);
return deleted;
} else {
throw new UserInputError ("AudioStorage ID is not found.");
}
},
} ,
};
export default AudioStorageResolver;
80 changes: 80 additions & 0 deletions src/resolvers/Branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { ApolloError, UserInputError } from "apollo-server-express";
import Branch from "../models/Branch";

const BranchResolver = {
Query: {
branches: async(root, { filter } , context, info) => {
if (!filter){
throw new UserInputError("No filter provided");
}
try {
const filteredBranchs = await Branch.find(JSON.parse(filter));
return filteredBranchs;
}catch(err){
throw new UserInputError("No branches are found with filter")
}

},
branch: async (root, { id }, context, info) => {
const invalid_input = id.length === 0;
if (invalid_input) {
throw new UserInputError("Invalid ID number provided");
}
try{
const branch = await Branch.findById(id);
return branch;
} catch(err){
throw new ApolloError(err.message,500)
}
}
},
Mutation: {
async createBranch(_, {branchInput}) {
if (branchInput === null) {
throw new ApolloError("Invalid input for new Branch");
}
const newBranch = new Branch({
...branchInput,
});
const branch = await newBranch.save();
return branch
.populate("businesshours")
.populate("services")
.execPopulate();
},
async updateBranch(_, {branchInput}) {
if(!branchInput.id) {
throw new UserInputError("Unable to update Branch with invalid id");
}
try{
let branch = await Branch.findByIdAndUpdate(branchInput.id, {
...branchInput,
}, {new: true}).then (res => { return res; })
return branch
.populate("businesshours")
.populate("services")
.execPopulate();
} catch (err) {
throw new ApolloError(err.message,500);
}

},
async deleteBranch(_, { id } ) {
if(!id){
throw new UserInputError ("No id is provided.");
}
try{
const branch = await Branch.findById(id);
if (branch){
const deleted = await Branch.findByIdAndRemove(id);
return deleted;
} else {
throw new UserInputError ("Branch ID is not found.");
}
}catch (err){
throw new ApolloError(err.message,500);
}
}
} ,
};
export default BranchResolver;
89 changes: 89 additions & 0 deletions src/resolvers/Company.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { ApolloError, UserInputError } from "apollo-server-express";
import Company from "../models/Company";
import User from "../models/User";

const CompanyResolver = {
Query: {
companies: async(root, { filter } , context, info) => {
if (!filter){
throw new UserInputError("No filter provided");
}
try{
const filteredCompanies = await Company.find(JSON.parse(filter));
return filteredCompanies;
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
}catch(err){
throw new ApolloError(err.message,500);
}

},
company: async (root, { id }, context, info) => {
const invalid_input = id.length === 0;
if (invalid_input) {
throw new UserInputError("Invalid ID number provided");
}
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
try{
const company = await Company.findById(id);
return company;
}catch(err){
throw new ApolloError(err.message,500)
}

}
},
Mutation: {
async createCompany(_, {companyInput}) {
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
if (companyInput === null) {
throw new ApolloError("Invalid input for new Company");
}
const ownerID = await User.findById(companyInput.ownerID);
if (ownerID){
const company = await Company.create(companyInput);
return company;
}else{
throw new UserInputError("Owner is not a registered as user.")
}

},
async updateCompany(_, {companyInput}) {
if(!companyInput.id) {
throw new UserInputError("Unable to update Company with invalid id");
}
try{
let company = await Company.findById(companyInput.id)
if (company){
const ownerID = await User.findById(companyInput.ownerID);
if(!ownerID){
throw new UserInputError("No owner found with id")
}
let updateCompany = await Company.findByIdAndUpdate(companyInput.id, {
...companyInput
}, {new: true});
return updateCompany
}else{
throw new UserInputError("No company found by id")
}
} catch (err) {
throw new ApolloError(err.message,500);
}

},
async deleteCompany(_, { id } ) {
if(!id){
throw new UserInputError ("Invalid ID unable to delete Company");
}
try{
const company = await Company.findById(id);
if (company){
const deleted = await Company.findByIdAndRemove(id);
return deleted;

} else {
throw new UserInputError ("Company ID is not found.");
}
} catch(err){
LUXIANZE marked this conversation as resolved.
Show resolved Hide resolved
throw new ApolloError(err.message,500);
}
},
} ,
};
export default CompanyResolver;
9 changes: 9 additions & 0 deletions src/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import MessageResolver from "./Message";
import UserResolver from "./User";
import PromotionResolver from "./Promotion";
import DispatchServiceResolver from "./DispatchService";
import AppointmentResolver from "./Appointment";
import AudioStorageResolver from "./AudioStorage";
import BranchResolver from "./Branch";
import CompanyResolver from "./Company";
import ServiceResolver from "./Service";
import VideoStorageResolver from "./VideoStorage";

Expand All @@ -14,6 +18,11 @@ export default [
UserResolver,
PromotionResolver,
DispatchServiceResolver,
AppointmentResolver,
AudioStorageResolver,
BranchResolver,
CompanyResolver
ServiceResolver,
VideoStorageResolver,
];