|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/diggerhq/digger/backend/models" |
| 5 | + "github.com/gin-gonic/gin" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +func (d DiggerController) UpsertOrgInternal(c *gin.Context) { |
| 11 | + type OrgCreateRequest struct { |
| 12 | + Name string `json:"org_name"` |
| 13 | + ExternalSource string `json:"external_source"` |
| 14 | + ExternalId string `json:"external_id"` |
| 15 | + } |
| 16 | + |
| 17 | + var request OrgCreateRequest |
| 18 | + err := c.BindJSON(&request) |
| 19 | + if err != nil { |
| 20 | + log.Printf("Error binding JSON: %v", err) |
| 21 | + c.JSON(http.StatusBadRequest, gin.H{"error": "Error binding JSON"}) |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + name := request.Name |
| 26 | + externalSource := request.ExternalSource |
| 27 | + externalId := request.ExternalId |
| 28 | + |
| 29 | + log.Printf("creating org for %v %v %v", name, externalSource, externalId) |
| 30 | + var org *models.Organisation |
| 31 | + org, err = models.DB.GetOrganisation(externalId) |
| 32 | + if err != nil { |
| 33 | + log.Printf("Error while retriving org: %v", err) |
| 34 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Error creating org"}) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + if org == nil { |
| 39 | + org, err = models.DB.CreateOrganisation(name, externalSource, externalId) |
| 40 | + } |
| 41 | + |
| 42 | + if err != nil { |
| 43 | + log.Printf("Error creating org: %v", err) |
| 44 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Error creating org"}) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + c.JSON(http.StatusOK, gin.H{"status": "success", "org_id": org.ID}) |
| 49 | +} |
| 50 | + |
| 51 | +func (d DiggerController) CreateUserInternal(c *gin.Context) { |
| 52 | + type UserCreateRequest struct { |
| 53 | + UserExternalSource string `json:"external_source"` |
| 54 | + UserExternalId string `json:"external_id"` |
| 55 | + UserEmail string `json:"email"` |
| 56 | + OrgExternalId string `json:"external_org_id"` |
| 57 | + } |
| 58 | + |
| 59 | + var request UserCreateRequest |
| 60 | + err := c.BindJSON(&request) |
| 61 | + if err != nil { |
| 62 | + log.Printf("Error binding JSON: %v", err) |
| 63 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Error binding JSON"}) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + extUserId := request.UserExternalId |
| 68 | + extUserSource := request.UserExternalSource |
| 69 | + userEmail := request.UserEmail |
| 70 | + externalOrgId := request.OrgExternalId |
| 71 | + |
| 72 | + org, err := models.DB.GetOrganisation(externalOrgId) |
| 73 | + if err != nil { |
| 74 | + log.Printf("Error retrieving org: %v", err) |
| 75 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Error retrieving org"}) |
| 76 | + } |
| 77 | + |
| 78 | + // for now using email for username since we want to deprecate that field |
| 79 | + username := userEmail |
| 80 | + user, err := models.DB.CreateUser(userEmail, extUserSource, extUserId, org.ID, username) |
| 81 | + if err != nil { |
| 82 | + log.Printf("Error creating user: %v", err) |
| 83 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Error creating user"}) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + c.JSON(200, gin.H{"status": "success", "user_id": user.ID}) |
| 88 | +} |
0 commit comments