Skip to content

Commit cfc7d34

Browse files
Merge pull request #490 from daostack/backend/main
Changes
2 parents 3f6f3f0 + 9c9cd30 commit cfc7d34

File tree

24 files changed

+28347
-109
lines changed

24 files changed

+28347
-109
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"compile": "tsc -p tsconfig.json && yarn fix:paths",
99
"compile:watch": "tsc-watch --onSuccess \"yarn fix:paths\"",
1010
"fix:paths": "tscpaths -p tsconfig.json -s ./src -o ./dist",
11-
"seed": "ts-node -r dotenv/config prisma/seed.ts",
1211
"studio": "prisma studio",
12+
"seed": "prisma migrate reset --force && ts-node --transpile -r dotenv/config prisma/seed.ts",
1313
"db:sync": "prisma migrate deploy",
1414
"create:schema": "find prisma -name '*.prisma' -not -name \"schema.prisma\" -exec cat {} + > prisma/schema.prisma && prisma format",
1515
"create:migration": "yarn create:schema && prisma migrate dev"

packages/core/prisma/firestore/firestore.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ import { FirebaseToolkit } from '@common/core';
22

33
FirebaseToolkit.InitializeFirebase();
44

5-
export const db = FirebaseToolkit.getDb();
5+
export const db = FirebaseToolkit.getDb();
6+
7+
export const ProposalsCollection = db.collection('proposals');
8+
export const PaymentsCollection = db.collection('payments');
9+
export const CardsCollection = db.collection('cards');
10+
export const UsersCollection = db.collection('users');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { UsersCollection } from '../firestore';
2+
3+
export const getEmail = async (uid: string): Promise<string> => {
4+
return (await UsersCollection
5+
.doc(uid)
6+
.get()).data()?.email;
7+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// @ts-ignore
2+
import path from 'path';
3+
// @ts-ignore
4+
import fs from 'fs';
5+
6+
import { CardsCollection } from '../firestore';
7+
import { seeder } from '../../seed';
8+
import { CardNetwork } from '@prisma/client';
9+
10+
export const importCards = async (date: Date) => {
11+
const firebaseCards = (await CardsCollection.get())
12+
.docs.map(e => e.data());
13+
14+
const promises: Promise<void>[] = [];
15+
16+
const results: any[] = [];
17+
const errored: any[] = [];
18+
19+
for (const fc of firebaseCards) {
20+
promises.push((async () => {
21+
try {
22+
const card = await seeder.card
23+
.create({
24+
data: {
25+
id: fc.id,
26+
27+
circleCardId: fc.circleCardId,
28+
29+
cvvCheck: fc.verification?.cvv || 'no verification',
30+
avsCheck: 'no verification',
31+
32+
digits: fc.metadata?.digits || '',
33+
network: fc.metadata?.network || CardNetwork.VISA,
34+
35+
user: {
36+
connect: {
37+
id: fc.ownerId
38+
}
39+
}
40+
}
41+
});
42+
43+
results.push(card);
44+
} catch (e) {
45+
errored.push({
46+
proposal: fc,
47+
error: {
48+
message: e.message,
49+
stack: e.stack
50+
}
51+
});
52+
}
53+
})());
54+
}
55+
56+
await Promise.all(promises);
57+
58+
fs.writeFileSync(path.join(__dirname, `../../result/${+date}/cardImports-errors.json`), JSON.stringify(errored));
59+
fs.writeFileSync(path.join(__dirname, `../../result/${+date}/cardImports-results.json`), JSON.stringify(results));
60+
};

packages/core/prisma/firestore/importers/importCommons.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export const importCommons = async () => {
2828
name: common.name,
2929
image: common.image,
3030

31-
balance: common.balance,
32-
raised: common.raised,
31+
balance: Math.round(common.balance),
32+
raised: Math.round(common.raised),
3333

3434
byline: common.metadata.byline,
3535
description: common.metadata.description,
@@ -81,6 +81,5 @@ export const importCommons = async () => {
8181
}
8282
}
8383

84-
console.log(failedCommons);
85-
console.log(createdCommons);
84+
console.log('[LogTag: 1333]', failedCommons);
8685
};
Lines changed: 88 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { db } from '../firestore';
1+
import { ProposalsCollection } from '../firestore';
22
import { seeder } from '../../seed';
33
import { IFundingRequestProposal, FundingRequestState } from '@common/types';
44
import { FundingState, ProposalState, ProposalType } from '@prisma/client';
5+
import { getEmail } from '../helpers/getEmail';
56

6-
const transformState = (state: FundingRequestState): ProposalState => {
7+
export const transformState = (state: FundingRequestState): ProposalState => {
78
switch (state) {
89
case 'failed':
910
return ProposalState.Rejected;
@@ -16,81 +17,110 @@ const transformState = (state: FundingRequestState): ProposalState => {
1617
};
1718

1819
export const importFundingProposals = async () => {
19-
const ProposalsCollection = db.collection('proposals');
20-
2120
const firebaseFundingProposals = (await ProposalsCollection
2221
.where('type', '==', 'fundingRequest').get())
2322
.docs.map(e => e.data());
2423

24+
const promises: Promise<void>[] = [];
25+
2526
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+
});
3747

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
4356
}
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+
},
4669

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,
5280

53-
type: ProposalType.FundingRequest,
81+
files: (fp.description as any)?.files?.map((f: any) => ({
82+
value: f.value
83+
})) || [],
5484

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+
})) || [],
5788

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+
})) || [],
6193

62-
images: (fp.description as any)?.images.map((f: any) => ({
63-
value: f.value
64-
})),
94+
state: transformState(fp.state),
6595

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+
),
70101

71-
state: transformState(fp.state),
102+
votesFor: fp.votesFor,
103+
votesAgainst: fp.votesAgainst,
72104

73-
expiresAt:
74-
new Date(
75-
fp.createdAt.toDate().getTime() +
76-
fp.countdownPeriod * 1000
77-
),
105+
importedFrom: JSON.stringify(fp),
78106

79-
votesFor: fp.votesFor,
80-
votesAgainst: fp.votesAgainst,
107+
funding: {
108+
create: {
109+
id: fp.id,
81110

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+
}
86114
}
87115
}
88-
}
89-
});
116+
});
90117

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+
})());
95123
}
124+
125+
await Promise.all(promises);
96126
};

0 commit comments

Comments
 (0)