diff --git a/main b/main index ccf28fd..d21f9e6 100755 Binary files a/main and b/main differ diff --git a/routers/router/api/v1/productpipeline/productpipeline.go b/routers/router/api/v1/productpipeline/productpipeline.go index d719d4d..addedb7 100644 --- a/routers/router/api/v1/productpipeline/productpipeline.go +++ b/routers/router/api/v1/productpipeline/productpipeline.go @@ -117,7 +117,46 @@ func fetchProducts(srv *sheets.Service) error { var inWorkProducts []structs.InWorkProduct var futureProducts []structs.FutureProduct - for _, row := range resp.Values[1:] { // skip first google worksheet row + var ( + rowIndexProductId int = -1 + rowIndexStatus int = -1 + rowIndexName int = -1 + rowIndexProductVariant int = -1 + rowIndexProductCharacteristics int = -1 + rowIndexShopProductLink int = -1 + rowIndexPublishedAt int = -1 + ) + + for i, row := range resp.Values[1] { + switch row { + case "#id": + rowIndexProductId = i + case "Status": + rowIndexStatus = i + case "Name": + rowIndexName = i + case "Produktvariante": + rowIndexProductVariant = i + case "Produktfarbe/Characteristics": + rowIndexProductCharacteristics = i + case "Shop Produkt Link": + rowIndexShopProductLink = i + case "Veröffentlicht am": + rowIndexPublishedAt = i + } + } + + if rowIndexProductId == -1 || rowIndexStatus == -1 || rowIndexName == -1 || rowIndexProductVariant == -1 || rowIndexProductCharacteristics == -1 || rowIndexShopProductLink == -1 || rowIndexPublishedAt == -1 { + notification.AddNotification(nil, structs.AddNotificationRequest{ + UserIds: config.Cfg.NotificationUserIds, + Type: 3, + Title: "Admin-Dashboard: Failed to get row index. Please check if the row names are equal to the names of the google sheet table header of " + spreadsheetId, + }) + + return errors.New("Failed to get row index. Please check if the row names are equal to the names of the google sheet table header of " + spreadsheetId) + } + + for _, row := range resp.Values[2:] { // skip first two google worksheet row // maybe some error in google sheets table if len(row) < 3 { logger.AddSystemLog(rslogger.LogTypeError, "Skipped row, because row length less than 2") @@ -125,24 +164,24 @@ func fetchProducts(srv *sheets.Service) error { } // skip to next if id, status or name is empty - if row[0] == "" || row[1] == "" || row[2] == "" { + if row[rowIndexProductId] == "" || row[rowIndexStatus] == "" || row[rowIndexName] == "" { continue } - productId := fmt.Sprintf("%v", row[0]) - status := fmt.Sprintf("%v", row[1]) - name := fmt.Sprintf("%v", row[2]) + productId := fmt.Sprintf("%v", row[rowIndexProductId]) + status := fmt.Sprintf("%v", row[rowIndexStatus]) + name := fmt.Sprintf("%v", row[rowIndexName]) var productVariant string - if len(row) > 3 && row[3] != "" { - productVariant = fmt.Sprintf("%v", row[3]) + if len(row) > rowIndexProductVariant && row[rowIndexProductVariant] != "" { + productVariant = fmt.Sprintf("%v", row[rowIndexProductVariant]) } var productCharacteristics string - if len(row) > 4 && row[4] != "" { - productCharacteristics = fmt.Sprintf("%v", row[4]) + if len(row) > rowIndexProductCharacteristics && row[rowIndexProductCharacteristics] != "" { + productCharacteristics = fmt.Sprintf("%v", row[rowIndexProductCharacteristics]) } state := getStateByStatus(status) @@ -176,20 +215,28 @@ func fetchProducts(srv *sheets.Service) error { var url string var publishedAt string - if len(row) > 8 && row[8] != "" { - url = fmt.Sprintf("%v", row[8]) + if len(row) > rowIndexShopProductLink && row[rowIndexShopProductLink] != "" { + url = fmt.Sprintf("%v", row[rowIndexShopProductLink]) } else { logger.AddSystemLog(rslogger.LogTypeWarning, "Url missing for product id: %s name: %s", productId, name) notification.AddNotification(nil, structs.AddNotificationRequest{ UserIds: config.Cfg.NotificationUserIds, Type: 3, - Title: "Missing 'Shop Produkt Link' for product pipeline\n\nproduct id: " + productId + "\nname: " + name + "\n\nDie URL für das Produkt in dem Google Sheet 'Produkte' -> Worksheet: 'Pipeline' eintragen!", + Title: "Missing 'Shop Produkt Link' for product pipeline\n\nproduct id: " + productId + "\nname: " + name + "\n\nGoogle Sheet 'Produkte' -> Worksheet: 'Pipeline' eintragen!", }) } - if len(row) > 9 && row[9] != "" { - publishedAt = fmt.Sprintf("%v", row[9]) + if len(row) > rowIndexPublishedAt && row[rowIndexPublishedAt] != "" { + publishedAt = fmt.Sprintf("%v", row[rowIndexPublishedAt]) + } else { + logger.AddSystemLog(rslogger.LogTypeWarning, "'Veröffentlicht am' date missing for product id: %s name: %s", productId, name) + + notification.AddNotification(nil, structs.AddNotificationRequest{ + UserIds: config.Cfg.NotificationUserIds, + Type: 3, + Title: "Missing 'Veröffentlicht am' for product pipeline\n\nproduct id: " + productId + "\nname: " + name + "\n\nGoogle Sheet 'Produkte' -> Worksheet: 'Pipeline' eintragen!", + }) } newProducts = append(newProducts, structs.NewProduct{ diff --git a/start.sh b/start.sh index d250f75..ef3535a 100755 --- a/start.sh +++ b/start.sh @@ -1,5 +1,35 @@ +#!/bin/bash + #screen -dmS admin-dashboard-backend | exit 0 #screen -S admin-dashboard-backend -p 0 -X stuff 'go run main.go\n' +# Set variables +BACKUP_DIR="backups" +DATE=$(date +'%Y-%m-%d_%H-%M-%S') +MAIN_FILE="main" +BACKUP_FILE="$BACKUP_DIR/main-$DATE" +MAX_BACKUPS=10 + +# Create backup directory if it doesn't exist +mkdir -p $BACKUP_DIR + +# Check if the main file exists and move it to the backup directory with the current date and time +if [ -f $MAIN_FILE ]; then + mv $MAIN_FILE $BACKUP_FILE + echo "Backup of '$MAIN_FILE' created at '$BACKUP_FILE'" +else + echo "No existing main file to backup." +fi + +# Ensure there are at most MAX_BACKUPS backups in the directory +BACKUPS_COUNT=$(ls $BACKUP_DIR | wc -l) +if [ $BACKUPS_COUNT -gt $MAX_BACKUPS ]; then + BACKUPS_TO_DELETE=$(expr $BACKUPS_COUNT - $MAX_BACKUPS) + ls -t $BACKUP_DIR | tail -n $BACKUPS_TO_DELETE | while read -r file; do + rm "$BACKUP_DIR/$file" + echo "Deleted old backup '$BACKUP_DIR/$file'" + done +fi + go build main.go sudo systemctl restart admin-dashboard-backend.service \ No newline at end of file