diff --git a/api/api.go b/api/api.go index 6224ef84..e0b0e702 100644 --- a/api/api.go +++ b/api/api.go @@ -93,7 +93,7 @@ func handleClientIos(w http.ResponseWriter, r *http.Request, u *models.User) { } response := ResponseClientIos{ - Fieldseeker: toResponseFieldseeker(csync), + Fieldseeker: toResponseFieldseeker(csync.Fieldseeker), } if err := render.Render(w, r, response); err != nil { render.Render(w, r, errRender(err)) @@ -345,4 +345,3 @@ func parseTime(x string) (*time.Time, error) { created := time.UnixMilli(created_epoch) return &created, nil } - diff --git a/api/types.go b/api/types.go index 154c061f..56a151bc 100644 --- a/api/types.go +++ b/api/types.go @@ -334,8 +334,12 @@ func NewResponseTrapData(data *models.FieldseekerTraplocationSlice) []ResponseTr return results } -func toResponseFieldseeker(csync platform.ClientSync) ResponseFieldseeker { - return ResponseFieldseeker{} +func toResponseFieldseeker(sync platform.FieldseekerRecordsSync) ResponseFieldseeker { + return ResponseFieldseeker{ + MosquitoSources: NewResponseMosquitoSources(sync.MosquitoSources), + ServiceRequests: NewResponseServiceRequests(sync.ServiceRequests), + TrapData: NewResponseTrapData(sync.TrapData), + } } func formatTime(t null.Val[time.Time]) string { diff --git a/platform/ios.go b/platform/ios.go index 2fe2fac5..bc760e37 100644 --- a/platform/ios.go +++ b/platform/ios.go @@ -2,18 +2,71 @@ package platform import ( "context" + "fmt" "time" "github.com/Gleipnir-Technology/nidus-sync/db" "github.com/Gleipnir-Technology/nidus-sync/db/models" + "github.com/google/uuid" ) func fieldseeker(ctx context.Context, u *models.User, since *time.Time) (fsync FieldseekerRecordsSync, err error) { pl, err := u.R.Organization.Pointlocations().All(ctx, db.PGInstance.BobDB) if err != nil { - return + return fsync, fmt.Errorf("Failed to get point locations: %w", err) } - fsync.MosquitoSources = pl + inspections, err := u.R.Organization.Mosquitoinspections().All(ctx, db.PGInstance.BobDB) + 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 + } + treatments, err := u.R.Organization.Treatments().All(ctx, db.PGInstance.BobDB) + 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 + } + sources := make([]*MosquitoSource, 0) + 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{ + PointLocation: p, + Inspections: &inspections, + Treatments: &treatments, + } + sources = append(sources, &ms) + } + fsync.MosquitoSources = &sources return fsync, err } diff --git a/platform/type.go b/platform/type.go index 0ab884f5..8d0825f9 100644 --- a/platform/type.go +++ b/platform/type.go @@ -1,4 +1,5 @@ package platform + import ( "time" @@ -7,15 +8,17 @@ import ( type ClientSync struct { Fieldseeker FieldseekerRecordsSync - Since time.Time + Since time.Time } type FieldseekerRecordsSync struct { - MosquitoSources models.FieldseekerPointlocationSlice + MosquitoSources *[]*MosquitoSource + ServiceRequests *models.FieldseekerServicerequestSlice + TrapData *models.FieldseekerTraplocationSlice } type MosquitoSource struct { PointLocation *models.FieldseekerPointlocation - Inspections *models.FieldseekerMosquitoinspectionSlice - Treatments *models.FieldseekerTreatmentSlice + Inspections *models.FieldseekerMosquitoinspectionSlice + Treatments *models.FieldseekerTreatmentSlice }