From 7fda97d09eae0a3837a0663df9d5501d2c6b62dc Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 4 Sep 2024 22:57:15 +0200 Subject: [PATCH] update organization settings --- modules/structs/app.go | 17 +++++++ modules/structs/organizations.go | 5 ++ routers/router/api/v1/app/app.go | 50 +++++++++++++++++++ .../router/api/v1/organization/settings.go | 34 +++++++++++++ routers/router/api/v1/user/user.go | 12 ----- routers/router/router.go | 7 ++- 6 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 modules/structs/app.go create mode 100644 routers/router/api/v1/app/app.go diff --git a/modules/structs/app.go b/modules/structs/app.go new file mode 100644 index 0000000..ec9582e --- /dev/null +++ b/modules/structs/app.go @@ -0,0 +1,17 @@ +package structs + +type GetAppResponse struct { + User AppUser + Organization AppOrganization +} + +type AppUser struct { + ProfilePictureUrl string +} + +type AppOrganization struct { + CompanyName string + PrimaryColor string + LogoUrl string + BannerUrl string +} diff --git a/modules/structs/organizations.go b/modules/structs/organizations.go index 89d58f6..e5806eb 100644 --- a/modules/structs/organizations.go +++ b/modules/structs/organizations.go @@ -34,3 +34,8 @@ type GetOrganizationSettingsResponse struct { LogoUrl string BannerUrl string } + +type UpdateOrganizationSettingsRequest struct { + CompanyName string + PrimaryColor string +} diff --git a/routers/router/api/v1/app/app.go b/routers/router/api/v1/app/app.go new file mode 100644 index 0000000..b028037 --- /dev/null +++ b/routers/router/api/v1/app/app.go @@ -0,0 +1,50 @@ +package app + +import ( + "github.com/gofiber/fiber/v2" + "lms.de/backend/modules/database" + "lms.de/backend/modules/structs" +) + +func GetApp(c *fiber.Ctx) error { + // swagger:operation GET /app app getApp + // --- + // summary: Get app + // consumes: + // - application/json + // produces: + // - application/json + // responses: + // '200': + // description: App fetched successfully + // schema: + // "$ref": "#/definitions/GetAppResponse" + // '400': + // description: Invalid request body + // '500': + // description: Failed to fetch app + + var user structs.User + + database.DB.Model(&structs.User{ + Id: c.Locals("userId").(string), + }).Select("profile_picture_url").First(&user) + + var organization structs.Organization + + database.DB.Model(&structs.Organization{ + Id: c.Locals("organizationId").(string), + }).Select("company_name", "primary_color", "logo_url", "banner_url").First(&organization) + + return c.JSON(structs.GetAppResponse{ + User: structs.AppUser{ + ProfilePictureUrl: user.ProfilePictureUrl, + }, + Organization: structs.AppOrganization{ + CompanyName: organization.CompanyName, + PrimaryColor: organization.PrimaryColor, + LogoUrl: organization.LogoUrl, + BannerUrl: organization.BannerUrl, + }, + }) +} diff --git a/routers/router/api/v1/organization/settings.go b/routers/router/api/v1/organization/settings.go index b34f242..8bc60d1 100644 --- a/routers/router/api/v1/organization/settings.go +++ b/routers/router/api/v1/organization/settings.go @@ -30,3 +30,37 @@ func GetOrganizationSettings(c *fiber.Ctx) error { return c.JSON(organizationSettings) } + +func UpdateOrganizationSettings(c *fiber.Ctx) error { + // swagger:operation PATCH /organization/settings organization updateOrganizationSettings + // --- + // summary: Update settings + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: body + // in: body + // schema: + // "$ref": "#/definitions/GetOrganizationSettingsResponse" + // responses: + // '200': + // description: Settings updated successfully + // '400': + // description: Invalid request body + // '500': + // description: Failed to update settings + + var organizationSettings structs.UpdateOrganizationSettingsRequest + + if err := c.BodyParser(&organizationSettings); err != nil { + return c.SendStatus(fiber.StatusBadRequest) + } + + database.DB.Model(&structs.Organization{ + Id: c.Locals("organizationId").(string), + }).Updates(organizationSettings) + + return c.SendStatus(fiber.StatusOK) +} diff --git a/routers/router/api/v1/user/user.go b/routers/router/api/v1/user/user.go index c2d8736..a00006b 100644 --- a/routers/router/api/v1/user/user.go +++ b/routers/router/api/v1/user/user.go @@ -1,13 +1 @@ package user - -import ( - "github.com/gofiber/fiber/v2" - "lms.de/backend/modules/structs" -) - -func GetUser(c *fiber.Ctx) error { - - return c.JSON(structs.GetUserResponse{ - AvatarUrl: "", - }) -} diff --git a/routers/router/router.go b/routers/router/router.go index c2055e4..059a764 100644 --- a/routers/router/router.go +++ b/routers/router/router.go @@ -8,6 +8,7 @@ import ( "lms.de/backend/modules/database" "lms.de/backend/modules/structs" "lms.de/backend/modules/utils" + myapp "lms.de/backend/routers/router/api/v1/app" "lms.de/backend/routers/router/api/v1/lessons" "lms.de/backend/routers/router/api/v1/organization" "lms.de/backend/routers/router/api/v1/user" @@ -16,13 +17,15 @@ import ( func SetupRoutes(app *fiber.App) { v1 := app.Group("/v1") + v1.Get("/app", handleOrganizationSubdomain, requestAccessValidation, myapp.GetApp) + o := v1.Group("/organization") o.Post("/", organization.CreateOrganization) o.Get("/team/members", handleOrganizationSubdomain, requestAccessValidation, organization.GetTeamMembers) o.Get("/settings", handleOrganizationSubdomain, requestAccessValidation, organization.GetOrganizationSettings) + o.Patch("/settings", handleOrganizationSubdomain, requestAccessValidation, organization.UpdateOrganizationSettings) u := v1.Group("/user") - u.Get("/", handleOrganizationSubdomain, requestAccessValidation, user.GetUser) u.Post("/auth/login", handleOrganizationSubdomain, user.UserLogin) l := v1.Group("/lessons") @@ -58,7 +61,7 @@ func userSessionValidation(c *fiber.Ctx) error { } c.Locals("userId", userSession.UserId) - c.Locals("organizationId", c.Locals("organizationId")) + //c.Locals("organizationId", c.Locals("organizationId")) return c.Next() }