(rw http.ResponseWriter, r *http.Request)
| 1449 | } |
| 1450 | |
| 1451 | func (api *API) chatCostUsers(rw http.ResponseWriter, r *http.Request) { |
| 1452 | ctx := r.Context() |
| 1453 | if !api.Authorize(r, policy.ActionRead, rbac.ResourceChat) { |
| 1454 | httpapi.Forbidden(rw) |
| 1455 | return |
| 1456 | } |
| 1457 | |
| 1458 | now := time.Now() |
| 1459 | defaultStart := now.AddDate(0, 0, -30) |
| 1460 | |
| 1461 | qp := r.URL.Query() |
| 1462 | p := httpapi.NewQueryParamParser() |
| 1463 | startDate := p.Time(qp, defaultStart, "start_date", time.RFC3339) |
| 1464 | endDate := p.Time(qp, now, "end_date", time.RFC3339) |
| 1465 | username := strings.TrimSpace(p.String(qp, "", "username")) |
| 1466 | limit := p.Int(qp, 10, "limit") |
| 1467 | offset := p.Int(qp, 0, "offset") |
| 1468 | p.ErrorExcessParams(qp) |
| 1469 | if len(p.Errors) > 0 { |
| 1470 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1471 | Message: "Invalid query parameters.", |
| 1472 | Validations: p.Errors, |
| 1473 | }) |
| 1474 | return |
| 1475 | } |
| 1476 | if limit <= 0 { |
| 1477 | limit = 10 |
| 1478 | } |
| 1479 | if offset < 0 || offset > math.MaxInt32 || limit > math.MaxInt32 { |
| 1480 | validations := make([]codersdk.ValidationError, 0, 2) |
| 1481 | if offset < 0 { |
| 1482 | validations = append(validations, codersdk.ValidationError{ |
| 1483 | Field: "offset", |
| 1484 | Detail: "Must be greater than or equal to 0.", |
| 1485 | }) |
| 1486 | } |
| 1487 | if offset > math.MaxInt32 { |
| 1488 | validations = append(validations, codersdk.ValidationError{ |
| 1489 | Field: "offset", |
| 1490 | Detail: fmt.Sprintf("Must be less than or equal to %d.", math.MaxInt32), |
| 1491 | }) |
| 1492 | } |
| 1493 | if limit > math.MaxInt32 { |
| 1494 | validations = append(validations, codersdk.ValidationError{ |
| 1495 | Field: "limit", |
| 1496 | Detail: fmt.Sprintf("Must be less than or equal to %d.", math.MaxInt32), |
| 1497 | }) |
| 1498 | } |
| 1499 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1500 | Message: "Invalid query parameters.", |
| 1501 | Validations: validations, |
| 1502 | }) |
| 1503 | return |
| 1504 | } |
| 1505 | |
| 1506 | users, err := api.Database.GetChatCostPerUser(ctx, database.GetChatCostPerUserParams{ |
| 1507 | StartDate: startDate, |
| 1508 | EndDate: endDate, |
nothing calls this directly
no test coverage detected