MCPcopy
hub / github.com/superplanehq/superplane / createOrganization

Method createOrganization

pkg/public/server.go:892–994  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

890}
891
892func (s *Server) createOrganization(w http.ResponseWriter, r *http.Request) {
893 account, ok := middleware.GetAccountFromContext(r.Context())
894 if !ok {
895 http.Error(w, "", http.StatusUnauthorized)
896 return
897 }
898
899 body, err := io.ReadAll(r.Body)
900 if err != nil {
901 http.Error(w, "Invalid request body", http.StatusBadRequest)
902 return
903 }
904
905 var req OrganizationCreationRequest
906 err = json.Unmarshal(body, &req)
907 if err != nil {
908 http.Error(w, "Invalid request body", http.StatusBadRequest)
909 return
910 }
911
912 if req.Name == "" {
913 http.Error(w, "Name is required", http.StatusBadRequest)
914 return
915 }
916
917 creationStatus, err := s.describeOrganizationCreationStatus(r.Context(), account.ID.String())
918 if err != nil {
919 // describeOrganizationCreationStatus already logs the underlying
920 // error with stage-specific structured fields.
921 log.WithField("account_id", account.ID.String()).
922 Error("failed to check organization creation status before creating organization")
923 http.Error(w, "Failed to create organization", http.StatusInternalServerError)
924 return
925 }
926
927 if !creationStatus.Allowed {
928 http.Error(w, creationStatus.Message, http.StatusTooManyRequests)
929 return
930 }
931
932 //
933 // Create the organization
934 //
935 tx := database.Conn().Begin()
936 log.Infof("Creating organization %s for account %s", req.Name, account.Email)
937 organization, err := models.CreateOrganizationInTransaction(tx, req.Name, "")
938
939 if err != nil {
940 tx.Rollback()
941
942 if err.Error() == "name already used" {
943 http.Error(w, "Organization name already in use", http.StatusConflict)
944 return
945 }
946
947 log.Errorf("Error creating organization %s: %v", req.Name, err)
948 http.Error(w, "Failed to create organization", http.StatusInternalServerError)
949 return

Callers

nothing calls this directly

Calls 14

PublishMethod · 0.95
GetAccountFromContextFunction · 0.92
ConnFunction · 0.92
CreateUserInTransactionFunction · 0.92
InfofMethod · 0.80
ErrorfMethod · 0.80
ErrorMethod · 0.65
SetupOrganizationMethod · 0.65
CommitMethod · 0.65

Tested by

no test coverage detected