2026-03-04 18:29:52 +00:00
|
|
|
package platform
|
|
|
|
|
|
2026-03-05 01:22:21 +00:00
|
|
|
import (
|
2026-03-12 23:49:16 +00:00
|
|
|
"context"
|
2026-03-20 05:20:37 +00:00
|
|
|
"encoding/json"
|
2026-03-12 23:49:16 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
2026-03-05 01:22:21 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
2026-04-02 01:07:55 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-03-12 23:49:16 +00:00
|
|
|
//"github.com/google/uuid"
|
2026-03-05 01:22:21 +00:00
|
|
|
)
|
|
|
|
|
|
2026-03-04 18:29:52 +00:00
|
|
|
type Organization struct {
|
2026-04-02 01:07:55 +00:00
|
|
|
ID int32 `json:"id"`
|
|
|
|
|
ServiceArea *types.ServiceArea `json:"service_area"`
|
2026-03-12 23:49:16 +00:00
|
|
|
|
|
|
|
|
model *models.Organization
|
2026-03-04 18:29:52 +00:00
|
|
|
}
|
2026-03-05 01:22:21 +00:00
|
|
|
|
2026-03-12 23:49:16 +00:00
|
|
|
func (o Organization) ArcgisAccountID() string {
|
|
|
|
|
if o.model.ArcgisAccountID.IsNull() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return o.model.ArcgisAccountID.MustGet()
|
|
|
|
|
}
|
|
|
|
|
func (o Organization) CountServiceRequest(ctx context.Context) (uint, error) {
|
|
|
|
|
result, err := o.model.Servicerequests().Count(ctx, db.PGInstance.BobDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("get service request count: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return uint(result), nil
|
|
|
|
|
}
|
|
|
|
|
func (o Organization) CountSource(ctx context.Context) (uint, error) {
|
|
|
|
|
result, err := o.model.Pointlocations().Count(ctx, db.PGInstance.BobDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("get source count: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return uint(result), nil
|
|
|
|
|
}
|
|
|
|
|
func (o Organization) CountTrap(ctx context.Context) (uint, error) {
|
|
|
|
|
result, err := o.model.Traplocations().Count(ctx, db.PGInstance.BobDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("get trap count: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return uint(result), nil
|
|
|
|
|
}
|
2026-04-23 15:50:58 +00:00
|
|
|
func (o Organization) FieldseekerSyncLatest(ctx context.Context) (*models.FieldseekerSync, error) {
|
|
|
|
|
sync, err := o.model.FieldseekerSyncs(sm.OrderBy("created").Desc()).One(ctx, db.PGInstance.BobDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err.Error() == "sql: no rows in result set" {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("get syncs: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return sync, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 03:19:58 +00:00
|
|
|
func (o Organization) HasServiceArea() bool {
|
|
|
|
|
return o.model.ServiceAreaGeometry.IsValue()
|
|
|
|
|
}
|
2026-03-19 03:25:36 +00:00
|
|
|
func (o Organization) IsCatchall() bool {
|
|
|
|
|
return o.model.IsCatchall
|
|
|
|
|
}
|
2026-04-23 15:50:58 +00:00
|
|
|
func (o Organization) IsSyncOngoing() bool {
|
|
|
|
|
return IsSyncOngoing(o.ID)
|
|
|
|
|
}
|
|
|
|
|
func (o Organization) LobAddressID() string {
|
|
|
|
|
return o.model.LobAddressID.GetOr("")
|
|
|
|
|
}
|
2026-03-20 05:20:37 +00:00
|
|
|
func (o Organization) MarshalJSON() ([]byte, error) {
|
|
|
|
|
to_marshal := map[string]any{}
|
2026-03-22 09:54:02 +00:00
|
|
|
to_marshal["id"] = o.ID
|
2026-03-20 05:20:37 +00:00
|
|
|
to_marshal["name"] = o.Name()
|
2026-03-22 09:54:02 +00:00
|
|
|
to_marshal["service_area"] = o.ServiceArea
|
2026-04-23 15:50:58 +00:00
|
|
|
to_marshal["lob_address_id"] = o.model.LobAddressID
|
2026-03-20 05:20:37 +00:00
|
|
|
return json.Marshal(to_marshal)
|
|
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
func (o Organization) Name() string {
|
|
|
|
|
return o.model.Name
|
|
|
|
|
}
|
2026-04-07 16:25:14 +00:00
|
|
|
func (o Organization) PhoneOffice() string {
|
|
|
|
|
return o.model.OfficePhone.GetOr("")
|
|
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
func (o Organization) ServiceRequestRecent(ctx context.Context) ([]*models.FieldseekerServicerequest, error) {
|
|
|
|
|
results, err := o.model.Servicerequests(sm.OrderBy("creationdate").Desc(), sm.Limit(10)).All(ctx, db.PGInstance.BobDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []*models.FieldseekerServicerequest{}, fmt.Errorf("query service request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return results, nil
|
|
|
|
|
}
|
2026-04-03 18:17:19 +00:00
|
|
|
func (o Organization) Slug() string {
|
|
|
|
|
return o.model.Slug.GetOr("")
|
|
|
|
|
}
|
2026-04-08 17:49:32 +00:00
|
|
|
func (o Organization) Website() string {
|
|
|
|
|
return o.model.Website.GetOr("")
|
|
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
func OrganizationByID(ctx context.Context, id int) (*Organization, error) {
|
|
|
|
|
org, err := models.FindOrganization(ctx, db.PGInstance.BobDB, int32(id))
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err.Error() == "sql: no rows in result set" {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("query org: %w", err)
|
|
|
|
|
}
|
|
|
|
|
o := newOrganization(org)
|
|
|
|
|
return &o, nil
|
|
|
|
|
}
|
2026-04-03 18:17:19 +00:00
|
|
|
func OrganizationList(ctx context.Context) ([]*Organization, error) {
|
2026-04-01 20:22:15 +00:00
|
|
|
rows, err := models.Organizations.Query().All(ctx, db.PGInstance.BobDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("query orgs: %w", err)
|
|
|
|
|
}
|
|
|
|
|
results := make([]*Organization, len(rows))
|
|
|
|
|
for i, row := range rows {
|
|
|
|
|
o := newOrganization(row)
|
|
|
|
|
results[i] = &o
|
|
|
|
|
}
|
|
|
|
|
return results, err
|
|
|
|
|
}
|
2026-03-12 23:49:16 +00:00
|
|
|
func newOrganization(org *models.Organization) Organization {
|
2026-04-02 01:07:55 +00:00
|
|
|
var sa *types.ServiceArea
|
2026-03-22 01:22:44 +00:00
|
|
|
if org.ServiceAreaXmax.IsValue() &&
|
|
|
|
|
org.ServiceAreaXmin.IsValue() &&
|
|
|
|
|
org.ServiceAreaYmax.IsValue() &&
|
|
|
|
|
org.ServiceAreaYmin.IsValue() {
|
2026-04-02 01:07:55 +00:00
|
|
|
sa = &types.ServiceArea{
|
|
|
|
|
Min: types.Location{
|
|
|
|
|
Longitude: org.ServiceAreaXmin.MustGet(),
|
|
|
|
|
Latitude: org.ServiceAreaYmin.MustGet(),
|
2026-03-22 01:22:44 +00:00
|
|
|
},
|
2026-04-02 01:07:55 +00:00
|
|
|
Max: types.Location{
|
|
|
|
|
Longitude: org.ServiceAreaXmax.MustGet(),
|
|
|
|
|
Latitude: org.ServiceAreaYmax.MustGet(),
|
2026-03-22 01:22:44 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-05 01:22:21 +00:00
|
|
|
return Organization{
|
2026-03-22 01:22:44 +00:00
|
|
|
ID: org.ID,
|
|
|
|
|
ServiceArea: sa,
|
|
|
|
|
model: org,
|
2026-03-05 01:22:21 +00:00
|
|
|
}
|
|
|
|
|
}
|