@@ -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,6 +44,7 @@ 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
47
+ // TODO: replace this usage because of new schema
46
48
existingVaultEntries = await models . vaultEntry . findMany ( {
47
49
where : {
48
50
walletId : Number ( data . id )
@@ -159,17 +161,17 @@ const resolvers = {
159
161
throw new GqlAuthenticationError ( )
160
162
}
161
163
162
- return await models . wallet . findMany ( {
163
- include : {
164
- vaultEntries : true
165
- } ,
164
+ const wallets = await models . wallet . findMany ( {
165
+ include : vaultPrismaFragments ( ) . include ,
166
166
where : {
167
167
userId : me . id
168
168
} ,
169
169
orderBy : {
170
170
priority : 'asc'
171
171
}
172
172
} )
173
+
174
+ return wallets . map ( vaultNewSchematoTypedef )
173
175
} ,
174
176
withdrawl : getWithdrawl ,
175
177
direct : async ( parent , { id } , { me, models } ) => {
@@ -569,7 +571,11 @@ const resolvers = {
569
571
}
570
572
571
573
const logger = walletLogger ( { wallet, models } )
572
- await models . wallet . delete ( { where : { userId : me . id , id : Number ( id ) } } )
574
+
575
+ await models . $transaction ( [
576
+ deleteVault ( models , wallet ) ,
577
+ models . wallet . delete ( { where : { userId : me . id , id : Number ( id ) } } )
578
+ ] )
573
579
574
580
if ( canReceive ( { def : getWalletByType ( wallet . type ) , config : wallet . wallet } ) ) {
575
581
logger . info ( 'details for receiving deleted' )
@@ -838,15 +844,12 @@ async function upsertWallet (
838
844
839
845
const txs = [ ]
840
846
841
- if ( id ) {
842
- const oldVaultEntries = await models . vaultEntry . findMany ( { where : { userId : me . id , walletId : Number ( id ) } } )
847
+ const vaultFrags = vaultPrismaFragments ( { ...wallet , vaultEntries } )
843
848
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 ] ) } ) )
849
+ if ( id ) {
850
+ const dbWallet = await models . wallet . findUnique ( {
851
+ where : { id : Number ( id ) , userId : me . id }
852
+ } )
850
853
851
854
txs . push (
852
855
models . wallet . update ( {
@@ -859,57 +862,29 @@ async function upsertWallet (
859
862
? {
860
863
[ wallet . field ] : {
861
864
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
- } ) )
865
+ create : { ...recvConfig , ...vaultFrags ?. create } ,
866
+ update : { ...recvConfig , ...vaultFrags ?. upsert }
867
+ } ,
868
+ // XXX the check is required because the update would fail if there is no row to delete ...
869
+ update : await hasVault ( models , dbWallet ) ? vaultFrags ?. deleteOnUpdate : undefined
881
870
}
882
871
}
883
872
: { } )
884
-
885
873
} ,
886
- include : {
887
- vaultEntries : true
888
- }
874
+ include : vaultFrags ?. include
889
875
} )
890
876
)
891
877
} else {
892
878
txs . push (
893
879
models . wallet . create ( {
894
- include : {
895
- vaultEntries : true
896
- } ,
880
+ include : vaultFrags ?. include ,
897
881
data : {
898
882
enabled,
899
883
priority,
900
884
userId : me . id ,
901
885
type : wallet . type ,
902
886
// 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
- : { } )
887
+ ...( Object . keys ( recvConfig ) . length > 0 ? { [ wallet . field ] : { create : { ...recvConfig , ...vaultFrags ?. create } } } : { } )
913
888
}
914
889
} )
915
890
)
@@ -946,7 +921,9 @@ async function upsertWallet (
946
921
}
947
922
948
923
const [ upsertedWallet ] = await models . $transaction ( txs )
949
- return upsertedWallet
924
+
925
+ // migrate from old schema to new schema for vault
926
+ return vaultNewSchematoTypedef ( upsertedWallet )
950
927
}
951
928
952
929
export async function createWithdrawal ( parent , { invoice, maxFee } , { me, models, lnd, headers, wallet, logger } ) {
0 commit comments