This commit is contained in:
parent
8b203908a0
commit
725945d95c
22 changed files with 381 additions and 135 deletions
54
platform/contact.go
Normal file
54
platform/contact.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package platform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
querycomms "github.com/Gleipnir-Technology/nidus-sync/db/query/comms"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
)
|
||||
|
||||
func ContactsForOrganization(ctx context.Context, org_id int32) (results []types.Contact, err error) {
|
||||
txn := db.PGInstance.PGXPool
|
||||
rows, err := querycomms.ContactsFromOrganizationID(ctx, txn, int64(org_id))
|
||||
if err != nil {
|
||||
return results, fmt.Errorf("contacts from organization id: %w", err)
|
||||
}
|
||||
contact_ids := make([]int64, len(rows))
|
||||
for i, row := range rows {
|
||||
contact_ids[i] = int64(row.ID)
|
||||
}
|
||||
contact_emails_by_contact_id, err := querycomms.ContactEmailByContactIDs(ctx, txn, contact_ids)
|
||||
if err != nil {
|
||||
return results, fmt.Errorf("by contact ids: %w", err)
|
||||
}
|
||||
contact_phones_by_contact_id, err := querycomms.ContactPhoneByContactIDs(ctx, txn, contact_ids)
|
||||
if err != nil {
|
||||
return results, fmt.Errorf("by contact ids: %w", err)
|
||||
}
|
||||
|
||||
results = make([]types.Contact, len(rows))
|
||||
for i, row := range rows {
|
||||
contact_emails := contact_emails_by_contact_id[int64(row.ID)]
|
||||
emails := make([]string, len(contact_emails))
|
||||
for i, e := range contact_emails {
|
||||
emails[i] = e.Address
|
||||
}
|
||||
contact_phones := contact_phones_by_contact_id[int64(row.ID)]
|
||||
phones := make([]types.Phone, len(contact_phones))
|
||||
for i, p := range contact_phones {
|
||||
phones[i] = types.Phone{
|
||||
E164: p.E164,
|
||||
CanSMS: p.CanSms,
|
||||
}
|
||||
}
|
||||
results[i] = types.Contact{
|
||||
Emails: emails,
|
||||
ID: row.ID,
|
||||
Name: row.Name,
|
||||
Phones: phones,
|
||||
}
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@ package platform
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Gleipnir-Technology/bob"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
||||
|
|
@ -10,34 +9,12 @@ import (
|
|||
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
querypublic "github.com/Gleipnir-Technology/nidus-sync/db/query/public"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
"github.com/stephenafamo/scan"
|
||||
)
|
||||
|
||||
func MailerByID(ctx context.Context, user User, id int32) (*types.Mailer, error) {
|
||||
query := mailerQuery()
|
||||
query.Apply(
|
||||
sm.Where(models.ComplianceReportRequests.Columns.ID.EQ(psql.Arg(id))),
|
||||
sm.Where(
|
||||
models.Sites.Columns.OrganizationID.EQ(psql.Arg(user.Organization.ID)),
|
||||
),
|
||||
)
|
||||
mailers, err := mailerQueryToRows(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mailers[id], nil
|
||||
}
|
||||
func MailerList(ctx context.Context, user User, limit int) ([]*types.Mailer, error) {
|
||||
query := mailerQuery()
|
||||
query.Apply(
|
||||
sm.Where(
|
||||
models.Sites.Columns.OrganizationID.EQ(psql.Arg(user.Organization.ID)),
|
||||
),
|
||||
sm.OrderBy(models.ComplianceReportRequests.Columns.Created),
|
||||
sm.Limit(limit),
|
||||
)
|
||||
return mailerQueryToRows(ctx, query)
|
||||
func MailerList(ctx context.Context, user User, limit int) ([]types.Mailer, error) {
|
||||
return querypublic.MailersFromOrganizationID(ctx, db.PGInstance.PGXPool, int64(user.Organization.ID), int64(limit))
|
||||
}
|
||||
func mailerQuery() bob.BaseQuery[*dialect.SelectQuery] {
|
||||
return psql.Select(
|
||||
|
|
@ -78,11 +55,3 @@ func mailerQuery() bob.BaseQuery[*dialect.SelectQuery] {
|
|||
),
|
||||
)
|
||||
}
|
||||
func mailerQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQuery]) ([]*types.Mailer, error) {
|
||||
rows, err := bob.All(ctx, db.PGInstance.BobDB, query, scan.StructMapper[*types.Mailer]())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query mailers: %w", err)
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -143,13 +143,13 @@ func reportQueryToRows(ctx context.Context, reports []modelpublicreport.Report,
|
|||
DistrictID: &row.OrganizationID,
|
||||
District: nil,
|
||||
PublicID: row.PublicID,
|
||||
Reporter: types.Contact{
|
||||
CanSMS: &row.ReporterPhoneCanSms,
|
||||
Email: &row.ReporterEmail,
|
||||
HasEmail: row.ReporterEmail != "",
|
||||
HasPhone: row.ReporterPhone != "",
|
||||
Name: row.ReporterName,
|
||||
Phone: &row.ReporterPhone,
|
||||
Reporter: types.ContactReporter{
|
||||
Email: row.ReporterEmail,
|
||||
Name: row.ReporterName,
|
||||
Phone: types.PhoneReporter{
|
||||
CanSMS: row.ReporterPhoneCanSms,
|
||||
Number: row.ReporterPhone,
|
||||
},
|
||||
},
|
||||
Status: row.Status.String(),
|
||||
Type: row.ReportType.String(),
|
||||
|
|
|
|||
|
|
@ -1,37 +1,32 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
//"github.com/rs/zerolog/log"
|
||||
//"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Contact struct {
|
||||
CanSMS *bool `db:"can_sms" json:"can_sms"`
|
||||
Email *string `db:"email" json:"email"`
|
||||
HasEmail bool `json:"has_email"`
|
||||
HasPhone bool `json:"has_phone"`
|
||||
Name string `db:"name" json:"name"`
|
||||
Phone *string `db:"phone" json:"phone"`
|
||||
Emails []string `json:"emails"`
|
||||
ID int32 `json:"-"`
|
||||
Name string `json:"name"`
|
||||
Phones []Phone `json:"phones"`
|
||||
}
|
||||
type ContactReporter struct {
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
Phone PhoneReporter `json:"phone"`
|
||||
}
|
||||
type Phone struct {
|
||||
E164 string `json:"e164"`
|
||||
CanSMS bool `json:"can_sms"`
|
||||
}
|
||||
type PhoneReporter struct {
|
||||
CanSMS bool `json:"can_sms"`
|
||||
Number string `json:"number"`
|
||||
}
|
||||
|
||||
func (c Contact) MarshalJSON() ([]byte, error) {
|
||||
to_marshal := make(map[string]interface{}, 0)
|
||||
if c.CanSMS != nil {
|
||||
to_marshal["can_sms"] = *c.CanSMS
|
||||
}
|
||||
to_marshal["name"] = c.Name
|
||||
to_marshal["has_email"] = (c.Email != nil && *c.Email != "")
|
||||
to_marshal["has_phone"] = (c.Phone != nil && *c.Phone != "")
|
||||
if c.Email != nil {
|
||||
to_marshal["email"] = *c.Email
|
||||
} else {
|
||||
to_marshal["email"] = ""
|
||||
}
|
||||
if c.Phone != nil {
|
||||
to_marshal["phone"] = *c.Phone
|
||||
} else {
|
||||
to_marshal["phone"] = ""
|
||||
}
|
||||
//log.Debug().Msg("marshaling contact")
|
||||
return json.Marshal(to_marshal)
|
||||
/*
|
||||
func ContactFromModel(m model.Contact) Contact {
|
||||
return Contact{
|
||||
Emails:
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ type PublicReport struct {
|
|||
DistrictID *int32 `db:"organization_id" json:"-"`
|
||||
District *string `db:"-" json:"district"`
|
||||
PublicID string `db:"public_id" json:"public_id"`
|
||||
Reporter Contact `db:"reporter" json:"reporter"`
|
||||
Reporter ContactReporter `db:"reporter" json:"reporter"`
|
||||
Status string `db:"status" json:"status"`
|
||||
Type string `db:"report_type" json:"type"`
|
||||
URI string `db:"-" json:"uri"`
|
||||
|
|
|
|||
|
|
@ -40,8 +40,11 @@ func SiteFromModel(s *models.Site) Site {
|
|||
Notes: s.Notes,
|
||||
OrganizationID: s.OrganizationID,
|
||||
Owner: Contact{
|
||||
Name: s.OwnerName,
|
||||
Phone: &owner_phone,
|
||||
Name: s.OwnerName,
|
||||
Phones: []Phone{Phone{
|
||||
E164: owner_phone,
|
||||
CanSMS: false,
|
||||
}},
|
||||
},
|
||||
ResidentOwned: resident_owned,
|
||||
//ParcelID: s.ParcelID,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue