RemoveStudentsJob.java
package edu.ucsb.cs156.frontiers.jobs;
import edu.ucsb.cs156.frontiers.entities.RosterStudent;
import edu.ucsb.cs156.frontiers.enums.OrgStatus;
import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository;
import edu.ucsb.cs156.frontiers.services.OrganizationMemberService;
import edu.ucsb.cs156.jobs.services.JobContext;
import edu.ucsb.cs156.jobs.services.JobContextConsumer;
import java.util.List;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpStatusCodeException;
@Builder
@EqualsAndHashCode
public class RemoveStudentsJob implements JobContextConsumer {
private OrganizationMemberService organizationMemberService;
private List<RosterStudent> students;
private RosterStudentRepository rosterStudentRepository;
// Unscoped: students may span multiple courses in one run.
// getScopeType()/getScopeId() default to null (unscoped), same as the old getCourse() did.
@Override
public void accept(JobContext c) throws Exception {
for (RosterStudent student : students) {
// A student whose course has no linked org, or who has no GitHub login/id, never calls
// c.log() -- checkCancellation() gives this loop its own checkpoint independent of
// whether an iteration does anything at all.
c.checkCancellation();
if (student.getCourse().getOrgName() != null
&& student.getCourse().getInstallationId() != null) {
if (student.getGithubLogin() != null && student.getGithubId() != null) {
try {
organizationMemberService.removeOrganizationMember(student);
c.log("Removed student %s from Organization".formatted(student.getGithubLogin()));
} catch (HttpStatusCodeException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
c.log("Student %s not in Organization".formatted(student.getGithubLogin()));
} else {
throw new IllegalStateException(
"Failed to remove student "
+ student.getGithubLogin()
+ " from Organization: "
+ e.getMessage(),
e);
}
}
student.setGithubId(null);
student.setGithubLogin(null);
student.setOrgStatus(OrgStatus.REMOVED);
rosterStudentRepository.save(student);
}
}
}
}
}