@@ -221,15 +221,55 @@ getUserGroup team id_ includeChannels = do
221221 where ug.id = ($1 :: uuid) and ug.team_id = ($2 :: uuid)
222222 |]
223223
224-
225224getUserGroupsWithMembers ::
226225 forall r .
227- ( UserGroupStorePostgresEffectConstraints r ,
228- Member (Input (Local () )) r
226+ ( UserGroupStorePostgresEffectConstraints r
229227 ) =>
230228 UserGroupPageRequest ->
231229 Sem r UserGroupPageWithMembers
232- getUserGroupsWithMembers _req = pure $ UserGroupPage [] 0 -- this is the stub
230+ getUserGroupsWithMembers req = runTransaction TxSessions. ReadCommitted TxSessions. Read $ do
231+ groups :: [UserGroup ] <- Tx. statement () $ refineResult (mapM toUserGroup) $ buildStatement query rows
232+ count <- getCountSession req
233+ return $ UserGroupPage groups count
234+ where
235+ rows :: HD. Result [(UUID , Text , Int32 , UTCTime , Vector UUID , Int32 )]
236+ rows = HD. rowList $
237+ (,,,,,)
238+ <$> HD. column (HD. nonNullable HD. uuid)
239+ <*> HD. column (HD. nonNullable HD. text)
240+ <*> HD. column (HD. nonNullable HD. int4)
241+ <*> HD. column (HD. nonNullable HD. timestamptz)
242+ <*> decodeUuidVector
243+ <*> HD. column (HD. nonNullable HD. int4)
244+
245+ query :: QueryFragment
246+ query = mconcat $ map literal [
247+ " select" ,
248+ T. intercalate " , " fields,
249+ " from user_group g" ,
250+ " left join user_group_member gm on g.id = gm.user_group_id"
251+ ] <> [where_ (groupMatchIdName req <> groupPaginationWhereClause req)] <> map literal [
252+ " group by g.team_id, g.id"
253+ ] <> groupPaginationOrderBy req
254+
255+ fields = [
256+ " g.id :: uuid" ,
257+ " g.name :: text" ,
258+ " managed_by :: int" ,
259+ " created_at :: timestamptz" ,
260+ " coalesce(array_agg(gm.user_id), array[]::uuid[]) :: uuid[]" ,
261+ " count(*) :: int"
262+ ]
263+
264+ toUserGroup :: (UUID , Text , Int32 , UTCTime , Vector UUID , Int32 ) -> Either Text UserGroup
265+ toUserGroup (Id -> id_, name', managedBy', createdAt', members', Just . fromIntegral -> membersCount) = do
266+ name <- userGroupNameFromText name'
267+ managedBy <- parseManagedBy managedBy'
268+ let createdAt = toUTCTimeMillis createdAt'
269+ channels = Nothing
270+ channelsCount = Nothing
271+ members = Identity (fmap Id members' :: Vector UserId )
272+ pure $ UserGroup_ {.. }
233273
234274groupMatchIdName :: UserGroupPageRequest -> [QueryFragment ]
235275groupMatchIdName req =
0 commit comments