From ec87c78db87ceacc36c6baf023917aee72e4ccd8 Mon Sep 17 00:00:00 2001 From: Mohsen Karimi Date: Sun, 18 Aug 2024 09:45:32 +0200 Subject: [PATCH] refactor: add reason to day-off (#10) --- .../api/dayoff/controller/DayOffController.java | 3 ++- .../teamwize/api/dayoff/domain/entity/DayOff.java | 2 ++ .../domain/request/DayOffCreateRequest.java | 1 + .../dayoff/domain/response/DayOffResponse.java | 1 + .../api/dayoff/service/DayOffService.java | 10 +++++++--- .../user/domain/response/UserCompactResponse.java | 4 ++++ ...20240818-0933-add-reason-to-days-off-table.xml | 15 +++++++++++++++ .../db/changelog/db.changelog-master.yaml | 3 +++ 8 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/db/changelog/20240818-0933-add-reason-to-days-off-table.xml diff --git a/src/main/java/app/teamwize/api/dayoff/controller/DayOffController.java b/src/main/java/app/teamwize/api/dayoff/controller/DayOffController.java index 4473d44..0f5ae10 100644 --- a/src/main/java/app/teamwize/api/dayoff/controller/DayOffController.java +++ b/src/main/java/app/teamwize/api/dayoff/controller/DayOffController.java @@ -12,6 +12,7 @@ import app.teamwize.api.dayoff.exception.DayOffNotFoundException; import app.teamwize.api.dayoff.exception.DayOffUpdateStatusFailedException; import app.teamwize.api.dayoff.service.DayOffService; +import app.teamwize.api.user.exception.UserNotFoundException; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springdoc.core.annotations.ParameterObject; @@ -29,7 +30,7 @@ public class DayOffController { @PostMapping - public DayOffResponse create(@RequestBody DayOffCreateRequest request) { + public DayOffResponse create(@RequestBody DayOffCreateRequest request) throws UserNotFoundException { var dayOff = dayOffService.createDayOff(securityService.getUserOrganizationId(), securityService.getUserId(), request); return dayOffMapper.toDayOffResponse(dayOff); } diff --git a/src/main/java/app/teamwize/api/dayoff/domain/entity/DayOff.java b/src/main/java/app/teamwize/api/dayoff/domain/entity/DayOff.java index ec44266..2df3f0c 100644 --- a/src/main/java/app/teamwize/api/dayoff/domain/entity/DayOff.java +++ b/src/main/java/app/teamwize/api/dayoff/domain/entity/DayOff.java @@ -35,6 +35,8 @@ public class DayOff extends BaseAuditEntity { @ManyToOne(fetch = FetchType.LAZY) private User user; + private String reason; + @ManyToOne(fetch = FetchType.LAZY) private Organization organization; diff --git a/src/main/java/app/teamwize/api/dayoff/domain/request/DayOffCreateRequest.java b/src/main/java/app/teamwize/api/dayoff/domain/request/DayOffCreateRequest.java index ec17317..6693029 100644 --- a/src/main/java/app/teamwize/api/dayoff/domain/request/DayOffCreateRequest.java +++ b/src/main/java/app/teamwize/api/dayoff/domain/request/DayOffCreateRequest.java @@ -6,6 +6,7 @@ public record DayOffCreateRequest( DayOffType type, + String reason, LocalDateTime start, LocalDateTime end ) { diff --git a/src/main/java/app/teamwize/api/dayoff/domain/response/DayOffResponse.java b/src/main/java/app/teamwize/api/dayoff/domain/response/DayOffResponse.java index bacd630..f02ccbd 100644 --- a/src/main/java/app/teamwize/api/dayoff/domain/response/DayOffResponse.java +++ b/src/main/java/app/teamwize/api/dayoff/domain/response/DayOffResponse.java @@ -14,5 +14,6 @@ public record DayOffResponse( LocalDateTime endAt, DayOffStatus status, DayOffType type, + String reason, UserCompactResponse user) { } diff --git a/src/main/java/app/teamwize/api/dayoff/service/DayOffService.java b/src/main/java/app/teamwize/api/dayoff/service/DayOffService.java index a27e85f..4838de0 100644 --- a/src/main/java/app/teamwize/api/dayoff/service/DayOffService.java +++ b/src/main/java/app/teamwize/api/dayoff/service/DayOffService.java @@ -10,7 +10,8 @@ import app.teamwize.api.dayoff.exception.DayOffUpdateStatusFailedException; import app.teamwize.api.dayoff.repository.DayOffRepository; import app.teamwize.api.organization.domain.entity.Organization; -import app.teamwize.api.user.domain.entity.User; +import app.teamwize.api.user.exception.UserNotFoundException; +import app.teamwize.api.user.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -25,13 +26,16 @@ public class DayOffService { private final DayOffRepository dayOffRepository; + private final UserService userService; @Transactional - public DayOff createDayOff(Long organizationId, Long userId, DayOffCreateRequest request) { + public DayOff createDayOff(Long organizationId, Long userId, DayOffCreateRequest request) throws UserNotFoundException { + var user = userService.getUser(organizationId,userId); var dayOff = new DayOff() + .setReason(request.reason()) .setStartAt(request.start()) .setEndAt(request.end()) - .setUser(new User(userId)) + .setUser(user) .setOrganization(new Organization(organizationId)) .setStatus(DayOffStatus.PENDING) .setType(request.type()); diff --git a/src/main/java/app/teamwize/api/user/domain/response/UserCompactResponse.java b/src/main/java/app/teamwize/api/user/domain/response/UserCompactResponse.java index cb5a5bb..36a9457 100644 --- a/src/main/java/app/teamwize/api/user/domain/response/UserCompactResponse.java +++ b/src/main/java/app/teamwize/api/user/domain/response/UserCompactResponse.java @@ -21,6 +21,10 @@ public record UserCompactResponse( String lastName, @Nullable String phone, + @Nullable + String countryCode, + @Nullable + String timezone, @Nonnull TeamCompactResponse team) { } diff --git a/src/main/resources/db/changelog/20240818-0933-add-reason-to-days-off-table.xml b/src/main/resources/db/changelog/20240818-0933-add-reason-to-days-off-table.xml new file mode 100644 index 0000000..3cccf2c --- /dev/null +++ b/src/main/resources/db/changelog/20240818-0933-add-reason-to-days-off-table.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index e0de19a..09571e7 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -22,4 +22,7 @@ databaseChangeLog: relativeToChangelogFile: true - include: file: 20240815-2230-add-timezone-country-code-to-user-table.xml + relativeToChangelogFile: true + - include: + file: 20240818-0933-add-reason-to-days-off-table.xml relativeToChangelogFile: true \ No newline at end of file