@@ -3,11 +3,11 @@ import type { DGSupabaseClient } from "@repo/database/lib/client";
33
44type AgentType = Database [ "public" ] [ "Enums" ] [ "AgentType" ] | "group" ;
55
6- export const getSessionUserData = async (
6+ export const getSessionBaseUserData = async (
77 client : DGSupabaseClient ,
88) : Promise < {
99 id : string ;
10- name : string ;
10+ name ? : string ;
1111 type : AgentType ;
1212 email ?: string ;
1313} | null > => {
@@ -33,16 +33,38 @@ export const getSessionUserData = async (
3333 return { name, id, email, type : "group" } ;
3434 }
3535 }
36- const accountReq = await client
37- . from ( "PlatformAccount" )
38- . select ( "name" )
39- . eq ( "dg_account" , session . data . session . user . id )
40- . eq ( "agent_type" , "person" )
41- . maybeSingle ( ) ;
42- if ( accountReq . error || ! accountReq . data ) {
43- return null ;
36+ return { id, type : "person" , email } ;
37+ } ;
38+
39+ export const getSessionUserData = async (
40+ client : DGSupabaseClient ,
41+ ) : Promise < {
42+ id : string ;
43+ name : string ;
44+ type : AgentType ;
45+ email ?: string ;
46+ } | null > => {
47+ const data = await getSessionBaseUserData ( client ) ;
48+ if ( data === null ) return null ;
49+ if ( ! data . name && data . type === "person" ) {
50+ const accountReq = await client
51+ . from ( "PlatformAccount" )
52+ . select ( "name" )
53+ . eq ( "dg_account" , data . id )
54+ . eq ( "agent_type" , "person" )
55+ . maybeSingle ( ) ;
56+ if ( accountReq . error || ! accountReq . data ?. name ) {
57+ return null ;
58+ }
59+ return { ...data , name : accountReq . data . name } ;
4460 }
45- return { id, name : accountReq . data . name , type : "person" , email } ;
61+ if ( data . name === undefined ) return null ;
62+ return data as {
63+ id : string ;
64+ name : string ; // typescript is being dumb
65+ type : AgentType ;
66+ email ?: string ;
67+ } ;
4668} ;
4769
4870export const getGroupMemberList = async (
@@ -80,3 +102,42 @@ export const getGroupMemberList = async (
80102 } [ ]
81103 ) ;
82104} ;
105+
106+ export const createGroupInvitation = async ( {
107+ client,
108+ groupId,
109+ admin = false ,
110+ } : {
111+ client : DGSupabaseClient ;
112+ groupId : string ;
113+ admin : boolean ;
114+ } ) : Promise < string | null > => {
115+ const userData = await getSessionBaseUserData ( client ) ;
116+ if ( ! userData ) return null ;
117+ const membershipReq = await client
118+ . from ( "group_membership" )
119+ . select ( "admin" )
120+ . eq ( "group_id" , groupId )
121+ . eq ( "member_id" , userData . id )
122+ . maybeSingle ( ) ;
123+ if ( membershipReq . data ?. admin !== true ) return null ;
124+ const { data, error } = await client . rpc ( "create_secret_token" , {
125+ v_payload : JSON . stringify ( { groupId, type : "groupInvitation" , admin } ) ,
126+ expiry_interval : "60d" ,
127+ } ) ;
128+ if ( error || ! data ) return null ;
129+ return data ;
130+ } ;
131+
132+ export const acceptGroupInvitation = async (
133+ client : DGSupabaseClient ,
134+ token : string ,
135+ ) : Promise < string | null > => {
136+ const userData = await getSessionBaseUserData ( client ) ;
137+ if ( ! userData ) return "Not logged in" ;
138+ const { data, error } = await client . rpc ( "accept_group_invitation" , {
139+ token,
140+ } ) ;
141+ if ( error || ! data ) return "This token does not exist" ;
142+ return null ;
143+ } ;
0 commit comments