import Chat from "../../../entities/Chat" ; import Message from "../../../entities/Message" ; import User from "../../../entities/User" ; import { SendChatMessageMutationArgs , SendChatMessageResponse } from "../../../types/graph" ; import { Resolvers } from "../../../types/resolvers" ; import privateResolver from "../../../utils/privateResolver" ; const resolvers = { Mutation : { SendChatMessage : privateResolver ( async ( _ , args , { req , pubSub } ) : Promise < SendChatMessageResponse > => { const user = req . user ; try { const chat = await Chat . findOne ( { id : args . chatId } ) ; if ( chat ) { if ( chat . passengerId === user . id || chat . driverId === user . id ) { const message = await Message . create ( { text : args . text , chat , user } ) . save ( ) ; pubSub . publish ( "newChatMessage" , { MessageSubscription : message } ) ; return { ok : true , error : null , message } ; } else { return { ok : false , error : "Unauthorized" , message : null } ; } } else { return { ok : false , error : "Chat not found" , message : null } ; } } catch ( error ) { return { ok : false , error : error . message , message : null } ; } } ) } } ; export default resolvers ;