You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.1 KiB
72 lines
2.1 KiB
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"marmic/servicetrade-toolbox/internal/auth"
|
|
"strings"
|
|
)
|
|
|
|
func GetAttachmentsForJob(session *auth.Session, jobID string) (map[string]interface{}, error) {
|
|
url := fmt.Sprintf("%s/job/%s/paperwork", BaseURL, jobID)
|
|
req, err := AuthenticatedRequest(session, "GET", url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %v", err)
|
|
}
|
|
|
|
resp, err := DoAuthenticatedRequest(session, req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result map[string]interface{}
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return nil, fmt.Errorf("error decoding response: %v", err)
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func GenerateDeleteEndpoints(data map[string]interface{}, filenames []string) []string {
|
|
var endpoints []string
|
|
filenamesToDeleteMap := make(map[string]struct{})
|
|
for _, name := range filenames {
|
|
filenamesToDeleteMap[strings.ToLower(strings.TrimSpace(name))] = struct{}{}
|
|
}
|
|
|
|
if dataMap, ok := data["data"].(map[string]interface{}); ok {
|
|
if attachments, ok := dataMap["attachments"].([]interface{}); ok {
|
|
for _, item := range attachments {
|
|
attachment := item.(map[string]interface{})
|
|
if filename, ok := attachment["fileName"].(string); ok {
|
|
trimmedFilename := strings.ToLower(strings.TrimSpace(filename))
|
|
if _, exists := filenamesToDeleteMap[trimmedFilename]; exists {
|
|
endpoints = append(endpoints, fmt.Sprintf("%s/attachment/%d", BaseURL, int64(attachment["id"].(float64))))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return endpoints
|
|
}
|
|
|
|
func DeleteAttachment(session *auth.Session, endpoint string) error {
|
|
req, err := AuthenticatedRequest(session, "DELETE", endpoint, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create DELETE request: %v", err)
|
|
}
|
|
|
|
resp, err := DoAuthenticatedRequest(session, req)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to send DELETE request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 && resp.StatusCode != 204 {
|
|
return fmt.Errorf("failed to delete attachment: %s", resp.Status)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|