diff --git a/src/main/java/com/ays/assignment/model/dto/response/AssignmentResponse.java b/src/main/java/com/ays/assignment/model/dto/response/AssignmentResponse.java index 59ad6e3ce..6270634e1 100644 --- a/src/main/java/com/ays/assignment/model/dto/response/AssignmentResponse.java +++ b/src/main/java/com/ays/assignment/model/dto/response/AssignmentResponse.java @@ -1,8 +1,12 @@ package com.ays.assignment.model.dto.response; +import com.ays.assignment.model.enums.AssignmentStatus; +import com.ays.common.model.AysPhoneNumber; import com.ays.common.model.dto.response.BaseResponse; +import lombok.Builder; import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; import lombok.experimental.SuperBuilder; /** @@ -15,9 +19,32 @@ public class AssignmentResponse extends BaseResponse { private String id; private String description; + private AssignmentStatus status; private String firstName; private String lastName; - private Double longitude; - private Double latitude; + private AysPhoneNumber phoneNumber; + private Location location; + private User user; + + @Getter + @Setter + @Builder + public static class User { + + private String id; + private String firstName; + private String lastName; + private AysPhoneNumber phoneNumber; + private Location location; + + } + + @Getter + @Builder + public static class Location { + private Double longitude; + private Double latitude; + + } } diff --git a/src/main/java/com/ays/assignment/model/mapper/AssignmentEntityToAssignmentMapper.java b/src/main/java/com/ays/assignment/model/mapper/AssignmentEntityToAssignmentMapper.java index 2959e2296..d909d2427 100644 --- a/src/main/java/com/ays/assignment/model/mapper/AssignmentEntityToAssignmentMapper.java +++ b/src/main/java/com/ays/assignment/model/mapper/AssignmentEntityToAssignmentMapper.java @@ -18,8 +18,10 @@ public interface AssignmentEntityToAssignmentMapper extends BaseMapper { @Override - @Mapping(target = "phoneNumber.countryCode", source = "source.countryCode") - @Mapping(target = "phoneNumber.lineNumber", source = "source.lineNumber") + @Mapping(target = "phoneNumber.countryCode", source = "countryCode") + @Mapping(target = "phoneNumber.lineNumber", source = "lineNumber") + @Mapping(target = "user.phoneNumber.countryCode", source = "user.countryCode") + @Mapping(target = "user.phoneNumber.lineNumber", source = "user.lineNumber") Assignment map(AssignmentEntity source); /** diff --git a/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentResponseMapper.java b/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentResponseMapper.java index 7ec13941e..7d4b2d210 100644 --- a/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentResponseMapper.java +++ b/src/main/java/com/ays/assignment/model/mapper/AssignmentToAssignmentResponseMapper.java @@ -18,8 +18,8 @@ public interface AssignmentToAssignmentResponseMapper extends BaseMapper { @Override - @Mapping(target = "longitude", source = "source.point.x") - @Mapping(target = "latitude", source = "source.point.y") + @Mapping(target = "location.longitude", source = "point.x") + @Mapping(target = "location.latitude", source = "point.y") AssignmentResponse map(Assignment source); /** diff --git a/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java b/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java index aca36d5ae..f926b23af 100644 --- a/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java +++ b/src/main/java/com/ays/assignment/service/impl/AssignmentServiceImpl.java @@ -7,18 +7,26 @@ import com.ays.assignment.service.AssignmentService; import com.ays.assignment.util.exception.AysAssignmentNotExistByIdException; import com.ays.auth.model.AysIdentity; +import com.ays.location.model.UserLocation; +import com.ays.location.model.entity.UserLocationEntity; +import com.ays.location.model.mapper.UserLocationEntityToUserLocationMapper; +import com.ays.location.repository.UserLocationRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.Optional; + @Service @RequiredArgsConstructor class AssignmentServiceImpl implements AssignmentService { private final AssignmentRepository assignmentRepository; + private final UserLocationRepository userLocationRepository; private final AysIdentity identity; private static final AssignmentEntityToAssignmentMapper assignmentEntityToAssignmentMapper = AssignmentEntityToAssignmentMapper.initialize(); + private static final UserLocationEntityToUserLocationMapper userLocationEntityToUserLocationMapper = UserLocationEntityToUserLocationMapper.initialize(); /** * Retrieves an assignment by their ID. @@ -33,6 +41,20 @@ public Assignment getAssignmentById(String id) { final AssignmentEntity assignmentEntity = assignmentRepository.findByIdAndInstitutionId(id, identity.getInstitutionId()) .orElseThrow(() -> new AysAssignmentNotExistByIdException(id)); - return assignmentEntityToAssignmentMapper.map(assignmentEntity); + Assignment assignment = assignmentEntityToAssignmentMapper.map(assignmentEntity); + + Optional optionalUserLocationEntity = userLocationRepository + .findByUserId(assignmentEntity.getUserId()); + + if (optionalUserLocationEntity.isPresent()) { + + UserLocation userLocation = userLocationEntityToUserLocationMapper + .map(optionalUserLocationEntity.get()); + + assignment.getUser().setLocation(userLocation.getPoint()); + } + + return assignment; } + } diff --git a/src/main/java/com/ays/user/model/User.java b/src/main/java/com/ays/user/model/User.java index 11c07fdd7..ce4381171 100644 --- a/src/main/java/com/ays/user/model/User.java +++ b/src/main/java/com/ays/user/model/User.java @@ -6,9 +6,9 @@ import com.ays.user.model.enums.UserRole; import com.ays.user.model.enums.UserStatus; import com.ays.user.model.enums.UserSupportStatus; -import lombok.Data; -import lombok.EqualsAndHashCode; +import lombok.*; import lombok.experimental.SuperBuilder; +import org.locationtech.jts.geom.Point; /** * User Domain Model to perform data transfer from service layer to controller @@ -27,7 +27,22 @@ public class User extends BaseDomainModel { private UserStatus status; private UserSupportStatus supportStatus; private AysPhoneNumber phoneNumber; + private Location location; private Institution institution; + @Getter + @Builder + public static class Location { + private Double longitude; + private Double latitude; + } + + public void setLocation(Point point) { + this.location = Location.builder() + .longitude(point.getX()) + .latitude(point.getY()) + .build(); + } + } diff --git a/src/test/java/com/ays/assignment/service/impl/AssignmentServiceImplTest.java b/src/test/java/com/ays/assignment/service/impl/AssignmentServiceImplTest.java index 0a5394979..aa6a10421 100644 --- a/src/test/java/com/ays/assignment/service/impl/AssignmentServiceImplTest.java +++ b/src/test/java/com/ays/assignment/service/impl/AssignmentServiceImplTest.java @@ -9,6 +9,11 @@ import com.ays.assignment.util.exception.AysAssignmentNotExistByIdException; import com.ays.auth.model.AysIdentity; import com.ays.common.util.AysRandomUtil; +import com.ays.location.model.UserLocation; +import com.ays.location.model.entity.UserLocationEntity; +import com.ays.location.model.entity.UserLocationEntityBuilder; +import com.ays.location.model.mapper.UserLocationEntityToUserLocationMapper; +import com.ays.location.repository.UserLocationRepository; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -26,11 +31,14 @@ class AssignmentServiceImplTest extends AbstractUnitTest { @Mock private AssignmentRepository assignmentRepository; + @Mock + private UserLocationRepository userLocationRepository; + @Mock private AysIdentity identity; private static final AssignmentEntityToAssignmentMapper ASSIGNMENT_ENTITY_TO_ASSIGNMENT_MAPPER = AssignmentEntityToAssignmentMapper.initialize(); - + private static final UserLocationEntityToUserLocationMapper USER_LOCATION_ENTITY_TO_USER_LOCATION_MAPPER = UserLocationEntityToUserLocationMapper.initialize(); @Test void givenAssignmentId_whenGetAssignment_thenReturnAssignment() { @@ -42,14 +50,18 @@ void givenAssignmentId_whenGetAssignment_thenReturnAssignment() { .withValidFields() .withInstitutionId(mockInstitutionId).build(); String mockAssignmentId = mockAssignmentEntity.getId(); + UserLocationEntity mockUserLocationEntity = new UserLocationEntityBuilder().withValidFields().build(); + UserLocation mockUserLocation = USER_LOCATION_ENTITY_TO_USER_LOCATION_MAPPER.map(mockUserLocationEntity); Assignment mockAssignment = ASSIGNMENT_ENTITY_TO_ASSIGNMENT_MAPPER.map(mockAssignmentEntity); + mockAssignment.getUser().setLocation(mockUserLocation.getPoint()); // When Mockito.when(identity.getInstitutionId()) .thenReturn(mockInstitutionId); Mockito.when(assignmentRepository.findByIdAndInstitutionId(Mockito.anyString(), Mockito.anyString())) .thenReturn(Optional.of(mockAssignmentEntity)); + Mockito.when(userLocationRepository.findByUserId(Mockito.anyString())).thenReturn(Optional.of(mockUserLocationEntity)); // Then Assignment assignment = assignmentService.getAssignmentById(mockAssignmentId); @@ -61,6 +73,10 @@ void givenAssignmentId_whenGetAssignment_thenReturnAssignment() { Assertions.assertEquals(mockAssignment.getPhoneNumber().getLineNumber(), assignment.getPhoneNumber().getLineNumber()); Assertions.assertEquals(mockAssignment.getStatus(), assignment.getStatus()); Assertions.assertEquals(mockAssignment.getPoint(), assignment.getPoint()); + Assertions.assertEquals(mockAssignment.getUser().getLocation().getLongitude(), + assignment.getUser().getLocation().getLongitude()); + Assertions.assertEquals(mockAssignment.getUser().getLocation().getLatitude(), + assignment.getUser().getLocation().getLatitude()); Mockito.verify(assignmentRepository, Mockito.times(1)) .findByIdAndInstitutionId(mockAssignmentId, mockInstitutionId); @@ -93,4 +109,5 @@ void givenAssignmentId_whenAssignmentNotFound_thenThrowAysAssignmentNotExistById Mockito.verify(identity, Mockito.times(1)) .getInstitutionId(); } -} \ No newline at end of file + +} diff --git a/src/test/java/com/ays/location/model/entity/UserLocationEntityBuilder.java b/src/test/java/com/ays/location/model/entity/UserLocationEntityBuilder.java new file mode 100644 index 000000000..2f4b5632c --- /dev/null +++ b/src/test/java/com/ays/location/model/entity/UserLocationEntityBuilder.java @@ -0,0 +1,46 @@ +package com.ays.location.model.entity; + +import com.ays.common.model.TestDataBuilder; +import com.ays.user.model.entity.UserEntity; +import com.ays.user.model.entity.UserEntityBuilder; + +public class UserLocationEntityBuilder extends TestDataBuilder { + + public UserLocationEntityBuilder() { + super(UserLocationEntity.class); + } + + public UserLocationEntityBuilder withValidFields() { + + UserEntity userEntity = new UserEntityBuilder() + .withValidFields() + .build(); + + return this + .withId(1L) + .withUserId(userEntity.getId()) + .withUser(userEntity) + .withPoint(1.0, 2.0); + } + + public UserLocationEntityBuilder withId(Long id) { + data.setId(id); + return this; + } + + public UserLocationEntityBuilder withUserId(String userId) { + data.setUserId(userId); + return this; + } + + public UserLocationEntityBuilder withUser(UserEntity userEntity) { + data.setUser(userEntity); + return this; + } + + public UserLocationEntityBuilder withPoint(Double longitude, Double latitude) { + data.setPoint(longitude, latitude); + return this; + } + +}