Skip to content

Commit 462e2bd

Browse files
committed
#1526: added material inputs check helper function and updated material schema
1 parent c1fe274 commit 462e2bd

4 files changed

Lines changed: 48 additions & 22 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- DropIndex
2+
DROP INDEX "Material_name_key";

src/backend/src/prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ model Material {
491491
materialId String @id @default(uuid())
492492
assembly Assembly? @relation(fields: [assemblyId], references: [assemblyId])
493493
assemblyId String?
494-
name String @unique
494+
name String
495495
wbsElement WBS_Element @relation(fields: [wbsElementId], references: [wbsElementId])
496496
wbsElementId Int
497497
dateDeleted DateTime?

src/backend/src/services/projects.services.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '../utils/errors.utils';
1515
import {
1616
addDescriptionBullets,
17+
checkMaterialInputs,
1718
editDescriptionBullets,
1819
getHighestProjectNumber,
1920
getUserFullName
@@ -656,25 +657,7 @@ export default class ProjectsService {
656657

657658
if (!project) throw new NotFoundException('Project', wbsPipe(wbsNumber));
658659

659-
if (assemblyId) {
660-
const assembly = await prisma.assembly.findFirst({ where: { assemblyId } });
661-
if (!assembly) throw new NotFoundException('Assembly', assemblyId);
662-
}
663-
664-
const materialType = await prisma.material_Type.findFirst({
665-
where: { name: materialTypeName }
666-
});
667-
if (!materialType) throw new NotFoundException('Material Type', materialTypeName);
668-
669-
const manufacturer = await prisma.manufacturer.findFirst({
670-
where: { name: manufacturerName }
671-
});
672-
if (!manufacturer) throw new NotFoundException('Manufacturer', manufacturerName);
673-
674-
const unit = await prisma.unit.findFirst({
675-
where: { name: unitName }
676-
});
677-
if (!unit) throw new NotFoundException('Unit', unitName);
660+
await checkMaterialInputs(manufacturerName, unitName, assemblyId, materialTypeName);
678661

679662
const perms = isLeadership(creator.role) || isUserPartOfTeams(project.teams, creator);
680663

@@ -884,6 +867,8 @@ export default class ProjectsService {
884867
if (!project) throw new NotFoundException('Project', material.wbsElementId);
885868
if (project.wbsElement.dateDeleted) throw new DeletedException('Project', project.projectId);
886869

870+
await checkMaterialInputs(manufacturerName, unitName, assemblyId);
871+
887872
const perms = isLeadership(submitter.role) || isUserPartOfTeams(project.teams, submitter);
888873

889874
if (!perms) throw new AccessDeniedException('update material');
@@ -902,8 +887,8 @@ export default class ProjectsService {
902887
linkUrl,
903888
notes,
904889
wbsElementId: project.wbsElementId,
905-
assemblyId: assemblyId || undefined,
906-
pdmFileName: pdmFileName || undefined
890+
assemblyId: assemblyId,
891+
pdmFileName: pdmFileName
907892
}
908893
});
909894

src/backend/src/utils/projects.utils.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,42 @@ export const getUserFullName = async (userId: number | null): Promise<string | n
6060
if (!user) throw new NotFoundException('User', userId);
6161
return `${user.firstName} ${user.lastName}`;
6262
};
63+
64+
/**
65+
* Check if given assembly, material type, manufacturer, and unit exist in the app database
66+
* @param manufacturerName the manufacure of the material to check if it exists
67+
* @param unitName the unit of the material to check if it exists
68+
* @param assemblyId the assembly of the material to check if it exists
69+
* @param materialTypeName the material type of the material to check if it exists
70+
* @throws if any of these properties of the material does not exist in the db
71+
*/
72+
export const checkMaterialInputs = async (
73+
manufacturerName: string,
74+
unitName?: string,
75+
assemblyId?: string,
76+
materialTypeName?: string
77+
) => {
78+
if (!!assemblyId) {
79+
const assembly = await prisma.assembly.findFirst({ where: { assemblyId } });
80+
if (!assembly) throw new NotFoundException('Assembly', assemblyId);
81+
}
82+
83+
if (!!materialTypeName) {
84+
const materialType = await prisma.material_Type.findFirst({
85+
where: { name: materialTypeName }
86+
});
87+
if (!materialType) throw new NotFoundException('Material Type', materialTypeName);
88+
}
89+
90+
const manufacturer = await prisma.manufacturer.findFirst({
91+
where: { name: manufacturerName }
92+
});
93+
if (!manufacturer) throw new NotFoundException('Manufacturer', manufacturerName);
94+
95+
if (!!unitName) {
96+
const unit = await prisma.unit.findFirst({
97+
where: { name: unitName }
98+
});
99+
if (!unit) throw new NotFoundException('Unit', unitName);
100+
}
101+
};

0 commit comments

Comments
 (0)