Initial creation of endpoint to send messages to public reporters
This commit is contained in:
parent
9707e8793b
commit
cc95c38ab5
12 changed files with 240 additions and 20 deletions
|
|
@ -10,11 +10,14 @@ import (
|
|||
|
||||
var channelJobText chan text.Job
|
||||
|
||||
func ReportUserText(destination text.E164, report_id string, message string) {
|
||||
//enqueueJobText(text.N
|
||||
}
|
||||
func ReportSubscriptionConfirmationText(destination text.E164, report_id string) {
|
||||
enqueueJobText(text.NewJobReportSubscriptionConfirmation(
|
||||
destination,
|
||||
report_id,
|
||||
config.PhoneNumberReport,
|
||||
*text.NewE164(&config.PhoneNumberReport),
|
||||
))
|
||||
}
|
||||
|
||||
|
|
|
|||
10
platform/email/report.go
Normal file
10
platform/email/report.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
//"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
)
|
||||
|
||||
func ReportMessage(ctx context.Context, user_id int32, report_id, destination, message string) (*int32, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package platform
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
|
|
@ -11,20 +12,19 @@ import (
|
|||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
//"github.com/Gleipnir-Technology/nidus-sync/platform/background"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/email"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/event"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/text"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func PublicreportInvalid(ctx context.Context, user User, report_id string) error {
|
||||
location, err := models.PublicreportReportLocations.Query(
|
||||
models.SelectWhere.PublicreportReportLocations.PublicID.EQ(report_id),
|
||||
models.SelectWhere.PublicreportReportLocations.OrganizationID.EQ(user.Organization.ID()),
|
||||
).One(ctx, db.PGInstance.BobDB)
|
||||
tablename, _, err := reportFromID(ctx, user, report_id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("query report existence: %w", err)
|
||||
}
|
||||
|
||||
tablename := location.TableName.MustGet()
|
||||
_, err = psql.Update(
|
||||
um.Table("publicreport."+tablename),
|
||||
um.SetCol("reviewed").ToArg(time.Now()),
|
||||
|
|
@ -42,6 +42,27 @@ func PublicreportInvalid(ctx context.Context, user User, report_id string) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func PublicReportMessageCreate(ctx context.Context, user User, report_id, message string) (message_id *int32, err error) {
|
||||
_, report, err := reportFromID(ctx, user, report_id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query report existence: %w", err)
|
||||
}
|
||||
if report.ReporterPhone.GetOr("") != "" {
|
||||
msg_id, err := text.ReportMessage(ctx, int32(user.ID), report_id, report.ReporterPhone.MustGet(), message)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send text: %w", err)
|
||||
}
|
||||
return msg_id, nil
|
||||
} else if report.ReporterEmail.GetOr("") != "" {
|
||||
msg_id, err := email.ReportMessage(ctx, int32(user.ID), report_id, report.ReporterEmail.MustGet(), message)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send email: %w", err)
|
||||
}
|
||||
return msg_id, nil
|
||||
} else {
|
||||
return nil, errors.New("no contact methods available")
|
||||
}
|
||||
}
|
||||
func PublicReportReporterUpdated(ctx context.Context, org_id int32, report_id string, tablename string) {
|
||||
resource := resourceTypeFromTablename(tablename)
|
||||
event.Updated(resource, org_id, report_id)
|
||||
|
|
@ -56,3 +77,14 @@ func resourceTypeFromTablename(tablename string) event.ResourceType {
|
|||
return event.TypeUnknown
|
||||
}
|
||||
}
|
||||
func reportFromID(ctx context.Context, user User, report_id string) (string, *models.PublicreportReportLocation, error) {
|
||||
report, err := models.PublicreportReportLocations.Query(
|
||||
models.SelectWhere.PublicreportReportLocations.PublicID.EQ(report_id),
|
||||
models.SelectWhere.PublicreportReportLocations.OrganizationID.EQ(user.Organization.ID()),
|
||||
).One(ctx, db.PGInstance.BobDB)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
tablename := report.TableName.MustGet()
|
||||
return tablename, report, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func (j jobReportSubscription) content() string {
|
|||
return fmt.Sprintf("Thanks for submitting mosquito report %s. Text for any questions. We'll send you updates as we get them.", j.reportID)
|
||||
}
|
||||
func (j jobReportSubscription) destination() string {
|
||||
return phonenumbers.Format(&j.dst, phonenumbers.E164)
|
||||
return phonenumbers.Format(j.dst.number, phonenumbers.E164)
|
||||
}
|
||||
func (j jobReportSubscription) messageType() MessageType {
|
||||
return ReportSubscription
|
||||
|
|
@ -40,7 +40,7 @@ func (j jobReportSubscription) messageTypeName() string {
|
|||
return "report-subscription"
|
||||
}
|
||||
func (j jobReportSubscription) source() string {
|
||||
return phonenumbers.Format(&j.src, phonenumbers.E164)
|
||||
return phonenumbers.Format(j.src.number, phonenumbers.E164)
|
||||
}
|
||||
|
||||
func sendReportSubscription(ctx context.Context, job Job) error {
|
||||
|
|
|
|||
10
platform/text/report.go
Normal file
10
platform/text/report.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package text
|
||||
|
||||
import (
|
||||
"context"
|
||||
//"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
)
|
||||
|
||||
func ReportMessage(ctx context.Context, user_id int32, report_id, destination, message string) (*int32, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
@ -23,7 +23,15 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type E164 = phonenumbers.PhoneNumber
|
||||
type E164 struct {
|
||||
number *phonenumbers.PhoneNumber
|
||||
}
|
||||
|
||||
func NewE164(n *phonenumbers.PhoneNumber) *E164 {
|
||||
return &E164{
|
||||
number: n,
|
||||
}
|
||||
}
|
||||
|
||||
func EnsureInDB(ctx context.Context, ex bob.Executor, destination E164) (err error) {
|
||||
return ensureInDB(ctx, ex, PhoneString(destination))
|
||||
|
|
@ -96,11 +104,17 @@ func HandleTextMessage(src string, dst string, body string) {
|
|||
}
|
||||
|
||||
func ParsePhoneNumber(input string) (*E164, error) {
|
||||
return phonenumbers.Parse(input, "US")
|
||||
n, err := phonenumbers.Parse(input, "US")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &E164{
|
||||
number: n,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func PhoneString(p E164) string {
|
||||
return phonenumbers.Format(&p, phonenumbers.E164)
|
||||
return phonenumbers.Format(p.number, phonenumbers.E164)
|
||||
}
|
||||
|
||||
func StoreSources() error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue