MCPcopy Index your code
hub / github.com/coder/coder / upsertChatUsageLimitOverride

Method upsertChatUsageLimitOverride

coderd/exp_chats.go:1747–1805  ·  view source on GitHub ↗

@Summary Upsert chat usage limit override @x-apidocgen {"skip": true} EXPERIMENTAL: this endpoint is experimental and is subject to change.

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

1745// @x-apidocgen {"skip": true}
1746// EXPERIMENTAL: this endpoint is experimental and is subject to change.
1747func (api *API) upsertChatUsageLimitOverride(rw http.ResponseWriter, r *http.Request) {
1748 ctx := r.Context()
1749 if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) {
1750 httpapi.Forbidden(rw)
1751 return
1752 }
1753
1754 userID, ok := parseChatUsageLimitUserID(rw, r)
1755 if !ok {
1756 return
1757 }
1758
1759 var req codersdk.UpsertChatUsageLimitOverrideRequest
1760 if !httpapi.Read(ctx, rw, r, &req) {
1761 return
1762 }
1763 if req.SpendLimitMicros <= 0 {
1764 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
1765 Message: "Invalid chat usage limit override.",
1766 Detail: "Spend limit must be greater than 0.",
1767 })
1768 return
1769 }
1770
1771 user, err := api.Database.GetUserByID(ctx, userID)
1772 if err != nil {
1773 if errors.Is(err, sql.ErrNoRows) {
1774 httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
1775 Message: "User not found.",
1776 })
1777 return
1778 }
1779 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
1780 Message: "Failed to look up chat usage limit user.",
1781 Detail: err.Error(),
1782 })
1783 return
1784 }
1785
1786 _, err = api.Database.UpsertChatUsageLimitUserOverride(ctx, database.UpsertChatUsageLimitUserOverrideParams{
1787 UserID: userID,
1788 SpendLimitMicros: req.SpendLimitMicros,
1789 })
1790 if err != nil {
1791 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
1792 Message: "Failed to upsert chat usage limit override.",
1793 Detail: err.Error(),
1794 })
1795 return
1796 }
1797
1798 httpapi.Write(ctx, rw, http.StatusOK, codersdk.ChatUsageLimitOverride{
1799 UserID: user.ID,
1800 Username: user.Username,
1801 Name: user.Name,
1802 AvatarURL: user.AvatarURL,
1803 SpendLimitMicros: nullInt64Ptr(sql.NullInt64{Int64: req.SpendLimitMicros, Valid: true}),
1804 })

Callers 1

Calls 11

AuthorizeMethod · 0.95
ForbiddenFunction · 0.92
ReadFunction · 0.92
WriteFunction · 0.92
nullInt64PtrFunction · 0.70
ContextMethod · 0.65
GetUserByIDMethod · 0.65
IsMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected