Split out ability to upload flyover data from pool uploads
Tons of changes here, all in the name of quickly getting to where I can create test compliance letters.
This commit is contained in:
parent
9939434cb3
commit
ff2ec0ad14
38 changed files with 4204 additions and 233 deletions
|
|
@ -1,14 +1,16 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
//"strconv"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/html"
|
||||
//"github.com/google/uuid"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/pdf"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
|
|
@ -22,8 +24,26 @@ type contentMailer struct {
|
|||
ReportURL string
|
||||
}
|
||||
|
||||
// func getMailer(ctx context.Context, r *http.Request, org *models.Organization, user *models.User) (*response[contentMailer], *errorWithStatus) {
|
||||
func getMailer(w http.ResponseWriter, r *http.Request) {
|
||||
code := chi.URLParam(r, "code")
|
||||
if code == "" {
|
||||
http.Error(w, "empty code", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := pdf.GeneratePDF(r.Context(), code)
|
||||
if err != nil {
|
||||
respondError(w, "generate pdf failure", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/pdf")
|
||||
disposition := fmt.Sprintf("attachment; filename=\"compliance-mailer-%s.pdf\"", code)
|
||||
w.Header().Set("Content-Disposition", disposition)
|
||||
_, err = io.Copy(w, bytes.NewReader(content))
|
||||
if err != nil {
|
||||
respondError(w, "copy error", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
func getMailerPreview(w http.ResponseWriter, r *http.Request) {
|
||||
code := chi.URLParam(r, "code")
|
||||
|
|
|
|||
|
|
@ -60,11 +60,12 @@ func Router() chi.Router {
|
|||
r.Method("GET", "/configuration/pesticide/add", authenticatedHandler(getConfigurationPesticideAdd))
|
||||
r.Method("GET", "/configuration/upload", authenticatedHandler(getUploadList))
|
||||
r.Method("GET", "/configuration/upload/pool", authenticatedHandler(getUploadPool))
|
||||
r.Method("GET", "/configuration/upload/pool/bob", authenticatedHandler(getUploadPoolBobCreate))
|
||||
r.Method("POST", "/configuration/upload/pool/bob", authenticatedHandlerPostMultipart(postUploadPoolBobCreate))
|
||||
r.Method("GET", "/configuration/upload/pool/flyover", authenticatedHandler(getUploadPoolFlyoverCreate))
|
||||
r.Method("POST", "/configuration/upload/pool/flyover", authenticatedHandlerPostMultipart(postUploadPoolFlyoverCreate))
|
||||
r.Method("GET", "/configuration/upload/pool/custom", authenticatedHandler(getUploadPoolCustomCreate))
|
||||
r.Method("POST", "/configuration/upload/pool/custom", authenticatedHandlerPostMultipart(postUploadPoolCustomCreate))
|
||||
r.Method("GET", "/configuration/upload/{id}", authenticatedHandler(getUploadByID))
|
||||
r.Method("POST", "/configuration/upload/{id}/commit", authenticatedHandlerPost(postUploadCommit))
|
||||
r.Method("POST", "/configuration/upload/{id}/discard", authenticatedHandlerPost(postUploadDiscard))
|
||||
r.Method("GET", "/configuration/user", authenticatedHandler(getConfigurationUserList))
|
||||
r.Method("GET", "/configuration/user/add", authenticatedHandler(getConfigurationUserAdd))
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@ import (
|
|||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/userfile"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type contentUploadList struct {
|
||||
|
|
@ -25,25 +26,13 @@ func getUploadList(ctx context.Context, r *http.Request, org *models.Organizatio
|
|||
}), newErrorMaybe("get upload list: %w", err)
|
||||
}
|
||||
|
||||
type contentUploadURL struct {
|
||||
Discard string // URL for discarding the upload
|
||||
}
|
||||
|
||||
func newContentUploadURL(id int32) contentUploadURL {
|
||||
id_str := strconv.FormatInt(int64(id), 10)
|
||||
return contentUploadURL{
|
||||
Discard: config.MakeURLNidus("/upload/%s/discard", id_str),
|
||||
}
|
||||
}
|
||||
|
||||
type contentUploadDetail struct {
|
||||
CSVFileID int32
|
||||
Organization *models.Organization
|
||||
Upload platform.UploadPoolDetail
|
||||
URL contentUploadURL
|
||||
}
|
||||
type contentUploadPoolList struct {
|
||||
Uploads []platform.PoolUpload
|
||||
Uploads []platform.Upload
|
||||
}
|
||||
type contentUploadPool struct{}
|
||||
|
||||
|
|
@ -52,11 +41,11 @@ func getUploadPool(ctx context.Context, r *http.Request, org *models.Organizatio
|
|||
return newResponse("sync/upload-csv-pool.html", data), nil
|
||||
}
|
||||
|
||||
type contentUploadPoolBobCreate struct{}
|
||||
type contentUploadPoolFlyoverCreate struct{}
|
||||
|
||||
func getUploadPoolBobCreate(ctx context.Context, r *http.Request, org *models.Organization, u *models.User) (*response[contentUploadPoolBobCreate], *errorWithStatus) {
|
||||
data := contentUploadPoolBobCreate{}
|
||||
return newResponse("sync/upload-csv-pool-bob.html", data), nil
|
||||
func getUploadPoolFlyoverCreate(ctx context.Context, r *http.Request, org *models.Organization, u *models.User) (*response[contentUploadPoolFlyoverCreate], *errorWithStatus) {
|
||||
data := contentUploadPoolFlyoverCreate{}
|
||||
return newResponse("sync/upload-csv-pool-flyover.html", data), nil
|
||||
}
|
||||
|
||||
type contentUploadPoolCustomCreate struct{}
|
||||
|
|
@ -66,6 +55,8 @@ func getUploadPoolCustomCreate(ctx context.Context, r *http.Request, org *models
|
|||
return newResponse("sync/upload-csv-pool-custom.html", data), nil
|
||||
}
|
||||
func getUploadByID(ctx context.Context, r *http.Request, org *models.Organization, u *models.User) (*response[contentUploadDetail], *errorWithStatus) {
|
||||
test := newContentURLUpload()
|
||||
log.Info().Str("output", test.Discard(123)).Send()
|
||||
file_id_str := chi.URLParam(r, "id")
|
||||
file_id_, err := strconv.ParseInt(file_id_str, 10, 32)
|
||||
if err != nil {
|
||||
|
|
@ -80,11 +71,25 @@ func getUploadByID(ctx context.Context, r *http.Request, org *models.Organizatio
|
|||
CSVFileID: file_id,
|
||||
Organization: org,
|
||||
Upload: detail,
|
||||
URL: newContentUploadURL(file_id),
|
||||
}
|
||||
return newResponse("sync/upload-by-id.html", data), nil
|
||||
}
|
||||
|
||||
type FormUploadCommit struct{}
|
||||
|
||||
func postUploadCommit(ctx context.Context, r *http.Request, org *models.Organization, u *models.User, f FormUploadCommit) (string, *errorWithStatus) {
|
||||
file_id_str := chi.URLParam(r, "id")
|
||||
file_id_, err := strconv.ParseInt(file_id_str, 10, 32)
|
||||
if err != nil {
|
||||
return "", newError("Failed to parse file_id: %w", err)
|
||||
}
|
||||
err = platform.UploadCommit(ctx, org, int32(file_id_))
|
||||
if err != nil {
|
||||
return "", newError("Failed to mark discarded: %w", err)
|
||||
}
|
||||
return "/configuration/upload", nil
|
||||
}
|
||||
|
||||
type FormUploadDiscard struct{}
|
||||
|
||||
func postUploadDiscard(ctx context.Context, r *http.Request, org *models.Organization, u *models.User, f FormUploadDiscard) (string, *errorWithStatus) {
|
||||
|
|
@ -97,13 +102,28 @@ func postUploadDiscard(ctx context.Context, r *http.Request, org *models.Organiz
|
|||
if err != nil {
|
||||
return "", newError("Failed to mark discarded: %w", err)
|
||||
}
|
||||
return "/upload", nil
|
||||
return "/configuration/upload", nil
|
||||
}
|
||||
|
||||
type FormUploadPool struct{}
|
||||
|
||||
func postUploadPoolBobCreate(ctx context.Context, r *http.Request, org *models.Organization, u *models.User, f FormUploadPool) (string, *errorWithStatus) {
|
||||
return "", nil
|
||||
func postUploadPoolFlyoverCreate(ctx context.Context, r *http.Request, org *models.Organization, u *models.User, f FormUploadPool) (string, *errorWithStatus) {
|
||||
uploads, err := userfile.SaveFileUpload(r, "csvfile", userfile.CollectionCSV)
|
||||
if err != nil {
|
||||
return "", newError("Failed to extract image uploads: %s", err)
|
||||
}
|
||||
if len(uploads) == 0 {
|
||||
return "", newErrorStatus(http.StatusBadRequest, "No upload found")
|
||||
}
|
||||
if len(uploads) != 1 {
|
||||
return "", newErrorStatus(http.StatusBadRequest, "You must only submit one file at a time")
|
||||
}
|
||||
upload := uploads[0]
|
||||
saved_upload, err := platform.NewUpload(r.Context(), u, upload, enums.FileuploadCsvtypeFlyover)
|
||||
if err != nil {
|
||||
return "", newError("Failed to create new pool: %w", err)
|
||||
}
|
||||
return fmt.Sprintf("/configuration/upload/%d", saved_upload.ID), nil
|
||||
}
|
||||
func postUploadPoolCustomCreate(ctx context.Context, r *http.Request, org *models.Organization, u *models.User, f FormUploadPool) (string, *errorWithStatus) {
|
||||
uploads, err := userfile.SaveFileUpload(r, "csvfile", userfile.CollectionCSV)
|
||||
|
|
@ -117,9 +137,9 @@ func postUploadPoolCustomCreate(ctx context.Context, r *http.Request, org *model
|
|||
return "", newErrorStatus(http.StatusBadRequest, "You must only submit one file at a time")
|
||||
}
|
||||
upload := uploads[0]
|
||||
pool_upload, err := platform.NewPoolUpload(r.Context(), u, upload)
|
||||
pool_upload, err := platform.NewUpload(r.Context(), u, upload, enums.FileuploadCsvtypePoollist)
|
||||
if err != nil {
|
||||
return "", newError("Failed to create new pool: %w", err)
|
||||
}
|
||||
return fmt.Sprintf("/upload/%d", pool_upload.ID), nil
|
||||
return fmt.Sprintf("/configuration/upload/%d", pool_upload.ID), nil
|
||||
}
|
||||
|
|
|
|||
18
sync/url.go
18
sync/url.go
|
|
@ -1,6 +1,8 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
)
|
||||
|
||||
|
|
@ -74,17 +76,29 @@ func newContentURLSidebar() contentURLSidebar {
|
|||
}
|
||||
}
|
||||
|
||||
type urlForID = func(int) string
|
||||
|
||||
func makeURLForID(pattern string) urlForID {
|
||||
return func(id int) string {
|
||||
return config.MakeURLNidus(pattern, strconv.Itoa(id))
|
||||
}
|
||||
}
|
||||
|
||||
type contentURLUpload struct {
|
||||
Commit urlForID
|
||||
Discard urlForID
|
||||
Pool string
|
||||
PoolBob string
|
||||
PoolCustom string
|
||||
PoolFlyover string
|
||||
SamplePoolCSV string
|
||||
}
|
||||
|
||||
func newContentURLUpload() contentURLUpload {
|
||||
return contentURLUpload{
|
||||
Commit: makeURLForID("/configuration/upload/%s/commit"),
|
||||
Discard: makeURLForID("/configuration/upload/%s/discard"),
|
||||
Pool: config.MakeURLNidus("/configuration/upload/pool"),
|
||||
PoolBob: config.MakeURLNidus("/configuration/upload/pool/bob"),
|
||||
PoolFlyover: config.MakeURLNidus("/configuration/upload/pool/flyover"),
|
||||
PoolCustom: config.MakeURLNidus("/configuration/upload/pool/custom"),
|
||||
SamplePoolCSV: config.MakeURLNidus("/static/file/sample-pool.csv"),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue