2025-03-17 18:14:15 +00:00
|
|
|
-- name: GetDocumentSummary :one
|
|
|
|
|
SELECT id, clientId, hash FROM documents WHERE id = $1;
|
|
|
|
|
|
|
|
|
|
-- name: GetDocumentExternal :one
|
|
|
|
|
WITH
|
|
|
|
|
docs AS (
|
|
|
|
|
SELECT id, hash, clientId
|
|
|
|
|
FROM documents
|
|
|
|
|
WHERE id = @documentId
|
|
|
|
|
),
|
|
|
|
|
namedResults AS (
|
|
|
|
|
SELECT
|
|
|
|
|
q.name,
|
|
|
|
|
dd.id,
|
|
|
|
|
d.value
|
|
|
|
|
FROM currentCollectorQueries AS q
|
|
|
|
|
JOIN docs as dd on dd.clientId = q.clientId
|
|
|
|
|
LEFT JOIN listValidDocumentResults(@documentId) AS d
|
|
|
|
|
ON d.queryId = q.queryId and q.name is not null
|
|
|
|
|
)
|
|
|
|
|
SELECT
|
|
|
|
|
d.id,
|
2025-04-02 18:50:03 +00:00
|
|
|
d.clientId,
|
2025-03-17 18:14:15 +00:00
|
|
|
d.hash,
|
|
|
|
|
COALESCE(jsonb_object_agg(r.name, r.value) FILTER (WHERE r.name IS NOT NULL), '{}') AS fields
|
|
|
|
|
FROM docs AS d
|
|
|
|
|
LEFT JOIN namedResults AS r ON d.id = r.id
|
2025-04-02 18:50:03 +00:00
|
|
|
GROUP BY d.id, d.clientId, d.hash;
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
-- name: ListDocumentsByClient :many
|
|
|
|
|
SELECT id, hash from documents where clientId = @clientId;
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-01-23 14:56:20 +00:00
|
|
|
-- name: CreateDocument :one
|
2025-08-04 10:54:22 -07:00
|
|
|
INSERT INTO documents (clientId, hash, batch_id) VALUES ($1, $2, $3) RETURNING id;
|
2025-02-05 17:44:01 +00:00
|
|
|
|
|
|
|
|
-- name: AddDocumentEntry :exec
|
2025-02-07 12:12:51 +00:00
|
|
|
INSERT INTO documentEntries (documentId, bucket, key) VALUES ($1, $2, $3);
|
|
|
|
|
|
|
|
|
|
-- name: GetDocumentEntry :one
|
|
|
|
|
SELECT documentId, bucket, key FROM documentEntries WHERE documentId = $1 ORDER BY id DESC LIMIT 1;
|
2025-02-05 17:44:01 +00:00
|
|
|
|
|
|
|
|
-- name: GetDocumentIDByHash :one
|
2025-03-10 11:03:00 +00:00
|
|
|
SELECT id FROM documents WHERE hash = $1 and clientId = $2;
|
|
|
|
|
|
|
|
|
|
-- name: ListDocumentIDsBatch :many
|
|
|
|
|
SELECT id, totalCount FROM listDocumentIDs(@clientId, @batchSize, @pageOffset);
|
2025-05-27 15:28:46 +00:00
|
|
|
|
|
|
|
|
-- name: AddDocumentUpload :exec
|
|
|
|
|
INSERT INTO documentUploads (id, clientId, bucket, key, part, createdAt) VALUES ($1, $2, $3, $4, $5, $6);
|
|
|
|
|
|
|
|
|
|
-- name: GetDocumentUploadCurrentPart :one
|
|
|
|
|
WITH client as (
|
|
|
|
|
select clientId from clients where clientId = @clientId
|
|
|
|
|
),
|
|
|
|
|
parts as (
|
|
|
|
|
SELECT docs.part
|
|
|
|
|
FROM client as c
|
|
|
|
|
JOIN documentUploads as docs on docs.clientId = c.clientId
|
|
|
|
|
WHERE date(docs.createdAt) = date(@queryDate)
|
|
|
|
|
),
|
|
|
|
|
max_part as (
|
|
|
|
|
SELECT COALESCE(max(part), 0) as max_part_num FROM parts
|
|
|
|
|
)
|
|
|
|
|
SELECT
|
|
|
|
|
COALESCE(p.part, 0)::unsignedsmallint as part,
|
|
|
|
|
COUNT(p.part) as count
|
|
|
|
|
FROM parts p
|
|
|
|
|
RIGHT JOIN max_part mp ON p.part = mp.max_part_num
|
|
|
|
|
GROUP BY p.part;
|