feat: activity log producers

This commit is contained in:
fsylvain committed 2026-10-06 22:25:23 +02:00
1 parent 50b1649965
commit e37a46a027
93 files changed
+3396 -440

No files matched your search

+21 -4
View File
@@ -1,9 +1,11 @@
package useCases
import (
"RenewCMS/internal/domain/audit"
"RenewCMS/internal/domain/hash"
"RenewCMS/internal/domain/mail"
"RenewCMS/internal/domain/user"
"context"
"errors"
"fmt"
"time"
@@ -16,6 +18,7 @@ type CreateUserUseCase struct {
hasher hash.Hasher
mailRepository mail.Repository
publicURL string
recorder audit.Recorder
}
type CreateUserCommand struct {
@@ -29,11 +32,25 @@ type MailValidationData struct {
VerificationCode string
}
func NewCreateUserUseCase(userRepository user.Repository, hasher hash.Hasher, mailRepository mail.Repository, publicURL string) *CreateUserUseCase {
return &CreateUserUseCase{userRepository, hasher, mailRepository, publicURL}
func NewCreateUserUseCase(userRepository user.Repository, hasher hash.Hasher, mailRepository mail.Repository, publicURL string, recorder audit.Recorder) *CreateUserUseCase {
return &CreateUserUseCase{userRepository, hasher, mailRepository, publicURL, recorder}
}
func (g *CreateUserUseCase) CreateUser(createUser CreateUserCommand) (user.User, error) {
func (g *CreateUserUseCase) CreateUser(ctx context.Context, createUser CreateUserCommand) (user.User, error) {
event := audit.NewEvent(audit.ActionUserRegister, audit.CategoryUsers)
created, err := g.create(createUser)
if err != nil {
event.Actor = audit.AnonymousActor(createUser.Username)
recordFailure(ctx, g.recorder, event, err)
return user.User{}, err
}
event.Actor = audit.UserActor(created)
event.Target = audit.NewTarget(audit.TargetUser, created.ID, created.Username)
g.recorder.Record(ctx, event)
return created, nil
}
func (g *CreateUserUseCase) create(createUser CreateUserCommand) (user.User, error) {
if g.registrationClosed() {
return user.User{}, user.ErrRegistrationClosed
}
@@ -70,7 +87,7 @@ func (g *CreateUserUseCase) CreateUser(createUser CreateUserCommand) (user.User,
Host: g.publicURL,
VerificationCode: rawUuid,
}); err != nil {
sendErr := fmt.Errorf("failed to send verification email: %w", err)
sendErr := fmt.Errorf("%w: %w", user.ErrVerificationEmailNotSent, err)
if deleteErr := g.userRepository.Delete(createdUser.ID); deleteErr != nil {
return user.User{}, errors.Join(sendErr, deleteErr)
}