1
- import { db } from '../firestore' ;
1
+ import { ProposalsCollection } from '../firestore' ;
2
2
import { seeder } from '../../seed' ;
3
3
import { IFundingRequestProposal , FundingRequestState } from '@common/types' ;
4
4
import { FundingState , ProposalState , ProposalType } from '@prisma/client' ;
5
+ import { getEmail } from '../helpers/getEmail' ;
5
6
6
- const transformState = ( state : FundingRequestState ) : ProposalState => {
7
+ export const transformState = ( state : FundingRequestState ) : ProposalState => {
7
8
switch ( state ) {
8
9
case 'failed' :
9
10
return ProposalState . Rejected ;
@@ -16,81 +17,110 @@ const transformState = (state: FundingRequestState): ProposalState => {
16
17
} ;
17
18
18
19
export const importFundingProposals = async ( ) => {
19
- const ProposalsCollection = db . collection ( 'proposals' ) ;
20
-
21
20
const firebaseFundingProposals = ( await ProposalsCollection
22
21
. where ( 'type' , '==' , 'fundingRequest' ) . get ( ) )
23
22
. docs . map ( e => e . data ( ) ) ;
24
23
24
+ const promises : Promise < void > [ ] = [ ] ;
25
+
25
26
for ( const fp of firebaseFundingProposals as IFundingRequestProposal [ ] ) {
26
- console . log ( 'Importing proposal' ) ;
27
-
28
- try {
29
- const ifp = await seeder . proposal
30
- . create ( {
31
- data : {
32
- common : {
33
- connect : {
34
- id : fp . commonId
35
- }
36
- } ,
27
+ promises . push ( ( async ( ) => {
28
+ console . log ( 'Importing proposal' ) ;
29
+
30
+ try {
31
+ const memberExists = ! ! ( await seeder . commonMember . count ( {
32
+ where : {
33
+ userId : fp . proposerId ,
34
+ commonId : fp . commonId
35
+ }
36
+ } ) ) ;
37
+
38
+ const user = await seeder . user . findUnique ( {
39
+ where : {
40
+ id : fp . proposerId
41
+ }
42
+ } ) || await seeder . user . findUnique ( {
43
+ where : {
44
+ email : await getEmail ( fp . proposerId )
45
+ }
46
+ } ) ;
37
47
38
- commonMember : {
39
- connect : {
40
- userId_commonId : {
41
- userId : fp . proposerId ,
42
- commonId : fp . commonId
48
+ const ifp = await seeder . proposal
49
+ . create ( {
50
+ data : {
51
+ id : fp . id ,
52
+
53
+ common : {
54
+ connect : {
55
+ id : fp . commonId
43
56
}
44
- }
45
- } ,
57
+ } ,
58
+
59
+ ...memberExists && {
60
+ commonMember : {
61
+ connect : {
62
+ userId_commonId : {
63
+ userId : fp . proposerId ,
64
+ commonId : fp . commonId
65
+ }
66
+ }
67
+ }
68
+ } ,
46
69
47
- user : {
48
- connect : {
49
- id : fp . proposerId
50
- }
51
- } ,
70
+ user : {
71
+ connect : {
72
+ id : user ?. id || 'default'
73
+ }
74
+ } ,
75
+
76
+ type : ProposalType . FundingRequest ,
77
+
78
+ title : ( fp . description as any ) . title ,
79
+ description : ( fp . description as any ) . description ,
52
80
53
- type : ProposalType . FundingRequest ,
81
+ files : ( fp . description as any ) ?. files ?. map ( ( f : any ) => ( {
82
+ value : f . value
83
+ } ) ) || [ ] ,
54
84
55
- title : ( fp . description as any ) . title ,
56
- description : ( fp . description as any ) . description ,
85
+ images : ( fp . description as any ) ?. images ?. map ( ( f : any ) => ( {
86
+ value : f . value
87
+ } ) ) || [ ] ,
57
88
58
- files : ( fp . description as any ) ?. files . map ( ( f : any ) => ( {
59
- value : f . value
60
- } ) ) ,
89
+ links : ( fp . description as any ) ?. links ?. map ( ( f : any ) => ( {
90
+ title : f . title || '' ,
91
+ url : f . value
92
+ } ) ) || [ ] ,
61
93
62
- images : ( fp . description as any ) ?. images . map ( ( f : any ) => ( {
63
- value : f . value
64
- } ) ) ,
94
+ state : transformState ( fp . state ) ,
65
95
66
- links : ( fp . description as any ) ?. links . map ( ( f : any ) => ( {
67
- title : f . title || '' ,
68
- url : f . value
69
- } ) ) ,
96
+ expiresAt :
97
+ new Date (
98
+ fp . createdAt . toDate ( ) . getTime ( ) +
99
+ fp . countdownPeriod * 1000
100
+ ) ,
70
101
71
- state : transformState ( fp . state ) ,
102
+ votesFor : fp . votesFor ,
103
+ votesAgainst : fp . votesAgainst ,
72
104
73
- expiresAt :
74
- new Date (
75
- fp . createdAt . toDate ( ) . getTime ( ) +
76
- fp . countdownPeriod * 1000
77
- ) ,
105
+ importedFrom : JSON . stringify ( fp ) ,
78
106
79
- votesFor : fp . votesFor ,
80
- votesAgainst : fp . votesAgainst ,
107
+ funding : {
108
+ create : {
109
+ id : fp . id ,
81
110
82
- funding : {
83
- create : {
84
- fundingState : FundingState . NotEligible ,
85
- amount : fp . fundingRequest . amount
111
+ fundingState : FundingState . NotEligible ,
112
+ amount : Math . round ( fp . fundingRequest . amount )
113
+ }
86
114
}
87
115
}
88
- }
89
- } ) ;
116
+ } ) ;
90
117
91
- console . log ( 'Imported proposal' ) ;
92
- } catch ( e ) {
93
- console . log ( 'Failed importing funding request' , e ) ;
94
- }
118
+ console . log ( 'Imported proposal' ) ;
119
+ } catch ( e ) {
120
+ console . log ( 'Failed importing funding request' , fp , e ) ;
121
+ }
122
+ } ) ( ) ) ;
95
123
}
124
+
125
+ await Promise . all ( promises ) ;
96
126
} ;
0 commit comments