@@ -28,6 +28,7 @@ import { canReceive, getWalletByType } from '@/wallets/common'
28
28
import performPaidAction from '../paidAction'
29
29
import performPayingAction from '../payingAction'
30
30
import { timeoutSignal , withTimeout } from '@/lib/time'
31
+ import { deleteVault , hasVault , vaultNewSchematoTypedef , vaultPrismaFragments } from '@/wallets/vault'
31
32
32
33
function injectResolvers ( resolvers ) {
33
34
console . group ( 'injected GraphQL resolvers:' )
@@ -42,11 +43,13 @@ function injectResolvers (resolvers) {
42
43
// this mutation was sent from an unsynced client
43
44
// to pass validation, we need to add the existing vault entries for validation
44
45
// in case the client is removing the receiving config
45
- existingVaultEntries = await models . vaultEntry . findMany ( {
46
+ const wallet = await models . wallet . findUnique ( {
46
47
where : {
47
- walletId : Number ( data . id )
48
- }
48
+ id : Number ( data . id )
49
+ } ,
50
+ include : vaultPrismaFragments . include ( )
49
51
} )
52
+ existingVaultEntries = vaultNewSchematoTypedef ( wallet ) . vaultEntries
50
53
}
51
54
52
55
const validData = await validateWallet ( walletDef ,
@@ -158,17 +161,17 @@ const resolvers = {
158
161
throw new GqlAuthenticationError ( )
159
162
}
160
163
161
- return await models . wallet . findMany ( {
162
- include : {
163
- vaultEntries : true
164
- } ,
164
+ const wallets = await models . wallet . findMany ( {
165
+ include : vaultPrismaFragments . include ( ) ,
165
166
where : {
166
167
userId : me . id
167
168
} ,
168
169
orderBy : {
169
170
priority : 'asc'
170
171
}
171
172
} )
173
+
174
+ return wallets . map ( vaultNewSchematoTypedef )
172
175
} ,
173
176
withdrawl : getWithdrawl ,
174
177
direct : async ( parent , { id } , { me, models } ) => {
@@ -554,7 +557,11 @@ const resolvers = {
554
557
}
555
558
556
559
const logger = walletLogger ( { wallet, models } )
557
- await models . wallet . delete ( { where : { userId : me . id , id : Number ( id ) } } )
560
+
561
+ await models . $transaction ( [
562
+ hasVault ( wallet ) ? deleteVault ( models , wallet ) : null ,
563
+ models . wallet . delete ( { where : { userId : me . id , id : Number ( id ) } } )
564
+ ] . filter ( Boolean ) )
558
565
559
566
if ( canReceive ( { def : getWalletByType ( wallet . type ) , config : wallet . wallet } ) ) {
560
567
logger . info ( 'details for receiving deleted' )
@@ -823,78 +830,41 @@ async function upsertWallet (
823
830
824
831
const txs = [ ]
825
832
826
- if ( id ) {
827
- const oldVaultEntries = await models . vaultEntry . findMany ( { where : { userId : me . id , walletId : Number ( id ) } } )
833
+ const walletWithVault = { ...wallet , vaultEntries }
828
834
829
- // createMany is the set difference of the new - old
830
- // deleteMany is the set difference of the old - new
831
- // updateMany is the intersection of the old and new
832
- const difference = ( a = [ ] , b = [ ] , key = 'key' ) => a . filter ( x => ! b . find ( y => y [ key ] === x [ key ] ) )
833
- const intersectionMerge = ( a = [ ] , b = [ ] , key = 'key' ) => a . filter ( x => b . find ( y => y [ key ] === x [ key ] ) )
834
- . map ( x => ( { [ key ] : x [ key ] , ...b . find ( y => y [ key ] === x [ key ] ) } ) )
835
+ if ( id ) {
836
+ const dbWallet = await models . wallet . findUnique ( {
837
+ where : { id : Number ( id ) , userId : me . id }
838
+ } )
835
839
836
840
txs . push (
837
841
models . wallet . update ( {
838
842
where : { id : Number ( id ) , userId : me . id } ,
839
843
data : {
840
844
enabled,
841
845
priority,
842
- // client only wallets have no receive config and thus don't have their own table
843
- ...( Object . keys ( recvConfig ) . length > 0
844
- ? {
845
- [ wallet . field ] : {
846
- upsert : {
847
- create : recvConfig ,
848
- update : recvConfig
849
- }
850
- }
851
- }
852
- : { } ) ,
853
- ...( vaultEntries
854
- ? {
855
- vaultEntries : {
856
- deleteMany : difference ( oldVaultEntries , vaultEntries , 'key' ) . map ( ( { key } ) => ( {
857
- userId : me . id , key
858
- } ) ) ,
859
- create : difference ( vaultEntries , oldVaultEntries , 'key' ) . map ( ( { key, iv, value } ) => ( {
860
- key, iv, value, userId : me . id
861
- } ) ) ,
862
- update : intersectionMerge ( oldVaultEntries , vaultEntries , 'key' ) . map ( ( { key, iv, value } ) => ( {
863
- where : { userId_key : { userId : me . id , key } } ,
864
- data : { value, iv }
865
- } ) )
866
- }
867
- }
868
- : { } )
869
-
846
+ [ wallet . field ] : {
847
+ upsert : {
848
+ create : { ...recvConfig , ...vaultPrismaFragments . create ( walletWithVault ) } ,
849
+ update : { ...recvConfig , ...vaultPrismaFragments . upsert ( walletWithVault ) }
850
+ } ,
851
+ // XXX the check is required because the update would fail if there is no row to delete ...
852
+ update : hasVault ( dbWallet ) ? vaultPrismaFragments . deleteMissing ( walletWithVault ) : undefined
853
+ }
870
854
} ,
871
- include : {
872
- vaultEntries : true
873
- }
855
+ include : vaultPrismaFragments . include ( walletWithVault )
874
856
} )
875
857
)
876
858
} else {
877
859
txs . push (
878
860
models . wallet . create ( {
879
- include : {
880
- vaultEntries : true
881
- } ,
861
+ include : vaultPrismaFragments . include ( walletWithVault ) ,
882
862
data : {
883
863
enabled,
884
864
priority,
885
865
userId : me . id ,
886
866
type : wallet . type ,
887
- // client only wallets have no receive config and thus don't have their own table
888
- ...( Object . keys ( recvConfig ) . length > 0 ? { [ wallet . field ] : { create : recvConfig } } : { } ) ,
889
- ...( vaultEntries
890
- ? {
891
- vaultEntries : {
892
- createMany : {
893
- data : vaultEntries ?. map ( ( { key, iv, value } ) => ( { key, iv, value, userId : me . id } ) )
894
- }
895
- }
896
- }
897
- : { } )
867
+ [ wallet . field ] : { create : { ...recvConfig , ...vaultPrismaFragments . create ( walletWithVault ) } }
898
868
}
899
869
} )
900
870
)
@@ -931,7 +901,7 @@ async function upsertWallet (
931
901
}
932
902
933
903
const [ upsertedWallet ] = await models . $transaction ( txs )
934
- return upsertedWallet
904
+ return vaultNewSchematoTypedef ( upsertedWallet )
935
905
}
936
906
937
907
export async function createWithdrawal ( parent , { invoice, maxFee } , { me, models, lnd, headers, wallet, logger } ) {
0 commit comments