Update nuisance submission to go to submitted page
This commit is contained in:
parent
c41154a200
commit
2c0bfb9904
31 changed files with 747 additions and 228 deletions
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/publicreport"
|
||||
pr "github.com/Gleipnir-Technology/nidus-sync/platform/publicreport"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
|
|
@ -48,7 +48,7 @@ func toImageURLs(m map[string][]uuid.UUID, id string) []string {
|
|||
return urls
|
||||
}
|
||||
func (res *communicationR) List(ctx context.Context, r *http.Request, user platform.User, query QueryParams) (*communicationList, *nhttp.ErrorWithStatus) {
|
||||
reports, err := publicreport.ReportsForOrganization(ctx, user.Organization.ID)
|
||||
reports, err := pr.ReportsForOrganization(ctx, user.Organization.ID)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("nuisance report query: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,12 @@ package resource
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
//"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
|
@ -17,6 +21,7 @@ type district struct {
|
|||
PhoneOffice string `json:"phone_office"`
|
||||
Slug string `json:"slug"`
|
||||
URLLogo string `json:"url_logo"`
|
||||
URLWebsite string `json:"url_website"`
|
||||
}
|
||||
|
||||
func District(r *router) *districtR {
|
||||
|
|
@ -25,6 +30,23 @@ func District(r *router) *districtR {
|
|||
}
|
||||
}
|
||||
|
||||
func (res *districtR) GetByID(ctx context.Context, r *http.Request, query QueryParams) (*district, *nhttp.ErrorWithStatus) {
|
||||
vars := mux.Vars(r)
|
||||
id_str := vars["id"]
|
||||
id, err := strconv.Atoi(id_str)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewBadRequest("id conversion: %w", err)
|
||||
}
|
||||
org, err := platform.OrganizationByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("get org: %w", err)
|
||||
}
|
||||
district, err := newDistrict(res.router, org)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("new district: %w", err)
|
||||
}
|
||||
return district, nil
|
||||
}
|
||||
func (res *districtR) List(ctx context.Context, r *http.Request, query QueryParams) ([]*district, *nhttp.ErrorWithStatus) {
|
||||
organizations, err := platform.OrganizationList(ctx)
|
||||
if err != nil {
|
||||
|
|
@ -32,20 +54,32 @@ func (res *districtR) List(ctx context.Context, r *http.Request, query QueryPara
|
|||
}
|
||||
districts := make([]*district, 0)
|
||||
for _, org := range organizations {
|
||||
slug := org.Slug()
|
||||
if slug == "" {
|
||||
district, err := newDistrict(res.router, org)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("make district: %w", err)
|
||||
}
|
||||
if district == nil {
|
||||
continue
|
||||
}
|
||||
logo, err := res.router.SlugToURI("district.logo.BySlug", slug)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("logo url: %w", err)
|
||||
}
|
||||
districts = append(districts, &district{
|
||||
Name: org.Name(),
|
||||
PhoneOffice: org.PhoneOffice(),
|
||||
Slug: slug,
|
||||
URLLogo: logo,
|
||||
})
|
||||
districts = append(districts, district)
|
||||
}
|
||||
return districts, nil
|
||||
}
|
||||
|
||||
func newDistrict(r *router, org *platform.Organization) (*district, error) {
|
||||
slug := org.Slug()
|
||||
if slug == "" {
|
||||
return nil, nil
|
||||
}
|
||||
logo, err := r.SlugToURI("district.logo.BySlug", slug)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("logo url: %w", err)
|
||||
}
|
||||
return &district{
|
||||
Name: org.Name(),
|
||||
PhoneOffice: org.PhoneOffice(),
|
||||
Slug: slug,
|
||||
URLLogo: logo,
|
||||
URLWebsite: org.Website(),
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,9 @@ type nuisanceR struct {
|
|||
router *router
|
||||
}
|
||||
type nuisance struct {
|
||||
ID string `json:"id"`
|
||||
District string `json:"district"`
|
||||
ID string `json:"id"`
|
||||
URI string `json:"uri"`
|
||||
}
|
||||
type nuisanceForm struct {
|
||||
AdditionalInfo string `schema:"additional-info"`
|
||||
|
|
@ -181,7 +183,17 @@ func (res *nuisanceR) Create(ctx context.Context, r *http.Request, n nuisanceFor
|
|||
if err != nil {
|
||||
return nil, nhttp.NewError("create nuisance report: %w", err)
|
||||
}
|
||||
uri, err := res.router.IDStrToURI("publicreport.ByIDGet", report.PublicID)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("generate uri: %w", err)
|
||||
}
|
||||
district_uri, err := res.router.IDToURI("district.ByIDGet", int(report.OrganizationID))
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("generate district uri: %w", err)
|
||||
}
|
||||
return &nuisance{
|
||||
ID: report.PublicID,
|
||||
District: district_uri,
|
||||
ID: report.PublicID,
|
||||
URI: uri,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
46
resource/publicreport.go
Normal file
46
resource/publicreport.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
||||
"net/http"
|
||||
//"github.com/rs/zerolog/log"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type publicreportR struct {
|
||||
router *router
|
||||
}
|
||||
|
||||
type publicreport struct {
|
||||
ID string `json:"id"`
|
||||
District string `json:"district"`
|
||||
URI string `json:"uri"`
|
||||
}
|
||||
|
||||
func Publicreport(r *router) *publicreportR {
|
||||
return &publicreportR{
|
||||
router: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (res *publicreportR) ByID(ctx context.Context, r *http.Request, query QueryParams) (*publicreport, *nhttp.ErrorWithStatus) {
|
||||
vars := mux.Vars(r)
|
||||
public_id := vars["id"]
|
||||
if public_id == "" {
|
||||
return nil, nhttp.NewBadRequest("You must provid an ID")
|
||||
}
|
||||
report, err := platform.PublicreportByID(ctx, public_id)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("get report: %w", err)
|
||||
}
|
||||
district_uri, err := res.router.IDToURI("district.ByIDGet", int(report.OrganizationID))
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("district uri: %w", err)
|
||||
}
|
||||
return &publicreport{
|
||||
District: district_uri,
|
||||
ID: report.PublicID,
|
||||
}, nil
|
||||
}
|
||||
56
resource/publicreport_notification.go
Normal file
56
resource/publicreport_notification.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/text"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type publicreportNotificationR struct {
|
||||
router *router
|
||||
}
|
||||
|
||||
type publicreportNotification struct {
|
||||
Consent bool `json:"consent"`
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
Notification bool `json:"notification"`
|
||||
Phone string `json:"phone"`
|
||||
ReportID string `json:"report_id"`
|
||||
Subscription bool `json:"subscription"`
|
||||
}
|
||||
|
||||
func PublicreportNotification(r *router) *publicreportNotificationR {
|
||||
return &publicreportNotificationR{
|
||||
router: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (res *publicreportNotificationR) Create(ctx context.Context, r *http.Request, n publicreportNotification) (*publicreportNotification, *nhttp.ErrorWithStatus) {
|
||||
var err error
|
||||
var phone *types.E164
|
||||
if n.Phone != "" {
|
||||
phone, err = text.ParsePhoneNumber(n.Phone)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewBadRequest("can't parse phone: %w", err)
|
||||
}
|
||||
}
|
||||
err = platform.PublicreportNotificationCreate(ctx, platform.PublicreportNotification{
|
||||
Consent: n.Consent,
|
||||
Email: n.Email,
|
||||
Name: n.Name,
|
||||
Notification: n.Notification,
|
||||
Phone: phone,
|
||||
ReportID: n.ReportID,
|
||||
Subscription: n.Subscription,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("create notification: %w", err)
|
||||
}
|
||||
log.Info().Str("name", n.Name).Str("email", n.Email).Str("phone", n.Phone).Str("report_id", n.ReportID).Msg("Added reporter data")
|
||||
return &n, nil
|
||||
}
|
||||
|
|
@ -48,11 +48,14 @@ func (r *router) UUIDFromURI(route string, uri string) (*uuid.UUID, error) {
|
|||
}
|
||||
func (r *router) IDToURI(route string, id int) (string, error) {
|
||||
i := strconv.FormatInt(int64(id), 10)
|
||||
return r.IDStrToURI(route, i)
|
||||
}
|
||||
func (r *router) IDStrToURI(route string, id string) (string, error) {
|
||||
handler := r.router.Get(route)
|
||||
if handler == nil {
|
||||
return "", fmt.Errorf("nil handler '%s'", route)
|
||||
}
|
||||
uri, err := handler.URL("id", i)
|
||||
uri, err := handler.URL("id", id)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("build uri: %w", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue