From 0d1ad0f6b76f95c33204762bd53469ba616bffd7 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 11 May 2024 00:44:58 +0200 Subject: [PATCH] added webhook --- groupTasks/groups/requirements.txt | 3 +- .../groups/shx-order-package-label/index.html | 18 +++-- .../groups/shx-order-package-label/script.py | 47 +++++++++++- modules/config/config.go | 3 + routers/router/api/v1/crm/crm.go | 3 +- routers/router/api/v1/webhook/webhook.go | 75 +++++++++++++++++++ routers/router/router.go | 3 + 7 files changed, 139 insertions(+), 13 deletions(-) create mode 100644 routers/router/api/v1/webhook/webhook.go diff --git a/groupTasks/groups/requirements.txt b/groupTasks/groups/requirements.txt index 37dbd1c..d27b819 100644 --- a/groupTasks/groups/requirements.txt +++ b/groupTasks/groups/requirements.txt @@ -1 +1,2 @@ -numpy==1.24.3 \ No newline at end of file +numpy==1.24.3 +pdf2image==1.17.0 diff --git a/groupTasks/groups/shx-order-package-label/index.html b/groupTasks/groups/shx-order-package-label/index.html index a424ddb..ad15b9d 100644 --- a/groupTasks/groups/shx-order-package-label/index.html +++ b/groupTasks/groups/shx-order-package-label/index.html @@ -63,7 +63,7 @@ display: flex; justify-content: center; align-items: center; - gap: 20mm; + gap: 0mm; z-index: -10; opacity: 0.25; } @@ -90,16 +90,18 @@ display: flex; justify-content: space-between; width: 100%; - gap: 20mm; + gap: 15mm; } .bgContainer { display: flex; justify-content: center; align-items: center; - gap: 40mm; + gap: 50mm; flex-direction: column; - width: 60mm; + width: 80mm; + + position: relative; } .myCustomText { @@ -127,12 +129,12 @@

Folge uns

-

SHINNEX

+

shinnex.official

Folge uns

-

SHINNEX

+

shinnex.official

@@ -142,12 +144,12 @@

Folge uns

-

SHINNEX

+

shinnex.official

Folge uns

-

SHINNEX

+

shinnex.official

diff --git a/groupTasks/groups/shx-order-package-label/script.py b/groupTasks/groups/shx-order-package-label/script.py index 4c07a70..3e03598 100644 --- a/groupTasks/groups/shx-order-package-label/script.py +++ b/groupTasks/groups/shx-order-package-label/script.py @@ -4,6 +4,8 @@ import sys from PIL import Image import os +from pdf2image import convert_from_path + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) from libs.utils import utils @@ -44,7 +46,7 @@ def createHighDpiPng(sourceHtml, outputPng): print(stderr.decode()) # Decoding the stderr for better readability sys.exit(1) -def downloadFile(url, filename): +def downloadFilePNG(url, filename): command = [ "curl", "-o", @@ -72,6 +74,41 @@ def downloadFile(url, filename): rotated_image.save(filename) +def downloadFilePDF(url, filename): + pdf_filename = filename + ".pdf" + + command = [ + "curl", + "-L", # Follow redirects + "-o", + pdf_filename, + url, + ] + + process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + + if process.returncode != 0: + print("Error downloading file") + print("STDOUT:") + print(stdout.decode()) # Decoding the stdout for better readability + print("STDERR:") + print(stderr.decode()) # Decoding the stderr for better readability + sys.exit(1) + + # Convert the PDF to a PNG + images = convert_from_path(pdf_filename, dpi=300) + + page = images[0] + + h = page.height/2 + spacing = 250 + + # box=(left, upper, right, lower) + page.crop((0, spacing, page.width, h-spacing) + ).save(filename, 'PNG') + + def replacePlaceholder(): with open("index.html", "r") as file: index_html = file.read() @@ -87,10 +124,14 @@ if __name__ == "__main__": print(f"Creating package label for {customer_first_name}") - downloadFile(shipping_label_url, "label.png") + #if shipping_label_url contains .pdf, download the file and convert it to a PNG + if ".pdf" in shipping_label_url or "https://cloud.umbach.dev/" in shipping_label_url: + downloadFilePDF(shipping_label_url, "label.png") + else: + downloadFilePNG(shipping_label_url, "label.png") replacePlaceholder() createHighDpiPng("index.html", "Versandlabel.png") - utils.clear_workspace(["index.html", "label.png"]) + utils.clear_workspace(["index.html", "label.png.pdf", "label.png"]) diff --git a/modules/config/config.go b/modules/config/config.go index 50b5447..f0a9044 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -3,6 +3,7 @@ package config import ( "fmt" "os" + "strings" "github.com/joho/godotenv" ) @@ -20,6 +21,7 @@ type Config struct { FolderPaths FolderPaths MariaDB MariaDB InvexAPI InvexAPI + NotificationUserIds []string } type FolderPaths struct { @@ -72,6 +74,7 @@ func LoadConfig() { Base: os.Getenv("INVEX_API_BASE"), Token: os.Getenv("INVEX_API_TOKEN"), }, + NotificationUserIds: strings.Split(os.Getenv("NOTIFICATION_USER_IDS"), ","), } // load default values if not in docker diff --git a/routers/router/api/v1/crm/crm.go b/routers/router/api/v1/crm/crm.go index 80255d1..95f87b1 100644 --- a/routers/router/api/v1/crm/crm.go +++ b/routers/router/api/v1/crm/crm.go @@ -1,6 +1,7 @@ package crm import ( + "jannex/admin-dashboard-backend/modules/config" "jannex/admin-dashboard-backend/modules/database" "jannex/admin-dashboard-backend/modules/logger" "jannex/admin-dashboard-backend/modules/notification" @@ -771,7 +772,7 @@ func CrmUseLink(c *fiber.Ctx) error { database.DB.First(&crmCustomer, "id = ?", link.CustomerId) notification.AddNotification(nil, structs.AddNotificationRequest{ - UserIds: []string{"00de4e2c-4790-4272-bc30-e0b25d8426b6", "3d7f3dc2-dc81-4d5d-93a1-dc6690511ee3"}, + UserIds: config.Cfg.NotificationUserIds, Type: 1, Title: "Crm link used: " + link.Name + " by crm customer: " + crmCustomer.FirstName + " " + crmCustomer.Email + " (" + link.CustomerId + ")", }) diff --git a/routers/router/api/v1/webhook/webhook.go b/routers/router/api/v1/webhook/webhook.go new file mode 100644 index 0000000..9c7e6b5 --- /dev/null +++ b/routers/router/api/v1/webhook/webhook.go @@ -0,0 +1,75 @@ +package webhook + +import ( + "encoding/json" + "fmt" + "jannex/admin-dashboard-backend/modules/config" + "jannex/admin-dashboard-backend/modules/logger" + "jannex/admin-dashboard-backend/modules/notification" + "jannex/admin-dashboard-backend/modules/structs" + + "git.ex.umbach.dev/Alex/roese-utils/rslogger" + "github.com/gofiber/fiber/v2" +) + +type EventData struct { + Type string `json:"type"` +} + +type PrinterData struct { + ID int `json:"id"` + Name string `json:"name"` +} + +type PrintData struct { + ID int `json:"id"` + Filename string `json:"filename"` +} + +type Message struct { + Event EventData `json:"event"` + Printer PrinterData `json:"printer"` + Print PrintData `json:"print"` + ImgURL string `json:"img_url"` +} + +func Webhook(c *fiber.Ctx) error { + logger.AddSystemLog(rslogger.LogTypeInfo, "Received webhook: %v query: %v", string(c.Body()), c.Query("auth")) + + if c.Query("auth") == "oThBd3apemoexlRyyNmLvgLRswGH6Fp9niCzxuxZgFg8ahnYqE" { + var message Message + + if err := json.Unmarshal(c.Body(), &message); err != nil { + logger.AddSystemLog(rslogger.LogTypeError, "Failed to unmarshal body, err: %v", err.Error()) + return c.SendStatus(fiber.StatusOK) + } + + var state uint8 + + if message.Event.Type == "PrintFailure" { + state = 4 + } else if message.Event.Type == "PrintPaused" { + state = 3 + } else { + state = 0 + } + + notification.AddNotification(nil, structs.AddNotificationRequest{ + UserIds: config.Cfg.NotificationUserIds, + Type: state, + Title: fmt.Sprintf("Event: %s\nPrinter ID: %d\nPrinter Name: %s\nPrint ID: %d\nPrint Filename: %s\nImg Url: %s", + message.Event.Type, + message.Printer.ID, + message.Printer.Name, + message.Print.ID, + message.Print.Filename, + message.ImgURL), + }) + + return c.SendStatus(fiber.StatusOK) + } + + logger.AddSystemLog(rslogger.LogTypeWarning, "Received webhook with unknown auth") + + return c.SendStatus(fiber.StatusBadRequest) +} diff --git a/routers/router/router.go b/routers/router/router.go index 624ca1a..11e4eb1 100644 --- a/routers/router/router.go +++ b/routers/router/router.go @@ -15,6 +15,7 @@ import ( "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" + "jannex/admin-dashboard-backend/routers/router/api/v1/webhook" "jannex/admin-dashboard-backend/socketclients" "time" @@ -26,6 +27,8 @@ import ( func SetupRoutes(app *fiber.App) { v1 := app.Group("/v1") + v1.Post("/webhook", webhook.Webhook) + u := v1.Group("/user") u.Post("/auth/login", user.UserLogin) u.Delete("/auth/logout", requestAccessValidation, user.UserLogout)