@@ -972,14 +972,18 @@ type DocumentStorageBilling =
972972 | {
973973 readonly context : StorageBillingContext
974974 readonly bytes : number
975- readonly updatedUsage ?: number
976975 }
977976 | {
978977 readonly userId : string
979978 readonly bytes : number
980979 readonly sub : HighestPrioritySubscription | null
981980 }
982981
982+ interface DocumentStorageNotification {
983+ readonly context : StorageBillingContext
984+ readonly updatedUsage : number
985+ }
986+
983987/**
984988 * Resolves and checks one document-write storage payer. Workspace documents
985989 * use only the workspace-selected payer; workspace-less legacy documents retain
@@ -1043,12 +1047,12 @@ export async function createDocumentRecords(
10431047 requestId : string ,
10441048 uploadedBy : string | null = null
10451049) : Promise < DocumentData [ ] > {
1046- let storageBilling : DocumentStorageBilling | null = null
1047-
10481050 const totalBytes = documents . reduce ( ( sum , docData ) => sum + ( docData . fileSize || 0 ) , 0 )
10491051 const sub = totalBytes > 0 ? await resolveQuotaSubscription ( knowledgeBaseId , uploadedBy ) : null
10501052
1051- const returnData = await db . transaction ( async ( tx ) => {
1053+ const { returnData, storageNotification } = await db . transaction ( async ( tx ) => {
1054+ let storageNotification : DocumentStorageNotification | null = null
1055+
10521056 await tx . execute ( sql `SELECT 1 FROM knowledge_base WHERE id = ${ knowledgeBaseId } FOR UPDATE` )
10531057
10541058 const kb = await tx
@@ -1083,13 +1087,13 @@ export async function createDocumentRecords(
10831087 sub
10841088 )
10851089 if ( 'context' in preparedBilling ) {
1086- storageBilling = {
1087- ... preparedBilling ,
1088- updatedUsage : await incrementStorageUsageForBillingContextInTx (
1089- tx ,
1090- preparedBilling . context ,
1091- preparedBilling . bytes
1092- ) ,
1090+ const updatedUsage = await incrementStorageUsageForBillingContextInTx (
1091+ tx ,
1092+ preparedBilling . context ,
1093+ preparedBilling . bytes
1094+ )
1095+ if ( updatedUsage !== undefined ) {
1096+ storageNotification = { context : preparedBilling . context , updatedUsage }
10931097 }
10941098 } else {
10951099 const quotaCheck = await checkAndIncrementStorageUsageInTx (
@@ -1101,7 +1105,6 @@ export async function createDocumentRecords(
11011105 if ( ! quotaCheck . allowed ) {
11021106 throw new Error ( quotaCheck . error || 'Storage limit exceeded' )
11031107 }
1104- storageBilling = preparedBilling
11051108 }
11061109 }
11071110
@@ -1191,13 +1194,13 @@ export async function createDocumentRecords(
11911194 . where ( eq ( knowledgeBase . id , knowledgeBaseId ) )
11921195 }
11931196
1194- return returnData
1197+ return { returnData, storageNotification }
11951198 } )
11961199
1197- if ( storageBilling && 'context' in storageBilling && storageBilling . updatedUsage !== undefined ) {
1200+ if ( storageNotification ) {
11981201 void maybeNotifyStorageLimitForBillingContext (
1199- storageBilling . context ,
1200- storageBilling . updatedUsage
1202+ storageNotification . context ,
1203+ storageNotification . updatedUsage
12011204 )
12021205 }
12031206
@@ -1520,12 +1523,12 @@ export async function createSingleDocument(
15201523 ...processedTags ,
15211524 }
15221525
1523- let storageBilling : DocumentStorageBilling | null = null
1524-
15251526 const sub =
15261527 documentData . fileSize > 0 ? await resolveQuotaSubscription ( knowledgeBaseId , uploadedBy ) : null
15271528
1528- await db . transaction ( async ( tx ) => {
1529+ const storageNotification = await db . transaction ( async ( tx ) => {
1530+ let storageNotification : DocumentStorageNotification | null = null
1531+
15291532 await tx . execute ( sql `SELECT 1 FROM knowledge_base WHERE id = ${ knowledgeBaseId } FOR UPDATE` )
15301533
15311534 const kb = await tx
@@ -1559,13 +1562,13 @@ export async function createSingleDocument(
15591562 sub
15601563 )
15611564 if ( 'context' in preparedBilling ) {
1562- storageBilling = {
1563- ... preparedBilling ,
1564- updatedUsage : await incrementStorageUsageForBillingContextInTx (
1565- tx ,
1566- preparedBilling . context ,
1567- preparedBilling . bytes
1568- ) ,
1565+ const updatedUsage = await incrementStorageUsageForBillingContextInTx (
1566+ tx ,
1567+ preparedBilling . context ,
1568+ preparedBilling . bytes
1569+ )
1570+ if ( updatedUsage !== undefined ) {
1571+ storageNotification = { context : preparedBilling . context , updatedUsage }
15691572 }
15701573 } else {
15711574 const quotaCheck = await checkAndIncrementStorageUsageInTx (
@@ -1577,7 +1580,6 @@ export async function createSingleDocument(
15771580 if ( ! quotaCheck . allowed ) {
15781581 throw new Error ( quotaCheck . error || 'Storage limit exceeded' )
15791582 }
1580- storageBilling = preparedBilling
15811583 }
15821584 }
15831585
@@ -1587,12 +1589,14 @@ export async function createSingleDocument(
15871589 . update ( knowledgeBase )
15881590 . set ( { updatedAt : now } )
15891591 . where ( eq ( knowledgeBase . id , knowledgeBaseId ) )
1592+
1593+ return storageNotification
15901594 } )
15911595
1592- if ( storageBilling && 'context' in storageBilling && storageBilling . updatedUsage !== undefined ) {
1596+ if ( storageNotification ) {
15931597 void maybeNotifyStorageLimitForBillingContext (
1594- storageBilling . context ,
1595- storageBilling . updatedUsage
1598+ storageNotification . context ,
1599+ storageNotification . updatedUsage
15961600 )
15971601 }
15981602
0 commit comments