Skip to content

Commit 9c9cd30

Browse files
More Importers
Signed-off-by: Alexander Ivanov <[email protected]>
1 parent 690e41e commit 9c9cd30

File tree

18 files changed

+28195
-18
lines changed

18 files changed

+28195
-18
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"compile:watch": "tsc-watch --onSuccess \"yarn fix:paths\"",
1010
"fix:paths": "tscpaths -p tsconfig.json -s ./src -o ./dist",
1111
"studio": "prisma studio",
12-
"seed": "prisma migrate reset --force && ts-node -r dotenv/config prisma/seed.ts",
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: 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/importFundingProposals.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +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';
55
import { getEmail } from '../helpers/getEmail';
66

7-
const transformState = (state: FundingRequestState): ProposalState => {
7+
export const transformState = (state: FundingRequestState): ProposalState => {
88
switch (state) {
99
case 'failed':
1010
return ProposalState.Rejected;
@@ -17,8 +17,6 @@ const transformState = (state: FundingRequestState): ProposalState => {
1717
};
1818

1919
export const importFundingProposals = async () => {
20-
const ProposalsCollection = db.collection('proposals');
21-
2220
const firebaseFundingProposals = (await ProposalsCollection
2321
.where('type', '==', 'fundingRequest').get())
2422
.docs.map(e => e.data());
@@ -50,6 +48,8 @@ export const importFundingProposals = async () => {
5048
const ifp = await seeder.proposal
5149
.create({
5250
data: {
51+
id: fp.id,
52+
5353
common: {
5454
connect: {
5555
id: fp.commonId
@@ -106,6 +106,8 @@ export const importFundingProposals = async () => {
106106

107107
funding: {
108108
create: {
109+
id: fp.id,
110+
109111
fundingState: FundingState.NotEligible,
110112
amount: Math.round(fp.fundingRequest.amount)
111113
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// @ts-ignore
2+
import fs from 'fs';
3+
// @ts-ignore
4+
import path from 'path';
5+
6+
import { ProposalsCollection } from '../firestore';
7+
import { seeder } from '../../seed';
8+
import { getEmail } from '../helpers/getEmail';
9+
import { ProposalType } from '@prisma/client';
10+
import { transformState } from './importFundingProposals';
11+
import { FundingType } from 'admin/src/core/graphql';
12+
13+
export const transformFundingType = (ct: string): FundingType => {
14+
switch (ct) {
15+
case 'one-time':
16+
return FundingType.OneTime;
17+
case 'monthly':
18+
return FundingType.Monthly;
19+
default:
20+
return FundingType.OneTime;
21+
}
22+
};
23+
24+
export const importJoinProposals = async (date: Date) => {
25+
const firebaseJoinProposals = (await ProposalsCollection
26+
.where('type', '==', 'join').get())
27+
.docs.map(e => e.data());
28+
29+
const promises: Promise<void>[] = [];
30+
31+
const results: any[] = [];
32+
const errored: any[] = [];
33+
34+
for (const jp of firebaseJoinProposals) {
35+
promises.push((async () => {
36+
try {
37+
const memberExists = !!(await seeder.commonMember.count({
38+
where: {
39+
userId: jp.proposerId,
40+
commonId: jp.commonId
41+
}
42+
}));
43+
44+
const user = await seeder.user.findUnique({
45+
where: {
46+
id: jp.proposerId
47+
}
48+
}) || await seeder.user.findUnique({
49+
where: {
50+
email: await getEmail(jp.proposerId)
51+
}
52+
});
53+
54+
const cjp = await seeder.proposal
55+
.create({
56+
data: {
57+
id: jp.id,
58+
59+
common: {
60+
connect: {
61+
id: jp.commonId
62+
}
63+
},
64+
65+
...memberExists && {
66+
commonMember: {
67+
connect: {
68+
userId_commonId: {
69+
userId: jp.proposerId,
70+
commonId: jp.commonId
71+
}
72+
}
73+
}
74+
},
75+
76+
user: {
77+
connect: {
78+
id: user?.id || 'default'
79+
}
80+
},
81+
82+
type: ProposalType.JoinRequest,
83+
84+
title: (jp.description as any).title,
85+
description: (jp.description as any).description,
86+
87+
files: (jp.description as any)?.files?.map((f: any) => ({
88+
value: f.value
89+
})) || [],
90+
91+
images: (jp.description as any)?.images?.map((f: any) => ({
92+
value: f.value
93+
})) || [],
94+
95+
links: (jp.description as any)?.links?.map((f: any) => ({
96+
title: f.title || '',
97+
url: f.value
98+
})) || [],
99+
100+
state: transformState(jp.state),
101+
102+
ipAddress: jp.join.ip,
103+
104+
expiresAt:
105+
new Date(
106+
jp.createdAt.toDate().getTime() +
107+
jp.countdownPeriod * 1000
108+
),
109+
110+
votesFor: jp.votesFor,
111+
votesAgainst: jp.votesAgainst,
112+
113+
importedFrom: JSON.stringify(jp),
114+
115+
join: {
116+
create: {
117+
id: jp.id,
118+
119+
...(jp.join.cardId && {
120+
card: {
121+
connect: {
122+
id: jp.join.cardId
123+
}
124+
}
125+
}),
126+
127+
fundingType: transformFundingType(jp.join.contributionType),
128+
129+
funding: jp.join.funding
130+
}
131+
}
132+
}
133+
});
134+
135+
results.push(cjp);
136+
} catch (e) {
137+
errored.push({
138+
proposal: jp,
139+
error: {
140+
message: e.message,
141+
stack: e.stack
142+
}
143+
});
144+
}
145+
})());
146+
}
147+
148+
await Promise.all(promises);
149+
150+
fs.writeFileSync(path.join(__dirname, `../../result/${+date}/joinProposalsImport-errors.json`), JSON.stringify(errored));
151+
fs.writeFileSync(path.join(__dirname, `../../result/${+date}/joinProposalsImport-results.json`), JSON.stringify(results));
152+
};

0 commit comments

Comments
 (0)