Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

#13 add final destination name to train number #16

Merged
merged 2 commits into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions pkg/application/addtrainalarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (a *Application) AddTrainAlarm(ctx context.Context, cmd AddTrainAlarmCmd) e
cmd.StationEVA,
cmd.StationDate,
finalArrival,
train.FinalDestination,
)

_, err = a.repo.GetOrCreateTrainAlarm(ctx, alarm)
Expand Down
2 changes: 1 addition & 1 deletion pkg/application/deletetrainalarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestDeleteTrainAlarm(t *testing.T) {
app, repo := createTestCase(true)
ctx := context.Background()

alarm, err := trainalarm.NewTrainAlarm(uuid.New().String(), "telegram", "ice 4", 8503000, int64(1595797980000), time.Now())
alarm, err := trainalarm.NewTrainAlarm(uuid.New().String(), "telegram", "ice 4", 8503000, int64(1595797980000), time.Now(), "Berlin Ostbahnhof")
assert.Nil(t, err)
assert.NotNil(t, alarm)

Expand Down
2 changes: 2 additions & 0 deletions pkg/application/notifyuser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestNotifyUser(t *testing.T) {
8503000,
int64(1595797980000),
time.Now(),
"Berlin Ostbahnhof",
)
alarm.SetLastDelay(-100)
assert.Nil(t, err, testCase.Description)
Expand Down Expand Up @@ -103,6 +104,7 @@ func TestNotifyUsers(t *testing.T) {
8503000,
int64(1595797980000),
time.Now(),
"Berlin Ostbahnhof",
)
assert.Nil(t, err)
assert.NotNil(t, alarm)
Expand Down
1 change: 1 addition & 0 deletions pkg/application/updatetrainalarmthreshold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestUpdateTrainAlarmThreshold(t *testing.T) {
8503000,
int64(1595797980000),
time.Now(),
"Berlin Ostbahnhof",
)
assert.Nil(t, err)
assert.NotNil(t, alarm)
Expand Down
10 changes: 10 additions & 0 deletions pkg/domain/trainalarm/trainalarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type TrainAlarm struct {
stationEVA int
stationDate int64
finalArrivalAt time.Time
finalDestinationName string
delayThresholdMinutes int
lastNotificationAt *time.Time
lastDelayMinutes int
Expand All @@ -28,6 +29,7 @@ func NewTrainAlarm(
stationEVA int,
stationDate int64,
finalArrivalAt time.Time,
finalDestinationName string,
) (*TrainAlarm, error) {
return &TrainAlarm{
id: uuid.New().String(),
Expand All @@ -37,6 +39,7 @@ func NewTrainAlarm(
stationEVA: stationEVA,
stationDate: stationDate,
finalArrivalAt: finalArrivalAt,
finalDestinationName: finalDestinationName,
delayThresholdMinutes: 0,
lastDelayMinutes: 0,
}, nil
Expand All @@ -51,6 +54,7 @@ func NewTrainAlarmFromRepository(
stationEVA int,
stationDate int64,
finalArrivalAt time.Time,
finalDestinationName string,
delayThresholdMinutes int,
lastNotificationAt *time.Time,
lastDelayMinutes int,
Expand All @@ -63,6 +67,7 @@ func NewTrainAlarmFromRepository(
stationEVA: stationEVA,
stationDate: stationDate,
finalArrivalAt: finalArrivalAt,
finalDestinationName: finalDestinationName,
delayThresholdMinutes: delayThresholdMinutes,
lastNotificationAt: lastNotificationAt,
lastDelayMinutes: lastDelayMinutes,
Expand Down Expand Up @@ -110,6 +115,11 @@ func (t *TrainAlarm) GetFinalArrivalAt() time.Time {
return t.finalArrivalAt
}

// GetFinalDestinationName from train
func (t *TrainAlarm) GetFinalDestinationName() string {
return t.finalDestinationName
}

// GetLastNotificationAt returns nil if no notification is send yet
func (t *TrainAlarm) GetLastNotificationAt() *time.Time {
return t.lastNotificationAt
Expand Down
6 changes: 4 additions & 2 deletions pkg/domain/trainalarm/trainalarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestNewTrainAlarm(t *testing.T) {
alarm, err := NewTrainAlarm("identifyer", "telegram", "ice 6", 2342342, int64(234234234234), time.Now())
alarm, err := NewTrainAlarm("identifyer", "telegram", "ice 6", 2342342, int64(234234234234), time.Now(), "Berlin Ostbahnhof")
assert.Nil(t, err)
assert.NotNil(t, alarm)
}
Expand All @@ -21,7 +21,8 @@ func TestTrainAlarmGetters(t *testing.T) {
stationEVA := 2342342
stationDate := int64(234234234234)
finalArrivalAt := time.Now()
alarm, err := NewTrainAlarm(identifyer, plattform, trainName, stationEVA, stationDate, finalArrivalAt)
finalDestinationName := "Berlin Ostbahnhof"
alarm, err := NewTrainAlarm(identifyer, plattform, trainName, stationEVA, stationDate, finalArrivalAt, finalDestinationName)
assert.Nil(t, err)
assert.NotNil(t, alarm)

Expand All @@ -31,6 +32,7 @@ func TestTrainAlarmGetters(t *testing.T) {
assert.Equal(t, stationEVA, alarm.GetStationEVA())
assert.Equal(t, stationDate, alarm.GetStationDate())
assert.Equal(t, finalArrivalAt, alarm.GetFinalArrivalAt())
assert.Equal(t, finalDestinationName, alarm.GetFinalDestinationName())
assert.Nil(t, alarm.GetLastNotificationAt())
alarm.SetSuccessfulNotification()
assert.NotNil(t, alarm.GetLastNotificationAt())
Expand Down
3 changes: 3 additions & 0 deletions pkg/infrastructure/repository/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SQLTrainAlarmModel struct {
StationEVA int
StationDate int64
FinalArrivalAt time.Time
FinalDestinationName string
DelayThresholdMinutes int
LastNotificationAt *time.Time
LastDelayMinutes int
Expand All @@ -54,6 +55,7 @@ func NewSQLTrainAlarmModel(alarm *trainalarm.TrainAlarm) *SQLTrainAlarmModel {
StationEVA: alarm.GetStationEVA(),
StationDate: alarm.GetStationDate(),
FinalArrivalAt: alarm.GetFinalArrivalAt(),
FinalDestinationName: alarm.GetFinalDestinationName(),
DelayThresholdMinutes: alarm.GetDelayThresholdMinutes(),
LastNotificationAt: alarm.GetLastNotificationAt(),
LastDelayMinutes: alarm.GetLastDelay(),
Expand All @@ -70,6 +72,7 @@ func (s *SQLTrainAlarmModel) TrainAlarm() *trainalarm.TrainAlarm {
s.StationEVA,
s.StationDate,
s.FinalArrivalAt,
s.FinalDestinationName,
s.DelayThresholdMinutes,
s.LastNotificationAt,
s.LastDelayMinutes,
Expand Down
6 changes: 3 additions & 3 deletions pkg/infrastructure/repository/gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNewSQLDatabase(t *testing.T) {
}

func TestConvertModel(t *testing.T) {
alarm, err := trainalarm.NewTrainAlarm("mychatID", "telegram", "ICE 7", 123456, 1234567890, time.Now())
alarm, err := trainalarm.NewTrainAlarm("mychatID", "telegram", "ICE 7", 123456, 1234567890, time.Now(), "Berlin Ostbahnhof")
assert.NotNil(t, alarm)
assert.Nil(t, err)

Expand Down Expand Up @@ -127,7 +127,7 @@ func TestSQLGetTrainAlarms(t *testing.T) {
assert.Len(t, alarms, 0)

for i := 0; i < 4; i++ {
alarm, err := trainalarm.NewTrainAlarm("1234", "telegram", fmt.Sprintf("ice %d", i), i, int64(i), time.Now())
alarm, err := trainalarm.NewTrainAlarm("1234", "telegram", fmt.Sprintf("ice %d", i), i, int64(i), time.Now(), "Berlin Ostbahnhof")
assert.Nil(t, err)
if i > 1 {
alarm.SetSuccessfulNotification()
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestSQLGetTrainAlarmsSortByLastNotificationAt(t *testing.T) {
assert.Len(t, alarms, 0)

for i := 0; i < 4; i++ {
alarm, err := trainalarm.NewTrainAlarm("identifyer", "telegram", fmt.Sprintf("ice %d", i), i, int64(i), time.Now())
alarm, err := trainalarm.NewTrainAlarm("identifyer", "telegram", fmt.Sprintf("ice %d", i), i, int64(i), time.Now(), "Berlin Ostbahnhof")
assert.Nil(t, err)
if i > 1 {
alarm.SetSuccessfulNotification()
Expand Down
5 changes: 3 additions & 2 deletions pkg/infrastructure/repository/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestGetTrainAlarms(t *testing.T) {
assert.Len(t, alarms, 0)

for i := 0; i < 4; i++ {
alarm, err := trainalarm.NewTrainAlarm("1234", "telegram", fmt.Sprintf("ice %d", i), i, int64(i), time.Now())
alarm, err := trainalarm.NewTrainAlarm("1234", "telegram", fmt.Sprintf("ice %d", i), i, int64(i), time.Now(), "Berlin Ostbahnhof")
assert.Nil(t, err)
if i > 1 {
alarm.SetSuccessfulNotification()
Expand All @@ -96,7 +96,7 @@ func TestGetTrainAlarmsSortByLastNotificationAt(t *testing.T) {
assert.Len(t, alarms, 0)

for i := 0; i < 4; i++ {
alarm, err := trainalarm.NewTrainAlarm("identifyer", "telegram", fmt.Sprintf("ice %d", i), i, int64(i), time.Now())
alarm, err := trainalarm.NewTrainAlarm("identifyer", "telegram", fmt.Sprintf("ice %d", i), i, int64(i), time.Now(), "Berlin Ostbahnhof")
assert.Nil(t, err)
if i > 1 {
alarm.SetSuccessfulNotification()
Expand Down Expand Up @@ -207,6 +207,7 @@ func createTestTrainAlarm(finalArrival time.Time) *trainalarm.TrainAlarm {
11111111,
int64(12432342342334234),
finalArrival,
"Berlin Ostbahnhof",
)
return alarm
}
2 changes: 1 addition & 1 deletion pkg/interface/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *CronJob) NotifyUsers(ctx context.Context) {
c.application.NotifyUsers(ctx, func(ctx context.Context, alarm *trainalarm.TrainAlarm, train marudor.HafasTrain, diff time.Duration) error {
tctx := telegramconversation.NewTContext(alarm.GetIdentifyer())

txt := fmt.Sprintf("Zug %s hat %s Verspätung.", alarm.GetTrainName(), diff.String())
txt := fmt.Sprintf("Zug %s hat `%s` Verspätung.", alarm.GetTrainName(), diff.String())

tctx.Send(txt)

Expand Down
2 changes: 1 addition & 1 deletion pkg/interface/telegram/alarmmenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (t *TelegramService) AlarmMenu(ctx telegramconversation.TContext) telegramc
return ctx.SendWithState("Alarm nicht gefunden.", "start")
}

txt := fmt.Sprintf("Was möchtest du für %s (Alarm ab %dm) ändern?", alarm.GetTrainName(), alarm.GetDelayThresholdMinutes())
txt := fmt.Sprintf("Was möchtest du für `%s > %s` (Alarm ab `%dm`) ändern?", alarm.GetTrainName(), alarm.GetFinalDestinationName(), alarm.GetDelayThresholdMinutes())
buttons := []telegramconversation.TButton{
telegramconversation.NewTButton("Alarm ab ...", fmt.Sprintf("editdelay|%s", alarm.GetID())),
telegramconversation.NewTButton("Löschen", fmt.Sprintf("deletealarm|%s", alarm.GetID())),
Expand Down
2 changes: 1 addition & 1 deletion pkg/interface/telegram/deletealarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func (t *TelegramService) DeleteAlarm(ctx telegramconversation.TContext) telegra
}
alarm, _ := t.application.DeleteTrainAlarm(context.Background(), cmd)

return ctx.SendWithState(fmt.Sprintf("Alarm %s gelöscht.", alarm.GetTrainName()), "start")
return ctx.SendWithState(fmt.Sprintf("Alarm `%s > %s` gelöscht.", alarm.GetTrainName(), alarm.GetFinalDestinationName()), "start")
}
2 changes: 1 addition & 1 deletion pkg/interface/telegram/editdelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ func (t *TelegramService) EditDelay(ctx telegramconversation.TContext) telegramc
}

ctx.SetStatePayload(alarm.GetID())
return ctx.SendWithState(fmt.Sprintf("Ab wie viel Abweichung von %s soll bescheid gesagt werden? z.B. 1h5min oder 10m", alarm.GetTrainName()), "savedelay")
return ctx.SendWithState(fmt.Sprintf("Ab wie viel Abweichung von `%s > %s` soll bescheid gesagt werden? z.B. 1h5min oder 10m", alarm.GetTrainName(), alarm.GetFinalDestinationName()), "savedelay")
}
3 changes: 2 additions & 1 deletion pkg/interface/telegram/listtrainalarms.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (t *TelegramService) ListTrainAlarms(ctx telegramconversation.TContext) tel
txt := "Welcher Alarm soll bearbeitet werden?"
buttons := []telegramconversation.TButton{}
for _, alarm := range alarms {
button := telegramconversation.NewTButton(alarm.GetTrainName(), fmt.Sprintf("alarm|%s", alarm.GetID()))
trainName := fmt.Sprintf("%s > %s", alarm.GetTrainName(), alarm.GetFinalDestinationName())
button := telegramconversation.NewTButton(trainName, fmt.Sprintf("alarm|%s", alarm.GetID()))
buttons = append(buttons, button)
}
buttons = append(buttons, telegramconversation.NewTButton("Abbruch", "cancel"))
Expand Down
3 changes: 2 additions & 1 deletion pkg/interface/telegram/newalarmselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func (t *TelegramService) NewAlarmSelect(ctx telegramconversation.TContext) tele

buttons := []telegramconversation.TButton{}
for _, train := range *results {
button := telegramconversation.NewTButton(train.Train.Name, fmt.Sprintf("savealarm|%s %s|%s|%d", train.Train.Type, train.Train.Number, train.FirstStop.Station.ID, train.FirstStop.Departure.ScheduledTime))
trainName := fmt.Sprintf("%s > %s", train.Train.Name, train.LastStop.Station.Title)
button := telegramconversation.NewTButton(trainName, fmt.Sprintf("savealarm|%s %s|%s|%d", train.Train.Type, train.Train.Number, train.FirstStop.Station.ID, train.FirstStop.Departure.ScheduledTime))
buttons = append(buttons, button)
}
buttons = append(buttons, telegramconversation.NewTButton("Abbruch", "cancel"))
Expand Down
2 changes: 1 addition & 1 deletion pkg/interface/telegram/savealarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ func (t *TelegramService) SaveAlarm(ctx telegramconversation.TContext) telegramc
return ctx.SendWithState("Irgendetwas ist schief gelaufen. :/", "start")
}

txt := fmt.Sprintf("Neuer Alarm %s hinzugefügt. Über /myalarms kann die erlaubte Abweichung geändert werden.", trainName)
txt := fmt.Sprintf("Neuer Alarm `%s` hinzugefügt. Über /myalarms kann die erlaubte Abweichung geändert werden.", trainName)
return ctx.SendWithState(txt, "start")
}