Return communication database rows from communication API

This is a pretty big refactor of how communication works to start moving
us in the direction we want to go long-term. This adds the new
communication row and migrates existing reports to add rows for
communication.

There's also a bunch of automatic fixes from the new linter. I should
have added them separately, but whatever.
This commit is contained in:
Eli Ribble 2026-05-01 20:49:37 +00:00
parent a6ce0b7e67
commit a82732a49c
No known key found for this signature in database
41 changed files with 365 additions and 220 deletions

View file

@ -7,6 +7,7 @@ import (
"time"
"github.com/Gleipnir-Technology/nidus-sync/config"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model"
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
"github.com/Gleipnir-Technology/nidus-sync/platform"
"github.com/google/uuid"
@ -25,14 +26,10 @@ func Communication(r *router) *communicationR {
}
type communication struct {
Created time.Time `json:"created"`
ID string `json:"id"`
PublicReport string `json:"public_report"`
Source string `json:"source"`
Type string `json:"type"`
}
type communicationList struct {
Communications []communication `json:"communications"`
Created time.Time `json:"created"`
ID int32 `json:"id"`
Source string `json:"source"`
Type string `json:"type"`
}
func toImageURLs(m map[string][]uuid.UUID, id string) []string {
@ -46,24 +43,61 @@ func toImageURLs(m map[string][]uuid.UUID, id string) []string {
}
return urls
}
func (res *communicationR) List(ctx context.Context, r *http.Request, user platform.User, query QueryParams) (*communicationList, *nhttp.ErrorWithStatus) {
reports, err := platform.PublicReportsForOrganization(ctx, user.Organization.ID, false)
func (res *communicationR) List(ctx context.Context, r *http.Request, user platform.User, query QueryParams) ([]*communication, *nhttp.ErrorWithStatus) {
comms, err := platform.CommunicationsForOrganization(ctx, int64(user.Organization.ID))
if err != nil {
return nil, nhttp.NewError("nuisance report query: %w", err)
}
comms := make([]communication, len(reports))
for i, report := range reports {
populateDistrictURI(report, res.router)
populateReportURI(report, res.router, false)
comms[i] = communication{
Created: report.Created,
ID: report.PublicID,
PublicReport: report.URI,
Type: "publicreport." + string(report.Type),
report_ids := make([]int64, 0)
for _, comm := range comms {
if comm.SourceReportID != nil {
report_ids = append(report_ids, int64(*comm.SourceReportID))
}
}
_by_created := func(a, b communication) int {
if a.Created == b.Created {
public_reports, err := platform.PublicReportsFromIDs(ctx, report_ids)
if err != nil {
return nil, nhttp.NewError("public reports from IDs: %w", err)
}
public_report_id_to_report := make(map[int32]*model.Report, 0)
for _, pr := range public_reports {
public_report_id_to_report[pr.ID] = pr
}
result := make([]*communication, len(comms))
for i, comm := range comms {
source_uri := "unknown"
type_ := "unknown"
if comm.SourceReportID != nil {
public_report, ok := public_report_id_to_report[*comm.SourceReportID]
if !ok {
return nil, nhttp.NewError("lookup report id %d failed", comm.SourceReportID)
}
source_uri, err = reportURI(res.router, "", public_report.PublicID)
if err != nil {
return nil, nhttp.NewError("gen report URI: %w", err)
}
type_ = "publicreport." + public_report.ReportType.String()
} else if comm.SourceEmailLogID != nil {
source_uri, err = emailURI(res.router, *comm.SourceEmailLogID)
if err != nil {
return nil, nhttp.NewError("gen email URI: %w", err)
}
type_ = "email"
} else if comm.SourceTextLogID != nil {
source_uri, err = textURI(res.router, *comm.SourceTextLogID)
if err != nil {
return nil, nhttp.NewError("gen email URI: %w", err)
}
source_uri = "text"
}
result[i] = &communication{
Created: comm.Created,
ID: comm.ID,
Source: source_uri,
Type: type_,
}
}
_by_created := func(a, b *communication) int {
if a.Created.Equal(b.Created) {
return 0
} else if a.Created.Before(b.Created) {
return 1
@ -71,8 +105,13 @@ func (res *communicationR) List(ctx context.Context, r *http.Request, user platf
return -1
}
}
slices.SortFunc(comms, _by_created)
return &communicationList{
Communications: comms,
}, nil
slices.SortFunc(result, _by_created)
return result, nil
}
func emailURI(r *router, id int32) (string, error) {
return "fake email uri", nil
}
func textURI(r *router, id int32) (string, error) {
return "fake text uri", nil
}

View file

@ -118,7 +118,7 @@ func reportURI(r *router, report_type string, public_id string) (string, error)
case "water":
route_name = "publicreport.water.ByIDGet"
default:
return "", fmt.Errorf("Unrecognized report type '%s'", report_type)
route_name = "publicreport.ByIDGet"
}
uri, err := r.IDStrToURI(route_name, public_id)
if err != nil {

View file

@ -20,10 +20,8 @@ func (qp QueryParams) SortOrDefault(default_name string, ascending bool) (string
if s == "" {
return default_name, ascending
}
a := true
if s[0] == '-' {
a = false
}
a := !(s[0] == '-')
if s[0] == '+' || s[0] == '-' {
s = s[1:]
}

View file

@ -98,7 +98,7 @@ func (res *uploadR) Discard(ctx context.Context, r *http.Request, u platform.Use
func (res *uploadR) PoolFlyoverCreate(ctx context.Context, r *http.Request, u platform.User, uploads []file.Upload) (string, *nhttp.ErrorWithStatus) {
// If the organization we're uploading to doesn't have a service area, we can't process the upload correctly
if !(u.Organization.HasServiceArea() || u.Organization.IsCatchall()) {
if !u.Organization.HasServiceArea() && !u.Organization.IsCatchall() {
return "", nhttp.NewErrorStatus(http.StatusConflict, "Your organization does not yet have a service area")
}
if len(uploads) == 0 {

View file

@ -94,7 +94,7 @@ func (res *userR) ByIDPut(ctx context.Context, r *http.Request, user platform.Us
return "", nhttp.NewErrorStatus(http.StatusBadRequest, "user id conversion: %w", err)
}
user_changes := &models.UserSetter{}
if !(user.HasRoot() || user.IsAccountOwner() || user.ID == user_id) {
if !user.HasRoot() && !user.IsAccountOwner() && user.ID != user_id {
return "", nhttp.NewForbidden("Only account owners can change other users")
}
if updates.Avatar.IsValue() {