@@ -125,17 +125,18 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
125
125
// @Produce json
126
126
// @Tags Organizations
127
127
// @Param request body codersdk.PatchOrganizationRequest true "Patch organization request"
128
- // @Success 201 {object} codersdk.Organization
128
+ // @Success 200 {object} codersdk.Organization
129
129
// @Router /organizations/{organization} [patch]
130
130
func (api * API ) patchOrganization (rw http.ResponseWriter , r * http.Request ) {
131
131
ctx := r .Context ()
132
+ organization := httpmw .OrganizationParam (r )
132
133
133
134
var req codersdk.PatchOrganizationRequest
134
135
if ! httpapi .Read (ctx , rw , r , & req ) {
135
136
return
136
137
}
137
138
138
- if req .Name == codersdk .DefaultOrganization {
139
+ if req .Name == codersdk .DefaultOrganization && ! organization . IsDefault {
139
140
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
140
141
Message : fmt .Sprintf ("Organization name %q is reserved." , codersdk .DefaultOrganization ),
141
142
})
@@ -157,22 +158,55 @@ func (api *API) patchOrganization(rw http.ResponseWriter, r *http.Request) {
157
158
return
158
159
}
159
160
160
- organization , err := api .Database .UpdateOrganization (ctx , database.UpdateOrganizationParams {
161
- ID : uuid .New (),
162
- Name : req .Name ,
161
+ organization , err = api .Database .UpdateOrganization (ctx , database.UpdateOrganizationParams {
162
+ ID : organization .ID ,
163
163
UpdatedAt : dbtime .Now (),
164
+ Name : req .Name ,
164
165
})
165
166
if err != nil {
166
167
httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
167
- Message : "Internal error inserting organization member ." ,
168
- Detail : fmt .Sprintf ("update organization: %w " , err ),
168
+ Message : "Internal error updating organization." ,
169
+ Detail : fmt .Sprintf ("update organization: %s " , err . Error () ),
169
170
})
170
171
return
171
172
}
172
173
173
174
httpapi .Write (ctx , rw , http .StatusOK , convertOrganization (organization ))
174
175
}
175
176
177
+ // @Summary Delete organization
178
+ // @ID delete-organization
179
+ // @Security CoderSessionToken
180
+ // @Accept json
181
+ // @Produce json
182
+ // @Tags Organizations
183
+ // @Success 200
184
+ // @Router /organizations/{organization} [delete]
185
+ func (api * API ) deleteOrganization (rw http.ResponseWriter , r * http.Request ) {
186
+ ctx := r .Context ()
187
+ organization := httpmw .OrganizationParam (r )
188
+
189
+ if organization .IsDefault {
190
+ httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
191
+ Message : fmt .Sprintf ("Organization name %q is reserved." , codersdk .DefaultOrganization ),
192
+ })
193
+ return
194
+ }
195
+
196
+ err := api .Database .DeleteOrganization (ctx , organization .ID )
197
+ if err != nil {
198
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
199
+ Message : "Internal error deleting organization." ,
200
+ Detail : fmt .Sprintf ("delete organization: %s" , err .Error ()),
201
+ })
202
+ return
203
+ }
204
+
205
+ httpapi .Write (ctx , rw , http .StatusOK , codersdk.Response {
206
+ Message : "Organization has been deleted." ,
207
+ })
208
+ }
209
+
176
210
// convertOrganization consumes the database representation and outputs an API friendly representation.
177
211
func convertOrganization (organization database.Organization ) codersdk.Organization {
178
212
return codersdk.Organization {
0 commit comments