Skip to content

Commit

Permalink
Fix : Schedules missing with same date/times
Browse files Browse the repository at this point in the history
  • Loading branch information
ianouf committed Oct 29, 2023
1 parent e12fa3a commit d7b92df
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# GoLand IDE
.idea/

# Fleet IDE
.fleet/

# Compiled Go programs and executables
*.exe
*.out
Expand Down
2 changes: 1 addition & 1 deletion pkg/scraper/mada_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func setupDatabase() *gorm.DB {
if err != nil {
panic("failed to connect to test database")
}

err = db.AutoMigrate(&api.User{}, &api.Station{}, &api.StationStatus{}, &api.StationSchedule{})
if err != nil {
log.Fatalf("Failed to migrate... %+v", err)
Expand Down Expand Up @@ -127,7 +128,6 @@ func TestScrapeMada(t *testing.T) {
if len(scheduledYesterday) > 0 {
t.Fatal("no yesterday dates should be present in schedule")
}

todaySchedule := schedule.FilterByDate(today)
if len(todaySchedule) != 5 {
t.Fatal(fmt.Sprintf("today should have 5 schedule points, has: %d", len(todaySchedule)))
Expand Down
28 changes: 11 additions & 17 deletions pkg/scraper/schedule_datawriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func (p ScheduleDataWriter) processDonationDetails(tx *gorm.DB, donationDetails
}
station.Address = stationAddress

if err := tx.Save(&station).Error; err != nil {
log.Printf("Error while saving station: %v", err)
return err
}

if !p.isDatePassed(donation.DateDonation) {
var schedule = api.StationSchedule{}
if err := tx.FirstOrInit(&schedule, api.StationSchedule{
Expand All @@ -65,24 +70,13 @@ func (p ScheduleDataWriter) processDonationDetails(tx *gorm.DB, donationDetails
schedule.StationStatus = &[]api.StationStatus{{IsOpen: true}}
}

if station.StationSchedule == nil {
station.StationSchedule = &[]api.StationSchedule{schedule}
} else {
*station.StationSchedule = append(*station.StationSchedule, schedule)
if err := tx.Save(&schedule).Error; err != nil {
log.Printf("Error while saving schedule: %v", err)
return err
}
}

//Gorm save station, schedule, and status at this point. That's why we are taking schedule. ID only after.
if err := tx.Save(&station).Error; err != nil {
log.Printf("Error while saving station: %v", err)
return err
}

if station.StationSchedule != nil {
for _, schedule := range *station.StationSchedule {
if schedule.Id != nil {
schedulesIdsMap[*schedule.Id] = true
}
if schedule.Id != nil {
schedulesIdsMap[*schedule.Id] = true
}
}
}
Expand All @@ -93,7 +87,7 @@ func (p ScheduleDataWriter) processDonationDetails(tx *gorm.DB, donationDetails
}

var otherSchedules []api.StationSchedule
if err := tx.Not("id", schedulesIds).Where("DATE(date) >= CURRENT_DATE").Find(&otherSchedules).Error; err != nil {
if err := tx.Not("id", schedulesIds).Where("DATE(date) >= ?", p.SinceTime.Format("2006-01-02")).Find(&otherSchedules).Error; err != nil {
log.Printf("Error while searching other schedules: %v", err)
return err
}
Expand Down

0 comments on commit d7b92df

Please sign in to comment.