From 4b79784b3544d754e829c665336c352b6c452f9b Mon Sep 17 00:00:00 2001 From: Russell Tran Date: Sun, 21 Mar 2021 22:06:14 -0700 Subject: [PATCH 1/5] Add recordhook that works on webhooks.site --- src/retool/custom_components/flatfile.html | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/retool/custom_components/flatfile.html b/src/retool/custom_components/flatfile.html index 9952e9c..2b41163 100644 --- a/src/retool/custom_components/flatfile.html +++ b/src/retool/custom_components/flatfile.html @@ -55,6 +55,48 @@ name: JSON.stringify(model.superFields) }); + importer.registerRecordHook(async record => { + let returnedEmails = await fetch('https://fake-api-call.com/emails', { + method: 'POST', + mode: 'cors', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(record) + }).then(response => response.json()); + // assuming the POST request returns the proper data format + return returnedEmails; + }); + + importer.registerRecordHook(record => { + console.log(record); + return record + }); + + + + /* + let out = {} + if (!record.zip && !record.city && !record.state) { + out.state = { + info: [ + { + message: 'City and State required without Zip code', + level: 'error' + } + ] + } + } + return out + }); +{ + info: [ + { + message: 'City and State required without Zip code', + level: 'error' + } + ] +} + */ + $("#portal").click(function () { importer From d6efb79f7a7a48d578facc33fbdcd30e4f3ab0fa Mon Sep 17 00:00:00 2001 From: Russell Tran Date: Sun, 21 Mar 2021 22:07:15 -0700 Subject: [PATCH 2/5] WIP validatePlant but it's slowly dawning on me that Golang is absolutely the wrong language for this particular loose-footed data handling and validation --- src/aws-lambda/flatfile-csv/main.go | 49 ++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/aws-lambda/flatfile-csv/main.go b/src/aws-lambda/flatfile-csv/main.go index 5359d7d..ceed329 100644 --- a/src/aws-lambda/flatfile-csv/main.go +++ b/src/aws-lambda/flatfile-csv/main.go @@ -5,7 +5,9 @@ import ( "log" "context" "fmt" + "strings" "os" + "strconv" "io/ioutil" "encoding/json" "net/http" @@ -64,6 +66,51 @@ func sendErrorResponse(w http.ResponseWriter, err error) { log.Println("Error:", err) } +func getKeys(m map[string]interface{}) []string { + keys := make([]string, len(m)) + i := 0 + for k := range m { + keys[i] = k + i++ + } + return keys +} + +func ValidatePlant(w http.ResponseWriter, r *http.Request) { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + sendErrorResponse(w, err) + return + } + + var data map[string]interface{} + err := json.Unmarshal(body, &data) + if err != nil { + sendErrorResponse(w, err) + return + } + + if plant_id, ok := data["plant_id"]; !ok { + sendErrorResponse(w, errors.New("plant_id was missing from row")) // TODO: reflect error to user? + return + } + + keys := getKeys(data) + // Complicated way to basically select only the fields which were given in the input data + // while ensuring inputs are still sanitized per good practice + columnsPlaceholder := "" + for i, _ := range keys { + columnsPlaceholder += "$" + strconv.Itoa(i) + "," + " " + } + query := "SELECT " + columnsPlaceholder + "FROM plant WHERE plant_id = " + "$" + Itoa(len(keys)) + preexistingRow, err := db.QueryRow(query, keys..., plant_id) + + // TODO: row.scan https://kylewbanks.com/blog/query-result-to-map-in-golang + // and compare each thingy except those that are null + // return good or bad with user-formatted error + +} + // adds a copy of each metadata field to each row func SynthesizeSubmitPayload(payload string) (string, error) { // metadata stored as stringified json in data.customer.name @@ -123,7 +170,7 @@ func GenerateSetListing(fields []string) string { x := "" for i, field := range fields { if i != 0 { - x += " " + x += strings.Repeat(" ", 4) } x += fmt.Sprintf("%s = subquery.%s", field, field) if i != len(fields) - 1 { From c6292e9715b6f59d5843d941160d176e2f24153e Mon Sep 17 00:00:00 2001 From: Russell Tran Date: Sun, 21 Mar 2021 22:16:01 -0700 Subject: [PATCH 3/5] Remove junk recordhook --- src/retool/custom_components/flatfile.html | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/retool/custom_components/flatfile.html b/src/retool/custom_components/flatfile.html index 2b41163..6a1be11 100644 --- a/src/retool/custom_components/flatfile.html +++ b/src/retool/custom_components/flatfile.html @@ -66,13 +66,6 @@ return returnedEmails; }); - importer.registerRecordHook(record => { - console.log(record); - return record - }); - - - /* let out = {} if (!record.zip && !record.city && !record.state) { From 30ca8d17bb4aa75579c0392e497ef6f9986db8b0 Mon Sep 17 00:00:00 2001 From: Russell Tran Date: Mon, 22 Mar 2021 09:13:33 -0700 Subject: [PATCH 4/5] Comment out unfinished function --- src/aws-lambda/flatfile-csv/main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/aws-lambda/flatfile-csv/main.go b/src/aws-lambda/flatfile-csv/main.go index ceed329..7f8b979 100644 --- a/src/aws-lambda/flatfile-csv/main.go +++ b/src/aws-lambda/flatfile-csv/main.go @@ -77,6 +77,11 @@ func getKeys(m map[string]interface{}) []string { } func ValidatePlant(w http.ResponseWriter, r *http.Request) { + + sendErrorResponse(w, errors.New("Function not written yet")) + return + + /* body, err := ioutil.ReadAll(r.Body) if err != nil { sendErrorResponse(w, err) @@ -104,6 +109,7 @@ func ValidatePlant(w http.ResponseWriter, r *http.Request) { } query := "SELECT " + columnsPlaceholder + "FROM plant WHERE plant_id = " + "$" + Itoa(len(keys)) preexistingRow, err := db.QueryRow(query, keys..., plant_id) + */ // TODO: row.scan https://kylewbanks.com/blog/query-result-to-map-in-golang // and compare each thingy except those that are null From 7bdd7bd3a0585cfa466f2ab1809c7213f5fff795 Mon Sep 17 00:00:00 2001 From: Russell Tran Date: Mon, 22 Mar 2021 09:14:04 -0700 Subject: [PATCH 5/5] Comment out unused library --- src/aws-lambda/flatfile-csv/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aws-lambda/flatfile-csv/main.go b/src/aws-lambda/flatfile-csv/main.go index 7f8b979..cc493d8 100644 --- a/src/aws-lambda/flatfile-csv/main.go +++ b/src/aws-lambda/flatfile-csv/main.go @@ -7,7 +7,7 @@ import ( "fmt" "strings" "os" - "strconv" + // "strconv" "io/ioutil" "encoding/json" "net/http" @@ -80,7 +80,7 @@ func ValidatePlant(w http.ResponseWriter, r *http.Request) { sendErrorResponse(w, errors.New("Function not written yet")) return - + /* body, err := ioutil.ReadAll(r.Body) if err != nil {