@@ -59,6 +59,7 @@ import {
5959 type SchemaInsert ,
6060 type SchemaRow ,
6161 type SecretInsert ,
62+ type SenderEmailStatusType ,
6263 scheduledTriggers ,
6364 schemas ,
6465 secrets ,
@@ -863,6 +864,8 @@ export async function getEmails(
863864 . select ( {
864865 id : emails . id ,
865866 name : emails . name ,
867+ senderEmail : emails . senderEmail ,
868+ senderEmailStatus : emails . senderEmailStatus ,
866869 createdAt : emails . createdAt ,
867870 updatedAt : emails . updatedAt ,
868871 } )
@@ -2169,6 +2172,75 @@ export async function getOrganizationBillingInfo(
21692172 return organization ;
21702173}
21712174
2175+ /**
2176+ * Get the sender email configuration for an email
2177+ */
2178+ export async function getEmailSenderEmail (
2179+ db : ReturnType < typeof createDatabase > ,
2180+ emailId : string ,
2181+ organizationId : string
2182+ ) : Promise <
2183+ | {
2184+ senderEmail : string | null ;
2185+ senderEmailStatus : SenderEmailStatusType | null ;
2186+ }
2187+ | undefined
2188+ > {
2189+ const [ email ] = await db
2190+ . select ( {
2191+ senderEmail : emails . senderEmail ,
2192+ senderEmailStatus : emails . senderEmailStatus ,
2193+ } )
2194+ . from ( emails )
2195+ . where (
2196+ and ( eq ( emails . id , emailId ) , eq ( emails . organizationId , organizationId ) )
2197+ )
2198+ . limit ( 1 ) ;
2199+ return email ;
2200+ }
2201+
2202+ /**
2203+ * Update the sender email and status for an email
2204+ */
2205+ export async function updateEmailSenderEmail (
2206+ db : ReturnType < typeof createDatabase > ,
2207+ emailId : string ,
2208+ organizationId : string ,
2209+ senderEmailAddress : string ,
2210+ status : SenderEmailStatusType
2211+ ) {
2212+ await db
2213+ . update ( emails )
2214+ . set ( {
2215+ senderEmail : senderEmailAddress ,
2216+ senderEmailStatus : status ,
2217+ updatedAt : new Date ( ) ,
2218+ } )
2219+ . where (
2220+ and ( eq ( emails . id , emailId ) , eq ( emails . organizationId , organizationId ) )
2221+ ) ;
2222+ }
2223+
2224+ /**
2225+ * Clear the sender email configuration for an email
2226+ */
2227+ export async function clearEmailSenderEmail (
2228+ db : ReturnType < typeof createDatabase > ,
2229+ emailId : string ,
2230+ organizationId : string
2231+ ) {
2232+ await db
2233+ . update ( emails )
2234+ . set ( {
2235+ senderEmail : null ,
2236+ senderEmailStatus : null ,
2237+ updatedAt : new Date ( ) ,
2238+ } )
2239+ . where (
2240+ and ( eq ( emails . id , emailId ) , eq ( emails . organizationId , organizationId ) )
2241+ ) ;
2242+ }
2243+
21722244/**
21732245 * Derive user plan from organization billing info.
21742246 * Pro if has active subscription OR canceled but still in billing period.
@@ -2851,6 +2923,31 @@ export async function isOrganizationOwner(
28512923 return ! ! membership ;
28522924}
28532925
2926+ /**
2927+ * Check if a user is an admin or owner of an organization
2928+ */
2929+ export async function isOrganizationAdminOrOwner (
2930+ db : ReturnType < typeof createDatabase > ,
2931+ organizationId : string ,
2932+ userId : string
2933+ ) : Promise < boolean > {
2934+ const [ membership ] = await db
2935+ . select ( { role : memberships . role } )
2936+ . from ( memberships )
2937+ . where (
2938+ and (
2939+ eq ( memberships . userId , userId ) ,
2940+ eq ( memberships . organizationId , organizationId )
2941+ )
2942+ )
2943+ . limit ( 1 ) ;
2944+
2945+ return (
2946+ membership ?. role === OrganizationRole . OWNER ||
2947+ membership ?. role === OrganizationRole . ADMIN
2948+ ) ;
2949+ }
2950+
28542951/**
28552952 * Add or update a user's membership in an organization
28562953 *
0 commit comments