@@ -29,6 +29,7 @@ import { canReceive, getWalletByType } from '@/wallets/common'
29
29
import performPaidAction from '../paidAction'
30
30
import performPayingAction from '../payingAction'
31
31
import { timeoutSignal , withTimeout } from '@/lib/time'
32
+ import { deleteVault , hasVault , vaultNewSchematoTypedef , vaultPrismaFragments } from '@/wallets/vault'
32
33
33
34
function injectResolvers ( resolvers ) {
34
35
console . group ( 'injected GraphQL resolvers:' )
@@ -43,11 +44,13 @@ function injectResolvers (resolvers) {
43
44
// this mutation was sent from an unsynced client
44
45
// to pass validation, we need to add the existing vault entries for validation
45
46
// in case the client is removing the receiving config
46
- existingVaultEntries = await models . vaultEntry . findMany ( {
47
+ const wallet = await models . wallet . findUnique ( {
47
48
where : {
48
- walletId : Number ( data . id )
49
- }
49
+ id : Number ( data . id )
50
+ } ,
51
+ include : vaultPrismaFragments . include ( )
50
52
} )
53
+ existingVaultEntries = vaultNewSchematoTypedef ( wallet ) . vaultEntries
51
54
}
52
55
53
56
const validData = await validateWallet ( walletDef ,
@@ -159,17 +162,17 @@ const resolvers = {
159
162
throw new GqlAuthenticationError ( )
160
163
}
161
164
162
- return await models . wallet . findMany ( {
163
- include : {
164
- vaultEntries : true
165
- } ,
165
+ const wallets = await models . wallet . findMany ( {
166
+ include : vaultPrismaFragments . include ( ) ,
166
167
where : {
167
168
userId : me . id
168
169
} ,
169
170
orderBy : {
170
171
priority : 'asc'
171
172
}
172
173
} )
174
+
175
+ return wallets . map ( vaultNewSchematoTypedef )
173
176
} ,
174
177
withdrawl : getWithdrawl ,
175
178
direct : async ( parent , { id } , { me, models } ) => {
@@ -569,7 +572,11 @@ const resolvers = {
569
572
}
570
573
571
574
const logger = walletLogger ( { wallet, models } )
572
- await models . wallet . delete ( { where : { userId : me . id , id : Number ( id ) } } )
575
+
576
+ await models . $transaction ( [
577
+ hasVault ( wallet ) ? deleteVault ( models , wallet ) : null ,
578
+ models . wallet . delete ( { where : { userId : me . id , id : Number ( id ) } } )
579
+ ] . filter ( Boolean ) )
573
580
574
581
if ( canReceive ( { def : getWalletByType ( wallet . type ) , config : wallet . wallet } ) ) {
575
582
logger . info ( 'details for receiving deleted' )
@@ -838,78 +845,41 @@ async function upsertWallet (
838
845
839
846
const txs = [ ]
840
847
841
- if ( id ) {
842
- const oldVaultEntries = await models . vaultEntry . findMany ( { where : { userId : me . id , walletId : Number ( id ) } } )
848
+ const walletWithVault = { ...wallet , vaultEntries }
843
849
844
- // createMany is the set difference of the new - old
845
- // deleteMany is the set difference of the old - new
846
- // updateMany is the intersection of the old and new
847
- const difference = ( a = [ ] , b = [ ] , key = 'key' ) => a . filter ( x => ! b . find ( y => y [ key ] === x [ key ] ) )
848
- const intersectionMerge = ( a = [ ] , b = [ ] , key = 'key' ) => a . filter ( x => b . find ( y => y [ key ] === x [ key ] ) )
849
- . map ( x => ( { [ key ] : x [ key ] , ...b . find ( y => y [ key ] === x [ key ] ) } ) )
850
+ if ( id ) {
851
+ const dbWallet = await models . wallet . findUnique ( {
852
+ where : { id : Number ( id ) , userId : me . id }
853
+ } )
850
854
851
855
txs . push (
852
856
models . wallet . update ( {
853
857
where : { id : Number ( id ) , userId : me . id } ,
854
858
data : {
855
859
enabled,
856
860
priority,
857
- // client only wallets have no receive config and thus don't have their own table
858
- ...( Object . keys ( recvConfig ) . length > 0
859
- ? {
860
- [ wallet . field ] : {
861
- upsert : {
862
- create : recvConfig ,
863
- update : recvConfig
864
- }
865
- }
866
- }
867
- : { } ) ,
868
- ...( vaultEntries
869
- ? {
870
- vaultEntries : {
871
- deleteMany : difference ( oldVaultEntries , vaultEntries , 'key' ) . map ( ( { key } ) => ( {
872
- userId : me . id , key
873
- } ) ) ,
874
- create : difference ( vaultEntries , oldVaultEntries , 'key' ) . map ( ( { key, iv, value } ) => ( {
875
- key, iv, value, userId : me . id
876
- } ) ) ,
877
- update : intersectionMerge ( oldVaultEntries , vaultEntries , 'key' ) . map ( ( { key, iv, value } ) => ( {
878
- where : { userId_key : { userId : me . id , key } } ,
879
- data : { value, iv }
880
- } ) )
881
- }
882
- }
883
- : { } )
884
-
861
+ [ wallet . field ] : {
862
+ upsert : {
863
+ create : { ...recvConfig , ...vaultPrismaFragments . create ( walletWithVault ) } ,
864
+ update : { ...recvConfig , ...vaultPrismaFragments . upsert ( walletWithVault ) }
865
+ } ,
866
+ // XXX the check is required because the update would fail if there is no row to delete ...
867
+ update : hasVault ( dbWallet ) ? vaultPrismaFragments . deleteMissing ( walletWithVault ) : undefined
868
+ }
885
869
} ,
886
- include : {
887
- vaultEntries : true
888
- }
870
+ include : vaultPrismaFragments . include ( walletWithVault )
889
871
} )
890
872
)
891
873
} else {
892
874
txs . push (
893
875
models . wallet . create ( {
894
- include : {
895
- vaultEntries : true
896
- } ,
876
+ include : vaultPrismaFragments . include ( walletWithVault ) ,
897
877
data : {
898
878
enabled,
899
879
priority,
900
880
userId : me . id ,
901
881
type : wallet . type ,
902
- // client only wallets have no receive config and thus don't have their own table
903
- ...( Object . keys ( recvConfig ) . length > 0 ? { [ wallet . field ] : { create : recvConfig } } : { } ) ,
904
- ...( vaultEntries
905
- ? {
906
- vaultEntries : {
907
- createMany : {
908
- data : vaultEntries ?. map ( ( { key, iv, value } ) => ( { key, iv, value, userId : me . id } ) )
909
- }
910
- }
911
- }
912
- : { } )
882
+ [ wallet . field ] : { create : { ...recvConfig , ...vaultPrismaFragments . create ( walletWithVault ) } }
913
883
}
914
884
} )
915
885
)
@@ -946,7 +916,7 @@ async function upsertWallet (
946
916
}
947
917
948
918
const [ upsertedWallet ] = await models . $transaction ( txs )
949
- return upsertedWallet
919
+ return vaultNewSchematoTypedef ( upsertedWallet )
950
920
}
951
921
952
922
export async function createWithdrawal ( parent , { invoice, maxFee } , { me, models, lnd, headers, wallet, logger } ) {
0 commit comments