import { Between , getRepository } from "typeorm" ; import Ride from "../../../entities/Ride" ; import User from "../../../entities/User" ; import { GetNearbyRideResponse } from "../../../types/graph" ; import { Resolvers } from "../../../types/resolvers" ; import privateResolver from "../../../utils/privateResolver" ; const resolvers = { Query : { GetNearbyRide : privateResolver ( async ( _ , __ , { req } ) : Promise < GetNearbyRideResponse > => { const user = req . user ; if ( user . isDriving ) { const { lastLat , lastLng } = user ; try { const ride = await getRepository ( Ride ) . findOne ( { status : "REQUESTING" , pickUpLat : Between ( lastLat - 0.05 , lastLat + 0.05 ) , pickUpLng : Between ( lastLng - 0.05 , lastLng + 0.05 ) } , { relations : [ "passenger" ] } ) ; if ( ride ) { return { ok : true , error : null , ride } ; } else { return { ok : true , error : null , ride : null } ; } } catch ( error ) { return { ok : false , error : error . message , ride : null } ; } } else { return { ok : false , error : "You are not a driver" , ride : null } ; } } ) } } ; export default resolvers ;