Start work on populating context of communication at API layer
Remove communication stub, it's a performance enhancement.
This commit is contained in:
parent
266004624d
commit
e569bb32b7
9 changed files with 331 additions and 119 deletions
|
|
@ -7,7 +7,8 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model"
|
||||
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"
|
||||
querycomms "github.com/Gleipnir-Technology/nidus-sync/db/query/comms"
|
||||
querypublic "github.com/Gleipnir-Technology/nidus-sync/db/query/public"
|
||||
querypublicreport "github.com/Gleipnir-Technology/nidus-sync/db/query/publicreport"
|
||||
|
|
@ -16,12 +17,37 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type RelatedRecord struct {
|
||||
ID int32
|
||||
Type string
|
||||
type RelatedRecordType int
|
||||
|
||||
const (
|
||||
RelatedRecordTypeUnknown RelatedRecordType = iota
|
||||
RelatedRecordTypeEmail
|
||||
RelatedRecordTypeReportCompliance
|
||||
RelatedRecordTypeReportNuisance
|
||||
RelatedRecordTypeReportWater
|
||||
RelatedRecordTypeText
|
||||
)
|
||||
|
||||
func recordTypeFromReportType(t modelpublicreport.Reporttype) RelatedRecordType {
|
||||
switch t {
|
||||
case modelpublicreport.Reporttype_Compliance:
|
||||
return RelatedRecordTypeReportCompliance
|
||||
case modelpublicreport.Reporttype_Nuisance:
|
||||
return RelatedRecordTypeReportNuisance
|
||||
case modelpublicreport.Reporttype_Water:
|
||||
return RelatedRecordTypeReportWater
|
||||
default:
|
||||
return RelatedRecordTypeUnknown
|
||||
}
|
||||
}
|
||||
|
||||
func CommunicationRelatedRecords(ctx context.Context, user User, comm *model.Communication) ([]RelatedRecord, error) {
|
||||
type RelatedRecord struct {
|
||||
Created time.Time
|
||||
ID string
|
||||
Type RelatedRecordType
|
||||
}
|
||||
|
||||
func CommunicationRelatedRecords(ctx context.Context, user User, comm *modelpublic.Communication) ([]RelatedRecord, error) {
|
||||
// Gather associated records
|
||||
// * address
|
||||
// * phone number
|
||||
|
|
@ -33,28 +59,72 @@ func CommunicationRelatedRecords(ctx context.Context, user User, comm *model.Com
|
|||
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 {
|
||||
email_logs, err := querycomms.EmailLogsFromAddress(ctx, email_log.Source)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("email log from ID: %w", err)
|
||||
}
|
||||
for _, log := range email_logs {
|
||||
result = append(result, RelatedRecord{
|
||||
Created: log.Created,
|
||||
ID: strconv.Itoa(int(log.ID)),
|
||||
Type: RelatedRecordTypeEmail,
|
||||
})
|
||||
}
|
||||
} else 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 {
|
||||
text_logs, err := querycomms.EmailLogsFromAddress(ctx, text_log.Source)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("text log from ID: %w", err)
|
||||
}
|
||||
for _, log := range text_logs {
|
||||
result = append(result, RelatedRecord{
|
||||
Created: log.Created,
|
||||
ID: strconv.Itoa(int(log.ID)),
|
||||
Type: RelatedRecordTypeText,
|
||||
})
|
||||
}
|
||||
} else 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()
|
||||
if report.ReporterName != "" {
|
||||
reports_by_name, err := querypublicreport.ReportsFromReporterName(ctx, db.PGInstance.PGXPool, int64(user.Organization.ID), report.ReporterName)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("reports from reporter name '%s': %w", report.ReporterName, err)
|
||||
}
|
||||
for _, r := range reports_by_name {
|
||||
record_type := recordTypeFromReportType(r.ReportType)
|
||||
result = append(result, RelatedRecord{
|
||||
Created: r.Created,
|
||||
ID: r.PublicID,
|
||||
Type: record_type,
|
||||
})
|
||||
}
|
||||
}
|
||||
if report.AddressID != nil {
|
||||
reports_by_address, err := querypublicreport.ReportsFromAddressID(ctx, db.PGInstance.PGXPool, int64(user.Organization.ID), int64(*report.AddressID))
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("reports from reporter name '%s': %w", report.ReporterName, err)
|
||||
}
|
||||
for _, r := range reports_by_address {
|
||||
record_type := recordTypeFromReportType(r.ReportType)
|
||||
result = append(result, RelatedRecord{
|
||||
ID: r.PublicID,
|
||||
Type: record_type,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
func CommunicationsForOrganization(ctx context.Context, org_id int64) ([]model.Communication, error) {
|
||||
func CommunicationsForOrganization(ctx context.Context, org_id int64) ([]modelpublic.Communication, error) {
|
||||
return querypublic.CommunicationsFromOrganization(ctx, org_id)
|
||||
}
|
||||
func CommunicationFromID(ctx context.Context, user User, comm_id int64) (*model.Communication, error) {
|
||||
func CommunicationFromID(ctx context.Context, user User, comm_id int64) (*modelpublic.Communication, error) {
|
||||
comm, err := querypublic.CommunicationFromID(ctx, comm_id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -65,19 +135,19 @@ func CommunicationFromID(ctx context.Context, user User, comm_id int64) (*model.
|
|||
return &comm, nil
|
||||
}
|
||||
func CommunicationMarkInvalid(ctx context.Context, user User, comm_id int32) error {
|
||||
return communicationMark(ctx, user, comm_id, model.Communicationstatus_Invalid, model.Communicationlogentry_StatusInvalidated)
|
||||
return communicationMark(ctx, user, comm_id, modelpublic.Communicationstatus_Invalid, modelpublic.Communicationlogentry_StatusInvalidated)
|
||||
}
|
||||
func CommunicationMarkPendingResponse(ctx context.Context, user User, comm_id int32) error {
|
||||
return communicationMark(ctx, user, comm_id, model.Communicationstatus_Pending, model.Communicationlogentry_StatusPending)
|
||||
return communicationMark(ctx, user, comm_id, modelpublic.Communicationstatus_Pending, modelpublic.Communicationlogentry_StatusPending)
|
||||
}
|
||||
func CommunicationMarkPossibleIssue(ctx context.Context, user User, comm_id int32) error {
|
||||
return communicationMark(ctx, user, comm_id, model.Communicationstatus_PossibleIssue, model.Communicationlogentry_StatusPossibleIssue)
|
||||
return communicationMark(ctx, user, comm_id, modelpublic.Communicationstatus_PossibleIssue, modelpublic.Communicationlogentry_StatusPossibleIssue)
|
||||
}
|
||||
func CommunicationMarkPossibleResolved(ctx context.Context, user User, comm_id int32) error {
|
||||
return communicationMark(ctx, user, comm_id, model.Communicationstatus_PossibleResolved, model.Communicationlogentry_StatusPossibleResolved)
|
||||
return communicationMark(ctx, user, comm_id, modelpublic.Communicationstatus_PossibleResolved, modelpublic.Communicationlogentry_StatusPossibleResolved)
|
||||
}
|
||||
|
||||
func communicationMark(ctx context.Context, user User, comm_id int32, status model.Communicationstatus, log_type model.Communicationlogentry) error {
|
||||
func communicationMark(ctx context.Context, user User, comm_id int32, status modelpublic.Communicationstatus, log_type modelpublic.Communicationlogentry) error {
|
||||
txn, err := db.BeginTxn(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin txn: %w", err)
|
||||
|
|
@ -88,7 +158,7 @@ func communicationMark(ctx context.Context, user User, comm_id int32, status mod
|
|||
return fmt.Errorf("mark: %w", err)
|
||||
}
|
||||
user_id := int32(user.ID)
|
||||
log_entry := model.CommunicationLogEntry{
|
||||
log_entry := modelpublic.CommunicationLogEntry{
|
||||
CommunicationID: comm_id,
|
||||
Created: time.Now(),
|
||||
Type: log_type,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue