Repository: ita-social-projects/greencity
Issue: [Event] User is able to edit ended event.

**Environment:** macOS Monterey 12.4
Safari 15.5
**Reproducible:** always
**Build found:** 22.07.22

**Steps to reproduce**

1. Sign in Green City project.
2. Create event.
3. Open the event page when it ends.

**Actual result**
The "Edit" link is present and the user is able to edit the event.

**Expected result**
There is no "Edit" link.

**User story and test case links**
E.g.: "User story #3195 
[Test case](https://jira.softserve.academy/browse/GC-3161)"

**Labels to be added**
"Bug", Priority ("priority: medium "), Severity ("severity: major").


Changed files:
service/src/test/java/greencity/ModelUtils.java, service/src/main/java/greencity/service/EventServiceImpl.java, service/src/test/java/greencity/service/EventServiceImplTest.java

diff --git a/service/src/main/java/greencity/service/EventServiceImpl.java b/service/src/main/java/greencity/service/EventServiceImpl.java
index a4e4641ac..23a4afbd0 100644
--- a/service/src/main/java/greencity/service/EventServiceImpl.java
+++ b/service/src/main/java/greencity/service/EventServiceImpl.java
@@ -223,7 +223,7 @@ public class EventServiceImpl implements EventService {
             throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
         }
 
-        if (!toUpdate.isOpen()) {
+        if (findLastEventDateTime(toUpdate).isBefore(ZonedDateTime.now())) {
             throw new BadRequestException(ErrorMessage.EVENT_IS_FINISHED);
         }
 
diff --git a/service/src/test/java/greencity/ModelUtils.java b/service/src/test/java/greencity/ModelUtils.java
index ad92087d0..feff3b22b 100644
--- a/service/src/test/java/greencity/ModelUtils.java
+++ b/service/src/test/java/greencity/ModelUtils.java
@@ -1740,12 +1740,29 @@ public class ModelUtils {
         event.setTitle("Title");
         List<EventDateLocation> dates = new ArrayList<>();
         dates.add(new EventDateLocation(1L, event,
-            ZonedDateTime.of(2000, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
-            ZonedDateTime.of(2000, 2, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
+            ZonedDateTime.of(2098, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
+            ZonedDateTime.of(2099, 2, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
             new Coordinates(45.45, 45.45, "Ua Address", "En Address"), null));
         dates.add(new EventDateLocation(2L, event,
-            ZonedDateTime.of(2002, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
-            ZonedDateTime.of(2002, 2, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
+            ZonedDateTime.of(2099, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
+            ZonedDateTime.of(2100, 2, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
+            new Coordinates(45.45, 45.45, "Ua Address", "En Address"), null));
+        event.setDates(dates);
+        event.setTags(List.of(getEventTag()));
+        event.setTitleImage(AppConstant.DEFAULT_HABIT_IMAGE);
+        return event;
+    }
+
+    public static Event getEventWithFinishedDate() {
+        Event event = new Event();
+        event.setDescription("Description");
+        event.setId(1L);
+        event.setOrganizer(getUser());
+        event.setTitle("Title");
+        List<EventDateLocation> dates = new ArrayList<>();
+        dates.add(new EventDateLocation(1L, event,
+            ZonedDateTime.of(2000, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
+            ZonedDateTime.of(2001, 2, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
             new Coordinates(45.45, 45.45, "Ua Address", "En Address"), null));
         event.setDates(dates);
         event.setTags(List.of(getEventTag()));
diff --git a/service/src/test/java/greencity/service/EventServiceImplTest.java b/service/src/test/java/greencity/service/EventServiceImplTest.java
index 896917989..b811299ac 100644
--- a/service/src/test/java/greencity/service/EventServiceImplTest.java
+++ b/service/src/test/java/greencity/service/EventServiceImplTest.java
@@ -21,7 +21,6 @@ import greencity.repository.EventRepo;
 import lombok.SneakyThrows;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.jupiter.params.provider.EnumSource;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.modelmapper.ModelMapper;
@@ -115,8 +114,7 @@ class EventServiceImplTest {
 
     @Test
     void updateFinishedEvent() {
-        Event actualEvent = ModelUtils.getEvent();
-        actualEvent.setOpen(false);
+        Event actualEvent = ModelUtils.getEventWithFinishedDate();
         UpdateEventDto eventToUpdateDto = ModelUtils.getUpdateEventDto();
         String userEmail = ModelUtils.getUser().getEmail();
 
@@ -589,7 +587,7 @@ class EventServiceImplTest {
 
     @Test
     void rateEvent() {
-        Event event = ModelUtils.getEvent();
+        Event event = ModelUtils.getEventWithFinishedDate();
         User user = ModelUtils.getAttenderUser();
         event.setAttenders(Set.of(user));
         when(eventRepo.findById(any())).thenReturn(Optional.of(event));