-
Notifications
You must be signed in to change notification settings - Fork 6
/
addFakeDataScript.ts
127 lines (111 loc) · 4.78 KB
/
addFakeDataScript.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { faker } from "@faker-js/faker";
import {
Distributions,
Distribution_cause_areas,
Distribution_cause_area_organizations,
Cause_areas,
Donations,
Donors,
Organizations,
Payment,
Payment_intent,
Tax_unit,
} from "@prisma/client";
import fs from "fs";
import path from "path";
import {
generateFakeDistribution,
generateFakeDonation,
generateFakeDonor,
generateFakePaymentIntent,
generateFakeTaxUnit,
} from "./fakeDataGenerator";
const AMOUNT_OF_DONORS: number = 50;
const MAX_DONATIONS_PER_DONOR: number = 10;
const fakeDonors: Donors[] = []; // readAndParseJSON("fakeDonors.json");
const fakeDonations: Donations[] = []; //readAndParseJSON("fakeDonations.json");
const fakeTaxUnits: Tax_unit[] = []; // readAndParseJSON("fakeTaxUnits.json");
const fakePaymentIntents: Payment_intent[] = []; // readAndParseJSON("fakePaymentIntents.json");
const fakePayments: Payment[] = readAndParseJSON("fakePayments.json");
const fakeOrganizations: Organizations[] = readAndParseJSON("fakeOrganizations.json");
const fakeCauseAreas: Cause_areas[] = readAndParseJSON("fakeCauseAreas.json");
const fakeDistributions: Distributions[] = []; //readAndParseJSON("fakeDistributions.json");
const fakeDistributionCauseAreas: Distribution_cause_areas[] = []; // readAndParseJSON("fakeDistributionCauseAreas.json");
const fakeDistributionCauseAreaOrganizations: Distribution_cause_area_organizations[] = []; // readAndParseJSON("fakeDistributionCauseAreaOrganizations.json");
populateFakeDataArrays();
writeToJSON("/fakeDonors.json", fakeDonors);
writeToJSON("/fakeDonations.json", fakeDonations);
writeToJSON("/fakeTaxUnits.json", fakeTaxUnits);
writeToJSON("/fakePaymentIntents.json", fakePaymentIntents);
writeToJSON("/fakeDistributions.json", fakeDistributions);
writeToJSON("/fakeDistributionCauseAreas.json", fakeDistributionCauseAreas);
writeToJSON("/fakeDistributionCauseAreaOrganizations.json", fakeDistributionCauseAreaOrganizations);
function populateFakeDataArrays() {
const lastDonorID: number = getLastID(fakeDonors);
const lastTaxUnitID: number = getLastID(fakeTaxUnits);
for (let i = 1; i <= AMOUNT_OF_DONORS; i++) {
const fakeDonor = createFakeDonor(lastDonorID + i);
const fakeTaxUnit = createFakeTaxUnit(lastTaxUnitID + i, fakeDonor);
createFakeDataToDonor(fakeDonor, fakeTaxUnit.ID);
}
}
function createFakeDonor(id: number) {
const fakeDonor = generateFakeDonor(id);
fakeDonors.push(fakeDonor);
return fakeDonor;
}
function createFakeTaxUnit(id: number, fakeDonor: Donors) {
const fakeTaxUnit = generateFakeTaxUnit(id, fakeDonor);
fakeTaxUnits.push(fakeTaxUnit);
return fakeTaxUnit;
}
function createFakeDataToDonor(donor: Donors, taxUnitID: number) {
const lastDonationID: number = getLastID(fakeDonations);
const howManyDonations: number = faker.number.int({ min: 0, max: MAX_DONATIONS_PER_DONOR });
for (let i = 1; i <= howManyDonations; i++) {
let incrementedID: number = lastDonationID + i;
const fakeDonation = createFakeDonation(donor, incrementedID);
createFakePaymentIntent(incrementedID, fakeDonation);
const fakeDistribution = generateFakeDistribution(
fakeDonation.Donor_ID,
taxUnitID,
fakeDonation,
fakeCauseAreas,
fakeOrganizations,
getLastID(fakeDistributionCauseAreas),
getLastID(fakeDistributionCauseAreaOrganizations),
);
fakeDistributions.push(fakeDistribution.distribution);
fakeDistributionCauseAreas.push(...fakeDistribution.distributionCauseAreas);
//console.log(fakeDistribution.distributionCauseAreaOrganizations, getLastID(fakeDistributionCauseAreaOrganizations))
fakeDistributionCauseAreaOrganizations.push(
...fakeDistribution.distributionCauseAreaOrganizations,
);
//console.log(fakeDistributionCauseAreaOrganizations.slice(-fakeDistribution.distributionCauseAreaOrganizations.length), getLastID(fakeDistributionCauseAreaOrganizations))
}
}
function createFakeDonation(donor: Donors, donationID: number) {
const fakeDonation = generateFakeDonation(donor, donationID, fakePayments);
fakeDonations.push(fakeDonation);
return fakeDonation;
}
function createFakePaymentIntent(id: number, donation: Donations) {
const fakePaymentIntent = generateFakePaymentIntent(id, donation);
fakePaymentIntents.push(fakePaymentIntent);
}
function getLastID(dataArray: any[]): number {
return dataArray[dataArray.length - 1]?.ID ?? 0;
}
function writeToJSON(pathToJSON: string, data: Object) {
const basePath: string = path.resolve(__dirname, "json/");
fs.writeFile(basePath + pathToJSON, JSON.stringify(data), "utf8", (err) => {
if (err) {
console.error(`Error writing file: ${err}`);
return;
}
});
}
function readAndParseJSON(filePath: string) {
const jsonFile = fs.readFileSync("prisma/fakedata/json/" + filePath, "utf8");
return JSON.parse(jsonFile);
}