MCPcopy Create free account
hub / github.com/SwishHQ/spread / InitUser

Method InitUser

src/controller/user_controller.go:85–117  ·  view source on GitHub ↗
(ctx *fiber.Ctx)

Source from the content-addressed store, hash-verified

83}
84
85func (c *userController) InitUser(ctx *fiber.Ctx) error {
86 // Check if any users exist
87 count, err := c.userService.Count(ctx.Context())
88 if err != nil {
89 logger.L.Error("In InitUser: Error getting user count", zap.Error(err))
90 return utils.ErrorResponse(ctx, err.Error())
91 }
92
93 if count > 0 {
94 logger.L.Error("In InitUser: Users already exist, cannot initialize")
95 return utils.ErrorResponse(ctx, "Users already exist. Cannot initialize first user.")
96 }
97
98 // Parse and validate the request
99 user := types.CreateUserRequest{}
100 validationErrors := utils.BindAndValidate(ctx, &user)
101 if len(validationErrors) > 0 {
102 logger.L.Error("In InitUser: Validation errors", zap.Any("validationErrors", validationErrors))
103 return utils.ValidationErrorResponse(ctx, validationErrors)
104 }
105
106 // Force the first user to be an admin
107 user.Roles = []string{"admin"}
108
109 createdUser, err := c.userService.Create(&user)
110 if err != nil {
111 logger.L.Error("In InitUser: Error creating user", zap.Error(err))
112 return utils.ErrorResponse(ctx, err.Error())
113 }
114
115 logger.L.Info("In InitUser: First user created successfully", zap.String("username", createdUser.Username))
116 return utils.SuccessResponse(ctx, createdUser)
117}

Callers

nothing calls this directly

Calls 6

ErrorResponseFunction · 0.92
BindAndValidateFunction · 0.92
ValidationErrorResponseFunction · 0.92
SuccessResponseFunction · 0.92
CountMethod · 0.65
CreateMethod · 0.65

Tested by

no test coverage detected