diff --git a/platform/src/main/java/com/flickmatch/platform/dynamodb/model/Event.java b/platform/src/main/java/com/flickmatch/platform/dynamodb/model/Event.java index dbfd9d89..fc5622cf 100644 --- a/platform/src/main/java/com/flickmatch/platform/dynamodb/model/Event.java +++ b/platform/src/main/java/com/flickmatch/platform/dynamodb/model/Event.java @@ -8,6 +8,7 @@ import java.util.Date; import java.util.List; +import java.util.Objects; @DynamoDBTable(tableName="Event") @Builder @@ -248,5 +249,19 @@ public void setDate(String date) { this.date = date; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + EventId eventId = (EventId) o; + return Objects.equals(cityId, eventId.cityId) && + Objects.equals(date, eventId.date); + } + + @Override + public int hashCode() { + return Objects.hash(cityId, date); + } + } } diff --git a/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java b/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java index 9e00eb54..204c4ec3 100644 --- a/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java +++ b/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java @@ -25,6 +25,8 @@ import java.util.*; import java.util.concurrent.atomic.AtomicInteger; +import static java.lang.String.format; + @Service @Log4j2 public class EventBuilder { @@ -90,19 +92,23 @@ public Event joinEventRazorPayment(final RazorPaymentRequest paymentRequest) { public List getEvents(String cityId, String localTimeZone) { Date currentTime = new Date(System.currentTimeMillis()); List eventList = new ArrayList<>(); - List prevDayList = getPastEvents(cityId, 1, localTimeZone); + List prevDayList = getPastEvents(cityId, 2, localTimeZone); + eventList.addAll(prevDayList); final int GET_EVENT_DAYS = 7; - eventList.addAll(prevDayList); + log.info(format("Fetching events for cityId %s in the next %d days", cityId, GET_EVENT_DAYS)); + for (int i = 0; i < GET_EVENT_DAYS; i++) { Date currentDate = Date.from(currentTime.toInstant().plus(i, ChronoUnit.DAYS)); String formattedDate = DateUtil.extractDateFromISOFormatDate(currentDate, localTimeZone); - System.out.println(formattedDate); - Optional eventData = eventRepository.findById(new Event.EventId(cityId, formattedDate)); - System.out.println(eventData); + Event.EventId eventId = Event.EventId.builder().cityId(cityId).date(formattedDate).build(); + Optional eventData = eventRepository.findById(eventId); + if (eventData.isPresent()) { List dailyEventList = eventData.get().getEventDetailsList().stream() + // TODO: Add condition here to check the the event is starting no later than 7 days + .filter(eventDetails -> eventDetails.getStartTime().after(currentTime)) .map(eventDetails -> mapEventToGQLType(eventDetails, formattedDate, localTimeZone, cityId)) .toList(); eventList.addAll(dailyEventList); @@ -117,6 +123,7 @@ public List getPastEvents(String cit Date currentTime = new Date(System.currentTimeMillis()); // Calculate the date before inDays Date dateBeforeInDays = Date.from(currentTime.toInstant().minus(inDays, ChronoUnit.DAYS)); + log.info(format("Fetching events for cityId %s in the last %d days.", cityId, inDays)); eventRepository.findAll().forEach(event -> { if (event.getCityId().equals(cityId)) { List pastEventsInCity = @@ -130,6 +137,7 @@ public List getPastEvents(String cit pastEventList.addAll(pastEventsInCity); } }); + log.info(format("Found a total of %d events", pastEventList.size())); return pastEventList; } diff --git a/platform/src/main/java/com/flickmatch/platform/rest/RazorPaymentCallbackController.java b/platform/src/main/java/com/flickmatch/platform/rest/RazorPaymentCallbackController.java index a9864a06..bcf9928c 100644 --- a/platform/src/main/java/com/flickmatch/platform/rest/RazorPaymentCallbackController.java +++ b/platform/src/main/java/com/flickmatch/platform/rest/RazorPaymentCallbackController.java @@ -74,7 +74,7 @@ public ResponseEntity processRazorCallback(@RequestParam("razorpay_order_id") } catch (Exception e) { - log.error("Error processing callback: {}", e.getStackTrace()); + log.error("Error processing callback: ", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback"); } diff --git a/platform/src/test/java/com/flickmatch/platform/graphql/builder/EventBuilderTest.java b/platform/src/test/java/com/flickmatch/platform/graphql/builder/EventBuilderTest.java index f4ab769c..38a60f49 100644 --- a/platform/src/test/java/com/flickmatch/platform/graphql/builder/EventBuilderTest.java +++ b/platform/src/test/java/com/flickmatch/platform/graphql/builder/EventBuilderTest.java @@ -1,23 +1,32 @@ package com.flickmatch.platform.graphql.builder; +import static com.flickmatch.platform.graphql.util.DateUtil.extractDateFromISOFormatDate; +import static java.time.Instant.now; +import static java.time.temporal.ChronoUnit.DAYS; +import static java.time.temporal.ChronoUnit.HOURS; +import static java.util.Date.from; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.flickmatch.platform.dynamodb.model.Event; import com.flickmatch.platform.dynamodb.repository.EventRepository; import com.flickmatch.platform.graphql.input.JoinEventInput; import com.flickmatch.platform.graphql.input.PlayerInput; +import com.flickmatch.platform.graphql.util.DateUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; import java.sql.Time; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.Instant; import java.time.LocalDate; +import java.time.temporal.ChronoUnit; import java.util.*; public class EventBuilderTest { @@ -92,34 +101,42 @@ private Optional createEventMockData() { .build(); List eventDetailsList = new ArrayList<>(); + eventDetailsList.add(getEventDetails(new Date(), new Date())); - Event.PlayerDetails playerDetails = Event.PlayerDetails.builder() + Optional event = Optional.ofNullable(Event.builder() + .eventId(eventId) + .eventDetailsList(eventDetailsList) + .build()); + return event; + } + + private List getListOfPlayerDetails() { + List playerDetailsList = new ArrayList<>(); + playerDetailsList.add(getPlayerDetails()); + + return playerDetailsList; + } + + private Event.PlayerDetails getPlayerDetails() { + return Event.PlayerDetails.builder() .name("name") .waNumber("waNumber") .build(); + } - List playerDetailsList = new ArrayList<>(); - playerDetailsList.add(playerDetails); - - Event.EventDetails eventDetails = Event.EventDetails.builder() + private Event.EventDetails getEventDetails(Date startTime, Date endTime) { + return Event.EventDetails.builder() .index(1) - .startTime(new Date()) - .endTime(new Date()) + .startTime(startTime) + .endTime(endTime) .charges(10.0) .reservedPlayersCount(0) .waitListPlayersCount(1) .sportName("Football") .venueName("venueName") .venueLocationLink("venueLocationLink") - .playerDetailsList(playerDetailsList) + .playerDetailsList(getListOfPlayerDetails()) .build(); - eventDetailsList.add(eventDetails); - - Optional event = Optional.ofNullable(Event.builder() - .eventId(eventId) - .eventDetailsList(eventDetailsList) - .build()); - return event; } @Test @@ -164,47 +181,53 @@ public void testGetEvents() { // Mock input data String cityId = "2"; String localTimeZone = "GMT+05:30"; - List events = createMockEvents(cityId); - // Print details of each event -// for (Event event : events) { -// System.out.println("Event Details:"); -// System.out.println("City ID: " + event.getCityId()); -// System.out.println("Date: " + event.getDate()); -// // Add more details as needed -// } + + List pastEvents = new ArrayList<>(); + + Instant currentTime = Instant.now(); + + String todayDate = extractDateFromISOFormatDate(from(currentTime), localTimeZone); + String yesterdayDate = extractDateFromISOFormatDate(from(currentTime.minus(1, DAYS)), localTimeZone); + Event eventToday = getMockEvent(cityId, todayDate, getEventDetailsList(currentTime)); + Event eventYesterday = getMockEvent(cityId, yesterdayDate, getEventDetailsList(currentTime)); + pastEvents.add(eventYesterday); + + Event.EventId eventIdForToday = Event.EventId.builder().cityId(cityId).date(todayDate).build(); + // Mock the event repository to return the list of events - when(eventRepository.findAll()).thenReturn(events); + when(eventRepository.findById(eq(eventIdForToday))).thenReturn(Optional.of(eventToday)); + when(eventRepository.findAll()).thenReturn(pastEvents); + // Call the method under test - List result = eventBuilder.getEvents("2", "GMT+5:30"); + List result = eventBuilder.getEvents(cityId, localTimeZone); // Verify the result assertEquals(2, result.size()); } - private List createMockEvents(String cityId) { - List events = new ArrayList<>(); + private List getEventDetailsList(Instant currentTime) { - // Get today's date - LocalDate today = LocalDate.now(); + Event.EventDetails eventInFuture1 = getEventDetails(from(currentTime.plus(1, HOURS)), + from(currentTime.plus(2, HOURS))); - // Create an event for today - Event eventToday = new Event(); - eventToday.setCityId(cityId); - eventToday.setDate(today.toString()); - eventToday.setEventDetailsList(new ArrayList<>()); - events.add(eventToday); + Event.EventDetails eventInPast1 = getEventDetails(from(currentTime.minus(4, HOURS)), + from(currentTime.minus(3, HOURS))); - // Get yesterday's date - LocalDate yesterday = today.minusDays(1); + Event.EventDetails eventInPast2 = getEventDetails(from(currentTime.minus(4, DAYS)), + from(currentTime.minus(3, DAYS))); - // Create an event for yesterday - Event eventYesterday = new Event(); - eventYesterday.setCityId(cityId); - eventYesterday.setDate(yesterday.toString()); - eventYesterday.setEventDetailsList(new ArrayList<>()); - events.add(eventYesterday); + List listOfEventDetailsForADay = new ArrayList<>(); + listOfEventDetailsForADay.add(eventInFuture1); + listOfEventDetailsForADay.add(eventInPast1); - return events; + return listOfEventDetailsForADay; + } + + private Event getMockEvent(String cityId, String date, List eventDetailsList) { + return Event.builder() + .eventId(Event.EventId.builder().cityId(cityId).date(date).build()) + .eventDetailsList(eventDetailsList) + .build(); } }