syrup-rail-postgres 0.5.0

Canonical provider-neutral PostgreSQL schema contract and SQLx orchestration for Syrup Rail
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use std::time::Duration;

use chrono::{DateTime, Utc};
use sqlx::{PgPool, Row};
use syrup_rail::{
    ActorId, BillingScopeId, GatewayAccountId, GatewayLifecycleAccount,
    GatewayLifecycleQuarantineReason, GatewayLifecycleQuarantineResolutionReason,
};
use uuid::Uuid;

use crate::lifecycle_reconciliation::{
    GatewayLifecycleReconciliationError, ensure_account, set_timeouts,
};

const MAX_REVIEW_PAGE_SIZE: i64 = 101;
const INVALID_QUARANTINE_REASON: &str = "canonical gateway lifecycle quarantine reason is invalid";
const INVALID_QUARANTINE_HISTORY: &str =
    "canonical gateway lifecycle quarantine resolution history is invalid";

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GatewayLifecycleQuarantineAlert {
    unresolved_count: i64,
    oldest_first_seen_at: DateTime<Utc>,
    latest_last_seen_at: DateTime<Utc>,
}

impl GatewayLifecycleQuarantineAlert {
    pub const fn unresolved_count(self) -> i64 {
        self.unresolved_count
    }

    pub const fn oldest_first_seen_at(self) -> DateTime<Utc> {
        self.oldest_first_seen_at
    }

    pub const fn latest_last_seen_at(self) -> DateTime<Utc> {
        self.latest_last_seen_at
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GatewayLifecycleQuarantineResolutionRecord {
    quarantine_id: Uuid,
    actor_id: ActorId,
    reason: GatewayLifecycleQuarantineResolutionReason,
    observed_occurrence_count: i64,
    observed_last_seen_at: DateTime<Utc>,
    resolved_at: DateTime<Utc>,
}

impl GatewayLifecycleQuarantineResolutionRecord {
    pub const fn quarantine_id(&self) -> Uuid {
        self.quarantine_id
    }

    pub const fn actor_id(&self) -> ActorId {
        self.actor_id
    }

    pub const fn reason(&self) -> &GatewayLifecycleQuarantineResolutionReason {
        &self.reason
    }

    pub const fn observed_occurrence_count(&self) -> i64 {
        self.observed_occurrence_count
    }

    pub const fn observed_last_seen_at(&self) -> DateTime<Utc> {
        self.observed_last_seen_at
    }

    pub const fn resolved_at(&self) -> DateTime<Utc> {
        self.resolved_at
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GatewayLifecycleQuarantineReviewRecord {
    id: Uuid,
    billing_scope_id: BillingScopeId,
    gateway_account_id: GatewayAccountId,
    transaction_id: Option<String>,
    order_id: Option<String>,
    reason: GatewayLifecycleQuarantineReason,
    first_seen_at: DateTime<Utc>,
    last_seen_at: DateTime<Utc>,
    occurrence_count: i64,
    resolution_count: i64,
    latest_resolution: Option<GatewayLifecycleQuarantineResolutionRecord>,
}

impl GatewayLifecycleQuarantineReviewRecord {
    pub const fn id(&self) -> Uuid {
        self.id
    }

    pub const fn billing_scope_id(&self) -> BillingScopeId {
        self.billing_scope_id
    }

    pub const fn gateway_account_id(&self) -> GatewayAccountId {
        self.gateway_account_id
    }

    /// Persisted review evidence only; this is not a provider-command capability.
    pub fn transaction_id(&self) -> Option<&str> {
        self.transaction_id.as_deref()
    }

    /// Persisted review evidence only; this is not a provider-command capability.
    pub fn order_id(&self) -> Option<&str> {
        self.order_id.as_deref()
    }

    pub const fn reason(&self) -> GatewayLifecycleQuarantineReason {
        self.reason
    }

    pub const fn first_seen_at(&self) -> DateTime<Utc> {
        self.first_seen_at
    }

    pub const fn last_seen_at(&self) -> DateTime<Utc> {
        self.last_seen_at
    }

    pub const fn occurrence_count(&self) -> i64 {
        self.occurrence_count
    }

    pub const fn resolution_count(&self) -> i64 {
        self.resolution_count
    }

    pub const fn latest_resolution(&self) -> Option<&GatewayLifecycleQuarantineResolutionRecord> {
        self.latest_resolution.as_ref()
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GatewayLifecycleQuarantineResolutionOutcome {
    Resolved(GatewayLifecycleQuarantineResolutionRecord),
    Replayed(GatewayLifecycleQuarantineResolutionRecord),
    AlreadyResolved,
    Stale,
    NotFound,
}

pub async fn claim_gateway_lifecycle_quarantine_alert(
    pool: &PgPool,
    account: &GatewayLifecycleAccount,
    alert_after: Duration,
) -> Result<Option<GatewayLifecycleQuarantineAlert>, GatewayLifecycleReconciliationError> {
    let alert_after_seconds = i64::try_from(alert_after.as_secs()).map_err(|_| {
        GatewayLifecycleReconciliationError::InvalidState(
            "gateway lifecycle quarantine alert cadence is too large",
        )
    })?;
    let mut transaction = pool.begin().await?;
    set_timeouts(&mut transaction).await?;
    ensure_account(&mut transaction, account).await?;
    let row = sqlx::query(
        r#"
        WITH due AS MATERIALIZED (
            SELECT EXISTS (
                SELECT 1
                FROM billing_gateway_lifecycle_quarantines
                WHERE billing_scope_id = $1
                    AND gateway_account_id = $2
                    AND resolved_at IS NULL
                    AND (
                        last_operator_alerted_at IS NULL
                        OR last_operator_alerted_at
                            <= now() - ($3::bigint * interval '1 second')
                    )
            ) AS should_claim
        ),
        claimed AS (
            UPDATE billing_gateway_lifecycle_quarantines
            SET last_operator_alerted_at = now()
            WHERE billing_scope_id = $1
                AND gateway_account_id = $2
                AND resolved_at IS NULL
                AND (SELECT should_claim FROM due)
            RETURNING id
        )
        SELECT
            COUNT(*)::bigint AS unresolved_count,
            MIN(first_seen_at) AS oldest_first_seen_at,
            MAX(last_seen_at) AS latest_last_seen_at
        FROM billing_gateway_lifecycle_quarantines
        WHERE billing_scope_id = $1
            AND gateway_account_id = $2
            AND resolved_at IS NULL
        HAVING EXISTS (SELECT 1 FROM claimed)
        "#,
    )
    .bind(account.billing_scope_id().as_uuid())
    .bind(account.gateway_account_id().as_uuid())
    .bind(alert_after_seconds)
    .fetch_optional(&mut *transaction)
    .await?;
    transaction.commit().await?;
    row.map(|row| {
        Ok(GatewayLifecycleQuarantineAlert {
            unresolved_count: row.try_get("unresolved_count")?,
            oldest_first_seen_at: row.try_get("oldest_first_seen_at")?,
            latest_last_seen_at: row.try_get("latest_last_seen_at")?,
        })
    })
    .transpose()
}

pub async fn gateway_lifecycle_quarantine_review_page(
    pool: &PgPool,
    limit: i64,
    cursor: Option<(DateTime<Utc>, Uuid)>,
) -> Result<Vec<GatewayLifecycleQuarantineReviewRecord>, GatewayLifecycleReconciliationError> {
    if !(1..=MAX_REVIEW_PAGE_SIZE).contains(&limit) {
        return Err(GatewayLifecycleReconciliationError::InvalidState(
            "gateway lifecycle quarantine review page size is invalid",
        ));
    }
    let rows = sqlx::query(
        r#"
        WITH page AS (
            SELECT quarantine.id,
                quarantine.billing_scope_id,
                quarantine.gateway_account_id,
                quarantine.gateway_transaction_id,
                quarantine.gateway_order_id,
                quarantine.reason_code,
                quarantine.first_seen_at,
                quarantine.last_seen_at,
                quarantine.occurrence_count
            FROM billing_gateway_lifecycle_quarantines quarantine
            WHERE quarantine.resolved_at IS NULL
                AND (
                    $1::timestamptz IS NULL
                    OR (quarantine.first_seen_at, quarantine.id)
                        > ($1::timestamptz, $2::uuid)
                )
            ORDER BY quarantine.first_seen_at, quarantine.id
            LIMIT $3
        )
        SELECT page.*,
            COALESCE(history.resolution_count, 0)::bigint AS resolution_count,
            history.actor_id AS latest_resolution_actor_id,
            history.reason AS latest_resolution_reason,
            history.observed_occurrence_count AS latest_observed_occurrence_count,
            history.observed_last_seen_at AS latest_observed_last_seen_at,
            history.resolved_at AS latest_resolved_at
        FROM page
        LEFT JOIN LATERAL (
            SELECT resolution.actor_id,
                resolution.reason,
                resolution.observed_occurrence_count,
                resolution.observed_last_seen_at,
                resolution.resolved_at,
                COUNT(*) OVER () AS resolution_count
            FROM billing_gateway_lifecycle_quarantine_resolutions resolution
            WHERE resolution.quarantine_id = page.id
            ORDER BY resolution.resolved_at DESC, resolution.id DESC
            LIMIT 1
        ) history ON TRUE
        ORDER BY page.first_seen_at, page.id
        "#,
    )
    .bind(cursor.as_ref().map(|(first_seen_at, _)| first_seen_at))
    .bind(cursor.as_ref().map(|(_, id)| id))
    .bind(limit)
    .fetch_all(pool)
    .await?;
    rows.into_iter().map(review_record).collect()
}

pub async fn resolve_gateway_lifecycle_quarantine(
    pool: &PgPool,
    quarantine_id: Uuid,
    actor_id: ActorId,
    expected_occurrence_count: i64,
    expected_last_seen_at: DateTime<Utc>,
    reason: &GatewayLifecycleQuarantineResolutionReason,
) -> Result<GatewayLifecycleQuarantineResolutionOutcome, GatewayLifecycleReconciliationError> {
    if expected_occurrence_count <= 0 {
        return Err(GatewayLifecycleReconciliationError::InvalidState(
            "gateway lifecycle quarantine observation is invalid",
        ));
    }
    let mut transaction = pool.begin().await?;
    set_timeouts(&mut transaction).await?;
    let quarantine = sqlx::query(
        r#"
        SELECT occurrence_count, last_seen_at, resolved_at
        FROM billing_gateway_lifecycle_quarantines
        WHERE id = $1
        FOR UPDATE
        "#,
    )
    .bind(quarantine_id)
    .fetch_optional(&mut *transaction)
    .await?;
    let Some(quarantine) = quarantine else {
        transaction.commit().await?;
        return Ok(GatewayLifecycleQuarantineResolutionOutcome::NotFound);
    };
    let occurrence_count: i64 = quarantine.try_get("occurrence_count")?;
    let last_seen_at: DateTime<Utc> = quarantine.try_get("last_seen_at")?;
    if occurrence_count != expected_occurrence_count || last_seen_at != expected_last_seen_at {
        transaction.commit().await?;
        return Ok(GatewayLifecycleQuarantineResolutionOutcome::Stale);
    }
    if quarantine
        .try_get::<Option<DateTime<Utc>>, _>("resolved_at")?
        .is_some()
    {
        let existing = sqlx::query(
            r#"
            SELECT quarantine_id, actor_id, reason,
                observed_occurrence_count, observed_last_seen_at, resolved_at
            FROM billing_gateway_lifecycle_quarantine_resolutions
            WHERE quarantine_id = $1
                AND observed_occurrence_count = $2
                AND observed_last_seen_at = $3
            "#,
        )
        .bind(quarantine_id)
        .bind(expected_occurrence_count)
        .bind(expected_last_seen_at)
        .fetch_optional(&mut *transaction)
        .await?;
        transaction.commit().await?;
        return Ok(match existing {
            Some(existing)
                if existing.try_get::<Uuid, _>("actor_id")? == *actor_id.as_uuid()
                    && existing.try_get::<String, _>("reason")? == reason.expose() =>
            {
                GatewayLifecycleQuarantineResolutionOutcome::Replayed(resolution_record(existing)?)
            }
            _ => GatewayLifecycleQuarantineResolutionOutcome::AlreadyResolved,
        });
    }
    let resolution = sqlx::query(
        r#"
        INSERT INTO billing_gateway_lifecycle_quarantine_resolutions (
            quarantine_id, actor_id, reason,
            observed_occurrence_count, observed_last_seen_at
        ) VALUES ($1, $2, $3, $4, $5)
        RETURNING quarantine_id, actor_id, reason,
            observed_occurrence_count, observed_last_seen_at, resolved_at
        "#,
    )
    .bind(quarantine_id)
    .bind(actor_id.as_uuid())
    .bind(reason.expose())
    .bind(expected_occurrence_count)
    .bind(expected_last_seen_at)
    .fetch_one(&mut *transaction)
    .await?;
    let resolved_at: DateTime<Utc> = resolution.try_get("resolved_at")?;
    let updated = sqlx::query(
        r#"
        UPDATE billing_gateway_lifecycle_quarantines
        SET resolved_at = $2
        WHERE id = $1
            AND resolved_at IS NULL
            AND occurrence_count = $3
            AND last_seen_at = $4
        "#,
    )
    .bind(quarantine_id)
    .bind(resolved_at)
    .bind(expected_occurrence_count)
    .bind(expected_last_seen_at)
    .execute(&mut *transaction)
    .await?;
    if updated.rows_affected() != 1 {
        return Err(GatewayLifecycleReconciliationError::InvalidState(
            "locked gateway lifecycle quarantine did not accept its operator resolution",
        ));
    }
    transaction.commit().await?;
    Ok(GatewayLifecycleQuarantineResolutionOutcome::Resolved(
        resolution_record(resolution)?,
    ))
}

fn review_record(
    row: sqlx::postgres::PgRow,
) -> Result<GatewayLifecycleQuarantineReviewRecord, GatewayLifecycleReconciliationError> {
    let latest_resolution = match (
        row.try_get::<Option<Uuid>, _>("latest_resolution_actor_id")?,
        row.try_get::<Option<String>, _>("latest_resolution_reason")?,
        row.try_get::<Option<i64>, _>("latest_observed_occurrence_count")?,
        row.try_get::<Option<DateTime<Utc>>, _>("latest_observed_last_seen_at")?,
        row.try_get::<Option<DateTime<Utc>>, _>("latest_resolved_at")?,
    ) {
        (Some(actor_id), Some(reason), Some(count), Some(last_seen_at), Some(resolved_at)) => {
            Some(GatewayLifecycleQuarantineResolutionRecord {
                quarantine_id: row.try_get("id")?,
                actor_id: ActorId::new(actor_id),
                reason: GatewayLifecycleQuarantineResolutionReason::new(reason).map_err(|_| {
                    GatewayLifecycleReconciliationError::InvalidState(INVALID_QUARANTINE_HISTORY)
                })?,
                observed_occurrence_count: count,
                observed_last_seen_at: last_seen_at,
                resolved_at,
            })
        }
        (None, None, None, None, None) => None,
        _ => {
            return Err(GatewayLifecycleReconciliationError::InvalidState(
                INVALID_QUARANTINE_HISTORY,
            ));
        }
    };
    let resolution_count: i64 = row.try_get("resolution_count")?;
    if (resolution_count == 0) != latest_resolution.is_none() {
        return Err(GatewayLifecycleReconciliationError::InvalidState(
            INVALID_QUARANTINE_HISTORY,
        ));
    }
    Ok(GatewayLifecycleQuarantineReviewRecord {
        id: row.try_get("id")?,
        billing_scope_id: BillingScopeId::new(row.try_get("billing_scope_id")?),
        gateway_account_id: GatewayAccountId::new(row.try_get("gateway_account_id")?),
        transaction_id: row.try_get("gateway_transaction_id")?,
        order_id: row.try_get("gateway_order_id")?,
        reason: quarantine_reason(&row.try_get::<String, _>("reason_code")?)?,
        first_seen_at: row.try_get("first_seen_at")?,
        last_seen_at: row.try_get("last_seen_at")?,
        occurrence_count: row.try_get("occurrence_count")?,
        resolution_count,
        latest_resolution,
    })
}

fn resolution_record(
    row: sqlx::postgres::PgRow,
) -> Result<GatewayLifecycleQuarantineResolutionRecord, GatewayLifecycleReconciliationError> {
    Ok(GatewayLifecycleQuarantineResolutionRecord {
        quarantine_id: row.try_get("quarantine_id")?,
        actor_id: ActorId::new(row.try_get("actor_id")?),
        reason: GatewayLifecycleQuarantineResolutionReason::new(
            row.try_get::<String, _>("reason")?,
        )
        .map_err(|_| {
            GatewayLifecycleReconciliationError::InvalidState(INVALID_QUARANTINE_HISTORY)
        })?,
        observed_occurrence_count: row.try_get("observed_occurrence_count")?,
        observed_last_seen_at: row.try_get("observed_last_seen_at")?,
        resolved_at: row.try_get("resolved_at")?,
    })
}

fn quarantine_reason(
    value: &str,
) -> Result<GatewayLifecycleQuarantineReason, GatewayLifecycleReconciliationError> {
    match value {
        "ambiguous_reversal_success" => {
            Ok(GatewayLifecycleQuarantineReason::AmbiguousReversalSuccess)
        }
        "invalid_refund_economics" => Ok(GatewayLifecycleQuarantineReason::InvalidRefundEconomics),
        "malformed_report_structure" => {
            Ok(GatewayLifecycleQuarantineReason::MalformedReportStructure)
        }
        _ => Err(GatewayLifecycleReconciliationError::InvalidState(
            INVALID_QUARANTINE_REASON,
        )),
    }
}

#[cfg(test)]
mod tests {
    use std::error::Error;

    use super::*;
    use crate::{
        record_gateway_lifecycle_quarantines,
        test_support::{TestDatabase, create_gateway_account},
    };
    use syrup_rail::{GatewayLifecycleQuarantine, GatewayProviderKey};

    #[tokio::test]
    async fn alert_review_resolution_replay_and_reopen_are_exact_and_auditable()
    -> Result<(), Box<dyn Error>> {
        let database = TestDatabase::start("rail_quarantine").await?;
        let fixture = create_gateway_account(&database.pool, "nmi").await?;
        let account = GatewayLifecycleAccount::new(
            BillingScopeId::new(fixture.billing_scope_id),
            GatewayAccountId::new(fixture.gateway_account_id),
            GatewayProviderKey::new("nmi")?,
        );
        let quarantine = GatewayLifecycleQuarantine::new(
            Some(syrup_rail::GatewayTransactionId::new("txn-review")?),
            None,
            GatewayLifecycleQuarantineReason::MalformedReportStructure,
        )?;
        record_gateway_lifecycle_quarantines(
            &database.pool,
            &account,
            std::slice::from_ref(&quarantine),
        )
        .await?;

        let alert = claim_gateway_lifecycle_quarantine_alert(
            &database.pool,
            &account,
            Duration::from_secs(3_600),
        )
        .await?
        .expect("first unresolved alert is due");
        assert_eq!(alert.unresolved_count(), 1);
        assert!(
            claim_gateway_lifecycle_quarantine_alert(
                &database.pool,
                &account,
                Duration::from_secs(3_600),
            )
            .await?
            .is_none()
        );

        let page = gateway_lifecycle_quarantine_review_page(&database.pool, 10, None).await?;
        assert_eq!(page.len(), 1);
        let incident = &page[0];
        assert_eq!(incident.billing_scope_id(), account.billing_scope_id());
        assert_eq!(incident.gateway_account_id(), account.gateway_account_id());
        assert_eq!(incident.transaction_id(), Some("txn-review"));
        assert_eq!(
            incident.reason(),
            GatewayLifecycleQuarantineReason::MalformedReportStructure
        );
        assert_eq!(incident.resolution_count(), 0);

        let actor = ActorId::new(Uuid::now_v7());
        let reason = GatewayLifecycleQuarantineResolutionReason::new("provider report reviewed")?;
        let resolved = resolve_gateway_lifecycle_quarantine(
            &database.pool,
            incident.id(),
            actor,
            incident.occurrence_count(),
            incident.last_seen_at(),
            &reason,
        )
        .await?;
        let GatewayLifecycleQuarantineResolutionOutcome::Resolved(resolution) = resolved else {
            panic!("expected a new resolution");
        };
        assert_eq!(resolution.actor_id(), actor);
        assert_eq!(resolution.reason(), &reason);
        assert!(
            gateway_lifecycle_quarantine_review_page(&database.pool, 10, None)
                .await?
                .is_empty()
        );

        assert!(matches!(
            resolve_gateway_lifecycle_quarantine(
                &database.pool,
                incident.id(),
                actor,
                incident.occurrence_count(),
                incident.last_seen_at(),
                &reason,
            )
            .await?,
            GatewayLifecycleQuarantineResolutionOutcome::Replayed(_)
        ));

        record_gateway_lifecycle_quarantines(&database.pool, &account, &[quarantine]).await?;
        let reopened = gateway_lifecycle_quarantine_review_page(&database.pool, 10, None).await?;
        assert_eq!(reopened.len(), 1);
        assert_eq!(reopened[0].resolution_count(), 1);
        assert_eq!(reopened[0].latest_resolution(), Some(&resolution));
        assert!(matches!(
            resolve_gateway_lifecycle_quarantine(
                &database.pool,
                reopened[0].id(),
                actor,
                incident.occurrence_count(),
                incident.last_seen_at(),
                &reason,
            )
            .await?,
            GatewayLifecycleQuarantineResolutionOutcome::Stale
        ));

        let wrong_provider = GatewayLifecycleAccount::new(
            account.billing_scope_id(),
            account.gateway_account_id(),
            GatewayProviderKey::new("other")?,
        );
        assert!(matches!(
            claim_gateway_lifecycle_quarantine_alert(
                &database.pool,
                &wrong_provider,
                Duration::ZERO,
            )
            .await,
            Err(GatewayLifecycleReconciliationError::AccountNotFound)
        ));

        database.cleanup().await?;
        Ok(())
    }
}