@Summary Update chat usage limit config @x-apidocgen {"skip": true} EXPERIMENTAL: this endpoint is experimental and is subject to change.
(rw http.ResponseWriter, r *http.Request)
| 1639 | // @x-apidocgen {"skip": true} |
| 1640 | // EXPERIMENTAL: this endpoint is experimental and is subject to change. |
| 1641 | func (api *API) updateChatUsageLimitConfig(rw http.ResponseWriter, r *http.Request) { |
| 1642 | ctx := r.Context() |
| 1643 | if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) { |
| 1644 | httpapi.Forbidden(rw) |
| 1645 | return |
| 1646 | } |
| 1647 | |
| 1648 | var req codersdk.ChatUsageLimitConfig |
| 1649 | if !httpapi.Read(ctx, rw, r, &req) { |
| 1650 | return |
| 1651 | } |
| 1652 | |
| 1653 | params := database.UpsertChatUsageLimitConfigParams{ |
| 1654 | Enabled: false, |
| 1655 | DefaultLimitMicros: 0, |
| 1656 | Period: "", |
| 1657 | } |
| 1658 | if req.SpendLimitMicros == nil { |
| 1659 | if req.Period != "" && !req.Period.Valid() { |
| 1660 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1661 | Message: "Invalid chat usage limit period.", |
| 1662 | Detail: "Period must be one of: day, week, month.", |
| 1663 | }) |
| 1664 | return |
| 1665 | } |
| 1666 | |
| 1667 | params.Enabled = false |
| 1668 | params.DefaultLimitMicros = 0 |
| 1669 | params.Period = string(req.Period) |
| 1670 | if params.Period == "" { |
| 1671 | params.Period = string(codersdk.ChatUsageLimitPeriodMonth) |
| 1672 | } |
| 1673 | } else { |
| 1674 | if *req.SpendLimitMicros <= 0 { |
| 1675 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1676 | Message: "Invalid chat usage limit spend limit.", |
| 1677 | Detail: "Spend limit must be greater than 0.", |
| 1678 | }) |
| 1679 | return |
| 1680 | } |
| 1681 | if !req.Period.Valid() { |
| 1682 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1683 | Message: "Invalid chat usage limit period.", |
| 1684 | Detail: "Period must be one of: day, week, month.", |
| 1685 | }) |
| 1686 | return |
| 1687 | } |
| 1688 | |
| 1689 | params.Enabled = true |
| 1690 | params.DefaultLimitMicros = *req.SpendLimitMicros |
| 1691 | params.Period = string(req.Period) |
| 1692 | } |
| 1693 | |
| 1694 | config, err := api.Database.UpsertChatUsageLimitConfig(ctx, params) |
| 1695 | if err != nil { |
| 1696 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 1697 | Message: "Failed to update chat usage limit config.", |
| 1698 | Detail: err.Error(), |
no test coverage detected