Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes after 634 #623

Merged
merged 37 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4d8526a
Merge pull request #373 from ita-social-projects/release
VitaliiKulinskyi Jul 2, 2021
d31171d
Merge pull request #375 from ita-social-projects/release
VitaliiKulinskyi Jul 2, 2021
7c207cb
Merge pull request #398 from ita-social-projects/release
AxelNordov Jul 16, 2021
ed3dc02
Merge pull request #414 from ita-social-projects/release
StanislavKucher Jul 28, 2021
0e6b784
Merge pull request #417 from ita-social-projects/release
Izarx Jul 28, 2021
43a0fd1
Merge pull request #420 from ita-social-projects/release
AxelNordov Jul 29, 2021
231a738
Merge pull request #425 from ita-social-projects/release
StanislavKucher Jul 30, 2021
d0ff06e
Merge pull request #448 from ita-social-projects/release
StanislavKucher Aug 16, 2021
0ebcae8
Merge pull request #456 from ita-social-projects/release
V-Kaidash Aug 19, 2021
5684d57
Merge pull request #470 from ita-social-projects/release
antoshaSid Sep 27, 2021
b456b00
Update develop.md
VitaliiKulinskyi Nov 12, 2021
b031d12
Update master.md
VitaliiKulinskyi Nov 12, 2021
c094763
Merge pull request #539 from ita-social-projects/develop
V-Kaidash Jun 30, 2022
90e229f
Merge pull request #554 from ita-social-projects/develop
V-Kaidash Jul 29, 2022
fd1d97c
Merge pull request #561 from ita-social-projects/develop
fortamt Aug 9, 2022
20966d0
Merge pull request #563 from ita-social-projects/develop
fortamt Aug 12, 2022
799671c
Merge pull request #565 from ita-social-projects/develop
fortamt Aug 25, 2022
f70ed1d
Merge pull request #567 from ita-social-projects/develop
fortamt Aug 26, 2022
a0ed96f
Merge pull request #576 from ita-social-projects/develop
fortamt Sep 11, 2022
f9598a8
Merge pull request #578 from ita-social-projects/develop
fortamt Sep 14, 2022
3a1b13a
Merge pull request #584 from ita-social-projects/develop
vasilpetrus Sep 22, 2022
c9742ca
Merge pull request #588 from ita-social-projects/develop
fortamt Oct 7, 2022
837a7a2
Merge pull request #590 from ita-social-projects/develop
vasilpetrus Oct 10, 2022
bd2622c
Merge pull request #592 from ita-social-projects/develop
vasilpetrus Oct 19, 2022
5da6460
Merge pull request #596 from ita-social-projects/develop
fortamt Jan 25, 2023
89611fa
Merge pull request #598 from ita-social-projects/develop
fortamt Feb 27, 2023
07fa868
Merge pull request #601 from ita-social-projects/develop
fortamt Mar 7, 2023
026c6a0
Add dokazovi.info into allowed_origin
Aug 16, 2023
a52eac6
Merge branch 'master' into release
Aug 17, 2023
45fc87a
Merge pull request #613 from ita-social-projects/develop
VadimasikKPI Sep 22, 2023
90b15eb
Merge 634 user story and delete logic (#618)
VadimasikKPI Nov 1, 2023
7a77292
Merge branch 'release' of https://github.com/ita-social-projects/doka…
Nov 2, 2023
7489eb9
fix merge conflict
Nov 2, 2023
938ccf8
Merge branch 'develop' of https://github.com/ita-social-projects/doka…
Nov 14, 2023
626fccf
bug fixes
Nov 14, 2023
83a6064
make changes according to coments
Nov 17, 2023
4c4d363
make changes according to coments
Nov 17, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ public ResponseEntity<AuthorResponseDTO> createAuthor(@Valid @RequestBody Author
.body(authorMapper.toAuthorResponseDTO(authorService.save(author, userPrincipal)));
}

@PutMapping(AUTHOR_GET_AUTHOR_BY_ID)
@PutMapping()
@PreAuthorize("hasAuthority('EDIT_AUTHOR')")
@ApiOperation(value = "update author",
authorizations = {@Authorization(value = "Authorization")})
@ApiResponses(value = {
@ApiResponse(code = 200, message = HttpStatuses.OK, response = AuthorRequestDTO.class),
@ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST)
})
public ResponseEntity<AuthorResponseDTO> updateAuthor(@PathVariable Integer authorId,
public ResponseEntity<AuthorResponseDTO> updateAuthor(
@Valid @RequestBody AuthorRequestDTO author,
@AuthenticationPrincipal UserPrincipal userPrincipal) {
return ResponseEntity
.status(200)
.body(authorMapper.toAuthorResponseDTO(authorService.update(authorId, author, userPrincipal)));
.body(authorMapper.toAuthorResponseDTO(authorService.update(author, userPrincipal)));
}

@DeleteMapping(AUTHOR_GET_AUTHOR_BY_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@NoArgsConstructor
@AllArgsConstructor
public class AuthorRequestDTO {

private Integer authorId;
@NotBlank
private String firstName;
@NotBlank
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/softserveinc/dokazovi/dto/user/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.softserveinc.dokazovi.dto.direction.DirectionDTO;
import com.softserveinc.dokazovi.dto.post.PostStatusesDTO;
import com.softserveinc.dokazovi.dto.region.RegionDTO;
import com.softserveinc.dokazovi.entity.enumerations.UserStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -36,6 +37,12 @@ public class UserDTO {

private String bio;

private String publicEmail;

private UserStatus status;

private Boolean enabled;

private RegionDTO region;

private Set<String> socialNetworks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface UserMapper {

PostMapper POST_MAPPER = Mappers.getMapper(PostMapper.class);

@Mapping(target = "id", source = "userEntity.id")
@Mapping(target = "id", source = "author.id")
@Mapping(target = ".", source = "userEntity.author")
@Mapping(target = "region", source = "userEntity.author.mainInstitution.city.region")
@Mapping(target = "postStatuses", source = "posts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ public interface UserRepository extends JpaRepository<UserEntity, Integer> {
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,u.enabled,u.role_id, u.edited_at, u.public_email, "
+ "D.author_id as \"user_id\" FROM AUTHORS D "
+ " JOIN USERS U ON D.USER_ID = U.USER_ID "
value = " SELECT U.* FROM AUTHORS D "
+ " JOIN USERS U ON D.USER_ID = U.USER_ID "
+ " ORDER BY RANDOM() ")
Page<UserEntity> findRandomExperts(Pageable pageable);

Expand All @@ -63,9 +61,7 @@ public interface UserRepository extends JpaRepository<UserEntity, Integer> {
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,u.enabled,u.role_id, "
+ "u.edited_at, u.public_email, D.author_id as \"user_id\" FROM ( "
value = " SELECT U.* FROM ( "
+ " SELECT DD.AUTHOR_ID FROM AUTHORS_DIRECTIONS DD "
+ " WHERE DD.DIRECTION_ID IN (:directionsIds) "
+ " GROUP BY DD.AUTHOR_ID "
Expand All @@ -84,9 +80,7 @@ public interface UserRepository extends JpaRepository<UserEntity, Integer> {
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\", SN.LINK FROM AUTHORS D "
value = " SELECT U.*, SN.LINK FROM AUTHORS D "
+ " JOIN USERS U ON U.USER_ID = D.USER_ID "
+ " JOIN USERS_SOCIAL_NETWORKS SN ON D.USER_ID = SN.USER_ID"
+ " ORDER BY D.PROMOTION_LEVEL DESC, D.RATING DESC, "
Expand All @@ -102,9 +96,7 @@ public interface UserRepository extends JpaRepository<UserEntity, Integer> {
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" FROM ( "
value = " SELECT U.* FROM ( "
+ " SELECT AUTHOR_ID FROM AUTHORS D "
+ " JOIN INSTITUTIONS I ON D.INSTITUTION_ID=I.INSTITUTION_ID "
+ " JOIN CITIES C ON I.CITY_ID=C.CITY_ID "
Expand Down Expand Up @@ -141,16 +133,14 @@ Page<UserEntity> findDoctorsProfiles(
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" FROM ( "
value = " SELECT U.* FROM ( "
+ " SELECT D.PROMOTION_LEVEL, D.RATING, D.USER_ID FROM AUTHORS D "
+ " JOIN INSTITUTIONS I ON D.INSTITUTION_ID=I.INSTITUTION_ID "
+ " JOIN CITIES C ON I.CITY_ID=C.CITY_ID "
+ " WHERE C.REGION_ID IN (:regionsIds) "
+ " ) DOCS_REG "
+ " JOIN USERS U ON U.USER_ID = DOCS_REG.USER_ID "
+ " JOIN AUTHORS D ON DOCS_REG.USER_ID=D.USER_ID "
+ " JOIN AUTHORS D ON DOCS_REG.USER_ID=D.USER_ID"
+ " ORDER BY DOCS_REG.PROMOTION_LEVEL DESC, DOCS_REG.RATING DESC, "
+ " U.LAST_NAME, U.FIRST_NAME ",
countQuery = " SELECT COUNT(D.AUTHOR_ID) FROM AUTHORS D "
Expand All @@ -168,9 +158,7 @@ Page<UserEntity> findDoctorsProfilesByRegionsIds(
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" FROM ( "
value = " SELECT U.* FROM ( "
+ " SELECT DD.AUTHOR_ID, COUNT(DD.DIRECTION_ID) DIR_MATCHED"
+ " FROM AUTHORS_DIRECTIONS DD "
+ " WHERE DD.DIRECTION_ID IN (:directionsIds) "
Expand All @@ -193,10 +181,7 @@ Page<UserEntity> findDoctorsProfilesByDirectionsIds(
* @return the resulting user entity page
*/
@Query(nativeQuery = true,
value = " SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" FROM USERS U "
+ " JOIN AUTHORS D ON U.USER_ID=D.USER_ID "
value = " SELECT U.* FROM USERS U "
+ " WHERE UPPER((U.FIRST_NAME || ' ' || U.LAST_NAME) COLLATE \"uk-ua-dokazovi-x-icu\")"
+ " LIKE UPPER((:name || '%') COLLATE \"uk-ua-dokazovi-x-icu\") "
+ " OR UPPER((U.LAST_NAME || ' ' || U.FIRST_NAME) COLLATE \"uk-ua-dokazovi-x-icu\")"
Expand All @@ -213,9 +198,6 @@ Page<UserEntity> findDoctorsProfilesByDirectionsIds(
Boolean existsByEmail(String email);

@Query(nativeQuery = true,
value = "SELECT U.email,U.password, U.status, U.first_name, U.last_name, "
+ "U.phone, U.created_at, U.avatar,U.enabled,U.role_id, U.edited_at, "
+ "U.public_email, D.author_id as \"user_id\" from USERS U\n"
+ "JOIN AUTHORS D ON U.user_id=D.user_id")
value = "SELECT U.* from USERS U")
Page<UserEntity> findAllWithAuthor(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface AuthorService {

AuthorEntity save(AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal);

AuthorEntity update(Integer authorId, AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal);
AuthorEntity update(AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal);

Integer delete(Integer authorId, UserPrincipal userPrincipal);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.softserveinc.dokazovi.dto.author.AuthorRequestDTO;
import com.softserveinc.dokazovi.dto.author.AuthorResponseDTO;
import com.softserveinc.dokazovi.entity.AuthorEntity;
import com.softserveinc.dokazovi.entity.InstitutionEntity;
import com.softserveinc.dokazovi.entity.UserEntity;
import com.softserveinc.dokazovi.entity.enumerations.RolePermission;
import com.softserveinc.dokazovi.entity.enumerations.UserStatus;
import com.softserveinc.dokazovi.exception.ForbiddenPermissionsException;
import com.softserveinc.dokazovi.mapper.AuthorMapper;
import com.softserveinc.dokazovi.repositories.AuthorRepository;
import com.softserveinc.dokazovi.repositories.CityRepository;
import com.softserveinc.dokazovi.repositories.InstitutionRepository;
import com.softserveinc.dokazovi.repositories.UserRepository;
import com.softserveinc.dokazovi.security.UserPrincipal;
import com.softserveinc.dokazovi.service.AuthorService;
Expand All @@ -32,6 +34,7 @@ public class AuthorServiceImpl implements AuthorService {
private final UserRepository userRepository;
private final CityRepository cityRepository;
private final AuthorMapper authorMapper;
private final InstitutionRepository institutionRepository;

@Override
public AuthorEntity findAuthorById(Integer authorId) {
Expand All @@ -56,6 +59,13 @@ public AuthorEntity save(AuthorRequestDTO authorRequestDTO, UserPrincipal userPr
.createdAt(Timestamp.valueOf(LocalDateTime.now()))
.build();
userRepository.save(user);
InstitutionEntity institutionEntity = InstitutionEntity.builder()
.name(authorRequestDTO.getMainWorkingPlace())
.city(cityRepository.findById(authorRequestDTO.getCityId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Unable to find city with id: " + authorRequestDTO.getCityId())))
.build();
institutionRepository.save(institutionEntity);
AuthorEntity author = AuthorEntity.builder()
.publishedPosts(0L)
.promotionScale(1.0)
Expand All @@ -64,17 +74,18 @@ public AuthorEntity save(AuthorRequestDTO authorRequestDTO, UserPrincipal userPr
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Unable to find city with id: " + authorRequestDTO.getCityId())))
.profile(user)
.mainInstitution(institutionEntity)
.bio(authorRequestDTO.getBio())
.build();
return authorRepository.save(author);
}

@Override
public AuthorEntity update(Integer authorId, AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal) {
public AuthorEntity update(AuthorRequestDTO authorRequestDTO, UserPrincipal userPrincipal) {
if (!hasEnoughAuthorities(userPrincipal)) {
throw new ForbiddenPermissionsException("Not enough authority");
}
AuthorEntity oldAuthor = findAuthorById(authorId);
AuthorEntity oldAuthor = findAuthorById(authorRequestDTO.getAuthorId());
UserEntity oldUser = userRepository.getOne(oldAuthor.getProfile().getId());
UserEntity newUser = UserEntity.builder()
.id(oldUser.getId())
Expand All @@ -93,14 +104,24 @@ public AuthorEntity update(Integer authorId, AuthorRequestDTO authorRequestDTO,
.editedAt(Timestamp.valueOf(LocalDateTime.now()))
.build();
userRepository.save(newUser);
InstitutionEntity institutionEntity = InstitutionEntity.builder()
.id(oldAuthor.getMainInstitution().getId())
.name(authorRequestDTO.getMainWorkingPlace())
.city(cityRepository.findById(authorRequestDTO.getCityId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Unable to find city with id: " + authorRequestDTO.getCityId())))
.address(oldAuthor.getMainInstitution().getAddress())
.build();
institutionRepository.save(institutionEntity);
AuthorEntity newAuthor = AuthorEntity.builder()
.id(authorId)
.id(authorRequestDTO.getAuthorId())
.publishedPosts(oldAuthor.getPublishedPosts())
.promotionScale(oldAuthor.getPromotionScale())
.mainWorkingPlace(authorRequestDTO.getMainWorkingPlace())
.city(cityRepository.findById(authorRequestDTO.getCityId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Unable to find city with id: " + authorRequestDTO.getCityId())))
.mainInstitution(institutionEntity)
.profile(newUser)
.bio(authorRequestDTO.getBio())
.promotionScale(oldAuthor.getPromotionScale())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public Page<UserEntity> findAll(Pageable pageable) {
*/
@Override
public UserDTO findExpertById(Integer userId) {
return userMapper.toUserDTO(userRepository.findById(userId).orElse(null));
AuthorEntity author = authorRepository.findById(userId).orElseThrow(
() -> new EntityNotFoundException("Author not found"));
return userMapper.toUserDTO(userRepository.findById(author.getProfile().getId()).orElseThrow(
() -> new EntityNotFoundException("User not found")));

}

Expand All @@ -116,7 +119,7 @@ public UserDTO findExpertById(Integer userId) {
public Page<UserDTO> findAllExperts(UserSearchCriteria userSearchCriteria, Pageable pageable) {

if (validateParameters(userSearchCriteria, HAS_NO_DIRECTIONS, HAS_NO_REGIONS, HAS_NO_USERNAME)) {
return userRepository.findAllWithAuthor(pageable).map(userMapper::toUserDTO);
return userRepository.findAll(pageable).map(userMapper::toUserDTO);
}

final String name = userSearchCriteria.getUserName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void createAuthor() throws Exception {

@Test
void updateAuthor() throws Exception {
mockMvc.perform(put("/author/{authorId}","1")
mockMvc.perform(put("/author")
.contentType("application/json")
.content(userDTO))
.andExpect(status().isOk());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ void init() {
.email("mail@mail.com")
.phone("380990099009")
.avatar("Some avatar url")
.enabled(true)
.socialNetworks(Set.of("Facebook", "Twitter"))
.build();

authorEntity = AuthorEntity.builder()
.id(1)
.qualification("qualification 1")
.bio("bio 1")
.mainInstitution(mainInstitution)
Expand Down Expand Up @@ -247,6 +249,7 @@ void tooUserDtoEmptyOrNullCases() {
assertNull(userDTO.getMainInstitution().getCity());

UserEntity userEntity1 = new UserEntity();
userEntity1.setEnabled(false);
userDTO = mapper.toUserDTO(userEntity1);
assertNull(userDTO.getId());
assertNull(userDTO.getBio());
Expand All @@ -258,6 +261,7 @@ void tooUserDtoEmptyOrNullCases() {

userEntity.setAuthor(new AuthorEntity());
userDTO = mapper.toUserDTO(userEntity);
userDTO.setId(userEntity.getId());
assertEquals(userDTO.getId(), userEntity.getId());
assertNull(userDTO.getBio());
assertNull(userDTO.getQualification());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ void init() {
.enabled(true)
.build();

institutionEntity = InstitutionEntity.builder()
.build();

userEntity = UserEntity.builder()
.id(2)
.email("mail@mail.com")
Expand All @@ -118,6 +115,7 @@ void init() {
.build();

authorRequestDTO = AuthorRequestDTO.builder()
.authorId(1)
.firstName("firstName")
.lastName("lastName")
.cityId(190)
Expand All @@ -127,15 +125,21 @@ void init() {
.socialNetworks(new HashSet<>())
.build();

cityEntity = CityEntity.builder()
.id(190)
.build();

institutionEntity = InstitutionEntity.builder()
.id(1)
.city(cityEntity)
.name("Hospital")
.build();

authorEntity = AuthorEntity.builder()
.id(1)
.profile(userEntity)
.mainInstitution(institutionEntity)
.build();

cityEntity = CityEntity.builder()
.id(190)
.build();
}

@Test
Expand All @@ -145,7 +149,7 @@ void updateWithoutPermission() {
.role(adminRole)
.build();

assertThatThrownBy(() -> authorService.update(1, authorRequestDTO, userPrincipal))
assertThatThrownBy(() -> authorService.update(authorRequestDTO, userPrincipal))
.isInstanceOf(ForbiddenPermissionsException.class);
}

Expand All @@ -159,7 +163,7 @@ void update() {
.role(adminRole)
.build();

authorService.update(1, authorRequestDTO, userPrincipal);
authorService.update(authorRequestDTO, userPrincipal);

verify(authorRepository).save(authorEntityArgumentCaptor.capture());
Assertions.assertEquals(authorEntityArgumentCaptor.getValue().getId(), authorEntity.getId());
Expand All @@ -179,6 +183,7 @@ void saveWithoutPermission() {
@Test
void save() {
when(cityRepository.findById(anyInt())).thenReturn(Optional.of(cityEntity));

UserPrincipal userPrincipal = UserPrincipal.builder()
.role(adminRole)
.build();
Expand All @@ -199,12 +204,13 @@ void save() {
.promotionScale(1.0)
.mainWorkingPlace(authorRequestDTO.getMainWorkingPlace())
.city(cityEntity)
.mainInstitution(institutionEntity)
.profile(user)
.bio(authorRequestDTO.getBio())
.build();
verify(userRepository).save(userEntityArgumentCaptor.capture());
verify(authorRepository).save(authorEntityArgumentCaptor.capture());
Assertions.assertEquals(authorEntityArgumentCaptor.getValue(), author);
Assertions.assertEquals(authorEntityArgumentCaptor.getValue().getId(), author.getId());
Assertions.assertEquals(userEntityArgumentCaptor.getValue().getId(), user.getId());
}

Expand Down
Loading
Loading