2026-05-01 20:49:37 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-05-04 20:57:50 +00:00
|
|
|
"fmt"
|
2026-05-11 22:17:18 +00:00
|
|
|
"strconv"
|
2026-05-07 10:39:17 +00:00
|
|
|
"time"
|
2026-05-01 20:49:37 +00:00
|
|
|
|
2026-05-04 20:57:50 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
2026-05-01 20:49:37 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model"
|
2026-05-12 03:21:18 +00:00
|
|
|
querycomms "github.com/Gleipnir-Technology/nidus-sync/db/query/comms"
|
2026-05-01 20:49:37 +00:00
|
|
|
querypublic "github.com/Gleipnir-Technology/nidus-sync/db/query/public"
|
2026-05-12 03:21:18 +00:00
|
|
|
querypublicreport "github.com/Gleipnir-Technology/nidus-sync/db/query/publicreport"
|
2026-05-04 20:57:50 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
2026-05-11 22:17:18 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/event"
|
2026-05-11 22:43:04 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2026-05-01 20:49:37 +00:00
|
|
|
)
|
|
|
|
|
|
2026-05-12 03:21:18 +00:00
|
|
|
type RelatedRecord struct {
|
|
|
|
|
ID int32
|
|
|
|
|
Type string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CommunicationRelatedRecords(ctx context.Context, user User, comm *model.Communication) ([]RelatedRecord, error) {
|
|
|
|
|
// Gather associated records
|
|
|
|
|
// * address
|
|
|
|
|
// * phone number
|
|
|
|
|
// * email
|
|
|
|
|
// * name
|
|
|
|
|
result := make([]RelatedRecord, 0)
|
|
|
|
|
if comm.SourceEmailLogID != nil {
|
|
|
|
|
email_log, err := querycomms.EmailLogFromID(ctx, int64(*comm.SourceEmailLogID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return result, fmt.Errorf("email log from ID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
log.Debug().Int32("id", email_log.ID).Send()
|
|
|
|
|
}
|
|
|
|
|
if comm.SourceTextLogID != nil {
|
|
|
|
|
text_log, err := querycomms.TextLogFromID(ctx, int64(*comm.SourceTextLogID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return result, fmt.Errorf("text log from ID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
log.Debug().Int32("id", text_log.ID).Send()
|
|
|
|
|
}
|
|
|
|
|
if comm.SourceReportID != nil {
|
|
|
|
|
report, err := querypublicreport.ReportFromID(ctx, int64(*comm.SourceReportID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return result, fmt.Errorf("report from ID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
log.Debug().Int32("id", report.ID).Send()
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
func CommunicationsForOrganization(ctx context.Context, org_id int64) ([]model.Communication, error) {
|
2026-05-01 20:49:37 +00:00
|
|
|
return querypublic.CommunicationsFromOrganization(ctx, org_id)
|
|
|
|
|
}
|
2026-05-04 19:07:29 +00:00
|
|
|
func CommunicationFromID(ctx context.Context, user User, comm_id int64) (*model.Communication, error) {
|
|
|
|
|
comm, err := querypublic.CommunicationFromID(ctx, comm_id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if comm.OrganizationID != user.Organization.ID {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
return &comm, nil
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
func CommunicationMarkInvalid(ctx context.Context, user User, comm_id int32) error {
|
|
|
|
|
return communicationMark(ctx, user, comm_id, model.Communicationstatus_Invalid, model.Communicationlogentry_StatusInvalidated)
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
func CommunicationMarkPendingResponse(ctx context.Context, user User, comm_id int32) error {
|
|
|
|
|
return communicationMark(ctx, user, comm_id, model.Communicationstatus_Pending, model.Communicationlogentry_StatusPending)
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
func CommunicationMarkPossibleIssue(ctx context.Context, user User, comm_id int32) error {
|
|
|
|
|
return communicationMark(ctx, user, comm_id, model.Communicationstatus_PossibleIssue, model.Communicationlogentry_StatusPossibleIssue)
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
func CommunicationMarkPossibleResolved(ctx context.Context, user User, comm_id int32) error {
|
|
|
|
|
return communicationMark(ctx, user, comm_id, model.Communicationstatus_PossibleResolved, model.Communicationlogentry_StatusPossibleResolved)
|
2026-05-04 20:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-07 10:39:17 +00:00
|
|
|
func communicationMark(ctx context.Context, user User, comm_id int32, status model.Communicationstatus, log_type model.Communicationlogentry) error {
|
2026-05-04 20:57:50 +00:00
|
|
|
txn, err := db.BeginTxn(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("begin txn: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback")
|
2026-05-07 10:39:17 +00:00
|
|
|
err = querypublic.CommunicationSetStatus(ctx, txn, int64(user.Organization.ID), int64(comm_id), status)
|
2026-05-04 20:57:50 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("mark: %w", err)
|
|
|
|
|
}
|
2026-05-07 10:39:17 +00:00
|
|
|
user_id := int32(user.ID)
|
|
|
|
|
log_entry := model.CommunicationLogEntry{
|
|
|
|
|
CommunicationID: comm_id,
|
|
|
|
|
Created: time.Now(),
|
|
|
|
|
Type: log_type,
|
|
|
|
|
User: &user_id,
|
|
|
|
|
}
|
2026-05-09 02:17:25 +00:00
|
|
|
_, err = querypublic.CommunicationLogEntryInsert(ctx, txn, log_entry)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("insert communication log entry: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if err := txn.Commit(ctx); err != nil {
|
|
|
|
|
return fmt.Errorf("commit: %w", err)
|
|
|
|
|
}
|
2026-05-11 22:43:04 +00:00
|
|
|
log.Info().Int32("communication", comm_id).Str("status", status.String()).Msg("Marked communication")
|
2026-05-11 22:17:18 +00:00
|
|
|
|
|
|
|
|
event.Updated(event.TypeCommunication, user.Organization.ID, strconv.Itoa(int(comm_id)))
|
2026-05-04 20:57:50 +00:00
|
|
|
return nil
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|