GWM : notification

This commit is contained in:
Gowtham M 2025-03-10 17:20:48 +05:30
parent fe522b5388
commit 9c92cd63e3
10 changed files with 75 additions and 30 deletions

93
main.go
View File

@ -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),

Binary file not shown.

BIN
pb_data/data.db-shm Normal file

Binary file not shown.

0
pb_data/data.db-wal Normal file
View File

Binary file not shown.

BIN
pb_data/logs.db-shm Normal file

Binary file not shown.

BIN
pb_data/logs.db-wal Normal file

Binary file not shown.

View File

@ -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)
}

View File

@ -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 {