package main import ( "context" "encoding/json" "fmt" "atlas9.dev/c/core" "atlas9.dev/c/core/outbox" "atlas9.dev/c/core/tokens" "atlas9.dev/c/mail/mail_ses" ) type UserRegisteredEvent struct { UserID core.ID Email string } const UserRegisteredEventType = "user.registered" func EmitUserRegistered(ctx context.Context, emitter outbox.Emitter, userID core.ID, email string) error { payload, err := json.Marshal(UserRegisteredEvent{ UserID: userID, Email: email, }) if err != nil { return fmt.Errorf("marshaling event payload: %w", err) } return emitter.Emit(ctx, UserRegisteredEventType, userID.String(), payload, nil) } type VerificationData struct { UserID core.ID } func SendWelcomeEmail(ctx context.Context, baseURL string, verificationTokens tokens.Store[VerificationData], mailer *mail_ses.Sender, event outbox.Event) error { var dat UserRegisteredEvent err := json.Unmarshal(event.Payload, &dat) if err != nil { return err } tok, err := verificationTokens.Put(ctx, dat.UserID.String(), VerificationData{ UserID: dat.UserID, }) if err != nil { return fmt.Errorf("creating verification token: %w", err) } verifyURL := baseURL + "/verify?token=" + tok.Key err = mailer.Send(ctx, dat.Email, "Verify your email", "Click here to verify your email: "+verifyURL) if err != nil { return err } return nil }