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.
189 lines
5.4 KiB
189 lines
5.4 KiB
package menu
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"marmic/servicetrade-toolbox/internal/api"
|
|
"marmic/servicetrade-toolbox/internal/ui"
|
|
)
|
|
|
|
func HandleJobs(session *api.Session) {
|
|
jobsMenu := getJobsMenu()
|
|
for {
|
|
choice := DisplayMenuAndGetChoice(jobsMenu, "Jobs Menu")
|
|
if choice == len(jobsMenu)+1 {
|
|
return // Go back to main menu
|
|
}
|
|
jobsMenu[choice-1].Handler(session)
|
|
}
|
|
}
|
|
|
|
func getJobsMenu() []MenuItem {
|
|
return []MenuItem{
|
|
{"Search Job by ID", searchJobByID, nil},
|
|
{"List Recent Jobs", listRecentJobs, nil},
|
|
{"Create New Job", createNewJob, nil},
|
|
{"Manage Job Attachments", manageJobAttachments, nil},
|
|
{"View Deficiencies", viewDeficiencyById, nil},
|
|
}
|
|
}
|
|
|
|
func searchJobByID(session *api.Session) {
|
|
ui.ClearScreen()
|
|
ui.DisplayMessage("We will search a job ID here.")
|
|
ui.PressEnterToContinue()
|
|
}
|
|
|
|
func listRecentJobs(session *api.Session) {
|
|
ui.ClearScreen()
|
|
ui.DisplayMessage("Listing recent jobs...")
|
|
ui.PressEnterToContinue()
|
|
}
|
|
|
|
func createNewJob(session *api.Session) {
|
|
ui.ClearScreen()
|
|
ui.DisplayMessage("Creating a new job...")
|
|
ui.PressEnterToContinue()
|
|
}
|
|
|
|
func manageJobAttachments(session *api.Session) {
|
|
ui.ClearScreen()
|
|
jobID := ui.PromptForInput("Enter Job ID: ")
|
|
|
|
attachments, err := session.GetAttachmentsForJob(jobID)
|
|
if err != nil {
|
|
ui.DisplayError("Failed to retrieve attachments:", err)
|
|
return
|
|
}
|
|
|
|
// Display attachments
|
|
ui.DisplayMessage("Attachments for Job " + jobID + ":")
|
|
if dataMap, ok := attachments["data"].(map[string]interface{}); ok {
|
|
if attachmentsList, ok := dataMap["attachments"].([]interface{}); ok {
|
|
for i, attachment := range attachmentsList {
|
|
if att, ok := attachment.(map[string]interface{}); ok {
|
|
fmt.Printf("%d. %s\n", i+1, att["fileName"])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Prompt for attachments to delete
|
|
toDelete := ui.PromptForInput("Enter the numbers of attachments to delete (comma-separated), or 'all' for all: ")
|
|
|
|
var filesToDelete []string
|
|
if toDelete == "all" {
|
|
if dataMap, ok := attachments["data"].(map[string]interface{}); ok {
|
|
if attachmentsList, ok := dataMap["attachments"].([]interface{}); ok {
|
|
for _, attachment := range attachmentsList {
|
|
if att, ok := attachment.(map[string]interface{}); ok {
|
|
filesToDelete = append(filesToDelete, att["fileName"].(string))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
// Parse the input and get the corresponding filenames
|
|
// This part needs to be implemented
|
|
}
|
|
|
|
// Generate delete endpoints
|
|
endpoints := generateDeleteEndpoints(attachments, filesToDelete)
|
|
|
|
// Confirm deletion
|
|
ui.DisplayMessage(fmt.Sprintf("You are about to delete %d attachments. Are you sure? (y/n)", len(endpoints)))
|
|
confirm := ui.PromptForInput("")
|
|
if confirm != "y" {
|
|
ui.DisplayMessage("Deletion cancelled.")
|
|
return
|
|
}
|
|
|
|
// Perform deletion
|
|
for _, endpoint := range endpoints {
|
|
err := session.DeleteAttachment(endpoint)
|
|
if err != nil {
|
|
ui.DisplayError(fmt.Sprintf("Failed to delete attachment %s:", endpoint), err)
|
|
} else {
|
|
ui.DisplayMessage(fmt.Sprintf("Successfully deleted attachment: %s", endpoint))
|
|
}
|
|
}
|
|
|
|
ui.DisplayMessage("Attachment management completed.")
|
|
ui.PressEnterToContinue()
|
|
}
|
|
|
|
func viewDeficiencyById(session *api.Session) {
|
|
ui.ClearScreen()
|
|
deficiencyId := ui.PromptForInput("Enter Deficiency ID: ")
|
|
|
|
ui.DisplayMessage(fmt.Sprintf("Fetching information for Deficiency %s...", deficiencyId))
|
|
|
|
result, err := session.GetDeficiencyById(deficiencyId)
|
|
if err != nil {
|
|
ui.DisplayError("Failed to retrieve deficiency information:", err)
|
|
ui.PressEnterToContinue()
|
|
return
|
|
}
|
|
|
|
ui.ClearScreen()
|
|
ui.DisplayMessage(fmt.Sprintf("Information for Deficiency %s:", deficiencyId))
|
|
|
|
if data, ok := result["data"].(map[string]interface{}); ok {
|
|
if len(data) == 0 {
|
|
ui.DisplayMessage("No information found for this deficiency.")
|
|
} else {
|
|
fmt.Println("Deficiency Details:")
|
|
for key, value := range data {
|
|
fmt.Printf("- %s:\n", key)
|
|
if details, ok := value.(map[string]interface{}); ok {
|
|
for detailKey, detailValue := range details {
|
|
if detailValue != nil {
|
|
// Handle potential json.Number values
|
|
if num, ok := detailValue.(json.Number); ok {
|
|
fmt.Printf(" %s: %s\n", detailKey, num.String())
|
|
} else {
|
|
fmt.Printf(" %s: %v\n", detailKey, detailValue)
|
|
}
|
|
}
|
|
}
|
|
} else if num, ok := value.(json.Number); ok {
|
|
fmt.Printf(" %s\n", num.String())
|
|
} else {
|
|
fmt.Printf(" %v\n", value)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|
|
} else {
|
|
ui.DisplayMessage("Unexpected data structure in the API response.")
|
|
fmt.Printf("Response structure: %+v\n", result)
|
|
}
|
|
|
|
ui.DisplayMessage("\nDeficiency information viewing completed.")
|
|
ui.PressEnterToContinue()
|
|
}
|
|
|
|
// Helper function to generate delete endpoints
|
|
func generateDeleteEndpoints(data map[string]interface{}, filenames []string) []string {
|
|
var endpoints []string
|
|
|
|
filenamesToDeleteMap := make(map[string]struct{})
|
|
for _, name := range filenames {
|
|
filenamesToDeleteMap[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 {
|
|
if _, exists := filenamesToDeleteMap[filename]; exists {
|
|
endpoints = append(endpoints, fmt.Sprintf("https://api.servicetrade.com/api/attachment/%d", int64(attachment["id"].(float64))))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return endpoints
|
|
}
|
|
|