2026-05-01 20:49:37 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-05-04 20:57:50 +00:00
|
|
|
"fmt"
|
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-07 10:39:17 +00:00
|
|
|
//"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/enum"
|
2026-05-01 20:49:37 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model"
|
|
|
|
|
querypublic "github.com/Gleipnir-Technology/nidus-sync/db/query/public"
|
2026-05-04 20:57:50 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
2026-05-09 01:48:56 +00:00
|
|
|
//"github.com/Gleipnir-Technology/jet/postgres"
|
2026-05-01 20:49:37 +00:00
|
|
|
)
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
querypublic.CommunicationLogEntryInsert(ctx, txn, log_entry)
|
2026-05-04 20:57:50 +00:00
|
|
|
txn.Commit(ctx)
|
|
|
|
|
return nil
|
2026-05-04 19:07:29 +00:00
|
|
|
}
|