diff --git a/dao/src/main/java/greencity/entity/HabitInvitation.java b/dao/src/main/java/greencity/entity/HabitInvitation.java index bff644682..b672b0db0 100644 --- a/dao/src/main/java/greencity/entity/HabitInvitation.java +++ b/dao/src/main/java/greencity/entity/HabitInvitation.java @@ -35,6 +35,14 @@ public class HabitInvitation { @JoinColumn(name = "invitee_habit_assign_id", nullable = false) private HabitAssign inviteeHabitAssign; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "inviter_id", nullable = false) + private User inviter; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "invitee_id", nullable = false) + private User invitee; + @Enumerated(EnumType.STRING) private HabitInvitationStatus status; } diff --git a/dao/src/main/java/greencity/repository/HabitInvitationRepo.java b/dao/src/main/java/greencity/repository/HabitInvitationRepo.java index 90316316d..d8cbb3b8d 100644 --- a/dao/src/main/java/greencity/repository/HabitInvitationRepo.java +++ b/dao/src/main/java/greencity/repository/HabitInvitationRepo.java @@ -82,14 +82,22 @@ invitations AS ( AND i.inviter_habit_assign_id IN ( SELECT ha.id FROM habit_assign ha WHERE ha.habit_id = :habitId ) + ), + habit_assignments AS ( + SELECT DISTINCT ha.user_id AS friend_id + FROM habit_assign ha + WHERE ha.habit_id = :habitId + AND ha.status = 'INPROGRESS' + AND ha.user_id IN (SELECT id FROM friends) ) SELECT f.id, f.name, f.email, f.profile_picture, - COALESCE(inv.has_invitation, FALSE) AS has_invitation + COALESCE(inv.friend_id, ha.friend_id) IS NOT NULL AS has_invitation FROM filtered_friends f LEFT JOIN invitations inv ON f.id = inv.friend_id + LEFT JOIN habit_assignments ha ON f.id = ha.friend_id """) List findUserFriendsWithHabitInvites( Long userId, String name, Long habitId, Pageable pageable); diff --git a/dao/src/main/resources/db/changelog/db.changelog-master.xml b/dao/src/main/resources/db/changelog/db.changelog-master.xml index 831890abb..c5e06e860 100644 --- a/dao/src/main/resources/db/changelog/db.changelog-master.xml +++ b/dao/src/main/resources/db/changelog/db.changelog-master.xml @@ -255,4 +255,5 @@ + diff --git a/dao/src/main/resources/db/changelog/logs/ch-add-invitee-id-and-inviter-id-to-habit-invite-table.xml b/dao/src/main/resources/db/changelog/logs/ch-add-invitee-id-and-inviter-id-to-habit-invite-table.xml new file mode 100644 index 000000000..b0dcea94c --- /dev/null +++ b/dao/src/main/resources/db/changelog/logs/ch-add-invitee-id-and-inviter-id-to-habit-invite-table.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + UPDATE habit_invitations hi + SET inviter_id = ha.user_id + FROM habit_assign ha + WHERE hi.inviter_habit_assign_id = ha.id + AND hi.inviter_id IS NULL; + + + + UPDATE habit_invitations hi + SET invitee_id = ha.user_id + FROM habit_assign ha + WHERE hi.invitee_habit_assign_id = ha.id + AND hi.invitee_id IS NULL; + + + + + + + + \ No newline at end of file diff --git a/service/src/main/java/greencity/service/HabitAssignServiceImpl.java b/service/src/main/java/greencity/service/HabitAssignServiceImpl.java index add59c2dc..b22413fff 100644 --- a/service/src/main/java/greencity/service/HabitAssignServiceImpl.java +++ b/service/src/main/java/greencity/service/HabitAssignServiceImpl.java @@ -1646,6 +1646,8 @@ private HabitInvitation createHabitInvitation(HabitAssign inviteeHabitAssign, Ha return HabitInvitation.builder() .inviteeHabitAssign(inviteeHabitAssign) .inviterHabitAssign(inviterHabitAssign) + .inviter(inviterHabitAssign.getUser()) + .invitee(inviteeHabitAssign.getUser()) .status(HabitInvitationStatus.PENDING) .build(); }