66
77namespace Cfo . Cats . Application . Features . PathwayPlans . Queries ;
88
9- public static class GetPathwayPlanReviewHistoryHistory
9+ public static class GetPathwayPlanReviewHistory
1010{
1111 [ RequestAuthorize ( Policy = SecurityPolicies . AuthorizedUser ) ]
1212 public class Query : ParticipantDetailsQuery < PathwayPlanReviewHistoryDto [ ] > ;
@@ -16,59 +16,79 @@ public class Handler(IUnitOfWork unitOfWork) : IRequestHandler<Query, Result<Pat
1616 public async Task < Result < PathwayPlanReviewHistoryDto [ ] > > Handle ( Query request ,
1717 CancellationToken cancellationToken )
1818 {
19- var baseQuery = unitOfWork . DbContext . PathwayPlans
20- . Where ( pp => pp . ParticipantId == request . ParticipantId )
21- . SelectMany ( pp => pp . PathwayPlanReviews , ( pp , review ) => new { pp , review } )
22- . Join ( unitOfWork . DbContext . Locations , x => x . review . LocationId , l => l . Id ,
23- ( x , l ) => new { x . review , x . pp , Location = l } )
24- . Join ( unitOfWork . DbContext . Users , x => x . review . CreatedBy , u => u . Id , ( x , u ) =>
25- new PathwayPlanReviewHistoryDto
26- {
27- Id = x . review . Id ,
28- ParticipantId = x . review . ParticipantId ,
29- ReviewDate = x . review . ReviewDate ,
30- ReviewedBy = u . DisplayName ! ,
31- LocationId = x . Location . Id ,
32- LocationName = x . Location . Name ,
33- ReviewReason = x . review . ReviewReason ,
34- Created = x . review . Created ! . Value ,
35- Review = x . review . Review
36- } )
37- . AsNoTracking ( )
38- . OrderByDescending ( r => r . ReviewDate ) ;
39-
40- var queryResultList = await baseQuery . ToListAsync ( cancellationToken ) ;
19+ var baseQuery =
20+ from review in unitOfWork . DbContext . PathwayPlanReviews . AsNoTracking ( )
21+
22+ join participant in unitOfWork . DbContext . Participants
23+ on review . PathwayPlan . ParticipantId equals participant . Id
24+ join location in unitOfWork . DbContext . Locations
25+ on review . LocationId equals location . Id
26+ join user in unitOfWork . DbContext . Users
27+ on review . CreatedBy equals user . Id
28+
29+ where review . PathwayPlan . ParticipantId == request . ParticipantId
30+ orderby review . ReviewDate descending
31+ select new
32+ {
33+ review . Id ,
34+ review . ParticipantId ,
35+ review . ReviewDate ,
36+ review . ReviewReason ,
37+ review . Review ,
38+ review . Created ,
39+ review . CreatedBy ,
40+
41+ UserDisplayName = user . DisplayName ,
4142
43+ LocationId = location . Id ,
44+ LocationName = location . Name ,
45+
46+ EnrolmentStatus = participant . EnrolmentStatus
47+ } ;
48+
49+ var queryResultList = await baseQuery
50+ . ToListAsync ( cancellationToken ) ;
51+
52+ var aWeekAgo = DateTime . UtcNow . Date . AddDays ( - 7 ) ;
53+ var count = queryResultList . Count ;
54+
4255 var result = queryResultList
43- . Select ( ( item , index ) =>
56+ . Select ( ( x , index ) =>
4457 {
4558 int ? daysSinceLastReview = null ;
4659
47- if ( index < queryResultList . Count - 1 )
60+ if ( index < count - 1 )
4861 {
4962 var previousReviewDate = queryResultList [ index + 1 ] . ReviewDate . Date ;
50- var currentReviewDate = item . ReviewDate . Date ;
63+ var currentReviewDate = x . ReviewDate . Date ;
5164
5265 daysSinceLastReview =
5366 ( currentReviewDate - previousReviewDate ) . Days ;
5467 }
5568
69+ var isActive = x . EnrolmentStatus ? . ParticipantIsActive ( ) == true ;
70+
71+ var canEdit = x . Created >= aWeekAgo ;
72+
73+ var correctUser = x . CreatedBy == request . CurrentUser . UserId ;
74+
5675 return new PathwayPlanReviewHistoryDto
5776 {
58- Id = item . Id ,
59- ParticipantId = item . ParticipantId ,
60- ReviewDate = item . ReviewDate ,
61- ReviewedBy = item . ReviewedBy ,
62- LocationId = item . LocationId ,
63- LocationName = item . LocationName ,
64- ReviewReason = item . ReviewReason ,
65- Review = item . Review ,
66- Created = item . Created ,
67- DaysSinceLastReview = daysSinceLastReview
77+ Id = x . Id ,
78+ ParticipantId = x . ParticipantId ,
79+ ReviewDate = x . ReviewDate ,
80+ ReviewedBy = x . UserDisplayName ,
81+ LocationId = x . LocationId ,
82+ LocationName = x . LocationName ,
83+ ReviewReason = x . ReviewReason ,
84+ Review = x . Review ,
85+ Created = x . Created ! . Value ,
86+ DaysSinceLastReview = daysSinceLastReview ,
87+ IsEditable = isActive && canEdit && correctUser
6888 } ;
6989 } )
7090 . ToArray ( ) ;
71-
91+
7292 return Result < PathwayPlanReviewHistoryDto [ ] > . Success ( result ) ;
7393 }
7494 }
@@ -82,22 +102,19 @@ public Validator(IUnitOfWork unitOfWork)
82102 _unitOfWork = unitOfWork ;
83103
84104 RuleFor ( x => x . ParticipantId )
85- . NotNull ( ) ;
86-
87- RuleFor ( x => x . ParticipantId )
88- . MinimumLength ( 9 )
89- . MaximumLength ( 9 )
105+ . NotEmpty ( )
106+ . Length ( 9 )
90107 . Matches ( ValidationConstants . AlphaNumeric )
91108 . WithMessage ( string . Format ( ValidationConstants . AlphaNumericMessage , "Participant Id" ) ) ;
92-
109+
93110 RuleSet ( ValidationConstants . RuleSet . MediatR , ( ) =>
94111 {
95112 RuleFor ( c => c . ParticipantId )
96113 . MustAsync ( Exist )
97114 . WithMessage ( "Participant does not exist" ) ;
98115 } ) ;
99116 }
100-
117+
101118 private async Task < bool > Exist ( string identifier , CancellationToken cancellationToken )
102119 => await _unitOfWork . DbContext . Participants . AnyAsync ( e => e . Id == identifier , cancellationToken ) ;
103120 }
0 commit comments