Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"compile": "tsc -p tsconfig.json && yarn fix:paths",
"compile:watch": "tsc-watch --onSuccess \"yarn fix:paths\"",
"fix:paths": "tscpaths -p tsconfig.json -s ./src -o ./dist",
"seed": "ts-node -r dotenv/config prisma/seed.ts",
"studio": "prisma studio",
"seed": "prisma migrate reset --force && ts-node --transpile -r dotenv/config prisma/seed.ts",
"db:sync": "prisma migrate deploy",
"create:schema": "find prisma -name '*.prisma' -not -name \"schema.prisma\" -exec cat {} + > prisma/schema.prisma && prisma format",
"create:migration": "yarn create:schema && prisma migrate dev"
Expand Down
7 changes: 6 additions & 1 deletion packages/core/prisma/firestore/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ import { FirebaseToolkit } from '@common/core';

FirebaseToolkit.InitializeFirebase();

export const db = FirebaseToolkit.getDb();
export const db = FirebaseToolkit.getDb();

export const ProposalsCollection = db.collection('proposals');
export const PaymentsCollection = db.collection('payments');
export const CardsCollection = db.collection('cards');
export const UsersCollection = db.collection('users');
7 changes: 7 additions & 0 deletions packages/core/prisma/firestore/helpers/getEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { UsersCollection } from '../firestore';

export const getEmail = async (uid: string): Promise<string> => {
return (await UsersCollection
.doc(uid)
.get()).data()?.email;
};
60 changes: 60 additions & 0 deletions packages/core/prisma/firestore/importers/importCards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// @ts-ignore
import path from 'path';
// @ts-ignore
import fs from 'fs';

import { CardsCollection } from '../firestore';
import { seeder } from '../../seed';
import { CardNetwork } from '@prisma/client';

export const importCards = async (date: Date) => {
const firebaseCards = (await CardsCollection.get())
.docs.map(e => e.data());

const promises: Promise<void>[] = [];

const results: any[] = [];
const errored: any[] = [];

for (const fc of firebaseCards) {
promises.push((async () => {
try {
const card = await seeder.card
.create({
data: {
id: fc.id,

circleCardId: fc.circleCardId,

cvvCheck: fc.verification?.cvv || 'no verification',
avsCheck: 'no verification',

digits: fc.metadata?.digits || '',
network: fc.metadata?.network || CardNetwork.VISA,

user: {
connect: {
id: fc.ownerId
}
}
}
});

results.push(card);
} catch (e) {
errored.push({
proposal: fc,
error: {
message: e.message,
stack: e.stack
}
});
}
})());
}

await Promise.all(promises);

fs.writeFileSync(path.join(__dirname, `../../result/${+date}/cardImports-errors.json`), JSON.stringify(errored));
fs.writeFileSync(path.join(__dirname, `../../result/${+date}/cardImports-results.json`), JSON.stringify(results));
};
7 changes: 3 additions & 4 deletions packages/core/prisma/firestore/importers/importCommons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const importCommons = async () => {
name: common.name,
image: common.image,

balance: common.balance,
raised: common.raised,
balance: Math.round(common.balance),
raised: Math.round(common.raised),

byline: common.metadata.byline,
description: common.metadata.description,
Expand Down Expand Up @@ -81,6 +81,5 @@ export const importCommons = async () => {
}
}

console.log(failedCommons);
console.log(createdCommons);
console.log('[LogTag: 1333]', failedCommons);
};
146 changes: 88 additions & 58 deletions packages/core/prisma/firestore/importers/importFundingProposals.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { db } from '../firestore';
import { ProposalsCollection } from '../firestore';
import { seeder } from '../../seed';
import { IFundingRequestProposal, FundingRequestState } from '@common/types';
import { FundingState, ProposalState, ProposalType } from '@prisma/client';
import { getEmail } from '../helpers/getEmail';

const transformState = (state: FundingRequestState): ProposalState => {
export const transformState = (state: FundingRequestState): ProposalState => {
switch (state) {
case 'failed':
return ProposalState.Rejected;
Expand All @@ -16,81 +17,110 @@ const transformState = (state: FundingRequestState): ProposalState => {
};

export const importFundingProposals = async () => {
const ProposalsCollection = db.collection('proposals');

const firebaseFundingProposals = (await ProposalsCollection
.where('type', '==', 'fundingRequest').get())
.docs.map(e => e.data());

const promises: Promise<void>[] = [];

for (const fp of firebaseFundingProposals as IFundingRequestProposal[]) {
console.log('Importing proposal');

try {
const ifp = await seeder.proposal
.create({
data: {
common: {
connect: {
id: fp.commonId
}
},
promises.push((async () => {
console.log('Importing proposal');

try {
const memberExists = !!(await seeder.commonMember.count({
where: {
userId: fp.proposerId,
commonId: fp.commonId
}
}));

const user = await seeder.user.findUnique({
where: {
id: fp.proposerId
}
}) || await seeder.user.findUnique({
where: {
email: await getEmail(fp.proposerId)
}
});

commonMember: {
connect: {
userId_commonId: {
userId: fp.proposerId,
commonId: fp.commonId
const ifp = await seeder.proposal
.create({
data: {
id: fp.id,

common: {
connect: {
id: fp.commonId
}
}
},
},

...memberExists && {
commonMember: {
connect: {
userId_commonId: {
userId: fp.proposerId,
commonId: fp.commonId
}
}
}
},

user: {
connect: {
id: fp.proposerId
}
},
user: {
connect: {
id: user?.id || 'default'
}
},

type: ProposalType.FundingRequest,

title: (fp.description as any).title,
description: (fp.description as any).description,

type: ProposalType.FundingRequest,
files: (fp.description as any)?.files?.map((f: any) => ({
value: f.value
})) || [],

title: (fp.description as any).title,
description: (fp.description as any).description,
images: (fp.description as any)?.images?.map((f: any) => ({
value: f.value
})) || [],

files: (fp.description as any)?.files.map((f: any) => ({
value: f.value
})),
links: (fp.description as any)?.links?.map((f: any) => ({
title: f.title || '',
url: f.value
})) || [],

images: (fp.description as any)?.images.map((f: any) => ({
value: f.value
})),
state: transformState(fp.state),

links: (fp.description as any)?.links.map((f: any) => ({
title: f.title || '',
url: f.value
})),
expiresAt:
new Date(
fp.createdAt.toDate().getTime() +
fp.countdownPeriod * 1000
),

state: transformState(fp.state),
votesFor: fp.votesFor,
votesAgainst: fp.votesAgainst,

expiresAt:
new Date(
fp.createdAt.toDate().getTime() +
fp.countdownPeriod * 1000
),
importedFrom: JSON.stringify(fp),

votesFor: fp.votesFor,
votesAgainst: fp.votesAgainst,
funding: {
create: {
id: fp.id,

funding: {
create: {
fundingState: FundingState.NotEligible,
amount: fp.fundingRequest.amount
fundingState: FundingState.NotEligible,
amount: Math.round(fp.fundingRequest.amount)
}
}
}
}
});
});

console.log('Imported proposal');
} catch (e) {
console.log('Failed importing funding request', e);
}
console.log('Imported proposal');
} catch (e) {
console.log('Failed importing funding request', fp, e);
}
})());
}

await Promise.all(promises);
};
Loading