@@ -70,6 +70,10 @@ export const updateProjectAndCreateChanges = async (
7070 links : { where : { dateDeleted : null } , ...getLinkQueryArgs ( organizationId ) } ,
7171 descriptionBullets : { where : { dateDeleted : null } , ...getDescriptionBulletQueryArgs ( organizationId ) }
7272 }
73+ } ,
74+ workPackages : {
75+ where : { wbsElement : { dateDeleted : null } } ,
76+ include : { wbsElement : true }
7377 }
7478 }
7579 } ) ;
@@ -79,7 +83,7 @@ export const updateProjectAndCreateChanges = async (
7983 if ( originalProject . wbsElement . dateDeleted ) throw new DeletedException ( 'Project' , projectId ) ;
8084 if ( originalProject . wbsElement . organizationId !== organizationId ) throw new InvalidOrganizationException ( 'Project' ) ;
8185
82- const { wbsElementId } = originalProject ;
86+ const { wbsElementId, workPackages : originalWorkPackages } = originalProject ;
8387
8488 const nameChangeJson = createChange (
8589 'name' ,
@@ -158,6 +162,48 @@ export const updateProjectAndCreateChanges = async (
158162
159163 changesJson = changesJson . concat ( descriptionBulletChanges . changes ) . concat ( linkChanges . changes ) ;
160164
165+ // if the project has no managerId and a managerId is provided, we need to update the work packages
166+ // that do not have a managerId and create changes for them
167+ // this is so that project manager is the default manager for all work packages
168+ if ( ! originalProject . wbsElement . managerId && managerId ) {
169+ const wpToUpdate = originalWorkPackages . filter ( ( wp ) => ! wp . wbsElement . managerId ) ;
170+
171+ const wpChanges = (
172+ await Promise . all (
173+ wpToUpdate . map ( async ( wp ) =>
174+ createChange ( 'manager' , null , await getUserFullName ( managerId ) , crId , implementerId , wp . wbsElementId , null , null )
175+ )
176+ )
177+ ) . filter ( ( change ) => change !== undefined ) ;
178+
179+ changesJson = changesJson . concat ( wpChanges ) ;
180+
181+ await prisma . wBS_Element . updateMany ( {
182+ where : { wbsElementId : { in : wpToUpdate . map ( ( wp ) => wp . wbsElementId ) } } ,
183+ data : { managerId }
184+ } ) ;
185+ }
186+
187+ // same goes for the project lead
188+ if ( ! originalProject . wbsElement . leadId && leadId ) {
189+ const wpToUpdate = originalWorkPackages . filter ( ( wp ) => ! wp . wbsElement . leadId ) ;
190+
191+ const wpChanges = (
192+ await Promise . all (
193+ wpToUpdate . map ( async ( wp ) =>
194+ createChange ( 'lead' , null , await getUserFullName ( leadId ) , crId , implementerId , wp . wbsElementId , null , null )
195+ )
196+ )
197+ ) . filter ( ( change ) => change !== undefined ) ;
198+
199+ changesJson = changesJson . concat ( wpChanges ) ;
200+
201+ await prisma . wBS_Element . updateMany ( {
202+ where : { wbsElementId : { in : wpToUpdate . map ( ( wp ) => wp . wbsElementId ) } } ,
203+ data : { leadId }
204+ } ) ;
205+ }
206+
161207 // update the project with the input fields
162208 const updatedProject = await prisma . project . update ( {
163209 where : {
0 commit comments