diff --git a/api/controllers/users.go b/api/controllers/users.go index af1f5af..6e326be 100644 --- a/api/controllers/users.go +++ b/api/controllers/users.go @@ -333,3 +333,46 @@ func UpdateUser(c *gin.Context) { "message": "User updated successfully", }) } + +// OnboardUser godoc +// @Router /api/users/onboard [post] +// @ID onboardUser +// @Tags users +// @Summary Onboard user +// @Description Onboard user +// @Accept json +// @Produce json +// @Param user body models.User true "User data" +// @Success 201 {object} models.User +// @Failure 400 +// @Failure 409 +// @Failure 500 +func OnboardUser(c *gin.Context) { + var user models.User + if err := c.ShouldBindJSON(&user); err != nil { + c.JSON(400, gin.H{ + "error": "Invalid user data", + }) + return + } + + err := database.OnboardUser(user) + if err != nil { + switch err { + case database.ErrUserAlreadyExists: + c.JSON(409, gin.H{ + "error": "User already exists", + }) + default: + c.JSON(500, gin.H{ + "error": "Failed to add user", + }) + } + return + } + + c.JSON(201, gin.H{ + "message": "User onboarded successfully", + "user": user, + }) +} diff --git a/api/database/file.go b/api/database/file.go index b6e7058..6d44dd8 100644 --- a/api/database/file.go +++ b/api/database/file.go @@ -86,6 +86,18 @@ func UpdateUserFromFile(user models.User) error { return errors.New("user not found") } +func OnboardUserToFile(user models.User) error { + users := GetUsersFromFile() + for i, u := range users { + if u.Email == user.Email { + user.Status = "onboarded" + users[i] = user + return nil + } + } + return errors.New("user not found") +} + func GetLocationsFromFile() []models.Location { locations := data.Locations return locations diff --git a/api/database/main.go b/api/database/main.go index 2a15f56..b8c1040 100644 --- a/api/database/main.go +++ b/api/database/main.go @@ -22,6 +22,9 @@ func DeleteUser(id int) error { func UpdateUser(user models.User) error { return UpdateUserFromFile(user) } +func OnboardUser(user models.User) error { + return OnboardUserToFile(user) +} func GetLocations() []models.Location { return GetLocationsFromFile() } diff --git a/api/routers/http/routes.go b/api/routers/http/routes.go index c56fbf3..22fd4c7 100644 --- a/api/routers/http/routes.go +++ b/api/routers/http/routes.go @@ -12,8 +12,8 @@ func AddRoutes(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.RouterG { api.POST("/login", authMiddleware.LoginHandler) - api.PATCH("/users/:id", func(c *gin.Context) { - controllers.UpdateUser(c) + api.POST("/users/onboard", func(c *gin.Context) { + controllers.OnboardUser(c) }) // Secured endpoints.. @@ -46,6 +46,10 @@ func AddRoutes(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.RouterG controllers.DeleteUser(c) }) + api.PATCH("/users/:id", func(c *gin.Context) { + controllers.UpdateUser(c) + }) + // Locations api.GET("/locations", func(c *gin.Context) { controllers.GetLocations(c) diff --git a/ui/src/app/onboarding/page.tsx b/ui/src/app/onboarding/page.tsx index 3e1d360..851f6fa 100644 --- a/ui/src/app/onboarding/page.tsx +++ b/ui/src/app/onboarding/page.tsx @@ -16,7 +16,7 @@ import FormComponent from "./components/FormComponent"; import { SubmitHandler } from "react-hook-form"; import * as z from "zod"; import { useRouter } from "next/navigation"; -import { useUpdateUserMutation } from "@/lib/services/users/userApi"; +import { useOnboardUserMutation } from "@/lib/services/users/userApi"; const schema = z.object({ firstName: z.string().min(1, "First Name is required"), @@ -37,7 +37,7 @@ const Onboarding: React.FC = () => { const [isRecording, setIsRecording] = useState(false); const [videoFile, setVideoFile] = useState(null); const [isSubmitted, setIsSubmitted] = useState(false); - const [updateUser] = useUpdateUserMutation(); + const [onboardUser] = useOnboardUserMutation(); const handleRecordingComplete = (recordedChunks: Blob[]) => { @@ -54,7 +54,7 @@ const Onboarding: React.FC = () => { const formDataWithVideo = { ...data, video: videoFile }; try { - await updateUser(formDataWithVideo).unwrap(); + await onboardUser(formDataWithVideo).unwrap(); console.log("User updated successfully"); setIsSubmitted(true); } catch (error) { diff --git a/ui/src/lib/services/users/userApi.ts b/ui/src/lib/services/users/userApi.ts index ba91433..75efbd2 100644 --- a/ui/src/lib/services/users/userApi.ts +++ b/ui/src/lib/services/users/userApi.ts @@ -54,6 +54,15 @@ export const userApi = createApi({ }), invalidatesTags: ["User"], }), + onboardUser: build.mutation({ + query: (user) => ({ + url: `users/onboard`, + method: "POST", + headers: { "Content-Type": "application/json" }, + body: user, + }), + invalidatesTags: ["User"], + }), }), }); @@ -64,4 +73,5 @@ export const { useUpdateUserMutation, useDeleteUserMutation, useInviteUserMutation, + useOnboardUserMutation } = userApi;