feat: added db transactions abstraction; cleaned up user registration; repos adapted new abstraction: all repos now support transactions, if present in ctx

This commit is contained in:
2026-09-01 14:55:20 +02:00
parent f87b0a0707
commit 2e3e82e776
14 changed files with 219 additions and 145 deletions
+10 -34
View File
@@ -10,13 +10,13 @@ import (
)
type UserHandler struct {
UserService *domain.UserService
AuthService *domain.AuthService
InviteService *domain.InviteService
UserService *domain.UserService
AuthService *domain.AuthService
RegistrationService *domain.RegistrationService
}
func NewUserHandler(userService *domain.UserService, authService *domain.AuthService, inviteService *domain.InviteService) *UserHandler {
return &UserHandler{UserService: userService, AuthService: authService, InviteService: inviteService}
func NewUserHandler(userService *domain.UserService, authService *domain.AuthService, registrationService *domain.RegistrationService) *UserHandler {
return &UserHandler{UserService: userService, AuthService: authService, RegistrationService: registrationService}
}
// CreateUser handles the creation of a user
@@ -42,41 +42,17 @@ func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
return
}
invite, err := h.InviteService.GetInvite(ctx, payload.InviteCode)
user, err := h.RegistrationService.Register(ctx, payload.InviteCode, payload.Email, payload.Name, payload.Password)
if err != nil {
slog.ErrorContext(ctx, "failed to get invite", slog.Any("error", err))
response.Error(ctx, w, http.StatusBadRequest, "invite is invalid")
return
}
// TODO: Creating user and consuming the invite must be in a transaction.
// The repositories must support tx in context, and the handler must be able to start a transaction
user, err := h.UserService.CreateUser(ctx, payload.Email, payload.Name, payload.Password)
if err != nil {
slog.ErrorContext(ctx, "failed to create user", slog.Any("error", err))
response.Error(ctx, w, http.StatusInternalServerError, "failed to create user")
return
}
err = h.InviteService.ConsumeInvite(ctx, invite.ID, user.ID)
if err != nil {
slog.ErrorContext(ctx, "failed to consume invite", slog.Any("error", err))
// TODO: This should be a transaction rollback, once we have db transactions in the handler
err = h.UserService.DeleteUser(ctx, user.ID)
if err != nil {
slog.ErrorContext(ctx, "failed to delete user again",
slog.Any("error", err),
slog.String("user_id", user.ID),
)
}
response.Error(ctx, w, http.StatusInternalServerError, "failed to consume invite")
slog.ErrorContext(ctx, "failed to register user",
slog.Any("error", err),
slog.Any("payload", payload))
response.Error(ctx, w, http.StatusInternalServerError, "failed to register")
return
}
slog.InfoContext(ctx, "created user successfully",
slog.String("user_id", user.ID),
slog.String("invite_id", invite.ID),
)
response.JSON(ctx, w, http.StatusCreated, user)
}