Skip to content

Commit

Permalink
Replace updateUser with onboardUser
Browse files Browse the repository at this point in the history
  • Loading branch information
KilianBoute committed Jul 2, 2024
1 parent defa578 commit 2d39307
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 5 deletions.
43 changes: 43 additions & 0 deletions api/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
12 changes: 12 additions & 0 deletions api/database/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions api/database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
8 changes: 6 additions & 2 deletions api/routers/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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..
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions ui/src/app/onboarding/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -37,7 +37,7 @@ const Onboarding: React.FC = () => {
const [isRecording, setIsRecording] = useState(false);
const [videoFile, setVideoFile] = useState<Blob | null>(null);
const [isSubmitted, setIsSubmitted] = useState(false);
const [updateUser] = useUpdateUserMutation();
const [onboardUser] = useOnboardUserMutation();


const handleRecordingComplete = (recordedChunks: Blob[]) => {
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions ui/src/lib/services/users/userApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}),
}),
});

Expand All @@ -64,4 +73,5 @@ export const {
useUpdateUserMutation,
useDeleteUserMutation,
useInviteUserMutation,
useOnboardUserMutation
} = userApi;

0 comments on commit 2d39307

Please sign in to comment.