diff --git a/platform/fieldseeker/point_location.go b/platform/fieldseeker/point_location.go new file mode 100644 index 00000000..fda86f87 --- /dev/null +++ b/platform/fieldseeker/point_location.go @@ -0,0 +1,26 @@ +package fieldseeker + +import ( + "context" + "fmt" + + //"github.com/Gleipnir-Technology/bob" + "github.com/Gleipnir-Technology/bob/dialect/psql" + "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/db/sql" + "github.com/google/uuid" +) + +func PointLocationList(ctx context.Context, point_location_ids []uuid.UUID) (models.FieldseekerPointlocationSlice, error) { + rows, err := models.FieldseekerPointlocations.Query( + sm.Where( + models.FieldseekerPointlocations.Columns.Globalid.EQ(psql.Any(point_location_ids)), + ), + ).All(ctx, db.PGInstance.BobDB) + if err != nil { + return nil, fmt.Errorf("query point locations: %w", err) + } + return rows, nil +} diff --git a/platform/service_request.go b/platform/service_request.go index 0b7d6517..b03adcc7 100644 --- a/platform/service_request.go +++ b/platform/service_request.go @@ -4,24 +4,83 @@ import ( "context" "fmt" - //"github.com/Gleipnir-Technology/bob/dialect/psql/sm" + "github.com/Gleipnir-Technology/bob" + "github.com/Gleipnir-Technology/bob/dialect/psql" + //"github.com/Gleipnir-Technology/bob/dialect/psql/dialect" + "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/db/models" + //"github.com/Gleipnir-Technology/nidus-sync/platform/fieldseeker" "github.com/Gleipnir-Technology/nidus-sync/platform/types" + //"github.com/google/uuid" + "github.com/stephenafamo/scan" ) 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) + query := psql.Select( + sm.Columns( + "COALESCE(sr.reqaddr1, '') AS \"address.raw\"", + "COALESCE(sr.assignedtech, '') AS \"assigned_technician\"", + "COALESCE(sr.reqcity, '') AS \"city\"", + "sr.creationdate AS \"created\"", + //"COALESCE(sr.h3cell, 0) AS \"h3cell\"", + "COALESCE(sr.dog, 0) AS \"has_dog\"", + "COALESCE(sr.spanish, 0) AS \"has_spanish_speaker\"", + "sr.globalid AS \"id\"", + "sr.priority AS \"priority\"", + "sr.recdatetime AS \"recorded_date\"", + "sr.source AS \"source\"", + "sr.reqtarget AS \"target\"", + "sr.reqzip AS \"zip\"", + "COALESCE(ST_X(pl.geospatial), 0) AS \"address.location.longitude\"", + "COALESCE(ST_Y(pl.geospatial), 0) AS \"address.location.latitude\"", + ), + sm.From("fieldseeker.servicerequest").As("sr"), + sm.LeftJoin("fieldseeker.pointlocation").As("pl").OnEQ( + psql.Quote("sr", "pointlocid"), + psql.Quote("pl", "globalid"), + ), + ) + results, err := bob.All(ctx, db.PGInstance.BobDB, query, scan.StructMapper[*types.ServiceRequest]()) 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 nil, fmt.Errorf("query service requests: %w", err) } + /* + service_requests, 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) + } + point_location_ids := make([]uuid.UUID, len(service_requests)) + for i, s := range service_requests { + p, ok := s.Pointlocid.Get() + if ok { + point_location_ids[i] = p + } + } + point_locations, err := fieldseeker.PointLocationList(ctx, point_location_ids) + if err != nil { + return nil, fmt.Errorf("list point locations: %w", err) + } + point_location_by_id := make(map[uuid.UUID]*models.FieldseekerPointlocation, len(point_locations)) + for _, pl := range point_locations { + point_location_by_id[pl.Globalid] = pl + } + results := make([]*types.ServiceRequest, len(service_requests)) + for i, s := range service_requests { + r := types.ServiceRequestFromModel(s) + loc_id, ok := s.Pointlocid.Get() + if ok { + pl, ok := point_location_by_id[loc_id] + if ok { + r.Location = types.LocationFromFS(pl) + } + } + results[i] = &r + point_location_ids[i] + } + */ return results, nil } diff --git a/platform/types/location.go b/platform/types/location.go index b5fda97f..95d73af1 100644 --- a/platform/types/location.go +++ b/platform/types/location.go @@ -3,6 +3,7 @@ package types import ( "fmt" + "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/Gleipnir-Technology/nidus-sync/h3utils" //"github.com/rs/zerolog/log" "github.com/uber/h3-go/v4" @@ -32,3 +33,6 @@ func (l Location) H3Cell() (*h3.Cell, error) { func (l Location) GeometryQuery() (string, error) { return fmt.Sprintf("ST_Point(%f, %f, 4326)", l.Longitude, l.Latitude), nil } +func LocationFromFS(pl *models.FieldseekerPointlocation) Location { + return Location{} +} diff --git a/platform/types/service_request.go b/platform/types/service_request.go index 0e66b5b8..f8203c80 100644 --- a/platform/types/service_request.go +++ b/platform/types/service_request.go @@ -10,29 +10,31 @@ import ( ) 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"` + Address Address `db:"address" json:"address"` + AssignedTechnician string `db:"assigned_technician" json:"assigned_technician"` + City string `db:"city" json:"city"` + Created time.Time `db:"created" json:"created"` + H3Cell int64 `db:"h3cell" json:"h3cell"` + HasDog *bool `db:"has_dog" json:"has_dog"` + HasSpanishSpeaker *bool `db:"has_spanish_speaker" json:"has_spanish_speaker"` + ID string `db:"id" json:"id"` + Priority string `db:"priority" json:"priority"` + RecordedDate string `db:"recorded_date" json:"recorded_date"` + Source string `db:"source" json:"source"` + Status string `db:"status" json:"status"` + Target string `db:"target" json:"target"` + Zip string `db:"zip" 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(""), + Address: Address{ + Raw: sr.Reqaddr1.GetOr(""), + }, AssignedTechnician: sr.Assignedtech.GetOr(""), City: sr.Reqcity.GetOr(""), - Created: formatTime(sr.Creationdate), + Created: sr.Creationdate.MustGet(), //H3Cell: sr.H3Cell, HasDog: toBool(sr.Dog), HasSpanishSpeaker: toBool(sr.Spanish), diff --git a/ts/components/ReviewPoolColumnDetail.vue b/ts/components/ReviewPoolColumnDetail.vue index 585b4aa7..f97efc95 100644 --- a/ts/components/ReviewPoolColumnDetail.vue +++ b/ts/components/ReviewPoolColumnDetail.vue @@ -38,7 +38,7 @@ :class="{ 'border-warning': poolLocation.longitude !== - selectedTask.pool?.location.longitude, + selectedTask.pool?.location?.longitude, }" />