@@ -45,7 +45,6 @@ import {
4545 incrementStorageUsageForBillingContextInTx ,
4646 lockWorkspaceStorageForMutationInTx ,
4747 maybeNotifyStorageLimitForBillingContext ,
48- prepareFileAccountingInTx ,
4948 resolveStorageBillingContext ,
5049} from '@/lib/billing/storage'
5150import {
@@ -783,6 +782,7 @@ export async function createOwnedWorkspaceFile<T>(params: {
783782 const name = params . fileName ( fileId )
784783 const buffer = Buffer . from ( params . content , 'utf-8' )
785784 const storageKey = generateWorkspaceFileKey ( params . workspaceId , name )
785+ const storageBillingContext = await resolveStorageBillingContext ( params . workspaceId )
786786 const uploadResult = await uploadFile ( {
787787 file : buffer ,
788788 fileName : storageKey ,
@@ -800,11 +800,7 @@ export async function createOwnedWorkspaceFile<T>(params: {
800800 persistMetadata : false ,
801801 } )
802802 try {
803- const { owner, updatedUsage, billing } = await db . transaction ( async ( tx ) => {
804- const accounting = await prepareFileAccountingInTx ( tx , {
805- entityType : 'workspace' ,
806- entityId : params . workspaceId ,
807- } )
803+ const { owner, updatedUsage } = await db . transaction ( async ( tx ) => {
808804 const inserted = await insertWorkspaceFileMetadataInTx ( tx , {
809805 id : fileId ,
810806 key : uploadResult . key ,
@@ -817,14 +813,14 @@ export async function createOwnedWorkspaceFile<T>(params: {
817813 context : params . context ,
818814 } )
819815 if ( ! inserted ) throw new Error ( `Owned ${ params . context } file ${ fileId } was not inserted` )
820- const updatedUsage = await accounting . mutation . applyDelta ( buffer . length )
821- return {
822- owner : await params . insertOwner ( tx , fileId ) ,
823- updatedUsage ,
824- billing : accounting . billing ,
825- }
816+ const updatedUsage = await incrementStorageUsageForBillingContextInTx (
817+ tx ,
818+ storageBillingContext ,
819+ buffer . length
820+ )
821+ return { owner : await params . insertOwner ( tx , fileId ) , updatedUsage }
826822 } )
827- void maybeNotifyStorageLimitForBillingContext ( billing , updatedUsage )
823+ void maybeNotifyStorageLimitForBillingContext ( storageBillingContext , updatedUsage )
828824 return { fileId, owner }
829825 } catch ( error ) {
830826 await cleanupWorkspaceStorageObject (
0 commit comments