@@ -186,25 +186,42 @@ export async function bumpEmailAddress(options: { unindexed?: boolean } = {}) {
186186 return mailbox ;
187187}
188188
189+ // Type for outbox email items (simplified - full type is EmailOutboxCrud["Server"]["Read"])
190+ type OutboxEmail = {
191+ id : string ,
192+ subject ?: string ,
193+ status : string ,
194+ simple_status : string ,
195+ to ?: {
196+ type : string ,
197+ user_id ?: string ,
198+ [ key : string ] : unknown ,
199+ } ,
200+ [ key : string ] : unknown ,
201+ } ;
202+
189203// Helper to get emails from the outbox, filtered by subject if provided
190- export async function getOutboxEmails ( options ?: { subject ?: string } ) {
204+ export async function getOutboxEmails ( options ?: { subject ?: string } ) : Promise < OutboxEmail [ ] > {
191205 const listResponse = await niceBackendFetch ( "/api/v1/emails/outbox" , {
192206 method : "GET" ,
193207 accessType : "server" ,
194208 } ) ;
209+ const items = listResponse . body . items as OutboxEmail [ ] ;
195210 if ( options ?. subject ) {
196- // eslint-disable-next-line @typescript-eslint/no-explicit-any
197- return listResponse . body . items . filter ( ( e : any ) => e . subject === options . subject ) ;
211+ return items . filter ( ( e ) => e . subject === options . subject ) ;
198212 }
199- return listResponse . body . items ;
213+ return items ;
200214}
201215
202- // Helper to poll the outbox until an email with the expected subject and status appears
203- export async function waitForOutboxEmailWithStatus ( subject : string , status : string ) {
216+ // Helper to poll the outbox until the most recent email with the expected subject has the expected status.
217+ // Note: emails are returned ordered by createdAt desc (newest first), so we check emails[0] specifically
218+ // to ensure we're waiting for the MOST RECENT email, not an older one with the same subject.
219+ export async function waitForOutboxEmailWithStatus ( subject : string , status : string ) : Promise < OutboxEmail [ ] > {
204220 const maxRetries = 24 ;
205- let emails : any [ ] = [ ] ;
221+ let emails : OutboxEmail [ ] = [ ] ;
206222 for ( let i = 0 ; i < maxRetries ; i ++ ) {
207223 emails = await getOutboxEmails ( { subject } ) ;
224+ // Check the most recent email (first in the list due to createdAt desc ordering)
208225 if ( emails . length > 0 && emails [ 0 ] . status === status ) {
209226 return emails ;
210227 }
0 commit comments