Skip to content
Merged
Prev Previous commit
Next Next commit
Unpack count and update types
  • Loading branch information
presleyp committed Nov 15, 2022
commit 31e8ae5e343ebfdfc2ad73df14c53fb03b6050b4
37 changes: 1 addition & 36 deletions coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,42 +60,7 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {

httpapi.Write(ctx, rw, http.StatusOK, codersdk.AuditLogResponse{
AuditLogs: convertAuditLogs(dblogs),
})
}

func (api *API) auditLogCount(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceAuditLog) {
httpapi.Forbidden(rw)
return
}

queryStr := r.URL.Query().Get("q")
filter, errs := auditSearchQuery(queryStr)
if len(errs) > 0 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid audit search query.",
Validations: errs,
})
return
}

count, err := api.Database.GetAuditLogCount(ctx, database.GetAuditLogCountParams{
ResourceType: filter.ResourceType,
ResourceID: filter.ResourceID,
Action: filter.Action,
Username: filter.Username,
Email: filter.Email,
DateFrom: filter.DateFrom,
DateTo: filter.DateTo,
})
if err != nil {
httpapi.InternalServerError(rw, err)
return
}

httpapi.Write(ctx, rw, http.StatusOK, codersdk.AuditLogCountResponse{
Count: count,
Count: dblogs[0].Count,
})
}

Expand Down
38 changes: 1 addition & 37 deletions codersdk/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,7 @@ type AuditLogsRequest struct {

type AuditLogResponse struct {
AuditLogs []AuditLog `json:"audit_logs"`
}

type AuditLogCountRequest struct {
SearchQuery string `json:"q,omitempty"`
}

type AuditLogCountResponse struct {
Count int64 `json:"count"`
Count int64 `json:"q, omitempty"`
}

type CreateTestAuditLogRequest struct {
Expand Down Expand Up @@ -159,35 +152,6 @@ func (c *Client) AuditLogs(ctx context.Context, req AuditLogsRequest) (AuditLogR
return logRes, nil
}

// AuditLogCount returns the count of all audit logs in the product.
func (c *Client) AuditLogCount(ctx context.Context, req AuditLogCountRequest) (AuditLogCountResponse, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/audit/count", nil, func(r *http.Request) {
q := r.URL.Query()
var params []string
if req.SearchQuery != "" {
params = append(params, req.SearchQuery)
}
q.Set("q", strings.Join(params, " "))
r.URL.RawQuery = q.Encode()
})
if err != nil {
return AuditLogCountResponse{}, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return AuditLogCountResponse{}, readBodyAsError(res)
}

var logRes AuditLogCountResponse
err = json.NewDecoder(res.Body).Decode(&logRes)
if err != nil {
return AuditLogCountResponse{}, err
}

return logRes, nil
}

func (c *Client) CreateTestAuditLog(ctx context.Context, req CreateTestAuditLogRequest) error {
res, err := c.Request(ctx, http.MethodPost, "/api/v2/audit/testgenerate", req)
if err != nil {
Expand Down
11 changes: 1 addition & 10 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,10 @@ export interface AuditLog {
readonly user?: User
}

// From codersdk/audit.go
export interface AuditLogCountRequest {
readonly q?: string
}

// From codersdk/audit.go
export interface AuditLogCountResponse {
readonly count: number
}

// From codersdk/audit.go
export interface AuditLogResponse {
readonly audit_logs: AuditLog[]
readonly q: number
}

// From codersdk/audit.go
Expand Down