Files
Jay Brown 8c218f162b Merged in feature/textExtractionsPart3 (pull request #195)
enrich doc responses

* enrich doc responses
2025-12-11 14:25:51 +00:00

268 lines
7.2 KiB
SQL

-- name: AddFieldExtraction :one
INSERT INTO documentFieldExtractions (
documentId,
fileName,
contractTitle,
aareteDerivedAmendmentNum,
clientName,
payerName,
payerState,
providerState,
filenameTin,
provGroupTin,
provGroupNpi,
provGroupNameFull,
provOtherTin,
provOtherNpi,
provOtherNameFull,
aareteDerivedEffectiveDt,
aareteDerivedTerminationDt,
autoRenewalInd,
autoRenewalTerm,
createdBy
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20
)
RETURNING *;
-- name: AddFieldExtractionArrayField :exec
INSERT INTO documentFieldExtractionArrayFields (
fieldExtractionId,
arrayIndex,
exhibitTitle,
exhibitPage,
reimbProvTin,
reimbProvNpi,
reimbProvName,
reimbEffectiveDt,
reimbTerminationDt,
aareteDerivedClaimTypeCd,
aareteDerivedProduct,
aareteDerivedLob,
aareteDerivedProgram,
aareteDerivedNetwork,
aareteDerivedProvType,
provTaxonomyCd,
provTaxonomyCdDesc,
provSpecialtyCd,
provSpecialtyCdDesc,
placeOfServiceCd,
placeOfServiceCdDesc,
billTypeCd,
billTypeCdDesc,
patientAgeMin,
patientAgeMax,
reimbTerm,
lobProgramRelationship,
lobProductRelationship,
carveoutInd,
carveoutCd,
lesserOfInd,
greaterOfInd,
aareteDerivedReimbMethod,
unitOfMeasure,
reimbPctRate,
reimbFeeRate,
reimbConversionFactor,
triggerCapThresholdAmt,
triggerBaseThreshold,
defaultInd,
additionDesc,
additionMaxFeeRateInc,
additionMaxPctRateInc,
aareteDerivedAdditionRateChangeTimeline,
aareteDerivedFeeSchedule,
aareteDerivedFeeScheduleVersion,
serviceTerm,
cpt4ProcCd,
cpt4ProcCdDesc,
cpt4ProcMod,
cpt4ProcModDesc,
revenueCd,
revenueCdDesc,
diagCd,
diagCdDesc,
ndcCd,
ndcCdDesc,
claimAdmitTypeCd,
authAdmitTypeDesc,
claimStatusCd,
claimStatusCdDesc,
grouperType,
grouperCd,
grouperCdDesc,
grouperPctRate,
grouperBaseRate,
aareteDerivedGrouperVersion,
grouperAlternativeLevelOfCare,
grouperSeverityInd,
grouperSeverity,
grouperRiskOfMortalitySubclass,
grouperTransferInd,
grouperReadmissionsInd,
grouperHacInd,
outlierTerm,
outlierFirstDollarInd,
rangeNbrDays,
outlierFixedLossNbrDaysThreshold,
outlierFixedLossThreshold,
outlierMaximum,
outlierMaximumFrequency,
outlierPctRate,
outlierExclusionCd,
outlierExclusionCdDesc,
facilityAdjustmentTerm,
dshInd,
dshPctRate,
dshFeeRate,
imeInd,
imePctRate,
imeFeeRate,
ntapInd,
ntapPctRate,
ntapFeeRate,
ucInd,
ucPctRate,
ucFeeRate,
gmeInd,
gmePctRate,
gmeFeeRate,
rateEscalatorInd,
rateEscalatorDesc,
rateEscalatorMaxRateIncPct,
rateEscalatorRateChangeTimeline,
stopLossTerm,
stopLossFirstDollarInd,
stopLossRangeNbrDays,
stopLossFixedLossThreshold,
stopLossMaximum,
stopLossMaximumFrequency,
stopLossDailyMaxRate,
stopLossPctRateOnExcessCharges,
stopLossExclusionCd,
stopLossExclusionDesc
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20,
$21, $22, $23, $24, $25, $26, $27, $28, $29, $30,
$31, $32, $33, $34, $35, $36, $37, $38, $39, $40,
$41, $42, $43, $44, $45, $46, $47, $48, $49, $50,
$51, $52, $53, $54, $55, $56, $57, $58, $59, $60,
$61, $62, $63, $64, $65, $66, $67, $68, $69, $70,
$71, $72, $73, $74, $75, $76, $77, $78, $79, $80,
$81, $82, $83, $84, $85, $86, $87, $88, $89, $90,
$91, $92, $93, $94, $95, $96, $97, $98, $99, $100,
$101, $102, $103, $104, $105, $106, $107, $108, $109, $110,
$111, $112, $113, $114
);
-- name: AddFieldExtractionEntry :exec
-- Insert a new version entry for a field extraction
-- documentId is required for the unique constraint enforcement
INSERT INTO documentFieldExtractionVersions (fieldExtractionId, documentId, version, createdBy)
VALUES ($1, $2, $3, $4);
-- name: LockFieldExtractionVersionsForDocument :many
-- 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;
-- name: GetMaxFieldExtractionVersion :one
-- 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;
-- name: GetCurrentFieldExtraction :one
SELECT * FROM currentFieldExtractions
WHERE documentId = $1;
-- name: GetFieldExtractionByVersion :one
-- 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;
-- name: GetCurrentFieldExtractionWithArrayCount :one
SELECT * FROM currentFieldExtractionsWithArrayCount
WHERE documentId = $1;
-- name: GetFieldExtractionArrayFields :many
SELECT * FROM documentFieldExtractionArrayFields
WHERE fieldExtractionId = $1
ORDER BY arrayIndex;
-- name: GetFieldExtractionHistory :many
SELECT
dfe.id,
dfe.documentId,
dfev.version,
dfev.createdBy,
dfev.createdAt
FROM documentFieldExtractions dfe
JOIN documentFieldExtractionVersions dfev
ON dfev.fieldExtractionId = dfe.id
WHERE dfe.documentId = $1
ORDER BY dfev.version DESC;
-- name: ValidateArrayFieldCount :one
SELECT COUNT(*) as arrayFieldCount
FROM documentFieldExtractionArrayFields
WHERE fieldExtractionId = $1;
-- name: GetFieldExtractionByID :one
SELECT * FROM documentFieldExtractions
WHERE id = $1;
-- name: GetFieldExtractionsByDocumentID :many
SELECT * FROM documentFieldExtractions
WHERE documentId = $1
ORDER BY createdAt DESC;
-- name: HasFieldExtraction :one
-- Checks if a field extraction exists for a document
SELECT EXISTS(
SELECT 1 FROM documentFieldExtractionVersions
WHERE documentId = $1
) as has_extraction;
-- name: HasFieldExtractionBatch :many
-- Checks if field extractions exist for multiple documents (for folder listing)
-- Returns one row per input document ID indicating whether extraction exists
SELECT
t.documentId,
EXISTS(SELECT 1 FROM documentFieldExtractionVersions dfev WHERE dfev.documentId = t.documentId) as has_extraction
FROM unnest(@documentIds::uuid[]) as t(documentId);