maybe better logic on project numbers...

This commit is contained in:
Erik Eckert 2023-12-21 11:52:52 -07:00
parent d21aab1514
commit b9d1de2d52

View File

@ -36,12 +36,6 @@ func MPE_IsProject(input *Payload) (projectnumber string, project_name string) {
// iterate through the path_data map. We want to find all strings that start with a number.
for _, value := range separated_path {
matched := project_regex.MatchString(value)
// matched, err := regexp.MatchString(`(^[0-9]{1,3})`, value)
// if err != nil {
// log.Fatal(err)
// fmt.Println("oops")
// return "RegexError", "RegExError"
// }
if matched {
// if we match the Regex above, write values to SB. This will give us a string like "73|10 Office Of Somone|153 HelpMe|002 Phase 2"
@ -54,8 +48,11 @@ func MPE_IsProject(input *Payload) (projectnumber string, project_name string) {
project_text := sb.String()
project_slice := strings.Split(project_text, "|")
// first value should be a digit, if not this can't be a project
isProject, _ := regexp.MatchString(`(^[0-9])`, project_slice[0])
if isProject {
isProject0, _ := regexp.MatchString(`(^[0-9])`, project_slice[0])
isProject1, _ := regexp.MatchString(`(^[0-9])`, project_slice[1])
isProject2, _ := regexp.MatchString(`(^[0-9])`, project_slice[2])
// isProject, _ = regexp.MatchString(`(^[0-9])`, project_slice[3])
if isProject0 || isProject1 || isProject2 {
project_regex, _ := regexp.Compile(`(^[0-9]{1,3})`) // regex to match against
regex_tidy_string, _ := regexp.Compile(`[-_]+|^\s`) //regex to tidy up folder paths
@ -71,6 +68,14 @@ func MPE_IsProject(input *Payload) (projectnumber string, project_name string) {
// do we have a third section of the project number?
project_third := strings.Split(project_slice[3], " ")[0]
project_third_match, _ := regexp.MatchString(`(^[0-9]{1,3})`, project_third)
/* TODO: Better identification of the third number.
Currently seeing paths such as "/shared/n-data/12/34 test/45 ProjectName/deliverables/05 geotech" generating project numbers of 1234-45-05,
where it should be 1234-45
This is due to the RegEx Match looking for all folders that start with digits, and not being concerned about where they are in the string.
*/
if project_third_match {
// if we have a third section, then add another section to the project number, and append to the project name
third_project_name := project_regex.ReplaceAllString(project_slice[3], "") // 7310-10-153
@ -114,9 +119,12 @@ func MPE_ValidateForward(in *Payload) bool {
func readDataStream(resp http.ResponseWriter, request *http.Request) {
// reqBody, _ := io.ReadAll(request.Body)
body_string, _ := io.ReadAll(request.Body)
// fmt.Println(string(body_string)) // debug code to show JASON data coming in
var request_body Payload
var log_values log_data
var request_body Payload // init request_body as data type Payload - uses custom structs
body_string, _ := io.ReadAll(request.Body) // ready Body Data
log_values.BodyData = string(body_string) // prep for logging
fmt.Printf("%+v\n", log_values) // debug code to show JASON data coming in
err := json.Unmarshal(body_string, &request_body)
if err != nil {
var error_sb strings.Builder
@ -132,20 +140,23 @@ func readDataStream(resp http.ResponseWriter, request *http.Request) {
return
}
var MPE_ShouldFoward = MPE_ValidateForward(&request_body)
var MPE_ProjectNumber, MPE_ProjectName = MPE_IsProject(&request_body)
// Collect EventID, JSON Content, and return values into data object to be logged to screen. Validate data visually for a bit against "live" webhook data.
var log_values log_data
log_values.EventID = request_body.Data[0].EventID
log_values.EventType = request_body.Data[0].EventType
log_values.User = request_body.Data[0].User.DisplayName
log_values.TargetPath = request_body.Data[0].PathData.TargetPath
fmt.Printf("%+v\n", log_values) // output log values to stdout - first round
var MPE_ShouldFoward = MPE_ValidateForward(&request_body)
var MPE_ProjectNumber, MPE_ProjectName = MPE_IsProject(&request_body)
log_values.ExternalShare = MPE_ShouldFoward
log_values.MPE_ProjectName = MPE_ProjectName
log_values.MPE_ProjectNumber = MPE_ProjectNumber
fmt.Printf("%+v\n", log_values) // output log values to stdout
fmt.Printf("%+v\n", log_values) // output log values to stdout - second round
// Collect EventID, JSON Content, and return values into data object to be logged to screen. Validate data visually for a bit against "live" webhook data.
// TODO: Once Data validation / Struct is built, pass data off to next Webhook URL for processing.
}
@ -221,4 +232,5 @@ type log_data struct {
ExternalShare bool
MPE_ProjectNumber string
MPE_ProjectName string
BodyData string
}