mirror of
https://github.com/PrivateCaptcha/PrivateCaptcha.git
synced 2026-02-09 07:19:08 -06:00
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ribtoks <505555+ribtoks@users.noreply.github.com>
242 lines
6.8 KiB
Go
242 lines
6.8 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: auditlog.sql
|
|
|
|
package generated
|
|
|
|
import (
|
|
"context"
|
|
"net/netip"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
type CreateAuditLogsParams struct {
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
Action AuditLogAction `db:"action" json:"action"`
|
|
Source AuditLogSource `db:"source" json:"source"`
|
|
EntityID pgtype.Int8 `db:"entity_id" json:"entity_id"`
|
|
EntityTable string `db:"entity_table" json:"entity_table"`
|
|
SessionID string `db:"session_id" json:"session_id"`
|
|
OldValue []byte `db:"old_value" json:"old_value"`
|
|
NewValue []byte `db:"new_value" json:"new_value"`
|
|
CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
|
|
IpAddress *netip.Addr `db:"ip_address" json:"ip_address"`
|
|
}
|
|
|
|
const deleteOldAuditLogs = `-- name: DeleteOldAuditLogs :exec
|
|
DELETE FROM backend.audit_logs WHERE created_at < $1
|
|
`
|
|
|
|
func (q *Queries) DeleteOldAuditLogs(ctx context.Context, createdAt pgtype.Timestamptz) error {
|
|
_, err := q.db.Exec(ctx, deleteOldAuditLogs, createdAt)
|
|
return err
|
|
}
|
|
|
|
const getOrgAuditLogs = `-- name: GetOrgAuditLogs :many
|
|
SELECT a.id, a.user_id, a.action, a.entity_id, a.entity_table, a.session_id, a.old_value, a.new_value, a.created_at, a.source, a.ip_address, u.name, u.email
|
|
FROM backend.audit_logs a
|
|
LEFT JOIN backend.users u ON u.id = a.user_id
|
|
WHERE (
|
|
((a.entity_table = 'organizations' OR a.entity_table = 'organization_users') AND a.entity_id = $1)
|
|
OR (
|
|
a.entity_table = 'properties'
|
|
AND ((a.old_value ->> 'org_id')::bigint = $1 OR (a.new_value ->> 'org_id')::bigint = $1)
|
|
)
|
|
)
|
|
AND a.created_at >= $2
|
|
ORDER BY a.created_at DESC
|
|
OFFSET $3
|
|
LIMIT $4
|
|
`
|
|
|
|
type GetOrgAuditLogsParams struct {
|
|
EntityID pgtype.Int8 `db:"entity_id" json:"entity_id"`
|
|
CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
|
|
Offset int32 `db:"offset" json:"offset"`
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
}
|
|
|
|
type GetOrgAuditLogsRow struct {
|
|
AuditLog AuditLog `db:"audit_log" json:"audit_log"`
|
|
Name pgtype.Text `db:"name" json:"name"`
|
|
Email pgtype.Text `db:"email" json:"email"`
|
|
}
|
|
|
|
func (q *Queries) GetOrgAuditLogs(ctx context.Context, arg *GetOrgAuditLogsParams) ([]*GetOrgAuditLogsRow, error) {
|
|
rows, err := q.db.Query(ctx, getOrgAuditLogs,
|
|
arg.EntityID,
|
|
arg.CreatedAt,
|
|
arg.Offset,
|
|
arg.Limit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*GetOrgAuditLogsRow
|
|
for rows.Next() {
|
|
var i GetOrgAuditLogsRow
|
|
if err := rows.Scan(
|
|
&i.AuditLog.ID,
|
|
&i.AuditLog.UserID,
|
|
&i.AuditLog.Action,
|
|
&i.AuditLog.EntityID,
|
|
&i.AuditLog.EntityTable,
|
|
&i.AuditLog.SessionID,
|
|
&i.AuditLog.OldValue,
|
|
&i.AuditLog.NewValue,
|
|
&i.AuditLog.CreatedAt,
|
|
&i.AuditLog.Source,
|
|
&i.AuditLog.IpAddress,
|
|
&i.Name,
|
|
&i.Email,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getPropertyAuditLogs = `-- name: GetPropertyAuditLogs :many
|
|
SELECT a.id, a.user_id, a.action, a.entity_id, a.entity_table, a.session_id, a.old_value, a.new_value, a.created_at, a.source, a.ip_address, u.name, u.email
|
|
FROM backend.audit_logs a
|
|
LEFT JOIN backend.users u ON u.id = a.user_id
|
|
WHERE a.entity_table = 'properties' AND a.entity_id = $1 AND a.created_at >= $2
|
|
ORDER BY a.created_at DESC
|
|
OFFSET $3
|
|
LIMIT $4
|
|
`
|
|
|
|
type GetPropertyAuditLogsParams struct {
|
|
EntityID pgtype.Int8 `db:"entity_id" json:"entity_id"`
|
|
CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
|
|
Offset int32 `db:"offset" json:"offset"`
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
}
|
|
|
|
type GetPropertyAuditLogsRow struct {
|
|
AuditLog AuditLog `db:"audit_log" json:"audit_log"`
|
|
Name pgtype.Text `db:"name" json:"name"`
|
|
Email pgtype.Text `db:"email" json:"email"`
|
|
}
|
|
|
|
func (q *Queries) GetPropertyAuditLogs(ctx context.Context, arg *GetPropertyAuditLogsParams) ([]*GetPropertyAuditLogsRow, error) {
|
|
rows, err := q.db.Query(ctx, getPropertyAuditLogs,
|
|
arg.EntityID,
|
|
arg.CreatedAt,
|
|
arg.Offset,
|
|
arg.Limit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*GetPropertyAuditLogsRow
|
|
for rows.Next() {
|
|
var i GetPropertyAuditLogsRow
|
|
if err := rows.Scan(
|
|
&i.AuditLog.ID,
|
|
&i.AuditLog.UserID,
|
|
&i.AuditLog.Action,
|
|
&i.AuditLog.EntityID,
|
|
&i.AuditLog.EntityTable,
|
|
&i.AuditLog.SessionID,
|
|
&i.AuditLog.OldValue,
|
|
&i.AuditLog.NewValue,
|
|
&i.AuditLog.CreatedAt,
|
|
&i.AuditLog.Source,
|
|
&i.AuditLog.IpAddress,
|
|
&i.Name,
|
|
&i.Email,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getUserAuditLogs = `-- name: GetUserAuditLogs :many
|
|
SELECT a.id, a.user_id, a.action, a.entity_id, a.entity_table, a.session_id, a.old_value, a.new_value, a.created_at, a.source, a.ip_address, u.name, u.email
|
|
FROM backend.audit_logs a
|
|
LEFT JOIN backend.users u ON u.id = a.user_id
|
|
WHERE (a.user_id = $1 OR
|
|
(
|
|
a.entity_table = 'users' AND a.entity_id = $1
|
|
) OR
|
|
(
|
|
a.entity_table = 'organization_users'
|
|
AND ((a.old_value ->> 'user_id')::bigint = $1 OR (a.new_value ->> 'user_id')::bigint = $1)
|
|
) OR
|
|
(
|
|
a.entity_table = 'properties'
|
|
AND ((a.old_value ->> 'creator_id')::bigint = $1 OR (a.new_value ->> 'creator_id')::bigint = $1)
|
|
)
|
|
)
|
|
AND a.created_at >= $2
|
|
ORDER BY a.created_at DESC
|
|
OFFSET $3
|
|
LIMIT $4
|
|
`
|
|
|
|
type GetUserAuditLogsParams struct {
|
|
UserID pgtype.Int4 `db:"user_id" json:"user_id"`
|
|
CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
|
|
Offset int32 `db:"offset" json:"offset"`
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
}
|
|
|
|
type GetUserAuditLogsRow struct {
|
|
AuditLog AuditLog `db:"audit_log" json:"audit_log"`
|
|
Name pgtype.Text `db:"name" json:"name"`
|
|
Email pgtype.Text `db:"email" json:"email"`
|
|
}
|
|
|
|
func (q *Queries) GetUserAuditLogs(ctx context.Context, arg *GetUserAuditLogsParams) ([]*GetUserAuditLogsRow, error) {
|
|
rows, err := q.db.Query(ctx, getUserAuditLogs,
|
|
arg.UserID,
|
|
arg.CreatedAt,
|
|
arg.Offset,
|
|
arg.Limit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []*GetUserAuditLogsRow
|
|
for rows.Next() {
|
|
var i GetUserAuditLogsRow
|
|
if err := rows.Scan(
|
|
&i.AuditLog.ID,
|
|
&i.AuditLog.UserID,
|
|
&i.AuditLog.Action,
|
|
&i.AuditLog.EntityID,
|
|
&i.AuditLog.EntityTable,
|
|
&i.AuditLog.SessionID,
|
|
&i.AuditLog.OldValue,
|
|
&i.AuditLog.NewValue,
|
|
&i.AuditLog.CreatedAt,
|
|
&i.AuditLog.Source,
|
|
&i.AuditLog.IpAddress,
|
|
&i.Name,
|
|
&i.Email,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|