2026-04-14 19:59:32 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2026-04-14 23:06:50 +00:00
|
|
|
"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"
|
2026-04-14 19:59:32 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
2026-04-14 23:06:50 +00:00
|
|
|
//"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
|
|
|
|
//"github.com/Gleipnir-Technology/nidus-sync/platform/fieldseeker"
|
2026-04-14 19:59:32 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-04-14 23:06:50 +00:00
|
|
|
//"github.com/google/uuid"
|
|
|
|
|
"github.com/stephenafamo/scan"
|
2026-04-14 19:59:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ServiceRequestList(ctx context.Context, user User, limit int) ([]*types.ServiceRequest, error) {
|
2026-04-14 23:06:50 +00:00
|
|
|
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]())
|
2026-04-14 19:59:32 +00:00
|
|
|
if err != nil {
|
2026-04-14 23:06:50 +00:00
|
|
|
return nil, fmt.Errorf("query service requests: %w", err)
|
2026-04-14 19:59:32 +00:00
|
|
|
}
|
2026-04-14 23:06:50 +00:00
|
|
|
/*
|
|
|
|
|
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]
|
|
|
|
|
}
|
|
|
|
|
*/
|
2026-04-14 19:59:32 +00:00
|
|
|
return results, nil
|
|
|
|
|
}
|