|
1 | | -import { Material_Type, User, Assembly } from '@prisma/client'; |
| 1 | +import { Material_Type, User, Assembly, Material_Status, Material } from '@prisma/client'; |
2 | 2 | import { isAdmin, isGuest, isLeadership, isProject, LinkCreateArgs, LinkType, Project, WbsNumber, wbsPipe } from 'shared'; |
3 | 3 | import projectQueryArgs from '../prisma-query-args/projects.query-args'; |
4 | 4 | import prisma from '../prisma/prisma'; |
@@ -605,6 +605,103 @@ export default class ProjectsService { |
605 | 605 | ).map(linkTypeTransformer); |
606 | 606 | } |
607 | 607 |
|
| 608 | + /** |
| 609 | + * Creates a new Material |
| 610 | + * @param creator the user creating the material |
| 611 | + * @param name the name of the material |
| 612 | + * @param status the Material Status of the material |
| 613 | + * @param materialTypeName the name of the Material Type |
| 614 | + * @param manufacturerName the name of the material's manufacturer |
| 615 | + * @param manufacturerPartNumber the manufacturer part number for the material |
| 616 | + * @param quantity the quantity of material as a number |
| 617 | + * @param unitName the name of the Quantity Unit the quantity is measured in |
| 618 | + * @param price the price of the material in whole cents |
| 619 | + * @param subtotal the subtotal of the price for the material in whole cents |
| 620 | + * @param linkUrl the url for the material's link as a string |
| 621 | + * @param notes any notes about the material as a string |
| 622 | + * @param wbsNumber the WBS number of the project associated with this material |
| 623 | + * @param assemblyId the id of the Assembly for the material |
| 624 | + * @param pdmFileName the name of the pdm file for the material |
| 625 | + * @returns the created material |
| 626 | + */ |
| 627 | + static async createMaterial( |
| 628 | + creator: User, |
| 629 | + name: string, |
| 630 | + status: Material_Status, |
| 631 | + materialTypeName: string, |
| 632 | + manufacturerName: string, |
| 633 | + manufacturerPartNumber: string, |
| 634 | + quantity: number, |
| 635 | + unitName: string, |
| 636 | + price: number, |
| 637 | + subtotal: number, |
| 638 | + linkUrl: string, |
| 639 | + notes: string, |
| 640 | + wbsNumber: WbsNumber, |
| 641 | + assemblyId?: string, |
| 642 | + pdmFileName?: string |
| 643 | + ): Promise<Material> { |
| 644 | + const project = await prisma.project.findFirst({ |
| 645 | + where: { |
| 646 | + wbsElement: { |
| 647 | + carNumber: wbsNumber.carNumber, |
| 648 | + projectNumber: wbsNumber.projectNumber, |
| 649 | + workPackageNumber: wbsNumber.workPackageNumber |
| 650 | + } |
| 651 | + }, |
| 652 | + ...projectQueryArgs |
| 653 | + }); |
| 654 | + |
| 655 | + if (!project) throw new NotFoundException('Project', wbsPipe(wbsNumber)); |
| 656 | + |
| 657 | + if (assemblyId) { |
| 658 | + const assembly = await prisma.assembly.findFirst({ where: { assemblyId } }); |
| 659 | + if (!assembly) throw new NotFoundException('Assembly', assemblyId); |
| 660 | + } |
| 661 | + |
| 662 | + const materialType = await prisma.material_Type.findFirst({ |
| 663 | + where: { name: materialTypeName } |
| 664 | + }); |
| 665 | + if (!materialType) throw new NotFoundException('Material Type', materialTypeName); |
| 666 | + |
| 667 | + const manufacturer = await prisma.manufacturer.findFirst({ |
| 668 | + where: { name: manufacturerName } |
| 669 | + }); |
| 670 | + if (!manufacturer) throw new NotFoundException('Manufacturer', manufacturerName); |
| 671 | + |
| 672 | + const unit = await prisma.unit.findFirst({ |
| 673 | + where: { name: unitName } |
| 674 | + }); |
| 675 | + if (!unit) throw new NotFoundException('Unit', unitName); |
| 676 | + |
| 677 | + const perms = isLeadership(creator.role) || isUserPartOfTeams(project.teams, creator); |
| 678 | + |
| 679 | + if (!perms) throw new AccessDeniedException('create materials'); |
| 680 | + |
| 681 | + const createdMaterial = await prisma.material.create({ |
| 682 | + data: { |
| 683 | + userCreatedId: creator.userId, |
| 684 | + name, |
| 685 | + assemblyId, |
| 686 | + status, |
| 687 | + materialTypeName, |
| 688 | + manufacturerName, |
| 689 | + manufacturerPartNumber, |
| 690 | + pdmFileName, |
| 691 | + quantity, |
| 692 | + unitName, |
| 693 | + price, |
| 694 | + subtotal, |
| 695 | + linkUrl, |
| 696 | + notes, |
| 697 | + dateCreated: new Date(), |
| 698 | + wbsElementId: project.wbsElementId |
| 699 | + } |
| 700 | + }); |
| 701 | + |
| 702 | + return createdMaterial; |
| 703 | + } |
| 704 | + |
608 | 705 | /** |
609 | 706 | * Create an assembly |
610 | 707 | * @param name The name of the assembly to be created |
|
0 commit comments