diff --git a/main.go b/main.go index b9fbd68..39b6497 100644 --- a/main.go +++ b/main.go @@ -383,23 +383,27 @@ func TranslateFilters(app *pocketbase.PocketBase, dataset string, filterGroups [ sourceData = append(sourceData, source) } - // Create a map for quick lookup of translations - translationMap := make(map[string]string) - for _, source := range sourceData { - var value string - switch langKey { - case "value_en": - value = source.ValueEn - case "value_ar": - value = source.ValueAr - default: - continue - } - translationMap[source.Value] = value - } - // Translate the filter data for i, filter := range filterGroups { + + // Create a map for quick lookup of translations + translationMap := make(map[string]string) + for _, source := range sourceData { + var value string + switch langKey { + case "value_en": + value = source.ValueEn + case "value_ar": + value = source.ValueAr + default: + continue + } + if source.Key == filter.FilterKey { + translationMap[source.Value] = value + } + + } + if filter.FilterKey == "TIME_PERIOD" { // Skip translation for TIME_PERIOD continue @@ -457,6 +461,7 @@ func main() { message_en := e.Record.GetString("message_en") title_ar := e.Record.GetString("title_ar") message_ar := e.Record.GetString("message_ar") + notificationID := e.Record.GetString("id") // Get the new notification ID // Initialize Firebase opt := option.WithCredentialsFile("firebase-adminsdk.json") @@ -513,6 +518,47 @@ func main() { } else { fmt.Printf("✅ Successfully sent message: %s\n", response) } + + // Update the pushed_notification field + var pushedNotifications []string + + // Get existing pushed_notification JSON field + pushedNotificationsJSON := user.GetString("pushed_notification") + if pushedNotificationsJSON != "" { + err := json.Unmarshal([]byte(pushedNotificationsJSON), &pushedNotifications) + if err != nil { + log.Printf("⚠️ Error parsing pushed_notification for user %s: %v", user.GetString("id"), err) + continue + } + } + + // Append the new notification ID if not already present + alreadyExists := false + for _, existingID := range pushedNotifications { + if existingID == notificationID { + alreadyExists = true + break + } + } + if !alreadyExists { + pushedNotifications = append(pushedNotifications, notificationID) + } + + // Convert back to JSON + updatedPushedNotifications, err := json.Marshal(pushedNotifications) + if err != nil { + log.Printf("⚠️ Error marshalling pushed_notification JSON: %v", err) + continue + } + + // Update the user record in the database + user.Set("pushed_notification", string(updatedPushedNotifications)) + if err := app.Dao().SaveRecord(user); err != nil { + log.Printf("❌ Error updating pushed_notification for user %s: %v", user.GetString("id"), err) + } else { + fmt.Printf("✅ Updated pushed_notification for user %s\n", user.GetString("id")) + } + } } @@ -777,7 +823,7 @@ func main() { ) } - cResRaw := utils.ProcessChartData(chartData, languageSourceConverted, langKey) + cResRaw := utils.TranslateChartData(chartData, languageSourceConverted, langKey) //responce data structure chartHeadingKey := "chart_heading_" + language @@ -785,14 +831,13 @@ func main() { tabHeadingKey := "tab_heading_" + language // Correct conditional assignment for "response" recordResult := map[string]interface{}{ - "url": apiURL, - "dataset": record.GetString("dataset"), - "main_id": record.GetString("main_id"), - "sub_id": record.GetString("sub_id"), - "kpi": record.GetString("kpi"), - "is_chart": record.GetString("is_chart"), - "chart_type": record.GetString("chart_type"), - // "response": cResRaw, + "url": apiURL, + "dataset": record.GetString("dataset"), + "main_id": record.GetString("main_id"), + "sub_id": record.GetString("sub_id"), + "kpi": record.GetString("kpi"), + "is_chart": record.GetString("is_chart"), + "chart_type": record.GetString("chart_type"), "group_by": record.GetString("group_by"), "chart_heading": record.GetString(chartHeadingKey), "chart_sub_heading": record.GetString(chartSubHeadingKey), diff --git a/pb_data/data.db b/pb_data/data.db index e565dfb..9c5ba8d 100644 Binary files a/pb_data/data.db and b/pb_data/data.db differ diff --git a/pb_data/data.db-shm b/pb_data/data.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/pb_data/data.db-shm differ diff --git a/pb_data/data.db-wal b/pb_data/data.db-wal new file mode 100644 index 0000000..e69de29 diff --git a/pb_data/logs.db b/pb_data/logs.db index 959ce5c..7664483 100644 Binary files a/pb_data/logs.db and b/pb_data/logs.db differ diff --git a/pb_data/logs.db-shm b/pb_data/logs.db-shm new file mode 100644 index 0000000..74052ab Binary files /dev/null and b/pb_data/logs.db-shm differ diff --git a/pb_data/logs.db-wal b/pb_data/logs.db-wal new file mode 100644 index 0000000..2e8e8df Binary files /dev/null and b/pb_data/logs.db-wal differ diff --git a/utils/card_calculation.go b/utils/calculation.go similarity index 98% rename from utils/card_calculation.go rename to utils/calculation.go index 0b71eb8..bd1e1f3 100644 --- a/utils/card_calculation.go +++ b/utils/calculation.go @@ -234,10 +234,10 @@ func difference(data []Observation) float64 { } // Function to add commas to a number string -func addCommas(num string) string { +func addCommas(num string, suffix string) string { n := len(num) if n <= 3 { - return num + return num + suffix // Attach suffix directly if no commas needed } var sb strings.Builder start := n % 3 @@ -249,7 +249,7 @@ func addCommas(num string) string { for i := start; i < n; i += 3 { sb.WriteString("," + num[i:i+3]) } - return sb.String() + return sb.String() + suffix } func formatNumberWithCommas(value float64, suffix string) string { @@ -293,7 +293,7 @@ func applyConversion(value float64, conversion string, numberFormat string) stri formatNumber := func(v float64, suffix string) string { if suffix == "" || suffix == "K" || suffix == "M" || suffix == "B" || suffix == "T" { if v == float64(int(v)) { // No decimal part - return addCommas(strconv.Itoa(int(v))) + return addCommas(strconv.Itoa(int(v)), suffix) } return formatNumberWithCommas(v, suffix) } diff --git a/utils/filter_utils.go b/utils/exclude_filter_data.go similarity index 100% rename from utils/filter_utils.go rename to utils/exclude_filter_data.go diff --git a/utils/process_chart_data.go b/utils/translate_chart_data.go similarity index 83% rename from utils/process_chart_data.go rename to utils/translate_chart_data.go index 2577917..dac68a3 100644 --- a/utils/process_chart_data.go +++ b/utils/translate_chart_data.go @@ -1,7 +1,7 @@ package utils -// ProcessChartData processes chart data with language source data to produce the result based on the language key. -func ProcessChartData(chartData []map[string]interface{}, languageSource []map[string]interface{}, langKey string) []map[string]interface{} { +// TranslateChartData processes chart data with language source data to produce the result based on the language key. +func TranslateChartData(chartData []map[string]interface{}, languageSource []map[string]interface{}, langKey string) []map[string]interface{} { // Helper function to find translation findTranslation := func(key string, value string) string { for _, langEntry := range languageSource {