Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple cause areas #622

Merged
merged 29 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d2b71ea
Updates schema to multiple cause areas and adds missing foreign keys
fellmirr Jul 21, 2023
e1e3419
Lots of restructuring
fellmirr Aug 11, 2023
8084313
Bugfixes and cleanup
fellmirr Aug 11, 2023
303b8f9
Return the standard percentage share
fellmirr Aug 11, 2023
ca448c4
Fixes faker data seed for database
fellmirr Aug 14, 2023
63bb226
Adds workaround for foreign key schema drift
fellmirr Aug 14, 2023
c9f6a4f
Add missing cause area
adamalfredsson Aug 21, 2023
616ac08
Remove json copy
adamalfredsson Aug 21, 2023
9fa430f
Fix test compilation errors
adamalfredsson Aug 21, 2023
46bb049
Fix failing tests
adamalfredsson Aug 21, 2023
fcb6a48
Merge pull request #620 from stiftelsen-effekt/fix-fake-data
fellmirr Aug 21, 2023
408333d
Removes console logs from distributions DAO
fellmirr Aug 22, 2023
22e7862
Merge branch 'master' into multiple-cause-areas
adamalfredsson Aug 31, 2023
ab0f1ce
Updates getAll function to new structure and adds simple tests
fellmirr Aug 31, 2023
024b1ba
Updates getAllByDonor to new schema and adds simple tests
fellmirr Aug 31, 2023
4b1bf09
Updates getByDonorId to new schema and adds simple test
fellmirr Aug 31, 2023
b090ef0
Merge pull request #671 from stiftelsen-effekt/#644-DAO-distributions…
fellmirr Sep 7, 2023
ddc097e
Adds tests and verification in the DB mapping
fellmirr Sep 7, 2023
73be81a
Merge pull request #670 from stiftelsen-effekt/#643-DAO-distributions…
fellmirr Sep 7, 2023
068a198
Merge pull request #663 from stiftelsen-effekt/#642-DAO-distributions…
fellmirr Sep 13, 2023
ec6195b
Upgrade typescript to enable satisfies operator
adamalfredsson Sep 28, 2023
f17493a
Add backwards compatible types
adamalfredsson Sep 28, 2023
bfe82b2
Merge pull request #684 from stiftelsen-effekt/backwards-compatible-r…
fellmirr Oct 13, 2023
05ab5fb
Merge branch 'master' into multiple-cause-areas
adamalfredsson Oct 13, 2023
fd53c9a
Satisfy expected results
adamalfredsson Oct 13, 2023
83be5ae
Keep donation registration input type
adamalfredsson Oct 13, 2023
08aee35
Merge branch 'master' into multiple-cause-areas
fellmirr Nov 8, 2023
66de511
Fixes migrations
fellmirr Nov 8, 2023
71314a3
Merge pull request #686 from stiftelsen-effekt/feature/backwards-comp…
fellmirr Nov 8, 2023
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"ssh2-sftp-client": "^9.1.0",
"swagger-jsdoc": "^6.1.0",
"swagger-ui-express": "^4.2.0",
"typescript": "^4.5.4",
"typescript": "4.9.5",
"ts-node": "^10.4.0",
"validator": "^13.7.0"
},
Expand Down
68 changes: 30 additions & 38 deletions prisma/fakedata/addFakeDataScript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { faker } from "@faker-js/faker";
import {
Combining_table,
Distribution,
Distributions,
Distribution_cause_areas,
Distribution_cause_area_organizations,
Cause_areas,
Donations,
Donors,
Organizations,
Expand All @@ -12,7 +14,6 @@ import {
import fs from "fs";
import path from "path";
import {
generateCombiningTable,
generateFakeDistribution,
generateFakeDonation,
generateFakeDonor,
Expand All @@ -23,22 +24,25 @@ import {
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 fakeDistributions: Distribution[] = readAndParseJSON("fakeDistributions.json");
const fakeCombiningTables: Combining_table[] = readAndParseJSON("fakeCombiningTables.json");
const fakePaymentIntents: Payment_intent[] = readAndParseJSON("fakePaymentIntents.json");
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("/fakeDistributions.json", fakeDistributions);
writeToJSON("/fakeCombiningTables.json", fakeCombiningTables);
writeToJSON("/fakePaymentIntents.json", fakePaymentIntents);
writeToJSON("/fakeDistributions.json", fakeDistributions);
writeToJSON("/fakeDistributionCauseAreas.json", fakeDistributionCauseAreas);
writeToJSON("/fakeDistributionCauseAreaOrganizations.json", fakeDistributionCauseAreaOrganizations);

function populateFakeDataArrays() {
const lastDonorID: number = getLastID(fakeDonors);
Expand Down Expand Up @@ -73,10 +77,22 @@ function createFakeDataToDonor(donor: Donors, taxUnitID: number) {
const fakeDonation = createFakeDonation(donor, incrementedID);
createFakePaymentIntent(incrementedID, fakeDonation);

const isStandardSplit: boolean = faker.datatype.boolean(0.4);
createFakeDistributions().forEach((distribution) =>
createFakeCombiningTable(donor.ID, distribution.ID, taxUnitID, fakeDonation, isStandardSplit),
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))
}
}

Expand All @@ -91,30 +107,6 @@ function createFakePaymentIntent(id: number, donation: Donations) {
fakePaymentIntents.push(fakePaymentIntent);
}

function createFakeDistributions() {
const lastDistributionID: number = getLastID(fakeDistributions);
const fakeDistribution = generateFakeDistribution(lastDistributionID + 1, fakeOrganizations);
fakeDistributions.push(...fakeDistribution);
return fakeDistribution;
}

function createFakeCombiningTable(
donorID: number,
distributionID: number,
taxUnitID: number,
donation: Donations,
isStandardSplit: boolean,
) {
const fakeCombiningTable = generateCombiningTable(
donorID,
distributionID,
taxUnitID,
donation,
isStandardSplit,
);
fakeCombiningTables.push(fakeCombiningTable);
}

function getLastID(dataArray: any[]): number {
return dataArray[dataArray.length - 1]?.ID ?? 0;
}
Expand Down
157 changes: 112 additions & 45 deletions prisma/fakedata/fakeDataGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import { faker } from "@faker-js/faker";
import {
Combining_table,
Distribution,
Distributions,
Distribution_cause_areas,
Distribution_cause_area_organizations,
Donations,
Donors,
Organizations,
Payment,
Payment_intent,
Prisma,
Tax_unit,
Cause_areas,
} from "@prisma/client";
import { KID } from "../../src/custom_modules/KID";

const getRandomArrayThatSumsTo100 = (elements: number): number[] => {
const arrayOfNumbers: number[] = [];
let sum: number = 0;

for (let i = 0; i < elements; i++) {
let randomNumber: number = faker.number.int({ min: Math.min(10, 100 - sum), max: 100 - sum });
if (sum + randomNumber > 100) {
randomNumber = 100 - sum;
}
arrayOfNumbers.push(randomNumber);
sum += randomNumber;
}

if (sum < 100) {
arrayOfNumbers[0] = arrayOfNumbers[0] + (100 - sum);
}

return arrayOfNumbers;
};

export function generateFakeDonor(donorID: number): Donors {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
Expand Down Expand Up @@ -67,57 +89,102 @@ function offsetDateByHours(date: Date, offsetHours: number): Date {
}

export function generateFakeDistribution(
initialDistributionID: number,
donorId: number,
taxUnitId: number,
donation: Donations,
causeAreas: Cause_areas[],
organizations: Organizations[],
): Distribution[] {
let distributionID: number = initialDistributionID;
const arrayOfDistributions: Distribution[] = [];

getRandomPercentageArrayThatSumsTo100().forEach((percentage) => {
arrayOfDistributions.push({
ID: distributionID,
OrgID: faker.helpers.arrayElement(organizations).ID,
percentage_share: new Prisma.Decimal(percentage),
lastDistributionCauseAreasId: number,
lastDistributionCauseAreaOrganizationsId: number,
): {
distribution: Distributions;
distributionCauseAreas: Distribution_cause_areas[];
distributionCauseAreaOrganizations: Distribution_cause_area_organizations[];
} {
const distribution: Distributions = {
Donor_ID: donorId,
Tax_unit_ID: taxUnitId,
KID: donation.KID_fordeling,
inserted: donation.timestamp_confirmed,
last_updated: donation.timestamp_confirmed,
Meta_owner_ID: 3,
Replaced_old_organizations: false,
};

const distributionCauseAreaOrganizations: Distribution_cause_area_organizations[] = [];
const distributionCauseAreas: Distribution_cause_areas[] = [];

const numberOfCauseAreas = faker.number.int({ min: 1, max: Math.min(causeAreas.length, 10) });
const causeAreaIdsSet = new Set(causeAreas.map((causeArea) => causeArea.ID));
const causeAreaPercentages = getRandomArrayThatSumsTo100(numberOfCauseAreas);

for (let i = 0; i < numberOfCauseAreas; i++) {
// Pick random cause area and remove from set
const causeAreaId: number = faker.helpers.arrayElement([...causeAreaIdsSet]);
causeAreaIdsSet.delete(causeAreaId);

const isStandard = faker.datatype.boolean();

distributionCauseAreas.push({
ID: lastDistributionCauseAreasId + i + 1,
Distribution_KID: distribution.KID,
Cause_area_ID: causeAreaId,
Standard_split: isStandard,
Percentage_share: new Prisma.Decimal(causeAreaPercentages[i]),
});
distributionID += 1;
});

return arrayOfDistributions;
}
const filteredOrganizations = organizations.filter(
(organization) => organization.cause_area_ID === causeAreaId,
);

if (!isStandard) {
const numberOfOrganizations = faker.number.int({
min: 1,
max: Math.min(filteredOrganizations.length, 10),
});

function getRandomPercentageArrayThatSumsTo100(): number[] {
let percentageSum = 0;
let percentageOptionsArray: number[] = [100.0, 100.0, 100.0, 90.0, 80.0, 70.0, 30.0, 20.0, 10.0];
const finalPercentageArray: number[] = [];
const removeInvalidPercentages = (percentage: number) => 100 - percentageSum >= percentage;
const organizationIdsSet = new Set(
filteredOrganizations.map((organization) => organization.ID),
);
const organizationPercentages = getRandomArrayThatSumsTo100(numberOfOrganizations);

while (percentageSum < 100) {
const percentageShare: number = faker.helpers.arrayElement(percentageOptionsArray);
for (let j = 0; j < numberOfOrganizations; j++) {
// Pick random organization and remove from set
const organizationId: number = faker.helpers.arrayElement([...organizationIdsSet]);
organizationIdsSet.delete(organizationId);

finalPercentageArray.push(percentageShare);
percentageSum += percentageShare;
percentageOptionsArray = percentageOptionsArray.filter(removeInvalidPercentages);
distributionCauseAreaOrganizations.push({
ID:
lastDistributionCauseAreaOrganizationsId +
distributionCauseAreaOrganizations.length +
1,
Distribution_cause_area_ID: distributionCauseAreas[i].ID,
Organization_ID: organizationId,
Percentage_share: new Prisma.Decimal(organizationPercentages[j]),
});
}
} else {
for (let j = 0; j < filteredOrganizations.length; j++) {
const organization = filteredOrganizations[j];
if (organization.std_percentage_share === null || organization.std_percentage_share == 0) {
continue;
}
distributionCauseAreaOrganizations.push({
ID:
lastDistributionCauseAreaOrganizationsId +
distributionCauseAreaOrganizations.length +
1,
Distribution_cause_area_ID: distributionCauseAreas[i].ID,
Organization_ID: organization.ID,
Percentage_share: new Prisma.Decimal(organization.std_percentage_share),
});
}
}
}
return finalPercentageArray;
}

export function generateCombiningTable(
donorID: number,
distributionID: number,
taxUnitID: number,
donation: Donations,
isStandardSplit: boolean,
): Combining_table {
return {
Donor_ID: donorID,
Distribution_ID: distributionID,
Tax_unit_ID: taxUnitID,
KID: donation.KID_fordeling,
timestamp_created: donation.timestamp_confirmed,
Meta_owner_ID: 3,
Replaced_old_organizations: null,
Standard_split: isStandardSplit,
};
console.log(distributionCauseAreaOrganizations.map((d) => d.ID));

return { distribution, distributionCauseAreas, distributionCauseAreaOrganizations };
}

export function generateFakeTaxUnit(id: number, donor: Donors): Tax_unit {
Expand Down
20 changes: 20 additions & 0 deletions prisma/fakedata/json/fakeCauseAreas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"ID": 2,
"name": "Animal welfare",
"short_desc": "Animal welfare",
"long_desc": "Animal welfare",
"info_url": "https://www.charitynavigator.org/index.cfm?bay=content.view&cpid=49",
"is_active": 1,
"ordering": 2
},
{
"ID": 3,
"name": "Future generations",
"short_desc": "Future generations",
"long_desc": "Future generations",
"info_url": "https://www.charitynavigator.org/index.cfm?bay=content.view&cpid=50",
"is_active": 1,
"ordering": 3
}
]
Loading
Loading