diff --git a/groupTasks/groups/production1/index.json b/groupTasks/groups/production1/index.json index 281ed7f..f682da9 100644 --- a/groupTasks/groups/production1/index.json +++ b/groupTasks/groups/production1/index.json @@ -16,6 +16,16 @@ "parameterName": "kiste2", "type": "textarea", "displayName": "Nummer der zweiten Kiste yooo" + }, + { + "parameterName": "3d_printer_machine_selection", + "type": "select_machine", + "displayName": "3D Drucker auswählen", + "options": { + "location": 1, + "whitelist": [1, 2], + "blacklist": [0, 5] + } } ], "tasks": [ diff --git a/modules/equipment/equipment.go b/modules/equipment/equipment.go index 0576022..5059a0d 100644 --- a/modules/equipment/equipment.go +++ b/modules/equipment/equipment.go @@ -6,6 +6,7 @@ import ( "jannex/admin-dashboard-backend/modules/config" "jannex/admin-dashboard-backend/modules/database" "jannex/admin-dashboard-backend/modules/logger" + "jannex/admin-dashboard-backend/modules/requestclient" "jannex/admin-dashboard-backend/modules/structs" "jannex/admin-dashboard-backend/modules/utils" "os" @@ -17,57 +18,7 @@ import ( "github.com/rs/zerolog/log" ) -const Base = "https://inv.ex.umbach.dev" -const apiBase = Base + "/api" -const ApiToken = "1367f15d21935e4eb540f897946fb5cd98485c3f" -func InvexApiRequestClient(requestMethod string, url string) (statusCode int, body []byte, err error) { - a := fiber.AcquireAgent() - - a.Add("Authorization", "Token "+ApiToken) - - req := a.Request() - req.Header.SetMethod(requestMethod) - req.SetRequestURI(url) - - if err := a.Parse(); err != nil { - log.Error().Msgf("Failed to parse request, err: %s", err) - return 0, nil, err - } - - code, body, _ := a.Bytes() - - if code == 401 { - log.Error().Msgf("invex not authorized, code: %d", code) - - logger.AddSystemLog(structs.LogMessage{ - Id: 29, - Type: utils.LogTypeInfo, - Messages: []structs.LogData{ - {Type: "error", Value: "invex not authorized"}, - {Type: "statusCode", Value: strconv.Itoa(code)}, - }, - }) - - return code, nil, err - } - - if code == 404 { - log.Error().Msgf("Invex stock item not found, code: %d", code) - - logger.AddSystemLog(structs.LogMessage{ - Id: 29, - Type: utils.LogTypeInfo, - Messages: []structs.LogData{ - {Type: "error", Value: "invex stock item not found"}, - {Type: "statusCode", Value: strconv.Itoa(code)}, - }, - }) - return code, nil, err - } - - return code, body, nil -} type Notes struct { Image string @@ -189,7 +140,7 @@ func GetEquipmentDocumentations(stockItemId string, query structs.PageQuery, c * if len(documentations) == 0 { // there are no documentations for this equipment on the our database // so there will be checked on invex if the stock item exists - statusCode, _, err = InvexApiRequestClient(fiber.MethodGet, apiBase+"/stock/"+stockItemId+"/") + statusCode, _, err = requestclient.InvexApiRequestClient(fiber.MethodGet, requestclient.ApiBase+"/stock/"+stockItemId+"/") if err != nil { log.Error().Msgf("Invex api request error: %s", err) @@ -341,7 +292,7 @@ func isInList(fileName string, notes []Notes) bool { // fetching the thumbnail from the invex server and sending it back to the client func GetEquipmentInvexThumbnail(c *fiber.Ctx, stockItemId string) error { // first request to /api/stock/:stockItemId/ to get the thumbnail url - _, body, err := InvexApiRequestClient(fiber.MethodGet, apiBase+"/stock/"+stockItemId+"/") + _, body, err := requestclient.InvexApiRequestClient(fiber.MethodGet, requestclient.ApiBase+"/stock/"+stockItemId+"/") if err != nil { log.Error().Msgf("Invex api request error: %s", err) @@ -360,7 +311,7 @@ func GetEquipmentInvexThumbnail(c *fiber.Ctx, stockItemId string) error { thumbnail := partDetail["thumbnail"].(string) // second request to /media/part_images/:thumbnail to get the thumbnail image - _, body, err = InvexApiRequestClient(fiber.MethodGet, Base+"/"+thumbnail) + _, body, err = requestclient.InvexApiRequestClient(fiber.MethodGet, requestclient.Base+"/"+thumbnail) if err != nil { log.Error().Msgf("Invex api request error: %s", err) diff --git a/modules/requestclient/requestclient.go b/modules/requestclient/requestclient.go new file mode 100644 index 0000000..1756171 --- /dev/null +++ b/modules/requestclient/requestclient.go @@ -0,0 +1,63 @@ +package requestclient + +import ( + "jannex/admin-dashboard-backend/modules/logger" + "jannex/admin-dashboard-backend/modules/structs" + "jannex/admin-dashboard-backend/modules/utils" + "strconv" + + "github.com/gofiber/fiber/v2" + "github.com/rs/zerolog/log" +) + +const Base = "https://inv.ex.umbach.dev" +const ApiBase = Base + "/api" +const ApiToken = "1367f15d21935e4eb540f897946fb5cd98485c3f" + +func InvexApiRequestClient(requestMethod string, url string) (statusCode int, body []byte, err error) { + a := fiber.AcquireAgent() + + a.Add("Authorization", "Token "+ApiToken) + + req := a.Request() + req.Header.SetMethod(requestMethod) + req.SetRequestURI(url) + + if err := a.Parse(); err != nil { + log.Error().Msgf("Failed to parse request, err: %s", err) + return 0, nil, err + } + + code, body, _ := a.Bytes() + + if code == 401 { + log.Error().Msgf("invex not authorized, code: %d", code) + + logger.AddSystemLog(structs.LogMessage{ + Id: 29, + Type: utils.LogTypeInfo, + Messages: []structs.LogData{ + {Type: "error", Value: "invex not authorized"}, + {Type: "statusCode", Value: strconv.Itoa(code)}, + }, + }) + + return code, nil, err + } + + if code == 404 { + log.Error().Msgf("Invex stock item not found, code: %d", code) + + logger.AddSystemLog(structs.LogMessage{ + Id: 29, + Type: utils.LogTypeInfo, + Messages: []structs.LogData{ + {Type: "error", Value: "invex stock item not found"}, + {Type: "statusCode", Value: strconv.Itoa(code)}, + }, + }) + return code, nil, err + } + + return code, body, nil +} \ No newline at end of file diff --git a/modules/structs/grouptasks.go b/modules/structs/grouptasks.go index 9ddc5d2..2e380dd 100644 --- a/modules/structs/grouptasks.go +++ b/modules/structs/grouptasks.go @@ -54,9 +54,10 @@ type Group struct { } type GlobalInputs struct { - ParameterName string `json:"parameterName"` - Type string `json:"type"` - DisplayName string `json:"displayName"` + ParameterName string `json:"parameterName"` + Type string `json:"type"` + DisplayName string `json:"displayName"` + Options interface{} `json:"options"` } type Task struct { @@ -69,10 +70,11 @@ type Task struct { } type TaskParameter struct { - ParameterName string `json:"parameterName"` - Type string `json:"type"` - DisplayName string `json:"displayName"` - Global bool `json:"global"` + ParameterName string `json:"parameterName"` + Type string `json:"type"` + DisplayName string `json:"displayName"` + Global bool `json:"global"` + Options interface{} `json:"options"` } // used for ui when a user is writing into input field to lock the task step for other users diff --git a/modules/structs/machines.go b/modules/structs/machines.go new file mode 100644 index 0000000..fe5463d --- /dev/null +++ b/modules/structs/machines.go @@ -0,0 +1,23 @@ +package structs + +type MachinesBody struct { + Location int + Whitelist []int + Blacklist []int +} + +/* +type MachinesResponse struct { + Machines []Machine +} + +type Machine struct { + PartName string + PartThumbnail string + Notes MachineNotes +} + +type MachineNotes struct { + DisplayName string `json:"displayName"` + Ip string `json:"ip"` +} */ diff --git a/routers/router/api/v1/machines/machines.go b/routers/router/api/v1/machines/machines.go new file mode 100644 index 0000000..4478c63 --- /dev/null +++ b/routers/router/api/v1/machines/machines.go @@ -0,0 +1,67 @@ +package machines + +import ( + "encoding/json" + "jannex/admin-dashboard-backend/modules/requestclient" + "jannex/admin-dashboard-backend/modules/structs" + "jannex/admin-dashboard-backend/modules/utils" + + "github.com/gofiber/fiber/v2" + "github.com/rs/zerolog/log" +) + +// POST /v1/machines/ + +/* +{ + location: 2, + whitelist: [], + blacklist: [] +} +*/ + +type InvexApiStockListResponse struct { + Results []struct { + Notes string + Status int + PartDetail struct { + Name string + Thumbnail string + } `json:"part_detail"` + } +} + +func GetMachines(c *fiber.Ctx) error { + var body structs.MachinesBody + + if err := utils.BodyParserHelper(c, &body); err != nil { + return c.SendStatus(fiber.StatusBadRequest) + } + + log.Info().Msgf("body %v", body) + + statusCode, reqBody, err := requestclient.InvexApiRequestClient(fiber.MethodGet, requestclient.ApiBase+"/stock/?search=&offset=0&limit=25&location=5&part_detail=true") + + log.Info().Msgf("statuscode %v", statusCode) + + if err != nil { + log.Error().Msgf("Invex api request error: %s", err) + return c.SendStatus(fiber.StatusInternalServerError) + } + + // parse body as json + var data InvexApiStockListResponse + + if err := json.Unmarshal(reqBody, &data); err != nil { + log.Error().Msgf("Failed to unmarshal json, err: %s", err) + return c.SendStatus(fiber.StatusInternalServerError) + } + + for locationItem, i := range data.Results { + log.Info().Msgf("lI %v %v", locationItem, i) + } + + //log.Info().Msgf("body %s", data) + + return c.JSON(data.Results) +} diff --git a/routers/router/router.go b/routers/router/router.go index bc77717..56b51bf 100644 --- a/routers/router/router.go +++ b/routers/router/router.go @@ -10,6 +10,7 @@ import ( "jannex/admin-dashboard-backend/routers/router/api/v1/equipment" "jannex/admin-dashboard-backend/routers/router/api/v1/grouptasks" log "jannex/admin-dashboard-backend/routers/router/api/v1/logger" + "jannex/admin-dashboard-backend/routers/router/api/v1/machines" "jannex/admin-dashboard-backend/routers/router/api/v1/notification" "jannex/admin-dashboard-backend/routers/router/api/v1/user" "jannex/admin-dashboard-backend/routers/router/api/v1/users" @@ -61,6 +62,9 @@ func SetupRoutes(app *fiber.App) { ns.Get("/", requestAccessValidation, notification.GetNotifications) ns.Post("/", requestAccessValidation, notification.AddNotification) + m := v1.Group("/machines") + m.Post("/", requestAccessValidation, machines.GetMachines) + app.Static("/", config.Cfg.FolderPaths.PublicStatic) }