@Summary Upsert chat usage limit group override @x-apidocgen {"skip": true} EXPERIMENTAL: this endpoint is experimental and is subject to change.
(rw http.ResponseWriter, r *http.Request)
| 1856 | // @x-apidocgen {"skip": true} |
| 1857 | // EXPERIMENTAL: this endpoint is experimental and is subject to change. |
| 1858 | func (api *API) upsertChatUsageLimitGroupOverride(rw http.ResponseWriter, r *http.Request) { |
| 1859 | ctx := r.Context() |
| 1860 | if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) { |
| 1861 | httpapi.Forbidden(rw) |
| 1862 | return |
| 1863 | } |
| 1864 | |
| 1865 | groupIDStr := chi.URLParam(r, "group") |
| 1866 | groupID, err := uuid.Parse(groupIDStr) |
| 1867 | if err != nil { |
| 1868 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1869 | Message: "Invalid group ID.", |
| 1870 | Detail: err.Error(), |
| 1871 | }) |
| 1872 | return |
| 1873 | } |
| 1874 | |
| 1875 | var req codersdk.UpdateChatUsageLimitGroupOverrideRequest |
| 1876 | if !httpapi.Read(ctx, rw, r, &req) { |
| 1877 | return |
| 1878 | } |
| 1879 | |
| 1880 | if req.SpendLimitMicros <= 0 { |
| 1881 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1882 | Message: "Invalid chat usage limit group override.", |
| 1883 | Detail: "Spend limit (in microdollars) must be greater than 0.", |
| 1884 | }) |
| 1885 | return |
| 1886 | } |
| 1887 | |
| 1888 | group, err := api.Database.GetGroupByID(ctx, groupID) |
| 1889 | if err != nil { |
| 1890 | if errors.Is(err, sql.ErrNoRows) { |
| 1891 | httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{ |
| 1892 | Message: "Group not found.", |
| 1893 | }) |
| 1894 | return |
| 1895 | } |
| 1896 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 1897 | Message: "Failed to look up group details.", |
| 1898 | Detail: err.Error(), |
| 1899 | }) |
| 1900 | return |
| 1901 | } |
| 1902 | |
| 1903 | _, err = api.Database.UpsertChatUsageLimitGroupOverride(ctx, database.UpsertChatUsageLimitGroupOverrideParams{ |
| 1904 | GroupID: groupID, |
| 1905 | SpendLimitMicros: req.SpendLimitMicros, |
| 1906 | }) |
| 1907 | if err != nil { |
| 1908 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 1909 | Message: "Failed to upsert group usage limit override.", |
| 1910 | Detail: err.Error(), |
| 1911 | }) |
| 1912 | return |
| 1913 | } |
| 1914 | |
| 1915 | memberCount, err := api.Database.GetGroupMembersCountByGroupID(ctx, database.GetGroupMembersCountByGroupIDParams{ |
no test coverage detected