2024-03-25 12:21:59 +00:00
|
|
|
package cif
|
2024-03-29 13:45:58 +00:00
|
|
|
|
2024-04-05 21:42:00 +01:00
|
|
|
import (
|
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/database"
|
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
|
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-04-04 22:39:09 +01:00
|
|
|
// Processes parsed CIF data and applies the data to the database
|
|
|
|
func processParsedCif(data *parsedData) error {
|
2024-04-05 21:42:00 +01:00
|
|
|
createTasks := make([]*upstreamApi.JsonScheduleV1, 0)
|
|
|
|
deleteTasks := make([]*upstreamApi.JsonScheduleV1, 0)
|
|
|
|
|
|
|
|
for _, item := range data.sched {
|
|
|
|
switch item.TransactionType {
|
|
|
|
case "Delete":
|
|
|
|
deleteTasks = append(deleteTasks, &item)
|
|
|
|
case "Create":
|
|
|
|
createTasks = append(createTasks, &item)
|
|
|
|
default:
|
|
|
|
log.Msg.Error("Unknown transaction type in CIF Schedule", zap.String("TransactionType", item.TransactionType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := doDeletions(deleteTasks)
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Error deleting CIF Entries", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = doCreations(createTasks)
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Error creating CIF Entries", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create delete query types and pass to the function which batches the deletions
|
|
|
|
func doDeletions(deletions []*upstreamApi.JsonScheduleV1) error {
|
|
|
|
log.Msg.Info("Preparing CIF update Delete tasks", zap.Int("Delete task count", len(deletions)))
|
|
|
|
|
|
|
|
deleteQueries := make([]database.DeleteQuery, 0)
|
|
|
|
for _, item := range deletions {
|
|
|
|
query := database.DeleteQuery{
|
|
|
|
ScheduleStartDate: ParseCifDate(item.ScheduleStartDate, "start"),
|
|
|
|
StpIndicator: item.CifStpIndicator,
|
|
|
|
TrainUid: item.CifTrainUid,
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteQueries = append(deleteQueries, query)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to the correct struct for the database and pass to the function which batches insertions
|
|
|
|
func doCreations(creations []*upstreamApi.JsonScheduleV1) error {
|
|
|
|
log.Msg.Info("Preparing CIF update Create tasks", zap.Int("Create task count", len(creations)))
|
|
|
|
|
|
|
|
createDocuments := make([]database.Service, 0)
|
|
|
|
for _, item := range creations {
|
|
|
|
document := database.Service{}
|
|
|
|
|
|
|
|
createDocuments = append(createDocuments, document)
|
|
|
|
}
|
2024-04-04 22:39:09 +01:00
|
|
|
return nil
|
2024-04-02 21:07:01 +01:00
|
|
|
}
|