@@ -9,6 +9,7 @@ import { deleteOrphanedOAuthAccount } from '@/lib/credentials/deletion'
99import { resumeConnectorsAfterCredentialReconnect } from '@/lib/knowledge/connectors/credential-recovery'
1010import { clearOAuthRefreshDeadFlag } from '@/lib/oauth/refresh-coordination'
1111import { captureServerEvent } from '@/lib/posthog/server'
12+ import { rememberShopifyCredentialScope } from '@/lib/shopify/privacy/installation-scopes'
1213
1314const logger = createLogger ( 'CredentialDraftHooks' )
1415
@@ -26,17 +27,32 @@ export async function handleCreateCredentialFromDraft(params: {
2627 const credentialId = generateId ( )
2728
2829 try {
29- await db . insert ( schema . credential ) . values ( {
30- id : credentialId ,
31- workspaceId : draft . workspaceId ,
32- type : 'oauth' ,
33- displayName : draft . displayName ,
34- description : draft . description ?? null ,
35- providerId,
36- accountId,
37- createdBy : userId ,
38- createdAt : now ,
39- updatedAt : now ,
30+ await db . transaction ( async ( tx ) => {
31+ await tx . insert ( schema . credential ) . values ( {
32+ id : credentialId ,
33+ workspaceId : draft . workspaceId ,
34+ type : 'oauth' ,
35+ displayName : draft . displayName ,
36+ description : draft . description ?? null ,
37+ providerId,
38+ accountId,
39+ createdBy : userId ,
40+ createdAt : now ,
41+ updatedAt : now ,
42+ } )
43+ await tx . insert ( schema . credentialMember ) . values ( {
44+ id : generateId ( ) ,
45+ credentialId,
46+ userId,
47+ role : 'admin' ,
48+ status : 'active' ,
49+ joinedAt : now ,
50+ invitedBy : userId ,
51+ createdAt : now ,
52+ updatedAt : now ,
53+ } )
54+ if ( providerId === 'shopify' )
55+ await rememberShopifyCredentialScope ( accountId , credentialId , tx )
4056 } )
4157 } catch ( insertError : unknown ) {
4258 if (
@@ -70,10 +86,14 @@ export async function handleCreateCredentialFromDraft(params: {
7086 accountId,
7187 } )
7288
73- await db
74- . update ( schema . credential )
75- . set ( { updatedAt : now } )
76- . where ( eq ( schema . credential . id , existingCredential . id ) )
89+ await db . transaction ( async ( tx ) => {
90+ await tx
91+ . update ( schema . credential )
92+ . set ( { updatedAt : now } )
93+ . where ( eq ( schema . credential . id , existingCredential . id ) )
94+ if ( providerId === 'shopify' )
95+ await rememberShopifyCredentialScope ( accountId , existingCredential . id , tx )
96+ } )
7797
7898 await clearOAuthRefreshDeadFlag ( accountId )
7999 await resumeConnectorsAfterCredentialReconnect ( accountId , now )
@@ -91,18 +111,6 @@ export async function handleCreateCredentialFromDraft(params: {
91111 return
92112 }
93113
94- await db . insert ( schema . credentialMember ) . values ( {
95- id : generateId ( ) ,
96- credentialId,
97- userId,
98- role : 'admin' ,
99- status : 'active' ,
100- joinedAt : now ,
101- invitedBy : userId ,
102- createdAt : now ,
103- updatedAt : now ,
104- } )
105-
106114 logger . info ( 'Created credential from draft' , {
107115 credentialId,
108116 displayName : draft . displayName ,
@@ -161,6 +169,7 @@ export async function handleReconnectCredential(params: {
161169 id : schema . credential . id ,
162170 accountId : schema . credential . accountId ,
163171 displayName : schema . credential . displayName ,
172+ providerId : schema . credential . providerId ,
164173 } )
165174 . from ( schema . credential )
166175 . where ( eq ( schema . credential . id , draft . credentialId ) )
@@ -194,10 +203,14 @@ export async function handleReconnectCredential(params: {
194203 }
195204 }
196205
197- await db
198- . update ( schema . credential )
199- . set ( { accountId : newAccountId , updatedAt : now } )
200- . where ( eq ( schema . credential . id , draft . credentialId ) )
206+ await db . transaction ( async ( tx ) => {
207+ await tx
208+ . update ( schema . credential )
209+ . set ( { accountId : newAccountId , updatedAt : now } )
210+ . where ( eq ( schema . credential . id , existingCredential . id ) )
211+ if ( existingCredential . providerId === 'shopify' )
212+ await rememberShopifyCredentialScope ( newAccountId , existingCredential . id , tx )
213+ } )
201214
202215 logger . info (
203216 accountChanged
0 commit comments