import { cloneDeep } from 'lodash' ; import { INVITATION_STATE } from 'common/enums' ; import { connectionFromArray , fromConnectionArgs } from 'common/utils' ; import { InvitesToAcceptedResolver } from 'definitions' ; const resolver = async ( { id , owner } , { input } , { dataSources : { atomService } , viewer , knex } ) => { const isOwner = owner === viewer . id ; if ( ! isOwner ) { return connectionFromArray ( [ ] , input ) ; } const { take , skip } = fromConnectionArgs ( input ) ; const states = Object . values ( INVITATION_STATE ) . filter ( ( state ) => state !== INVITATION_STATE . pending ) ; const table = 'circle_invitation' ; const base = knex . max ( 'id as id' ) . max ( 'accepted_at as accepted_at' ) . select ( 'user_id' ) . from ( table ) . where ( { circleId : id , inviter : owner } ) . whereIn ( 'state' , states ) . groupBy ( 'user_id' ) . orderBy ( 'accepted_at' , 'desc' ) ; const countQuery = knex . count ( ) . from ( cloneDeep ( base ) . as ( 'base' ) ) . first ( ) ; const invitesQuery = base . offset ( skip ) . limit ( take ) ; const [ count , invites ] = await Promise . all ( [ countQuery , invitesQuery ] ) ; const totalCount = parseInt ( count ? ( count . count as string ) : '0' , 10 ) ; const records = await Promise . all ( invites . map ( ( { id : inviteId } ) => atomService . findUnique ( { table , where : { id : inviteId } } ) ) ) ; return connectionFromArray ( records , input , totalCount ) ; } ; export default resolver ;