Merged in feature/textExtractionsPart2 (pull request #193)

Continue finishing the parts of the text extraction plan

* add missing fields
This commit is contained in:
Jay Brown
2025-12-02 19:13:08 +00:00
parent c45e1dd427
commit 3bdc27f4de
21 changed files with 2392 additions and 215 deletions
@@ -644,22 +644,29 @@ func (q *Queries) AddFieldExtractionArrayField(ctx context.Context, arg *AddFiel
}
const addFieldExtractionEntry = `-- name: AddFieldExtractionEntry :exec
INSERT INTO documentFieldExtractionVersions (fieldExtractionId, version, createdBy)
VALUES ($1, $2, $3)
INSERT INTO documentFieldExtractionVersions (fieldExtractionId, documentId, version, createdBy)
VALUES ($1, $2, $3, $4)
`
type AddFieldExtractionEntryParams struct {
Fieldextractionid uuid.UUID `db:"fieldextractionid"`
Documentid uuid.UUID `db:"documentid"`
Version int64 `db:"version"`
Createdby string `db:"createdby"`
}
// AddFieldExtractionEntry
// Insert a new version entry for a field extraction
// documentId is required for the unique constraint enforcement
//
// INSERT INTO documentFieldExtractionVersions (fieldExtractionId, version, createdBy)
// VALUES ($1, $2, $3)
// INSERT INTO documentFieldExtractionVersions (fieldExtractionId, documentId, version, createdBy)
// VALUES ($1, $2, $3, $4)
func (q *Queries) AddFieldExtractionEntry(ctx context.Context, arg *AddFieldExtractionEntryParams) error {
_, err := q.db.Exec(ctx, addFieldExtractionEntry, arg.Fieldextractionid, arg.Version, arg.Createdby)
_, err := q.db.Exec(ctx, addFieldExtractionEntry,
arg.Fieldextractionid,
arg.Documentid,
arg.Version,
arg.Createdby,
)
return err
}
@@ -930,6 +937,130 @@ func (q *Queries) GetFieldExtractionByID(ctx context.Context, id uuid.UUID) (*Do
return &i, err
}
const getFieldExtractionByVersion = `-- name: GetFieldExtractionByVersion :one
SELECT
dfe.id,
dfe.documentId,
dfe.fileName,
dfe.contractTitle,
dfe.aareteDerivedAmendmentNum,
dfe.clientName,
dfe.payerName,
dfe.payerState,
dfe.providerState,
dfe.filenameTin,
dfe.provGroupTin,
dfe.provGroupNpi,
dfe.provGroupNameFull,
dfe.provOtherTin,
dfe.provOtherNpi,
dfe.provOtherNameFull,
dfe.aareteDerivedEffectiveDt,
dfe.aareteDerivedTerminationDt,
dfe.autoRenewalInd,
dfe.autoRenewalTerm,
dfev.version,
dfev.createdBy,
dfev.createdAt
FROM documentFieldExtractions dfe
JOIN documentFieldExtractionVersions dfev
ON dfev.fieldExtractionId = dfe.id
WHERE dfev.documentId = $1 AND dfev.version = $2
`
type GetFieldExtractionByVersionParams struct {
Documentid uuid.UUID `db:"documentid"`
Version int64 `db:"version"`
}
type GetFieldExtractionByVersionRow struct {
ID uuid.UUID `db:"id"`
Documentid uuid.UUID `db:"documentid"`
Filename *string `db:"filename"`
Contracttitle *string `db:"contracttitle"`
Aaretederivedamendmentnum *int32 `db:"aaretederivedamendmentnum"`
Clientname *string `db:"clientname"`
Payername *string `db:"payername"`
Payerstate *string `db:"payerstate"`
Providerstate *string `db:"providerstate"`
Filenametin *string `db:"filenametin"`
Provgrouptin *string `db:"provgrouptin"`
Provgroupnpi *string `db:"provgroupnpi"`
Provgroupnamefull *string `db:"provgroupnamefull"`
Provothertin *string `db:"provothertin"`
Provothernpi *string `db:"provothernpi"`
Provothernamefull *string `db:"provothernamefull"`
Aaretederivedeffectivedt pgtype.Date `db:"aaretederivedeffectivedt"`
Aaretederivedterminationdt pgtype.Date `db:"aaretederivedterminationdt"`
Autorenewalind *bool `db:"autorenewalind"`
Autorenewalterm *string `db:"autorenewalterm"`
Version int64 `db:"version"`
Createdby string `db:"createdby"`
Createdat pgtype.Timestamp `db:"createdat"`
}
// Retrieves a specific version of field extraction for a document
// Returns the field extraction record with version metadata
//
// SELECT
// dfe.id,
// dfe.documentId,
// dfe.fileName,
// dfe.contractTitle,
// dfe.aareteDerivedAmendmentNum,
// dfe.clientName,
// dfe.payerName,
// dfe.payerState,
// dfe.providerState,
// dfe.filenameTin,
// dfe.provGroupTin,
// dfe.provGroupNpi,
// dfe.provGroupNameFull,
// dfe.provOtherTin,
// dfe.provOtherNpi,
// dfe.provOtherNameFull,
// dfe.aareteDerivedEffectiveDt,
// dfe.aareteDerivedTerminationDt,
// dfe.autoRenewalInd,
// dfe.autoRenewalTerm,
// dfev.version,
// dfev.createdBy,
// dfev.createdAt
// FROM documentFieldExtractions dfe
// JOIN documentFieldExtractionVersions dfev
// ON dfev.fieldExtractionId = dfe.id
// WHERE dfev.documentId = $1 AND dfev.version = $2
func (q *Queries) GetFieldExtractionByVersion(ctx context.Context, arg *GetFieldExtractionByVersionParams) (*GetFieldExtractionByVersionRow, error) {
row := q.db.QueryRow(ctx, getFieldExtractionByVersion, arg.Documentid, arg.Version)
var i GetFieldExtractionByVersionRow
err := row.Scan(
&i.ID,
&i.Documentid,
&i.Filename,
&i.Contracttitle,
&i.Aaretederivedamendmentnum,
&i.Clientname,
&i.Payername,
&i.Payerstate,
&i.Providerstate,
&i.Filenametin,
&i.Provgrouptin,
&i.Provgroupnpi,
&i.Provgroupnamefull,
&i.Provothertin,
&i.Provothernpi,
&i.Provothernamefull,
&i.Aaretederivedeffectivedt,
&i.Aaretederivedterminationdt,
&i.Autorenewalind,
&i.Autorenewalterm,
&i.Version,
&i.Createdby,
&i.Createdat,
)
return &i, err
}
const getFieldExtractionHistory = `-- name: GetFieldExtractionHistory :many
SELECT
dfe.id,
@@ -1045,6 +1176,64 @@ func (q *Queries) GetFieldExtractionsByDocumentID(ctx context.Context, documenti
return items, nil
}
const getMaxFieldExtractionVersion = `-- name: GetMaxFieldExtractionVersion :one
SELECT COALESCE(MAX(version), 0) as max_version
FROM documentFieldExtractionVersions
WHERE documentId = $1
`
// Get the current max version for a document (call after LockFieldExtractionVersionsForDocument)
// Returns 0 if no versions exist, otherwise the max version number
// COALESCE ensures we always get a non-NULL value that can be scanned into int64
//
// SELECT COALESCE(MAX(version), 0) as max_version
// FROM documentFieldExtractionVersions
// WHERE documentId = $1
func (q *Queries) GetMaxFieldExtractionVersion(ctx context.Context, documentid uuid.UUID) (*int64, error) {
row := q.db.QueryRow(ctx, getMaxFieldExtractionVersion, documentid)
var max_version *int64
err := row.Scan(&max_version)
return max_version, err
}
const lockFieldExtractionVersionsForDocument = `-- name: LockFieldExtractionVersionsForDocument :many
SELECT id, version FROM documentFieldExtractionVersions
WHERE documentId = $1
FOR UPDATE
`
type LockFieldExtractionVersionsForDocumentRow struct {
ID uuid.UUID `db:"id"`
Version int64 `db:"version"`
}
// Lock all existing version rows for a document to prevent concurrent inserts
// Must be called within a transaction before GetMaxFieldExtractionVersion
// Returns the locked rows (which may be empty for first version)
//
// SELECT id, version FROM documentFieldExtractionVersions
// WHERE documentId = $1
// FOR UPDATE
func (q *Queries) LockFieldExtractionVersionsForDocument(ctx context.Context, documentid uuid.UUID) ([]*LockFieldExtractionVersionsForDocumentRow, error) {
rows, err := q.db.Query(ctx, lockFieldExtractionVersionsForDocument, documentid)
if err != nil {
return nil, err
}
defer rows.Close()
items := []*LockFieldExtractionVersionsForDocumentRow{}
for rows.Next() {
var i LockFieldExtractionVersionsForDocumentRow
if err := rows.Scan(&i.ID, &i.Version); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const validateArrayFieldCount = `-- name: ValidateArrayFieldCount :one
SELECT COUNT(*) as arrayFieldCount
FROM documentFieldExtractionArrayFields