Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions doc/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,102 @@ paths:
$ref: '#/components/responses/429'
'500':
$ref: '#/components/responses/500'
/students/{studentId}/elevate:
put:
tags:
- Students
summary: Elevate a single student to ALUMNI
operationId: elevateStudentToAlumni
parameters:
- name: studentId
in: path
required: true
description: id of the student
schema:
type: string
responses:
'200':
description: Elevation result
content:
application/json:
schema:
type: object
properties:
id:
type: string
example: "STD25000"
elevated:
type: boolean
example: true
error:
type: string
nullable: true
example: null
'400':
description: Business logic error
content:
application/json:
schema:
type: object
properties:
id:
type: string
example: "STD25000"
elevated:
type: boolean
example: false
error:
type: string
example: "Unsufficient credits"
/students/elevate:
put:
tags:
- Students
summary: Elevate a group of students to ALUMNI
operationId: elevateStudentGroupToAlumni
requestBody:
required: true
content:
application/json:
schema:
type: array
items:
type: string
example: ["G1"]
responses:
'200':
description: Elevations results
content:
application/json:
schema:
type: array
properties:
id:
type: string
example: "STD25000"
elevated:
type: boolean
example: true
error:
type: string
nullable: true
example: null
'400':
description: Business logic error
content:
application/json:
schema:
type: array
properties:
id:
type: string
example: "STD25000"
elevated:
type: boolean
example: false
error:
type: string
example: "Unsufficient credits"
/monitors/{id}:
put:
tags:
Expand Down Expand Up @@ -6436,6 +6532,7 @@ components:
- ENABLED
- DISABLED
- SUSPENDED
- ALUMNI
FileType:
type: string
enum:
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/school/hei/haapi/endpoint/rest/controller/StudentController.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
import static school.hei.haapi.model.User.Role.STUDENT;

import java.lang.Exception;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -27,6 +31,7 @@
import school.hei.haapi.model.PageFromOne;
import school.hei.haapi.model.User;
import school.hei.haapi.service.GroupFlowService;
import school.hei.haapi.service.StudentElevationService;
import school.hei.haapi.service.UserService;

@RestController
Expand All @@ -37,6 +42,7 @@ public class StudentController {
private final GroupFlowService groupFlowService;
private final GroupFlowMapper groupFlowMapper;
private final StatusEnumMapper statusEnumMapper;
private final StudentElevationService studentElevationService;
private final SexEnumMapper sexEnumMapper;
private final CoordinatesValidator validator;

Expand Down Expand Up @@ -121,6 +127,22 @@ public Student updateStudent(
userService.updateUser(userMapper.toDomain(toUpdate), studentId));
}

@PutMapping("/students/{studentId}/elevate")
public ResponseEntity<Map<String, Object>> elevateStudent(@PathVariable String studentId) {
List<Map<String, Object>> results = studentElevationService.elevateStudentByUserIdOrGroupId(List.of(studentId));
return ResponseEntity.ok(results.getFirst());
}

@PutMapping("/students/elevate")
public ResponseEntity<List<Map<String, Object>>> elevateStudents(@RequestBody List <String> groupId) {
try {
List<Map<String, Object>> results = studentElevationService.elevateStudentByUserIdOrGroupId(groupId);
return ResponseEntity.ok(results);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(List.of(Map.of("error", "Unexpected error: " + e.getMessage())));
}
}

@PostMapping("/students/{id}/group_flows")
public List<GroupFlow> moveOrDeleteStudentInGroup(
@PathVariable(name = "id") String id, @RequestBody List<CreateGroupFlow> createGroupFlow) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/school/hei/haapi/endpoint/rest/mapper/StatusEnumMapper.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public EnableStatus toRestStatus(User.Status status) {
case ENABLED -> EnableStatus.ENABLED;
case DISABLED -> EnableStatus.DISABLED;
case SUSPENDED -> EnableStatus.SUSPENDED;
case ALUMNI -> EnableStatus.ALUMNI;
default -> throw new BadRequestException("Unexpected type " + status);
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/school/hei/haapi/model/User.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ public enum Sex {
public enum Status {
ENABLED,
DISABLED,
SUSPENDED;
SUSPENDED,
ALUMNI;
}

public enum Role {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package school.hei.haapi.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import school.hei.haapi.endpoint.rest.model.ResultSummary;
import school.hei.haapi.model.User;
import school.hei.haapi.model.exception.NotFoundException;
import school.hei.haapi.repository.UserRepository;
import static school.hei.haapi.model.User.Role.STUDENT;
import static school.hei.haapi.model.User.Status.ALUMNI;

@Service
@AllArgsConstructor
public class StudentElevationService {
private final GradeResultService gradeResultService;
private final UserRepository userRepository;
private final UserService userService;

@Transactional
public List<Map<String, Object>> elevateStudentByUserIdOrGroupId(List<String> ids) {
List<Map<String, Object>> results = new ArrayList<>();
Set<String> processedUserIds = new HashSet<>();
for(String id : ids) {
try {
User user = userService.findById(id);
if (user != null && !processedUserIds.contains(user.getId())) {
processedUserIds.add(user.getId());
results.add(elevateStudentToAlumni(user));
}
continue;
} catch (NotFoundException ignored) {

}
List<User> users = userRepository.findAllRemainingStudentsByGroupIds(List.of(id), Pageable.unpaged());
if (users == null || users.isEmpty()) {
Map<String, Object> notFound = new HashMap<>();
notFound.put("id", id);
notFound.put("elevated", false);
notFound.put("error", "GroupId not found or group contains no students");
results.add(notFound);
continue;
}
for (User user : users) {
if(user == null) continue;
if(processedUserIds.contains(user.getId())) continue;
processedUserIds.add(user.getId());
results.add(elevateStudentToAlumni(user));
}
}
return results;
}

private Map<String, Object> elevateStudentToAlumni(User user) {
Map<String, Object> result = new HashMap<>();
result.put("id", user.getId());
try{
if(user.getRole() != STUDENT) {
result.put("elevated", false);
result.put("error", "Not a student");
return result;
}
ResultSummary summary = gradeResultService.getStudentResultSummary(user.getId());
BigDecimal obtainedCredits = Optional.ofNullable(summary).map(ResultSummary::getObtainedCredits).orElse(BigDecimal.ZERO);
final short licenseCredit = 180;
if(obtainedCredits.compareTo(BigDecimal.valueOf(licenseCredit)) < 0) {
result.put("elevated", false);
result.put("error", "Insufficient credits");
return result;
}
user.setStatus(ALUMNI);
userRepository.save(user);
result.put("elevated", true);
result.put("error", null);
return result;
} catch (Exception e) {
result.put("elevated", false);
result.put("error", "Unexpected error: " + e.getMessage());
return result;
}
}
}
Loading
Loading