Skip to content

Commit 69129e4

Browse files
authored
Merge pull request #4270 from Northeastern-Electric-Racing/feature/seed-data-revamp
Feature/seed data revamp merging first 3 commits
2 parents 6cbe307 + 22956a8 commit 69129e4

20 files changed

Lines changed: 6175 additions & 1 deletion

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"@types/react-dom": "17.0.1"
7272
},
7373
"dependencies": {
74+
"@faker-js/faker": "^10.4.0",
7475
"@microsoft/clarity": "^1.0.0",
7576
"@types/multer": "^1.4.12",
7677
"canvas-confetti": "^1.9.3",

src/backend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"test": "vitest",
88
"build": "NODE_OPTIONS='--max-old-space-size=8192' tsc --noEmit false",
99
"start": "node -r dotenv/config dist/backend/index.js",
10-
"prisma:manual": "tsx --import dotenv/config ./src/prisma/manual.ts"
10+
"prisma:manual": "tsx --import dotenv/config ./src/prisma/manual.ts",
11+
"prisma:dev-seed": "tsx --import dotenv/config ./src/prisma/dev-seed.ts"
1112
},
1213
"dependencies": {
1314
"@prisma/client": "^6.2.1",

src/backend/src/prisma/context.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {
2+
Account_Code,
3+
Description_Bullet_Type,
4+
Index_Code,
5+
Link_Type,
6+
Manufacturer,
7+
Material_Type,
8+
Organization,
9+
Prisma,
10+
Reimbursement_Product_Other_Reason,
11+
Role,
12+
Team_Type,
13+
Unit,
14+
Vendor
15+
} from '@prisma/client';
16+
import { RoleEnum } from 'shared';
17+
import { getUserQueryArgs } from '../prisma-query-args/user.query-args.js';
18+
19+
// Organization Context
20+
export type OrganizationContext = {
21+
organization: Organization;
22+
bootstrapUserId: string;
23+
};
24+
25+
// User Context
26+
export type FullUser = Prisma.UserGetPayload<ReturnType<typeof getUserQueryArgs>>;
27+
28+
export type UsersContext = {
29+
appAdmins: FullUser[];
30+
admins: FullUser[];
31+
heads: FullUser[];
32+
leadership: FullUser[];
33+
members: FullUser[];
34+
guests: FullUser[];
35+
all: FullUser[];
36+
};
37+
38+
// Role Context
39+
export type RoleContext = {
40+
roles: Role[];
41+
rolesByType: Record<RoleEnum, Role[]>;
42+
};
43+
44+
// Config Data Context
45+
export type ConfigDataContext = {
46+
teamTypes: Team_Type[];
47+
linkTypes: Link_Type[];
48+
descriptionBulletTypes: Description_Bullet_Type[];
49+
materialTypes: Material_Type[];
50+
manufacturers: Manufacturer[];
51+
units: Unit[];
52+
accountCodes: Account_Code[];
53+
indexCodes: Index_Code[];
54+
vendors: Vendor[];
55+
reimbursementProductOtherReasons: Reimbursement_Product_Other_Reason[];
56+
};
57+
58+
// Main Seed Context
59+
export type SeedContext = OrganizationContext & UsersContext & RoleContext & CarOutput & ConfigDataContext;
60+
61+
// Car Context
62+
export type DateRange = {
63+
start: Date;
64+
end: Date;
65+
};
66+
67+
export type CarContext = {
68+
car: Prisma.CarGetPayload<{ include: { wbsElement: true } }>;
69+
year: number;
70+
dateRange: DateRange;
71+
};
72+
73+
export type CarOutput = {
74+
cars: CarContext[];
75+
currentYearCar: CarContext;
76+
};

src/backend/src/prisma/dates.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Faker } from '@faker-js/faker';
2+
/*
3+
https://fakerjs.dev/api/date.html
4+
*/
5+
6+
interface WithFaker {
7+
faker: Faker;
8+
}
9+
10+
export function generateRandomDate({ faker }: WithFaker, from?: Date, to?: Date) {
11+
return faker.date.between({ from: from ?? '2000-01-01', to: to ?? Date.now() });
12+
}
13+
14+
export function generateRandomDateAround({ faker }: WithFaker, date: Date) {
15+
return faker.date.recent({ days: 5, refDate: date });
16+
}

src/backend/src/prisma/dev-seed.ts

Lines changed: 5211 additions & 0 deletions
Large diffs are not rendered by default.

src/backend/src/prisma/distributions.ts

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Prisma } from '@prisma/client';
2+
import { base, en, Faker } from '@faker-js/faker';
3+
import { DateRange } from '../context.js';
4+
5+
const CURRENT_YEAR = new Date().getFullYear();
6+
const CAR_COUNT = 5;
7+
8+
const FROM_MONTH = 3; // April
9+
const FROM_DAY = 1;
10+
const TO_MONTH = 8; // September
11+
const TO_DAY = 30;
12+
13+
export const CAR_CONFIGS = Array.from({ length: CAR_COUNT }, (_, i) => {
14+
const carYear = CURRENT_YEAR - (CAR_COUNT - 1) + i;
15+
const shortYear = String(carYear).slice(2);
16+
17+
const carFaker = new Faker({ locale: [en, base] });
18+
carFaker.seed(carYear);
19+
20+
const start = carFaker.date.between({
21+
from: new Date(carYear - 1, FROM_MONTH, FROM_DAY),
22+
to: new Date(carYear - 1, TO_MONTH, TO_DAY)
23+
});
24+
25+
const end = carFaker.date.between({
26+
from: new Date(carYear, FROM_MONTH, FROM_DAY),
27+
to: new Date(carYear, TO_MONTH, TO_DAY)
28+
});
29+
30+
return {
31+
name: `NER-${shortYear}`,
32+
carNumber: carYear - 2000,
33+
year: carYear,
34+
dateRange: { start, end } as DateRange
35+
};
36+
});
37+
38+
export const carCreateInput = (name: string, carNumber: number, organizationId: string): Prisma.CarCreateInput => ({
39+
wbsElement: {
40+
create: {
41+
name,
42+
carNumber,
43+
projectNumber: 0,
44+
workPackageNumber: 0,
45+
organizationId
46+
}
47+
}
48+
});

0 commit comments

Comments
 (0)