diff --git a/apps/auth-service/src/emails/mod.rs b/apps/auth-service/src/emails/mod.rs
index 1a6058cd3..28aca5e66 100644
--- a/apps/auth-service/src/emails/mod.rs
+++ b/apps/auth-service/src/emails/mod.rs
@@ -238,6 +238,30 @@ impl EmailApplication {
Ok(())
}
+ /// Records `EmailVerified` for `user_id`'s address `email_id` as
+ /// `context`'s, then `PrimaryEmailChanged` with no previous address if it
+ /// became the `primary` one, the account having had none. Shared by
+ /// `verify_email` and `Added::verified`.
+ async fn record_verified(
+ tx: &mut DatabaseTransaction<'_>,
+ context: &EventContext,
+ user_id: UserId,
+ email_id: EmailId,
+ primary: bool,
+ ) -> Result<(), DatabaseError> {
+ let subject = Some(user_id);
+ tx.record(context, subject, DomainEvent::EmailVerified { email_id })
+ .await?;
+ if primary {
+ let event = DomainEvent::PrimaryEmailChanged {
+ email_id,
+ previous_email_id: None,
+ };
+ tx.record(context, subject, event).await?;
+ }
+ Ok(())
+ }
+
/// Refuses another address for an account that holds `count`.
const fn room(count: usize) -> Result<(), EmailError> {
if count >= MAX_EMAILS {
@@ -402,18 +426,7 @@ impl Added<'_> {
})
.await?;
self.record(tx, id).await?;
- let subject = Some(self.user_id);
- tx.record(
- self.context,
- subject,
- DomainEvent::EmailVerified { email_id: id },
- )
- .await?;
- let primary = DomainEvent::PrimaryEmailChanged {
- email_id: id,
- previous_email_id: None,
- };
- tx.record(self.context, subject, primary).await?;
+ EmailApplication::record_verified(tx, self.context, self.user_id, id, true).await?;
Ok(())
}
@@ -536,17 +549,14 @@ impl AccountService {
Err(DatabaseError::Conflict(_)) => return Err(EmailError::OwnedElsewhere.into()),
Err(error) => return Err(error.into()),
};
- let email_id = pending.email_id;
- let subject = Some(pending.user_id);
- tx.record(&context, subject, DomainEvent::EmailVerified { email_id })
- .await?;
- if primary {
- let event = DomainEvent::PrimaryEmailChanged {
- email_id,
- previous_email_id: None,
- };
- tx.record(&context, subject, event).await?;
- }
+ EmailApplication::record_verified(
+ &mut tx,
+ &context,
+ pending.user_id,
+ pending.email_id,
+ primary,
+ )
+ .await?;
tx.commit().await?;
Ok(Verified {
address: pending.address,
diff --git a/apps/auth-service/src/history/mod.rs b/apps/auth-service/src/history/mod.rs
index fa3bb94f8..e2e4f9fbd 100644
--- a/apps/auth-service/src/history/mod.rs
+++ b/apps/auth-service/src/history/mod.rs
@@ -178,16 +178,26 @@ impl HistoryItem {
user: UserId,
event_id: EventId,
) -> Result