11package controllers
22
33import (
4+ "encoding/json"
5+ "errors"
46 "fmt"
7+ "github.com/diggerhq/digger/backend/middleware"
8+ "gorm.io/gorm"
59 "log/slog"
610 "net/http"
711 "os"
@@ -17,6 +21,70 @@ type TenantCreatedEvent struct {
1721 Name string `json:"name,omitempty"`
1822}
1923
24+ func GetOrgSettingsApi (c * gin.Context ) {
25+ organisationId := c .GetString (middleware .ORGANISATION_ID_KEY )
26+ organisationSource := c .GetString (middleware .ORGANISATION_SOURCE_KEY )
27+
28+ var org models.Organisation
29+ err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
30+ if err != nil {
31+ if errors .Is (err , gorm .ErrRecordNotFound ) {
32+ slog .Info ("Organisation not found" , "organisationId" , organisationId , "source" , organisationSource )
33+ c .String (http .StatusNotFound , "Could not find organisation: " + organisationId )
34+ } else {
35+ slog .Error ("Error fetching organisation" , "organisationId" , organisationId , "source" , organisationSource , "error" , err )
36+ c .String (http .StatusInternalServerError , "Error fetching organisation" )
37+ }
38+ return
39+ }
40+
41+ c .JSON (http .StatusOK , gin.H {
42+ "drift_enabled" : org .DriftEnabled ,
43+ "drift_cron_tab" : org .DriftCronTab ,
44+ "drift_webhook_url" : org .DriftWebhookUrl ,
45+ })
46+ }
47+
48+ func UpdateOrgSettingsApi (c * gin.Context ) {
49+ organisationId := c .GetString (middleware .ORGANISATION_ID_KEY )
50+ organisationSource := c .GetString (middleware .ORGANISATION_SOURCE_KEY )
51+
52+ var org models.Organisation
53+ err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
54+ if err != nil {
55+ if errors .Is (err , gorm .ErrRecordNotFound ) {
56+ slog .Info ("Organisation not found" , "organisationId" , organisationId , "source" , organisationSource )
57+ c .String (http .StatusNotFound , "Could not find organisation: " + organisationId )
58+ } else {
59+ slog .Error ("Error fetching organisation" , "organisationId" , organisationId , "source" , organisationSource , "error" , err )
60+ c .String (http .StatusInternalServerError , "Error fetching organisation" )
61+ }
62+ return
63+ }
64+ var reqBody struct {
65+ DriftEnabled bool `json:"drift_enabled"`
66+ DriftCronTab string `json:"drift_cron_tab"`
67+ DriftWebhookUrl string `json:"drift_webhook_url"`
68+ }
69+ err = json .NewDecoder (c .Request .Body ).Decode (& reqBody )
70+ if err != nil {
71+ slog .Error ("Error decoding request body" , "error" , err )
72+ c .String (http .StatusBadRequest , "Error decoding request body" )
73+ return
74+ }
75+
76+ org .DriftEnabled = reqBody .DriftEnabled
77+ org .DriftCronTab = reqBody .DriftCronTab
78+ org .DriftWebhookUrl = reqBody .DriftWebhookUrl
79+ err = models .DB .GormDB .Save (& org ).Error
80+ if err != nil {
81+ slog .Error ("Error saving organisation" , "organisationId" , organisationId , "source" , organisationSource , "error" , err )
82+ c .String (http .StatusInternalServerError , "Error saving organisation" )
83+ return
84+ }
85+
86+ c .JSON (http .StatusOK , gin.H {})
87+ }
2088func CreateFronteggOrgFromWebhook (c * gin.Context ) {
2189 var json TenantCreatedEvent
2290
0 commit comments