diff --git a/src/aws-lambda/flatfile-csv/main.go b/src/aws-lambda/flatfile-csv/main.go index 5359d7d..cc493d8 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,57 @@ 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) { + + sendErrorResponse(w, errors.New("Function not written yet")) + return + + /* + 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 +176,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 { diff --git a/src/retool/custom_components/flatfile.html b/src/retool/custom_components/flatfile.html index 9952e9c..6a1be11 100644 --- a/src/retool/custom_components/flatfile.html +++ b/src/retool/custom_components/flatfile.html @@ -55,6 +55,41 @@ 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; + }); + + /* + 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