Skip to content

Commit 51b5ab4

Browse files
authored
Merge pull request #3878 from Northeastern-Electric-Racing/#3867-schema-changes
#3867 schema changes
2 parents 669a815 + dbe0dfa commit 51b5ab4

15 files changed

Lines changed: 254 additions & 136 deletions

File tree

src/backend/src/controllers/projects.controllers.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ export default class ProjectsController {
216216
name,
217217
status,
218218
materialTypeName,
219+
linkUrl,
220+
wbsNum,
221+
req.organization,
219222
manufacturerName,
220223
manufacturerPartNumber,
221224
quantity,
222225
price,
223226
subtotal,
224-
linkUrl,
225-
wbsNum,
226-
req.organization,
227227
notes,
228228
assemblyId,
229229
pdmFileName === '' ? undefined : pdmFileName,
@@ -379,13 +379,13 @@ export default class ProjectsController {
379379
name,
380380
status,
381381
materialTypeName,
382+
linkUrl,
383+
req.organization,
382384
manufacturerName,
383385
manufacturerPartNumber,
384386
quantity,
385387
price,
386388
subtotal,
387-
linkUrl,
388-
req.organization,
389389
notes,
390390
unitName,
391391
assemblyId,

src/backend/src/prisma-query-args/bom.query-args.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const getMaterialQueryArgs = (organizationId: string) =>
2525
materialType: true,
2626
unit: true,
2727
manufacturer: true,
28+
reimbursementProducts: false,
2829
reimbursementRequest: getReimbursementRequestQueryArgs(organizationId)
2930
}
3031
});
@@ -37,6 +38,7 @@ export const getMaterialPreviewQueryArgs = (organizationId: string) =>
3738
unit: true,
3839
manufacturer: true,
3940
materialType: true,
41+
reimbursementProducts: false,
4042
reimbursementRequest: getReimbursementRequestQueryArgs(organizationId)
4143
}
4244
});

src/backend/src/prisma-query-args/reimbursement-products.query-args.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const getReimbursementProductQueryArgs = (organizationId: string) =>
2525
Prisma.validator<Prisma.Reimbursement_ProductDefaultArgs>()({
2626
include: {
2727
refundSources: getRefundSourceQueryArgs(organizationId),
28-
reimbursementProductReason: getReimbursementProductReasonQueryArgs(organizationId)
28+
reimbursementProductReason: getReimbursementProductReasonQueryArgs(organizationId),
29+
material: true
2930
}
3031
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
-- DropForeignKey
2+
ALTER TABLE "Material" DROP CONSTRAINT "Material_manufacturerId_fkey";
3+
4+
-- AlterTable
5+
ALTER TABLE "Material" ALTER COLUMN "manufacturerId" DROP NOT NULL,
6+
ALTER COLUMN "manufacturerPartNumber" DROP NOT NULL,
7+
ALTER COLUMN "quantity" DROP NOT NULL,
8+
ALTER COLUMN "price" DROP NOT NULL,
9+
ALTER COLUMN "subtotal" DROP NOT NULL;
10+
11+
-- AlterTable
12+
ALTER TABLE "Reimbursement_Product" ADD COLUMN "materialId" TEXT;
13+
14+
-- CreateIndex
15+
CREATE INDEX "Reimbursement_Product_materialId_idx" ON "Reimbursement_Product"("materialId");
16+
17+
-- AddForeignKey
18+
ALTER TABLE "Reimbursement_Product" ADD CONSTRAINT "Reimbursement_Product_materialId_fkey" FOREIGN KEY ("materialId") REFERENCES "Material"("materialId") ON DELETE SET NULL ON UPDATE CASCADE;
19+
20+
-- AddForeignKey
21+
ALTER TABLE "Material" ADD CONSTRAINT "Material_manufacturerId_fkey" FOREIGN KEY ("manufacturerId") REFERENCES "Manufacturer"("id") ON DELETE SET NULL ON UPDATE CASCADE;
22+
23+
DO $$
24+
DECLARE
25+
material_record RECORD;
26+
new_reason_id TEXT;
27+
BEGIN
28+
-- Loop through all materials that are linked to RRs but don't have products yet
29+
FOR material_record IN
30+
SELECT
31+
m."materialId",
32+
m."name",
33+
m."subtotal",
34+
m."wbsElementId",
35+
m."reimbursementRequestId"
36+
FROM "Material" m
37+
WHERE m."reimbursementRequestId" IS NOT NULL
38+
AND m."dateDeleted" IS NULL
39+
AND EXISTS (
40+
-- Only migrate if the RR isn't deleted
41+
SELECT 1 FROM "Reimbursement_Request" rr
42+
WHERE rr."reimbursementRequestId" = m."reimbursementRequestId"
43+
AND rr."dateDeleted" IS NULL
44+
)
45+
AND EXISTS (
46+
-- Only migrate if the WBS element isn't deleted
47+
SELECT 1 FROM "WBS_Element" wbs
48+
WHERE wbs."wbsElementId" = m."wbsElementId"
49+
AND wbs."dateDeleted" IS NULL
50+
)
51+
AND NOT EXISTS (
52+
-- Skip if a product already links to this material for this RR
53+
SELECT 1 FROM "Reimbursement_Product" rp
54+
WHERE rp."materialId" = m."materialId"
55+
AND rp."reimbursementRequestId" = m."reimbursementRequestId"
56+
)
57+
LOOP
58+
-- Create a new reason for this material (can't reuse due to @unique and one to one relation)
59+
INSERT INTO "Reimbursement_Product_Reason" (
60+
"reimbursementProductReasonId",
61+
"wbsElementId"
62+
) VALUES (
63+
gen_random_uuid(),
64+
material_record."wbsElementId"
65+
)
66+
RETURNING "reimbursementProductReasonId" INTO new_reason_id;
67+
68+
-- Create the product linked to the material
69+
INSERT INTO "Reimbursement_Product" (
70+
"reimbursementProductId",
71+
"name",
72+
"cost",
73+
"materialId",
74+
"reimbursementProductReasonId",
75+
"reimbursementRequestId"
76+
) VALUES (
77+
gen_random_uuid(),
78+
material_record."name",
79+
COALESCE(material_record."subtotal", 0),
80+
material_record."materialId",
81+
new_reason_id,
82+
material_record."reimbursementRequestId"
83+
);
84+
END LOOP;
85+
END $$;

src/backend/src/prisma/schema.prisma

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ model Reimbursement_Product {
736736
name String
737737
dateDeleted DateTime?
738738
cost Int
739+
material Material? @relation(fields: [materialId], references: [materialId])
740+
materialId String?
739741
reimbursementProductReasonId String @unique
740742
reimbursementProductReason Reimbursement_Product_Reason @relation(fields: [reimbursementProductReasonId], references: [reimbursementProductReasonId])
741743
reimbursementRequestId String
@@ -744,6 +746,7 @@ model Reimbursement_Product {
744746
745747
@@index([reimbursementRequestId])
746748
@@index([reimbursementProductReasonId])
749+
@@index([materialId])
747750
}
748751

749752
model Refund_Source {
@@ -908,19 +911,20 @@ model Material {
908911
status Material_Status
909912
materialType Material_Type @relation(fields: [materialTypeId], references: [id])
910913
materialTypeId String
911-
manufacturer Manufacturer @relation(fields: [manufacturerId], references: [id])
912-
manufacturerId String
913-
manufacturerPartNumber String
914+
manufacturer Manufacturer? @relation(fields: [manufacturerId], references: [id])
915+
manufacturerId String?
916+
manufacturerPartNumber String?
914917
pdmFileName String?
915-
quantity Decimal
918+
quantity Decimal?
916919
unit Unit? @relation(fields: [unitId], references: [id])
917920
unitId String?
918-
price Int
919-
subtotal Int
921+
price Int?
922+
subtotal Int?
920923
linkUrl String
921924
notes String?
922925
reimbursementRequest Reimbursement_Request? @relation(fields: [reimbursementRequestId], references: [reimbursementRequestId])
923926
reimbursementRequestId String?
927+
reimbursementProducts Reimbursement_Product[]
924928
925929
@@index([assemblyId])
926930
@@index([materialTypeId])

src/backend/src/prisma/seed.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,58 +2289,66 @@ const performSeed: () => Promise<void> = async () => {
22892289
'10k Resistor',
22902290
MaterialStatus.Ordered,
22912291
'Resistor',
2292-
'Digikey',
2293-
'abcdef',
2294-
new Decimal(20),
2295-
30,
2296-
600,
22972292
'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
22982293
{
22992294
carNumber: 0,
23002295
projectNumber: 1,
23012296
workPackageNumber: 0
23022297
},
23032298
ner,
2304-
'Here are some notes'
2299+
'Digikey',
2300+
'abcdef',
2301+
new Decimal(20),
2302+
30,
2303+
600,
2304+
'Here are some notes',
2305+
assembly1.assemblyId,
2306+
undefined,
2307+
undefined,
2308+
undefined
23052309
);
23062310

23072311
await BillOfMaterialsService.createMaterial(
23082312
thomasEmrax,
23092313
'20k Resistor',
23102314
MaterialStatus.Ordered,
23112315
'Resistor',
2312-
'Digikey',
2313-
'bacfed',
2314-
new Decimal(10),
2315-
7,
2316-
70,
23172316
'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
23182317
{
23192318
carNumber: 0,
23202319
projectNumber: 1,
23212320
workPackageNumber: 0
23222321
},
23232322
ner,
2324-
'Here are some more notes'
2323+
'Digikey',
2324+
'bacfed',
2325+
new Decimal(10),
2326+
7,
2327+
70,
2328+
'Here are some more notes',
2329+
undefined,
2330+
undefined,
2331+
undefined,
2332+
undefined
23252333
);
23262334

23272335
await BillOfMaterialsService.createMaterial(
23282336
thomasEmrax,
23292337
'100k Resistor',
23302338
MaterialStatus.ReadyToOrder,
23312339
'Resistor',
2332-
'Digikey',
2333-
'lalsd',
2334-
new Decimal(5),
2335-
10,
2336-
50,
23372340
'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
23382341
{
23392342
carNumber: 0,
23402343
projectNumber: 1,
23412344
workPackageNumber: 0
23422345
},
23432346
ner,
2347+
'Digikey',
2348+
'lalsd',
2349+
new Decimal(5),
2350+
10,
2351+
50,
23442352
undefined,
23452353
undefined,
23462354
undefined,

0 commit comments

Comments
 (0)