(w http.ResponseWriter, r *http.Request)
| 890 | } |
| 891 | |
| 892 | func (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 |
nothing calls this directly
no test coverage detected