@@ -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 > => {
@@ -34,14 +34,75 @@ export const getSessionUserData = async (
3434 return { name, id, email, type : "group" } ;
3535 }
3636 }
37- const accountReq = await client
38- . from ( "PlatformAccount" )
39- . select ( "name" )
40- . eq ( "dg_account" , session . data . session . user . id )
41- . eq ( "agent_type" , "person" )
42- . maybeSingle ( ) ;
43- if ( accountReq . error || ! accountReq . data ) {
44- return null ;
37+ return { id, type : "person" , email } ;
38+ } ;
39+
40+ export const getSessionUserData = async (
41+ client : DGSupabaseClient ,
42+ ) : Promise < {
43+ id : string ;
44+ name : string ;
45+ type : AgentType ;
46+ email ?: string ;
47+ } | null > => {
48+ const data = await getSessionBaseUserData ( client ) ;
49+ if ( data === null ) return null ;
50+ if ( ! data . name && data . type === "person" ) {
51+ const accountReq = await client
52+ . from ( "PlatformAccount" )
53+ . select ( "name" )
54+ . eq ( "dg_account" , data . id )
55+ . eq ( "agent_type" , "person" )
56+ . maybeSingle ( ) ;
57+ if ( accountReq . error || ! accountReq . data ?. name ) {
58+ return null ;
59+ }
60+ return { ...data , name : accountReq . data . name } ;
4561 }
46- return { id, name : accountReq . data . name , type : "person" , email } ;
62+ if ( data . name === undefined ) return null ;
63+ return data as {
64+ id : string ;
65+ name : string ; // typescript is being dumb
66+ type : AgentType ;
67+ email ?: string ;
68+ } ;
69+ } ;
70+
71+ export const createGroupInvitation = async ( {
72+ client,
73+ groupId,
74+ admin = false ,
75+ } : {
76+ client : DGSupabaseClient ;
77+ groupId : string ;
78+ admin : boolean ;
79+ } ) : Promise < string | null > => {
80+ const userData = await getSessionBaseUserData ( client ) ;
81+ if ( ! userData ) return null ;
82+ const membershipReq = await client
83+ . from ( "group_membership" )
84+ . select ( "admin" )
85+ . eq ( "group_id" , groupId )
86+ . eq ( "member_id" , userData . id )
87+ . maybeSingle ( ) ;
88+ if ( membershipReq . data ?. admin !== true ) return null ;
89+ const { data, error } = await client . rpc ( "create_secret_token" , {
90+ v_payload : JSON . stringify ( { groupId, type : "groupInvitation" , admin } ) ,
91+ expiry_interval : "60d" ,
92+ } ) ;
93+ if ( error || ! data ) return null ;
94+ return data ;
95+ } ;
96+
97+ export const acceptGroupInvitation = async (
98+ client : DGSupabaseClient ,
99+ token : string ,
100+ ) : Promise < string | null > => {
101+ const userData = await getSessionBaseUserData ( client ) ;
102+ if ( ! userData ) return "Not logged in" ;
103+ const { data, error } = await client . rpc ( "accept_group_invitation" , {
104+ token,
105+ } ) ;
106+ if ( error || ! data ) return "This token does not exist" ;
107+ return null ;
47108} ;
0 commit comments