@@ -2,15 +2,13 @@ import { v4 as uuidv4 } from 'uuid';
22
33import { unitOfWork } from '../db/unitOfWork.ts' ;
44import { emailBringForwardNotification } from '../domains/noteHistory.ts' ;
5- import { SYSTEM_ID } from '../utils/constants/application.ts' ;
65import { BringForwardType } from '../utils/enums/projectCommon.ts' ;
76import { GroupName , Initiative , Resource } from '../utils/enums/application.ts' ;
87
98import type {
109 BringForward ,
1110 CurrentAuthorization ,
1211 CurrentContext ,
13- Enquiry ,
1412 NoteHistory ,
1513 NoteHistoryBase
1614} from '../types/index.ts' ;
@@ -55,112 +53,46 @@ export const deleteNoteHistoryService = async (noteHistoryId: string): Promise<v
5553
5654/**
5755 * Retrieve a list of bring forward type note histories by the given state
58- * @param initiative The initiative for which the note history belongs to
56+ * @param initiativeCode The initiative for which the note history belongs to
5957 * @param state The state to search for
6058 * @returns A Promise that resolves to the note histories for the given parameters
6159 */
6260export const listBringForwardsService = async (
63- initiative : Initiative ,
61+ initiativeCode : Exclude < Initiative , Initiative . PCNS > | undefined ,
6462 state : BringForwardType = BringForwardType . UNRESOLVED
6563) : Promise < BringForward [ ] > => {
66- return await unitOfWork . execute (
67- async ( { electrificationProject, enquiry, generalProject, housingProject, noteHistory, user } ) => {
68- const history = await noteHistory . findMany ( {
69- where : {
70- bringForwardState : state ,
71- activity : {
72- initiative : {
73- code : initiative
74- }
75- }
76- } ,
77- orderBy : {
78- createdAt : 'desc'
79- } ,
80- include : {
81- note : { orderBy : { createdAt : 'desc' } }
82- }
64+ return await unitOfWork . execute ( async ( { noteHistory, user } ) => {
65+ const history = await noteHistory . listBringForwards ( initiativeCode , state ) ;
66+
67+ if ( history . length ) {
68+ const users = await user . search ( {
69+ userId : history
70+ . map ( ( x ) => x . createdBy )
71+ . filter ( ( x ) => ! ! x )
72+ . map ( ( x ) => x ! )
8373 } ) ;
8474
85- if ( history . length ) {
86- const [ elecProj , generalProj , housingProj ] = await Promise . all ( [
87- electrificationProject . search ( {
88- activityId : history . map ( ( x ) => x . activityId )
89- } ) ,
90- generalProject . search ( {
91- activityId : history . map ( ( x ) => x . activityId )
92- } ) ,
93- housingProject . search ( {
94- activityId : history . map ( ( x ) => x . activityId )
95- } )
96- ] ) ;
97-
98- const users = await user . findMany ( {
99- where : {
100- AND : [
101- {
102- userId : {
103- in : history
104- . map ( ( x ) => x . createdBy )
105- . filter ( ( x ) => ! ! x )
106- . map ( ( x ) => x ! )
107- }
108- }
109- ] ,
110- NOT : [
111- {
112- userId : SYSTEM_ID
113- }
114- ]
115- }
116- } ) ;
75+ return history . map ( ( h ) => {
76+ const project = h . activity . housingProject ?? h . activity . generalProject ?? h . activity . electrificationProject ;
11777
118- const enquiries : Enquiry [ ] = (
119- await Promise . all ( [
120- enquiry . search (
121- {
122- activityId : history . map ( ( x ) => x . activityId )
123- } ,
124- Initiative . ELECTRIFICATION
125- ) ,
126- enquiry . search (
127- {
128- activityId : history . map ( ( x ) => x . activityId )
129- } ,
130- Initiative . GENERAL
131- ) ,
132- enquiry . search (
133- {
134- activityId : history . map ( ( x ) => x . activityId )
135- } ,
136- Initiative . HOUSING
137- )
138- ] )
139- ) . flat ( ) ;
140-
141- return history . map ( ( h ) => ( {
78+ return {
14279 activityId : h . activityId ,
143- noteId : h . noteHistoryId ,
144- electrificationProjectId : elecProj . find ( ( s ) => s . activityId === h . activityId ) ?. electrificationProjectId ,
145- generalProjectId : generalProj . find ( ( s ) => s . activityId === h . activityId ) ?. generalProjectId ,
146- housingProjectId : housingProj . find ( ( s ) => s . activityId === h . activityId ) ?. housingProjectId ,
147- enquiryId : enquiries . find ( ( s ) => s . activityId === h . activityId ) ?. enquiryId ,
80+ noteHistoryId : h . noteHistoryId ,
81+ projectId : project ?. projectId ,
82+ enquiryId : h . activity . enquiry ?. find ( ( e ) => e . activityId === h . activityId ) ?. enquiryId ,
83+ initiative : initiativeCode ,
14884 title : h . title ,
149- projectName :
150- elecProj . find ( ( s ) => s . activityId === h . activityId ) ?. projectName ??
151- generalProj . find ( ( s ) => s . activityId === h . activityId ) ?. projectName ??
152- housingProj . find ( ( s ) => s . activityId === h . activityId ) ?. projectName ??
153- null ,
85+ projectName : project ?. projectName ?? null ,
15486 createdByFullName : users . find ( ( u ) => u ?. userId === h . createdBy ) ?. fullName ?? null ,
15587 bringForwardDate : h . bringForwardDate ?. toISOString ( ) ,
15688 escalateToSupervisor : h . escalateToSupervisor ,
15789 escalateToDirector : h . escalateToDirector
158- } ) ) ;
159- } else {
160- return [ ] ;
161- }
90+ } satisfies BringForward ;
91+ } ) ;
92+ } else {
93+ return [ ] ;
16294 }
163- ) ;
95+ } ) ;
16496} ;
16597
16698/**
@@ -174,17 +106,7 @@ export const listNoteHistoriesService = async (
174106 activityId : string
175107) : Promise < NoteHistory [ ] > => {
176108 return await unitOfWork . execute ( async ( { noteHistory } ) => {
177- const result = await noteHistory . findMany ( {
178- where : {
179- activityId : activityId
180- } ,
181- orderBy : {
182- createdAt : 'desc'
183- } ,
184- include : {
185- note : { orderBy : { createdAt : 'desc' } }
186- }
187- } ) ;
109+ const result = await noteHistory . listNoteHistories ( activityId ) ;
188110
189111 if ( currentAuthorization ?. attributes . includes ( 'scope:self' ) ) {
190112 return result . filter ( ( x ) => x . shownToProponent ) ;
0 commit comments