2026-01-04 17:30:28 -07:00
|
|
|
package platform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-01-06 03:06:38 +00:00
|
|
|
"fmt"
|
2026-01-04 17:30:28 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
2026-01-06 03:06:38 +00:00
|
|
|
"github.com/google/uuid"
|
2026-01-04 17:30:28 -07:00
|
|
|
)
|
|
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
func getFieldseekerRecordsSync(ctx context.Context, u User, since *time.Time) (fsync FieldseekerRecordsSync, err error) {
|
2026-01-06 16:21:59 +00:00
|
|
|
db_connection := db.PGInstance.BobDB
|
2026-03-12 23:49:16 +00:00
|
|
|
pl, err := u.Organization.model.Pointlocations().All(ctx, db_connection)
|
2026-01-04 17:30:28 -07:00
|
|
|
if err != nil {
|
2026-01-06 03:06:38 +00:00
|
|
|
return fsync, fmt.Errorf("Failed to get point locations: %w", err)
|
2026-01-04 17:30:28 -07:00
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
inspections, err := u.Organization.model.Mosquitoinspections().All(ctx, db.PGInstance.BobDB)
|
2026-01-06 03:06:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fsync, fmt.Errorf("Failed to get mosquito inspections: %w", err)
|
|
|
|
|
}
|
|
|
|
|
inspections_by_location := make(map[uuid.UUID]models.FieldseekerMosquitoinspectionSlice, 0)
|
|
|
|
|
for _, i := range inspections {
|
|
|
|
|
if i.Pointlocid.IsNull() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
locid := i.Pointlocid.MustGet()
|
|
|
|
|
insp, ok := inspections_by_location[locid]
|
|
|
|
|
if !ok {
|
|
|
|
|
insp = make(models.FieldseekerMosquitoinspectionSlice, 0)
|
|
|
|
|
}
|
|
|
|
|
insp = append(insp, i)
|
|
|
|
|
inspections_by_location[locid] = insp
|
|
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
treatments, err := u.Organization.model.Treatments().All(ctx, db.PGInstance.BobDB)
|
2026-01-06 03:06:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fsync, fmt.Errorf("Failed to get treatment data: %w", err)
|
|
|
|
|
}
|
|
|
|
|
treatments_by_location := make(map[uuid.UUID]models.FieldseekerTreatmentSlice, 0)
|
|
|
|
|
for _, t := range treatments {
|
|
|
|
|
if t.Pointlocid.IsNull() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
locid := t.Pointlocid.MustGet()
|
|
|
|
|
ts, ok := treatments_by_location[locid]
|
|
|
|
|
if !ok {
|
|
|
|
|
ts = make(models.FieldseekerTreatmentSlice, 0)
|
|
|
|
|
}
|
|
|
|
|
ts = append(ts, t)
|
|
|
|
|
treatments_by_location[locid] = ts
|
|
|
|
|
}
|
2026-01-06 22:23:59 +00:00
|
|
|
sources := make([]MosquitoSource, 0)
|
2026-01-06 03:06:38 +00:00
|
|
|
for _, p := range pl {
|
|
|
|
|
inspections, ok := inspections_by_location[p.Globalid]
|
|
|
|
|
if !ok {
|
|
|
|
|
inspections = make(models.FieldseekerMosquitoinspectionSlice, 0)
|
|
|
|
|
}
|
|
|
|
|
treatments, ok := treatments_by_location[p.Globalid]
|
|
|
|
|
if !ok {
|
|
|
|
|
treatments = make(models.FieldseekerTreatmentSlice, 0)
|
|
|
|
|
}
|
|
|
|
|
ms := MosquitoSource{
|
2026-01-06 22:23:59 +00:00
|
|
|
PointLocation: *p,
|
|
|
|
|
Inspections: inspections,
|
|
|
|
|
Treatments: treatments,
|
2026-01-06 03:06:38 +00:00
|
|
|
}
|
2026-01-06 22:23:59 +00:00
|
|
|
sources = append(sources, ms)
|
2026-01-06 03:06:38 +00:00
|
|
|
}
|
2026-01-06 22:23:59 +00:00
|
|
|
fsync.MosquitoSources = sources
|
2026-01-04 17:30:28 -07:00
|
|
|
return fsync, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
func ContentClientIos(ctx context.Context, u User, since *time.Time) (csync ClientSync, err error) {
|
|
|
|
|
fsync, err := getFieldseekerRecordsSync(ctx, u, since)
|
2026-01-04 17:30:28 -07:00
|
|
|
return ClientSync{
|
|
|
|
|
Fieldseeker: fsync,
|
|
|
|
|
}, err
|
|
|
|
|
}
|