396 lines
9.3 KiB
Go
396 lines
9.3 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.27.0
|
|
// source: incidents.sql
|
|
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
|
|
"dynatron.me/x/stillbox/internal/jsontypes"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const addToIncident = `-- name: AddToIncident :exec
|
|
WITH inp AS (
|
|
SELECT
|
|
UNNEST($2::UUID[]) id,
|
|
UNNEST($3::JSONB[]) notes
|
|
) INSERT INTO incidents_calls(
|
|
incident_id,
|
|
call_id,
|
|
calls_tbl_id,
|
|
call_date,
|
|
notes
|
|
)
|
|
SELECT
|
|
$1::UUID,
|
|
inp.id,
|
|
inp.id,
|
|
c.call_date,
|
|
inp.notes
|
|
FROM inp
|
|
JOIN calls c ON c.id = inp.id
|
|
`
|
|
|
|
func (q *Queries) AddToIncident(ctx context.Context, incidentID uuid.UUID, callIds []uuid.UUID, notes [][]byte) error {
|
|
_, err := q.db.Exec(ctx, addToIncident, incidentID, callIds, notes)
|
|
return err
|
|
}
|
|
|
|
const createIncident = `-- name: CreateIncident :one
|
|
INSERT INTO incidents (
|
|
id,
|
|
name,
|
|
description,
|
|
start_time,
|
|
end_time,
|
|
location,
|
|
metadata
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7
|
|
)
|
|
RETURNING id, name, description, start_time, end_time, location, metadata
|
|
`
|
|
|
|
type CreateIncidentParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Description *string `json:"description"`
|
|
StartTime pgtype.Timestamptz `json:"start_time"`
|
|
EndTime pgtype.Timestamptz `json:"end_time"`
|
|
Location []byte `json:"location"`
|
|
Metadata jsontypes.Metadata `json:"metadata"`
|
|
}
|
|
|
|
func (q *Queries) CreateIncident(ctx context.Context, arg CreateIncidentParams) (Incident, error) {
|
|
row := q.db.QueryRow(ctx, createIncident,
|
|
arg.ID,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.StartTime,
|
|
arg.EndTime,
|
|
arg.Location,
|
|
arg.Metadata,
|
|
)
|
|
var i Incident
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.StartTime,
|
|
&i.EndTime,
|
|
&i.Location,
|
|
&i.Metadata,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteIncident = `-- name: DeleteIncident :exec
|
|
DELETE FROM incidents CASCADE WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteIncident(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteIncident, id)
|
|
return err
|
|
}
|
|
|
|
const getIncident = `-- name: GetIncident :one
|
|
SELECT
|
|
i.id,
|
|
i.name,
|
|
i.description,
|
|
i.start_time,
|
|
i.end_time,
|
|
i.location,
|
|
i.metadata
|
|
FROM incidents i
|
|
WHERE i.id = $1
|
|
`
|
|
|
|
func (q *Queries) GetIncident(ctx context.Context, id uuid.UUID) (Incident, error) {
|
|
row := q.db.QueryRow(ctx, getIncident, id)
|
|
var i Incident
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.StartTime,
|
|
&i.EndTime,
|
|
&i.Location,
|
|
&i.Metadata,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getIncidentCalls = `-- name: GetIncidentCalls :many
|
|
SELECT ic.call_id, ic.call_date, ic.notes, c.submitter, c.system, c.talkgroup, c.audio_name, c.duration, c.audio_type, c.audio_url, c.frequency, c.frequencies, c.patches, c.source, c.transcript
|
|
FROM incidents_calls ic, LATERAL (
|
|
SELECT
|
|
ca.submitter,
|
|
ca.system,
|
|
ca.talkgroup,
|
|
ca.audio_name,
|
|
ca.duration,
|
|
ca.audio_type,
|
|
ca.audio_url,
|
|
ca.frequency,
|
|
ca.frequencies,
|
|
ca.patches,
|
|
ca.source,
|
|
ca.transcript
|
|
FROM calls ca WHERE ca.id = ic.calls_tbl_id AND ca.call_date = ic.call_date
|
|
UNION
|
|
SELECT
|
|
sc.submitter,
|
|
sc.system,
|
|
sc.talkgroup,
|
|
sc.audio_name,
|
|
sc.duration,
|
|
sc.audio_type,
|
|
sc.audio_url,
|
|
sc.frequency,
|
|
sc.frequencies,
|
|
sc.patches,
|
|
sc.source,
|
|
sc.transcript
|
|
FROM swept_calls sc WHERE sc.id = ic.swept_call_id
|
|
) c
|
|
WHERE ic.incident_id = $1
|
|
`
|
|
|
|
type GetIncidentCallsRow struct {
|
|
CallID uuid.UUID `json:"call_id"`
|
|
CallDate pgtype.Timestamptz `json:"call_date"`
|
|
Notes []byte `json:"notes"`
|
|
Submitter *int32 `json:"submitter"`
|
|
System int `json:"system"`
|
|
Talkgroup int `json:"talkgroup"`
|
|
AudioName *string `json:"audio_name"`
|
|
Duration *int32 `json:"duration"`
|
|
AudioType *string `json:"audio_type"`
|
|
AudioUrl *string `json:"audio_url"`
|
|
Frequency int `json:"frequency"`
|
|
Frequencies []int `json:"frequencies"`
|
|
Patches []int `json:"patches"`
|
|
Source int `json:"source"`
|
|
Transcript *string `json:"transcript"`
|
|
}
|
|
|
|
func (q *Queries) GetIncidentCalls(ctx context.Context, id uuid.UUID) ([]GetIncidentCallsRow, error) {
|
|
rows, err := q.db.Query(ctx, getIncidentCalls, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetIncidentCallsRow
|
|
for rows.Next() {
|
|
var i GetIncidentCallsRow
|
|
if err := rows.Scan(
|
|
&i.CallID,
|
|
&i.CallDate,
|
|
&i.Notes,
|
|
&i.Submitter,
|
|
&i.System,
|
|
&i.Talkgroup,
|
|
&i.AudioName,
|
|
&i.Duration,
|
|
&i.AudioType,
|
|
&i.AudioUrl,
|
|
&i.Frequency,
|
|
&i.Frequencies,
|
|
&i.Patches,
|
|
&i.Source,
|
|
&i.Transcript,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listIncidentsCount = `-- name: ListIncidentsCount :one
|
|
SELECT COUNT(*)
|
|
FROM incidents i
|
|
WHERE
|
|
CASE WHEN $1::TIMESTAMPTZ IS NOT NULL THEN
|
|
i.start_time >= $1 ELSE TRUE END AND
|
|
CASE WHEN $2::TIMESTAMPTZ IS NOT NULL THEN
|
|
i.start_time <= $2 ELSE TRUE END AND
|
|
(CASE WHEN $3::TEXT IS NOT NULL THEN (
|
|
i.name ILIKE '%' || $3 || '%' OR
|
|
i.description ILIKE '%' || $3 || '%'
|
|
) ELSE TRUE END)
|
|
`
|
|
|
|
func (q *Queries) ListIncidentsCount(ctx context.Context, start pgtype.Timestamptz, end pgtype.Timestamptz, filter *string) (int64, error) {
|
|
row := q.db.QueryRow(ctx, listIncidentsCount, start, end, filter)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const listIncidentsP = `-- name: ListIncidentsP :many
|
|
SELECT
|
|
i.id,
|
|
i.name,
|
|
i.description,
|
|
i.start_time,
|
|
i.end_time,
|
|
i.location,
|
|
i.metadata,
|
|
COUNT(ic.incident_id) calls_count
|
|
FROM incidents i
|
|
LEFT JOIN incidents_calls ic ON i.id = ic.incident_id
|
|
WHERE
|
|
CASE WHEN $1::TIMESTAMPTZ IS NOT NULL THEN
|
|
i.start_time >= $1 ELSE TRUE END AND
|
|
CASE WHEN $2::TIMESTAMPTZ IS NOT NULL THEN
|
|
i.start_time <= $2 ELSE TRUE END AND
|
|
(CASE WHEN $3::TEXT IS NOT NULL THEN (
|
|
i.name ILIKE '%' || $3 || '%' OR
|
|
i.description ILIKE '%' || $3 || '%'
|
|
) ELSE TRUE END)
|
|
GROUP BY i.id
|
|
ORDER BY
|
|
CASE WHEN $4::TEXT = 'asc' THEN i.start_time END ASC,
|
|
CASE WHEN $4::TEXT = 'desc' THEN i.start_time END DESC
|
|
OFFSET $5 ROWS
|
|
FETCH NEXT $6 ROWS ONLY
|
|
`
|
|
|
|
type ListIncidentsPParams struct {
|
|
Start pgtype.Timestamptz `json:"start"`
|
|
End pgtype.Timestamptz `json:"end"`
|
|
Filter *string `json:"filter"`
|
|
Direction string `json:"direction"`
|
|
Offset int32 `json:"offset"`
|
|
PerPage int32 `json:"per_page"`
|
|
}
|
|
|
|
type ListIncidentsPRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Description *string `json:"description"`
|
|
StartTime pgtype.Timestamptz `json:"start_time"`
|
|
EndTime pgtype.Timestamptz `json:"end_time"`
|
|
Location []byte `json:"location"`
|
|
Metadata jsontypes.Metadata `json:"metadata"`
|
|
CallsCount int64 `json:"calls_count"`
|
|
}
|
|
|
|
func (q *Queries) ListIncidentsP(ctx context.Context, arg ListIncidentsPParams) ([]ListIncidentsPRow, error) {
|
|
rows, err := q.db.Query(ctx, listIncidentsP,
|
|
arg.Start,
|
|
arg.End,
|
|
arg.Filter,
|
|
arg.Direction,
|
|
arg.Offset,
|
|
arg.PerPage,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListIncidentsPRow
|
|
for rows.Next() {
|
|
var i ListIncidentsPRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.StartTime,
|
|
&i.EndTime,
|
|
&i.Location,
|
|
&i.Metadata,
|
|
&i.CallsCount,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const removeFromIncident = `-- name: RemoveFromIncident :exec
|
|
DELETE FROM incidents_calls ic
|
|
WHERE ic.incident_id = $1 AND ic.call_id = ANY($2::UUID[])
|
|
`
|
|
|
|
func (q *Queries) RemoveFromIncident(ctx context.Context, iD uuid.UUID, callIds []uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, removeFromIncident, iD, callIds)
|
|
return err
|
|
}
|
|
|
|
const updateCallIncidentNotes = `-- name: UpdateCallIncidentNotes :exec
|
|
UPDATE incidents_Calls
|
|
SET notes = $1
|
|
WHERE incident_id = $2 AND call_id = $3
|
|
`
|
|
|
|
func (q *Queries) UpdateCallIncidentNotes(ctx context.Context, notes []byte, incidentID uuid.UUID, callID uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, updateCallIncidentNotes, notes, incidentID, callID)
|
|
return err
|
|
}
|
|
|
|
const updateIncident = `-- name: UpdateIncident :one
|
|
UPDATE incidents
|
|
SET
|
|
name = COALESCE($1, name),
|
|
description = COALESCE($2, description),
|
|
start_time = COALESCE($3, start_time),
|
|
end_time = COALESCE($4, end_time),
|
|
location = COALESCE($5, location),
|
|
metadata = COALESCE($6, metadata)
|
|
WHERE
|
|
id = $7
|
|
RETURNING id, name, description, start_time, end_time, location, metadata
|
|
`
|
|
|
|
type UpdateIncidentParams struct {
|
|
Name *string `json:"name"`
|
|
Description *string `json:"description"`
|
|
StartTime pgtype.Timestamptz `json:"start_time"`
|
|
EndTime pgtype.Timestamptz `json:"end_time"`
|
|
Location []byte `json:"location"`
|
|
Metadata jsontypes.Metadata `json:"metadata"`
|
|
ID uuid.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateIncident(ctx context.Context, arg UpdateIncidentParams) (Incident, error) {
|
|
row := q.db.QueryRow(ctx, updateIncident,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.StartTime,
|
|
arg.EndTime,
|
|
arg.Location,
|
|
arg.Metadata,
|
|
arg.ID,
|
|
)
|
|
var i Incident
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.StartTime,
|
|
&i.EndTime,
|
|
&i.Location,
|
|
&i.Metadata,
|
|
)
|
|
return i, err
|
|
}
|