2026-04-01 18:12:46 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
2026-05-04 19:07:29 +00:00
|
|
|
"strconv"
|
2026-04-01 18:12:46 +00:00
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/config"
|
2026-05-02 00:41:31 +00:00
|
|
|
modelpublic "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model"
|
|
|
|
|
modelpublicreport "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model"
|
2026-04-01 18:12:46 +00:00
|
|
|
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
|
|
|
|
"github.com/google/uuid"
|
2026-05-04 19:07:29 +00:00
|
|
|
"github.com/gorilla/mux"
|
2026-05-04 20:30:36 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2026-04-01 18:12:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type communicationR struct {
|
2026-04-27 23:12:15 +00:00
|
|
|
router *router
|
2026-04-01 18:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 23:12:15 +00:00
|
|
|
func Communication(r *router) *communicationR {
|
2026-04-01 18:12:46 +00:00
|
|
|
return &communicationR{
|
|
|
|
|
router: r,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 10:39:17 +00:00
|
|
|
type communicationLog struct {
|
|
|
|
|
Created string `json:"string"`
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
User string `json:"user"`
|
|
|
|
|
}
|
2026-04-01 18:12:46 +00:00
|
|
|
type communication struct {
|
2026-05-07 10:39:17 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
|
Log []communicationLog `json:"log"`
|
|
|
|
|
Response string `json:"response"`
|
|
|
|
|
Source string `json:"source"`
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
URI string `json:"uri"`
|
2026-04-01 18:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toImageURLs(m map[string][]uuid.UUID, id string) []string {
|
|
|
|
|
uuids, ok := m[id]
|
|
|
|
|
if !ok {
|
|
|
|
|
return []string{}
|
|
|
|
|
}
|
|
|
|
|
urls := make([]string, len(uuids))
|
|
|
|
|
for i, u := range uuids {
|
|
|
|
|
urls[i] = config.MakeURLNidus("/api/image/%s/content", u.String())
|
|
|
|
|
}
|
|
|
|
|
return urls
|
|
|
|
|
}
|
2026-05-04 19:07:29 +00:00
|
|
|
func (res *communicationR) Get(ctx context.Context, r *http.Request, user platform.User, query QueryParams) (*communication, *nhttp.ErrorWithStatus) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
func (res *communicationR) List(ctx context.Context, r *http.Request, user platform.User, query QueryParams) ([]communication, *nhttp.ErrorWithStatus) {
|
2026-05-01 20:49:37 +00:00
|
|
|
comms, err := platform.CommunicationsForOrganization(ctx, int64(user.Organization.ID))
|
2026-04-01 18:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("nuisance report query: %w", err)
|
|
|
|
|
}
|
2026-05-01 20:49:37 +00:00
|
|
|
report_ids := make([]int64, 0)
|
|
|
|
|
for _, comm := range comms {
|
|
|
|
|
if comm.SourceReportID != nil {
|
|
|
|
|
report_ids = append(report_ids, int64(*comm.SourceReportID))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public_reports, err := platform.PublicReportsFromIDs(ctx, report_ids)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nhttp.NewError("public reports from IDs: %w", err)
|
|
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
public_report_id_to_report := make(map[int32]modelpublicreport.Report, 0)
|
2026-05-01 20:49:37 +00:00
|
|
|
for _, pr := range public_reports {
|
|
|
|
|
public_report_id_to_report[pr.ID] = pr
|
|
|
|
|
}
|
2026-05-04 19:07:29 +00:00
|
|
|
result := make([]communication, len(comms))
|
2026-05-01 20:49:37 +00:00
|
|
|
for i, comm := range comms {
|
2026-05-04 19:07:29 +00:00
|
|
|
public_report, ok := public_report_id_to_report[*comm.SourceReportID]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, nhttp.NewError("lookup report id %d failed", comm.SourceReportID)
|
2026-05-02 00:41:31 +00:00
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
c, err := res.hydrateCommunication(comm, &public_report)
|
2026-05-02 00:41:31 +00:00
|
|
|
if err != nil {
|
2026-05-04 19:07:29 +00:00
|
|
|
return nil, err
|
2026-04-01 18:12:46 +00:00
|
|
|
}
|
2026-05-04 19:07:29 +00:00
|
|
|
result[i] = c
|
2026-04-01 18:12:46 +00:00
|
|
|
}
|
2026-05-01 20:49:37 +00:00
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-04 19:39:03 +00:00
|
|
|
type communicationMarkRequest struct{}
|
|
|
|
|
|
2026-05-04 19:07:29 +00:00
|
|
|
func (res *communicationR) MarkInvalid(ctx context.Context, r *http.Request, user platform.User, cmr communicationMarkRequest) (communication, *nhttp.ErrorWithStatus) {
|
2026-05-04 20:30:36 +00:00
|
|
|
return res.markCommunication(ctx, r, user, "invalid", platform.CommunicationMarkInvalid)
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
func (res *communicationR) MarkPendingResponse(ctx context.Context, r *http.Request, user platform.User, cmr communicationMarkRequest) (communication, *nhttp.ErrorWithStatus) {
|
2026-05-04 20:30:36 +00:00
|
|
|
return res.markCommunication(ctx, r, user, "pending-response", platform.CommunicationMarkPendingResponse)
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
func (res *communicationR) MarkPossibleIssue(ctx context.Context, r *http.Request, user platform.User, cmr communicationMarkRequest) (communication, *nhttp.ErrorWithStatus) {
|
2026-05-04 20:30:36 +00:00
|
|
|
return res.markCommunication(ctx, r, user, "possible-issue", platform.CommunicationMarkPossibleIssue)
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
func (res *communicationR) MarkPossibleResolved(ctx context.Context, r *http.Request, user platform.User, cmr communicationMarkRequest) (communication, *nhttp.ErrorWithStatus) {
|
2026-05-04 20:30:36 +00:00
|
|
|
return res.markCommunication(ctx, r, user, "possible-resolved", platform.CommunicationMarkPossibleResolved)
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
func (res *communicationR) hydrateCommunication(comm modelpublic.Communication, public_report *modelpublicreport.Report) (communication, *nhttp.ErrorWithStatus) {
|
|
|
|
|
var err error
|
|
|
|
|
source_uri := "unknown"
|
|
|
|
|
type_ := "unknown"
|
2026-05-04 20:30:36 +00:00
|
|
|
if comm.SourceReportID != nil && public_report != nil {
|
2026-05-04 19:07:29 +00:00
|
|
|
source_uri, err = reportURI(res.router, "", public_report.PublicID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return communication{}, 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 communication{}, 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 communication{}, nhttp.NewError("gen email URI: %w", err)
|
|
|
|
|
}
|
|
|
|
|
source_uri = "text"
|
|
|
|
|
}
|
|
|
|
|
response, err := responseURI(*res.router, comm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return communication{}, nhttp.NewError("gen response URI: %w", err)
|
|
|
|
|
}
|
|
|
|
|
uri, err := res.router.IDToURI("communication.ByIDGet", int(comm.ID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return communication{}, nhttp.NewError("gen comm uri: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return communication{
|
2026-05-04 19:36:39 +00:00
|
|
|
ID: strconv.Itoa(int(comm.ID)),
|
2026-05-04 19:07:29 +00:00
|
|
|
Response: response,
|
|
|
|
|
Source: source_uri,
|
2026-05-07 10:39:17 +00:00
|
|
|
Status: comm.Status.String(),
|
2026-05-04 19:07:29 +00:00
|
|
|
Type: type_,
|
|
|
|
|
URI: uri,
|
|
|
|
|
}, nil
|
2026-05-01 20:49:37 +00:00
|
|
|
}
|
2026-05-04 19:07:29 +00:00
|
|
|
|
2026-05-07 10:39:17 +00:00
|
|
|
type markFunc = func(context.Context, platform.User, int32) error
|
2026-05-04 19:07:29 +00:00
|
|
|
|
2026-05-04 20:30:36 +00:00
|
|
|
func (res *communicationR) markCommunication(ctx context.Context, r *http.Request, user platform.User, status string, m markFunc) (communication, *nhttp.ErrorWithStatus) {
|
2026-05-04 19:07:29 +00:00
|
|
|
vars := mux.Vars(r)
|
2026-05-04 20:30:36 +00:00
|
|
|
comm_id_str := vars["id"]
|
|
|
|
|
if comm_id_str == "" {
|
2026-05-04 19:07:29 +00:00
|
|
|
return communication{}, nhttp.NewBadRequest("no id provided")
|
|
|
|
|
}
|
2026-05-04 20:30:36 +00:00
|
|
|
comm_id, err := strconv.Atoi(comm_id_str)
|
2026-05-04 19:07:29 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return communication{}, nhttp.NewBadRequest("can't turn report ID into an int: %w", err)
|
|
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
m(ctx, user, int32(comm_id))
|
2026-05-04 20:30:36 +00:00
|
|
|
result, err := platform.CommunicationFromID(ctx, user, int64(comm_id))
|
2026-05-04 19:07:29 +00:00
|
|
|
if result == nil {
|
2026-05-04 20:30:36 +00:00
|
|
|
return communication{}, nhttp.NewUnauthorized("you are not authorized to modify communication %d", comm_id)
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
var public_report modelpublicreport.Report
|
2026-05-04 20:30:36 +00:00
|
|
|
if result.SourceReportID != nil {
|
|
|
|
|
comm_ids := []int64{int64(*result.SourceReportID)}
|
|
|
|
|
public_reports, err := platform.PublicReportsFromIDs(ctx, comm_ids)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return communication{}, nhttp.NewError("Get report %d: %w", *result.SourceReportID, err)
|
|
|
|
|
}
|
|
|
|
|
public_report = public_reports[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Info().Int("communication", comm_id).Str("status", status).Msg("Marked communication")
|
2026-05-07 10:39:17 +00:00
|
|
|
return res.hydrateCommunication(*result, &public_report)
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
func responseURI(r router, comm modelpublic.Communication) (string, error) {
|
2026-05-02 00:41:31 +00:00
|
|
|
if comm.ResponseEmailLogID != nil {
|
|
|
|
|
return emailURI(r, *comm.ResponseEmailLogID)
|
|
|
|
|
} else if comm.ResponseTextLogID != nil {
|
|
|
|
|
return textURI(r, *comm.ResponseTextLogID)
|
|
|
|
|
} else {
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-04 19:07:29 +00:00
|
|
|
func emailURI(r router, id int32) (string, error) {
|
|
|
|
|
return "fake email uri", nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func textURI(r router, id int32) (string, error) {
|
2026-05-01 20:49:37 +00:00
|
|
|
return "fake text uri", nil
|
2026-04-01 18:12:46 +00:00
|
|
|
}
|
2026-05-02 00:41:31 +00:00
|
|
|
func userURI(r *router, id *int32) (string, error) {
|
|
|
|
|
if id == nil {
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
return r.IDToURI("user.ByIDGet", int(*id))
|
|
|
|
|
}
|