|
|
@@ -2,64 +2,82 @@ package com.drinkool.services;
|
|
|
|
|
|
|
|
|
|
|
|
import com.drinkool.InternalValue;
|
|
|
|
import com.drinkool.InternalValue;
|
|
|
|
import com.drinkool.Role;
|
|
|
|
import com.drinkool.Role;
|
|
|
|
import com.drinkool.Url;
|
|
|
|
import com.drinkool.dtos.CreateShift;
|
|
|
|
|
|
|
|
import com.drinkool.dtos.RegisterShift;
|
|
|
|
import com.drinkool.entities.ShiftEntity;
|
|
|
|
import com.drinkool.entities.ShiftEntity;
|
|
|
|
import com.drinkool.entities.ShiftRegistrationEntity;
|
|
|
|
import com.drinkool.entities.ShiftRegistrationEntity;
|
|
|
|
import com.drinkool.lib.dtos.CreateShift;
|
|
|
|
|
|
|
|
import com.drinkool.lib.dtos.RegisterShift;
|
|
|
|
|
|
|
|
import io.vertx.core.http.HttpServerRequest;
|
|
|
|
import io.vertx.core.http.HttpServerRequest;
|
|
|
|
import jakarta.annotation.security.RolesAllowed;
|
|
|
|
import jakarta.annotation.security.RolesAllowed;
|
|
|
|
|
|
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
|
|
import jakarta.inject.Inject;
|
|
|
|
import jakarta.inject.Inject;
|
|
|
|
import jakarta.transaction.Transactional;
|
|
|
|
import jakarta.transaction.Transactional;
|
|
|
|
import jakarta.ws.rs.GET;
|
|
|
|
|
|
|
|
import jakarta.ws.rs.POST;
|
|
|
|
|
|
|
|
import jakarta.ws.rs.Path;
|
|
|
|
|
|
|
|
import jakarta.ws.rs.core.Response;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import org.eclipse.microprofile.graphql.*;
|
|
|
|
|
|
|
|
|
|
|
|
@Path(Url.Shift)
|
|
|
|
@GraphQLApi
|
|
|
|
|
|
|
|
@ApplicationScoped
|
|
|
|
public class ShiftService {
|
|
|
|
public class ShiftService {
|
|
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
@Inject
|
|
|
|
HttpServerRequest request;
|
|
|
|
HttpServerRequest request;
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
// --- QUERIES ---
|
|
|
|
@Path("/me")
|
|
|
|
|
|
|
|
|
|
|
|
@Query("allShifts")
|
|
|
|
|
|
|
|
@Description("Lấy danh sách tất cả các loại ca trực")
|
|
|
|
|
|
|
|
@RolesAllowed({ Role.Staff, Role.Manager })
|
|
|
|
|
|
|
|
public List<ShiftEntity> getAllShifts() {
|
|
|
|
|
|
|
|
return ShiftEntity.listAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Query("myRegistrations")
|
|
|
|
|
|
|
|
@Description("Lấy danh sách ca trực mà nhân viên hiện tại đã đăng ký")
|
|
|
|
|
|
|
|
@RolesAllowed(Role.Staff)
|
|
|
|
|
|
|
|
public List<ShiftRegistrationEntity> getMyShifts() {
|
|
|
|
|
|
|
|
String userId = request.getHeader(InternalValue.userId);
|
|
|
|
|
|
|
|
return ShiftRegistrationEntity.list("staffId", UUID.fromString(userId));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Query("allRegistrations")
|
|
|
|
|
|
|
|
@Description("Manager xem toàn bộ danh sách đăng ký của tất cả nhân viên")
|
|
|
|
|
|
|
|
@RolesAllowed(Role.Manager)
|
|
|
|
|
|
|
|
public List<ShiftRegistrationEntity> getAllRegistrations() {
|
|
|
|
|
|
|
|
return ShiftRegistrationEntity.listAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- MUTATIONS ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mutation("registerShift")
|
|
|
|
|
|
|
|
@Description("Nhân viên đăng ký vào một ca trực cụ thể")
|
|
|
|
@RolesAllowed(Role.Staff)
|
|
|
|
@RolesAllowed(Role.Staff)
|
|
|
|
@Transactional
|
|
|
|
@Transactional
|
|
|
|
public Response registerShift(RegisterShift registration) {
|
|
|
|
public ShiftRegistrationEntity registerShift(
|
|
|
|
|
|
|
|
@Name("registration") RegisterShift registration
|
|
|
|
|
|
|
|
) throws GraphQLException {
|
|
|
|
String userIdStr = request.getHeader(InternalValue.userId);
|
|
|
|
String userIdStr = request.getHeader(InternalValue.userId);
|
|
|
|
|
|
|
|
|
|
|
|
UUID staffId = UUID.fromString(userIdStr);
|
|
|
|
UUID staffId = UUID.fromString(userIdStr);
|
|
|
|
|
|
|
|
|
|
|
|
ShiftEntity shift = ShiftEntity.findById(registration.shiftId);
|
|
|
|
ShiftEntity shift = ShiftEntity.findById(registration.shiftId);
|
|
|
|
if (shift == null) {
|
|
|
|
if (shift == null) throw new GraphQLException("NotFoundShift");
|
|
|
|
return Response.status(Response.Status.NOT_FOUND)
|
|
|
|
|
|
|
|
.entity("NotFoundShift")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<ShiftRegistrationEntity> overlappingShifts =
|
|
|
|
List<ShiftRegistrationEntity> overlappingShifts =
|
|
|
|
ShiftRegistrationEntity.findOverlappingShifts(staffId, shift);
|
|
|
|
ShiftRegistrationEntity.findOverlappingShifts(staffId, shift);
|
|
|
|
|
|
|
|
|
|
|
|
if (!overlappingShifts.isEmpty()) {
|
|
|
|
if (!overlappingShifts.isEmpty()) {
|
|
|
|
String overlappingNames = overlappingShifts
|
|
|
|
String ids = overlappingShifts
|
|
|
|
.stream()
|
|
|
|
.stream()
|
|
|
|
.map(reg -> reg.shift.id.toString())
|
|
|
|
.map(reg -> reg.shift.id.toString())
|
|
|
|
.distinct()
|
|
|
|
.distinct()
|
|
|
|
.collect(Collectors.joining(", "));
|
|
|
|
.collect(Collectors.joining(", "));
|
|
|
|
|
|
|
|
throw new GraphQLException("OverlappingShift: " + ids);
|
|
|
|
return Response.status(Response.Status.CONFLICT)
|
|
|
|
|
|
|
|
.entity(overlappingNames)
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long currentStaffCount = ShiftRegistrationEntity.countByShift(shift.id);
|
|
|
|
long currentStaffCount = ShiftRegistrationEntity.countByShift(shift.id);
|
|
|
|
if (shift.maxStaff != null && currentStaffCount >= shift.maxStaff) {
|
|
|
|
if (shift.maxStaff != null && currentStaffCount >= shift.maxStaff) {
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST)
|
|
|
|
throw new GraphQLException("FulledShift");
|
|
|
|
.entity("FulledShift")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ShiftRegistrationEntity record = new ShiftRegistrationEntity();
|
|
|
|
ShiftRegistrationEntity record = new ShiftRegistrationEntity();
|
|
|
@@ -67,51 +85,81 @@ public class ShiftService {
|
|
|
|
record.shift = shift;
|
|
|
|
record.shift = shift;
|
|
|
|
record.persist();
|
|
|
|
record.persist();
|
|
|
|
|
|
|
|
|
|
|
|
return Response.ok(record).build();
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Mutation("createShift")
|
|
|
|
@Path("/me")
|
|
|
|
@Description("Quản lý tạo mới một loại ca trực")
|
|
|
|
@RolesAllowed(Role.Staff)
|
|
|
|
@RolesAllowed(Role.Manager)
|
|
|
|
public List<ShiftRegistrationEntity> getMyShifts() {
|
|
|
|
|
|
|
|
String userId = request.getHeader(InternalValue.userId);
|
|
|
|
|
|
|
|
return ShiftRegistrationEntity.list("staffId", UUID.fromString(userId));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
|
|
|
@RolesAllowed({ Role.Staff, Role.Manager })
|
|
|
|
|
|
|
|
public List<ShiftEntity> getShift() {
|
|
|
|
|
|
|
|
return ShiftEntity.listAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
@Transactional
|
|
|
|
public Response createShift(CreateShift request) {
|
|
|
|
public ShiftEntity createShift(@Name("shiftInput") CreateShift input)
|
|
|
|
if (
|
|
|
|
throws GraphQLException {
|
|
|
|
request.name == null ||
|
|
|
|
|
|
|
|
request.name.isEmpty() ||
|
|
|
|
|
|
|
|
request.startTime == null ||
|
|
|
|
|
|
|
|
request.endTime == null
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST)
|
|
|
|
|
|
|
|
.entity("BadInput")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ShiftEntity shift = new ShiftEntity();
|
|
|
|
ShiftEntity shift = new ShiftEntity();
|
|
|
|
shift.name = request.name;
|
|
|
|
shift.name = input.name;
|
|
|
|
shift.startTime = request.startTime;
|
|
|
|
shift.startTime = input.startTime;
|
|
|
|
shift.endTime = request.endTime;
|
|
|
|
shift.endTime = input.endTime;
|
|
|
|
shift.maxStaff = request.maxStaff;
|
|
|
|
shift.maxStaff = input.maxStaff;
|
|
|
|
|
|
|
|
|
|
|
|
if (shift.endTime.isBefore(shift.startTime)) {
|
|
|
|
if (shift.endTime.isBefore(shift.startTime)) {
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST)
|
|
|
|
throw new GraphQLException("InvalidEndTime");
|
|
|
|
.entity("InvalidEndTime")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
shift.persist();
|
|
|
|
shift.persist();
|
|
|
|
|
|
|
|
return shift;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Response.status(Response.Status.CREATED).entity(shift).build();
|
|
|
|
@Mutation("updateShift")
|
|
|
|
|
|
|
|
@Description("Quản lý cập nhật thông tin ca trực")
|
|
|
|
|
|
|
|
@RolesAllowed(Role.Manager)
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
|
|
|
public ShiftEntity updateShift(
|
|
|
|
|
|
|
|
@Name("id") UUID id,
|
|
|
|
|
|
|
|
@Name("shiftInput") CreateShift input
|
|
|
|
|
|
|
|
) throws GraphQLException {
|
|
|
|
|
|
|
|
ShiftEntity shift = ShiftEntity.findById(id);
|
|
|
|
|
|
|
|
if (shift == null) throw new GraphQLException("NotFoundShift");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shift.name = input.name;
|
|
|
|
|
|
|
|
shift.startTime = input.startTime;
|
|
|
|
|
|
|
|
shift.endTime = input.endTime;
|
|
|
|
|
|
|
|
shift.maxStaff = input.maxStaff;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (shift.endTime.isBefore(shift.startTime)) {
|
|
|
|
|
|
|
|
throw new GraphQLException("InvalidEndTime");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return shift;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mutation("deleteShift")
|
|
|
|
|
|
|
|
@Description("Quản lý xóa một ca trực")
|
|
|
|
|
|
|
|
@RolesAllowed(Role.Manager)
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
|
|
|
public boolean deleteShift(@Name("id") Long id) throws GraphQLException {
|
|
|
|
|
|
|
|
long count = ShiftRegistrationEntity.count("shift.id", id);
|
|
|
|
|
|
|
|
if (count > 0) throw new GraphQLException(
|
|
|
|
|
|
|
|
"CannotDeleteShiftWithRegistrations"
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ShiftEntity.deleteById(id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Mutation("cancelRegistration")
|
|
|
|
|
|
|
|
@Description("Nhân viên hủy ca đã đăng ký")
|
|
|
|
|
|
|
|
@RolesAllowed(Role.Staff)
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
|
|
|
public boolean cancelRegistration(@Name("registrationId") UUID id)
|
|
|
|
|
|
|
|
throws GraphQLException {
|
|
|
|
|
|
|
|
String userId = request.getHeader(InternalValue.userId);
|
|
|
|
|
|
|
|
ShiftRegistrationEntity reg = ShiftRegistrationEntity.findById(id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (reg == null) throw new GraphQLException("RegistrationNotFound");
|
|
|
|
|
|
|
|
if (!reg.staffId.equals(UUID.fromString(userId))) {
|
|
|
|
|
|
|
|
throw new GraphQLException("NotYourRegistration");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reg.delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|