Skip to content

Commit c00a513

Browse files
authored
Merge pull request #491 from navikt/TOLK-3471-Course
TOLK-3471 Forbedre koden vår Apex Guru Del 2 Kurs
2 parents d91b5cd + efa7d76 commit c00a513

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

force-app/main/default/classes/CourseRegistrationHelper.cls

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,13 @@ public class CourseRegistrationHelper {
7777
return;
7878
}
7979

80-
Map<Id, Course__c> coursesWithCapacity = new Map<Id, Course__c>();
81-
for (Course__c course : [
82-
SELECT Id, MaxNumberOfParticipants__c, RegistrationSignupsCount__c
83-
FROM Course__c
84-
WHERE Id IN :courseIds
85-
]) {
86-
coursesWithCapacity.put(course.Id, course);
87-
}
80+
Map<Id, Course__c> coursesWithCapacity = new Map<Id, Course__c>(
81+
[
82+
SELECT Id, MaxNumberOfParticipants__c, RegistrationSignupsCount__c
83+
FROM Course__c
84+
WHERE Id IN :courseIds
85+
]
86+
);
8887

8988
for (Id courseId : courseIds) {
9089
Course__c relatedCourse = coursesWithCapacity.get(courseId);
@@ -156,7 +155,26 @@ public class CourseRegistrationHelper {
156155
FROM Course__c
157156
WHERE Id IN :courseIdsToProcess
158157
];
158+
159159
Map<Id, Course__c> courseMap = new Map<Id, Course__c>(courses);
160+
Map<Id, List<CourseRegistration__c>> waitlistMap = new Map<Id, List<CourseRegistration__c>>();
161+
162+
// Query all waitlisted registrations for the courses being processed, ordered by CreatedDate to ensure FIFO promotion
163+
List<CourseRegistration__c> allWaitlistedRegistrations = [
164+
SELECT Id, Status__c, Course__c, CreatedDate, NumberOfParticipants__c
165+
FROM CourseRegistration__c
166+
WHERE Course__c IN :courseIdsToProcess AND Status__c = 'Venteliste'
167+
ORDER BY CreatedDate ASC
168+
];
169+
170+
// Group waitlisted registrations by course
171+
for (CourseRegistration__c reg : allWaitlistedRegistrations) {
172+
// Create a new list for the course if it doesn't exist
173+
if (!waitlistMap.containsKey(reg.Course__c)) {
174+
waitlistMap.put(reg.Course__c, new List<CourseRegistration__c>());
175+
}
176+
waitlistMap.get(reg.Course__c).add(reg);
177+
}
160178

161179
for (Id courseId : courseIdsToProcess) {
162180
Course__c currentCourse = courseMap.get(courseId);
@@ -183,12 +201,10 @@ public class CourseRegistrationHelper {
183201
if (availableSlots <= 0)
184202
continue;
185203

186-
List<CourseRegistration__c> waitlistedRegistrations = [
187-
SELECT Id, Status__c, Course__c, CreatedDate, NumberOfParticipants__c
188-
FROM CourseRegistration__c
189-
WHERE Course__c = :courseId AND Status__c = 'Venteliste'
190-
ORDER BY CreatedDate ASC
191-
];
204+
// Get waitlisted registrations for this course
205+
List<CourseRegistration__c> waitlistedRegistrations = waitlistMap.containsKey(courseId)
206+
? waitlistMap.get(courseId)
207+
: new List<CourseRegistration__c>();
192208
if (waitlistedRegistrations.isEmpty())
193209
continue;
194210

0 commit comments

Comments
 (0)