Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatfile #2

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/aws-lambda/flatfile-csv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"log"
"context"
"fmt"
"strings"
"os"
// "strconv"
"io/ioutil"
"encoding/json"
"net/http"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions src/retool/custom_components/flatfile.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down