-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Informatik-Projekt-Kurs/IPK-236-Appointme…
…nt-Mutations Ipk 236 appointment mutations
- Loading branch information
Showing
5 changed files
with
205 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
package com.MeetMate.appointment; | ||
|
||
import com.MeetMate.enums.AppointmentStatus; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Document(collection = "appointments") | ||
@Data | ||
@NoArgsConstructor | ||
public class Appointment { | ||
long id; | ||
LocalDateTime from; | ||
LocalDateTime to; | ||
long companyID; | ||
long clientID; | ||
long assigneeID; | ||
// Select Prompt → f.E. medical industry: Untersuchung, Operation | ||
String description; | ||
String location; | ||
AppointmentStatus status; | ||
private long id; | ||
private String from; | ||
private String to; | ||
private long companyId; | ||
private long clientId; | ||
private long assigneeId; | ||
// Select Prompt → f.E. medical industry: Untersuchung, Operation | ||
private String description; | ||
private String location; | ||
private AppointmentStatus status; | ||
|
||
public Appointment(long id, long companyID, long clientID) { | ||
public Appointment(long id, long companyId) { | ||
this.id = id; | ||
this.companyID = companyID; | ||
this.clientID = clientID; | ||
this.companyId = companyId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 94 additions & 12 deletions
106
src/main/java/com/MeetMate/appointment/AppointmentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,120 @@ | ||
package com.MeetMate.appointment; | ||
|
||
import com.MeetMate.appointment.sequence.AppointmentSequenceService; | ||
import com.MeetMate.company.Company; | ||
import com.MeetMate.company.CompanyRepository; | ||
import com.MeetMate.enums.AppointmentStatus; | ||
import com.MeetMate.security.JwtService; | ||
import com.MeetMate.user.User; | ||
import com.MeetMate.user.UserRepository; | ||
import jakarta.persistence.EntityNotFoundException; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Criteria; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
import org.springframework.data.mongodb.core.query.Update; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Map; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AppointmentService { | ||
|
||
private final AppointmentRepository appointmentRepository; | ||
private final AppointmentSequenceService appointmentSequenceService; | ||
private final MongoTemplate mongoTemplate; | ||
private final JwtService jwtService; | ||
private final UserRepository userRepository; | ||
private final CompanyRepository companyRepository; | ||
|
||
public Appointment getAppointment(long id) { | ||
return appointmentRepository.findAppointmentById(id) | ||
public Appointment getAppointment(String token, long appointmentId) { | ||
if (userNotInAppointment(token, appointmentId)) | ||
throw new IllegalArgumentException("User is not eligible to edit this appointment"); | ||
return appointmentRepository.findAppointmentById(appointmentId) | ||
.orElseThrow(() -> new EntityNotFoundException("Appointment not found")); | ||
} | ||
|
||
@Transactional | ||
public void createAppointment(long companyId, long clientId, Map<String, Object> appointmentData) throws IllegalAccessException { | ||
public void createAppointment(String from, String to, long companyId, long clientId, long assigneeId, String description, String location, AppointmentStatus status) { | ||
//Check if IDs are valid | ||
if (companyRepository.findCompanyById(companyId).isEmpty()) | ||
throw new EntityNotFoundException("Company not found"); | ||
if (clientId != 0 && userRepository.findUserById(clientId).isEmpty()) | ||
throw new EntityNotFoundException("Client not found"); | ||
if (assigneeId != 0 && userRepository.findUserById(assigneeId).isEmpty()) | ||
throw new EntityNotFoundException("Assignee not found"); | ||
|
||
long appointmentId = appointmentSequenceService.getCurrentValue(); | ||
|
||
Appointment appointment = new Appointment(appointmentId, companyId, clientId); | ||
Field[] fields = Appointment.class.getDeclaredFields(); | ||
for(Field field : fields) { | ||
field.setAccessible(true); | ||
field.set(appointment, appointmentData.get(field.getName())); | ||
} | ||
Appointment appointment = new Appointment(appointmentId, companyId); | ||
if (from != null && !from.isEmpty()) appointment.setFrom(from); | ||
if (to != null && !to.isEmpty()) appointment.setTo(to); | ||
if (clientId != 0) appointment.setClientId(clientId); | ||
if (assigneeId != 0) appointment.setAssigneeId(assigneeId); | ||
if (description != null && !description.isEmpty()) appointment.setDescription(description); | ||
if (location != null && !location.isEmpty()) appointment.setLocation(location); | ||
if (status != null) appointment.setStatus(status); | ||
|
||
appointmentRepository.save(appointment); | ||
appointmentSequenceService.incrementId(); | ||
} | ||
|
||
@Transactional | ||
public void editAppointment(String token, long appointmentId, String from, String to, long clientId, long assigneeId, String description, String location, AppointmentStatus status) throws IllegalAccessException { | ||
if (userNotInAppointment(token, appointmentId)) | ||
throw new IllegalArgumentException("User is not eligible to edit this appointment"); | ||
|
||
//Check if IDs are valid | ||
if (clientId != 0 && userRepository.findUserById(clientId).isEmpty()) | ||
throw new EntityNotFoundException("Client not found"); | ||
if (assigneeId != 0 && userRepository.findUserById(assigneeId).isEmpty()) | ||
throw new EntityNotFoundException("Assignee not found"); | ||
|
||
Query query = new Query(Criteria.where("appointmentId").is(appointmentId)); | ||
Update update = new Update(); | ||
|
||
if (from != null && !from.isEmpty()) update.set("from", from); | ||
if (to != null && !to.isEmpty()) update.set("to", to); | ||
if (clientId != 0) update.set("clientId", clientId); | ||
if (assigneeId != 0) update.set("assigneeId", assigneeId); | ||
if (description != null && !description.isEmpty()) update.set("description", description); | ||
if (location != null && !location.isEmpty()) update.set("location", location); | ||
if (status != null) update.set("status", status); | ||
|
||
mongoTemplate.updateFirst(query, update, Company.class); | ||
} | ||
|
||
@Transactional | ||
public void deleteAppointment(String token, long appointmentId) { | ||
if (userNotInAppointment(token, appointmentId)) | ||
throw new IllegalArgumentException("User is not eligible to edit this appointment"); | ||
Appointment appointment = appointmentRepository.findAppointmentById(appointmentId) | ||
.orElseThrow(() -> new EntityNotFoundException("Appointment not found")); | ||
|
||
appointmentRepository.delete(appointment); | ||
} | ||
|
||
private boolean userNotInAppointment(String token, long appointmentId) throws IllegalArgumentException { | ||
String userEmail = jwtService.extractUserEmail(token); | ||
User user = userRepository.findUserByEmail(userEmail) | ||
.orElseThrow(() -> new EntityNotFoundException("User not found!")); | ||
Appointment appointment = appointmentRepository.findAppointmentById(appointmentId) | ||
.orElseThrow(() -> new EntityNotFoundException("Appointment not found!")); | ||
|
||
switch (user.getRole()) { | ||
case COMPANY_OWNER, COMPANY_MEMBER -> { | ||
if (appointment.getCompanyId() == user.getAssociatedCompany()) | ||
return false; | ||
} | ||
case CLIENT -> { | ||
if (appointment.getClientId() == user.getId()) | ||
return false; | ||
} | ||
default -> { | ||
throw new IllegalStateException("Role of user not found!"); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters