Add a resource for getting service requests

This commit is contained in:
Eli Ribble 2026-04-14 19:59:32 +00:00
parent 28ec1c3d67
commit 4a440e3022
No known key found for this signature in database
18 changed files with 387 additions and 51 deletions

View file

@ -10,6 +10,23 @@ import (
"github.com/google/uuid"
)
type ClientSync struct {
Fieldseeker FieldseekerRecordsSync
Since time.Time
}
type FieldseekerRecordsSync struct {
MosquitoSources []MosquitoSource
ServiceRequests models.FieldseekerServicerequestSlice
TrapData models.FieldseekerTraplocationSlice
}
type MosquitoSource struct {
PointLocation models.FieldseekerPointlocation
Inspections models.FieldseekerMosquitoinspectionSlice
Treatments models.FieldseekerTreatmentSlice
}
func getFieldseekerRecordsSync(ctx context.Context, u User, since *time.Time) (fsync FieldseekerRecordsSync, err error) {
db_connection := db.PGInstance.BobDB
pl, err := u.Organization.model.Pointlocations().All(ctx, db_connection)

View file

@ -11,13 +11,14 @@ import (
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/platform/geom"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/rs/zerolog/log"
)
// Create a lead from the given signal and site
func LeadCreate(ctx context.Context, user User, signal_id int32, site_id int32, pool_location *Location) (*int32, error) {
func LeadCreate(ctx context.Context, user User, signal_id int32, site_id int32, pool_location *types.Location) (*int32, error) {
txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil)
defer txn.Rollback(ctx)
if err != nil {

View file

@ -0,0 +1,27 @@
package platform
import (
"context"
"fmt"
//"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
)
func ServiceRequestList(ctx context.Context, user User, limit int) ([]*types.ServiceRequest, error) {
syncs, err := models.FieldseekerServicerequests.Query(
models.SelectWhere.FieldseekerServicerequests.OrganizationID.EQ(user.Organization.ID),
//sm.OrderBy(models.FieldseekerServicerequests.Columns.Created).Desc(),
).All(ctx, db.PGInstance.BobDB)
if err != nil {
return nil, fmt.Errorf("query sync: %w", err)
}
results := make([]*types.ServiceRequest, len(syncs))
for i, s := range syncs {
r := types.ServiceRequestFromModel(s)
results[i] = &r
}
return results, nil
}

View file

@ -76,7 +76,7 @@ func SignalCreateFromPublicreport(ctx context.Context, user User, report_id stri
} else if report.LocationLatitude.IsValue() && report.LocationLongitude.IsValue() {
lat := report.LocationLatitude.MustGet()
lng := report.LocationLongitude.MustGet()
site, err := siteFromLocation(ctx, txn, user, Location{
site, err := siteFromLocation(ctx, txn, user, types.Location{
Latitude: lat,
Longitude: lng,
})

View file

@ -14,7 +14,7 @@ import (
"github.com/Gleipnir-Technology/nidus-sync/db/models"
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
"github.com/Gleipnir-Technology/nidus-sync/platform/geocode"
//"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/stephenafamo/scan"
@ -89,7 +89,7 @@ func siteFromAddressRaw(ctx context.Context, txn bob.Tx, user User, address stri
}
return siteFromAddress(ctx, txn, user, a.ID)
}
func siteFromLocation(ctx context.Context, txn bob.Tx, user User, location Location) (*models.Site, error) {
func siteFromLocation(ctx context.Context, txn bob.Tx, user User, location types.Location) (*models.Site, error) {
// Reverse geocode at the location
resp, err := geocode.ReverseGeocode(ctx, location)
if err != nil {

27
platform/sync.go Normal file
View file

@ -0,0 +1,27 @@
package platform
import (
"context"
"fmt"
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
)
func SyncList(ctx context.Context, user User, limit int) ([]*types.Sync, error) {
syncs, err := models.FieldseekerSyncs.Query(
models.SelectWhere.FieldseekerSyncs.OrganizationID.EQ(user.Organization.ID),
sm.OrderBy(models.FieldseekerSyncs.Columns.Created).Desc(),
).All(ctx, db.PGInstance.BobDB)
if err != nil {
return nil, fmt.Errorf("query sync: %w", err)
}
results := make([]*types.Sync, len(syncs))
for i, s := range syncs {
r := types.SyncFromModel(s)
results[i] = &r
}
return results, nil
}

View file

@ -0,0 +1,71 @@
package types
import (
"net/http"
"time"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/aarondl/opt/null"
//"github.com/google/uuid"
)
type ServiceRequest struct {
Address string `json:"address"`
AssignedTechnician string `json:"assigned_technician"`
City string `json:"city"`
Created string `json:"created"`
H3Cell int64 `json:"h3cell"`
HasDog *bool `json:"has_dog"`
HasSpanishSpeaker *bool `json:"has_spanish_speaker"`
ID string `json:"id"`
Priority string `json:"priority"`
RecordedDate string `json:"recorded_date"`
Source string `json:"source"`
Status string `json:"status"`
Target string `json:"target"`
Zip string `json:"zip"`
}
func ServiceRequestFromModel(sr *models.FieldseekerServicerequest) ServiceRequest {
//log.Debug().Int32("id", m.ID).Float64("lat", m.LocationLatitude.GetOr(0.0)).Float64("lng", m.LocationLongitude.GetOr(0.0)).Msg("converting address")
return ServiceRequest{
Address: sr.Reqaddr1.GetOr(""),
AssignedTechnician: sr.Assignedtech.GetOr(""),
City: sr.Reqcity.GetOr(""),
Created: formatTime(sr.Creationdate),
//H3Cell: sr.H3Cell,
HasDog: toBool(sr.Dog),
HasSpanishSpeaker: toBool(sr.Spanish),
ID: sr.Globalid.String(),
Priority: sr.Priority.GetOr(""),
Status: sr.Status.GetOr(""),
Source: sr.Source.GetOr(""),
Target: sr.Reqtarget.GetOr(""),
Zip: sr.Reqzip.GetOr(""),
}
}
func (srr ServiceRequest) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
func formatTime(t null.Val[time.Time]) string {
if t.IsNull() {
return ""
}
v := t.MustGet()
return v.Format("2006-01-02T15:04:05.000Z")
}
func toBool(t null.Val[int32]) *bool {
if t.IsNull() {
return nil
}
val := t.MustGet()
var b bool
if val == 0 {
b = false
} else {
b = true
}
return &b
}

28
platform/types/sync.go Normal file
View file

@ -0,0 +1,28 @@
package types
import (
"time"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
)
type Sync struct {
Created time.Time `json:"created"`
ID int32 `json:"id"`
OrganizationID int32 `json:"organization_id"`
RecordsCreated int32 `json:"records_created"`
RecordsUnchanged int32 `json:"records_unchanged"`
RecordsUpdated int32 `json:"records_updated"`
}
func SyncFromModel(m *models.FieldseekerSync) Sync {
//log.Debug().Int32("id", m.ID).Float64("lat", m.LocationLatitude.GetOr(0.0)).Float64("lng", m.LocationLongitude.GetOr(0.0)).Msg("converting address")
return Sync{
Created: m.Created,
ID: m.ID,
OrganizationID: m.OrganizationID,
RecordsCreated: m.RecordsCreated,
RecordsUnchanged: m.RecordsUnchanged,
RecordsUpdated: m.RecordsUpdated,
}
}