|
5 | 5 | "fmt" |
6 | 6 |
|
7 | 7 | "github.com/naiba/bonds/internal/dto" |
| 8 | + "github.com/naiba/bonds/internal/i18n" |
8 | 9 | "gorm.io/gorm" |
9 | 10 | ) |
10 | 11 |
|
@@ -176,3 +177,95 @@ func (s *PersonalizeService) ListTemplates(accountID string) ([]map[string]inter |
176 | 177 | } |
177 | 178 | return results, nil |
178 | 179 | } |
| 180 | + |
| 181 | +type syncableEntity struct { |
| 182 | + table string |
| 183 | + displayCol string |
| 184 | + keyCol string |
| 185 | + ownerCol string |
| 186 | + parentTable string |
| 187 | + parentJoinCol string |
| 188 | +} |
| 189 | + |
| 190 | +var accountSyncEntities = []syncableEntity{ |
| 191 | + {table: "genders", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id"}, |
| 192 | + {table: "pronouns", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id"}, |
| 193 | + {table: "address_types", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id"}, |
| 194 | + {table: "pet_categories", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id"}, |
| 195 | + {table: "contact_information_types", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id"}, |
| 196 | + {table: "relationship_group_types", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id"}, |
| 197 | + {table: "relationship_types", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id", parentTable: "relationship_group_types", parentJoinCol: "relationship_group_type_id"}, |
| 198 | + {table: "relationship_types", displayCol: "name_reverse_relationship", keyCol: "name_reverse_relationship_translation_key", ownerCol: "account_id", parentTable: "relationship_group_types", parentJoinCol: "relationship_group_type_id"}, |
| 199 | + {table: "call_reason_types", displayCol: "label", keyCol: "label_translation_key", ownerCol: "account_id"}, |
| 200 | + {table: "call_reasons", displayCol: "label", keyCol: "label_translation_key", ownerCol: "account_id", parentTable: "call_reason_types", parentJoinCol: "call_reason_type_id"}, |
| 201 | + {table: "religions", displayCol: "name", keyCol: "translation_key", ownerCol: "account_id"}, |
| 202 | + {table: "group_types", displayCol: "label", keyCol: "label_translation_key", ownerCol: "account_id"}, |
| 203 | + {table: "group_type_roles", displayCol: "label", keyCol: "label_translation_key", ownerCol: "account_id", parentTable: "group_types", parentJoinCol: "group_type_id"}, |
| 204 | + {table: "emotions", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id"}, |
| 205 | + {table: "gift_occasions", displayCol: "label", keyCol: "label_translation_key", ownerCol: "account_id"}, |
| 206 | + {table: "gift_states", displayCol: "label", keyCol: "label_translation_key", ownerCol: "account_id"}, |
| 207 | + {table: "post_templates", displayCol: "label", keyCol: "label_translation_key", ownerCol: "account_id"}, |
| 208 | + {table: "templates", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id"}, |
| 209 | + {table: "template_pages", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id", parentTable: "templates", parentJoinCol: "template_id"}, |
| 210 | + {table: "modules", displayCol: "name", keyCol: "name_translation_key", ownerCol: "account_id"}, |
| 211 | +} |
| 212 | + |
| 213 | +var vaultSyncEntities = []syncableEntity{ |
| 214 | + {table: "mood_tracking_parameters", displayCol: "label", keyCol: "label_translation_key", ownerCol: "vault_id"}, |
| 215 | + {table: "life_event_categories", displayCol: "label", keyCol: "label_translation_key", ownerCol: "vault_id"}, |
| 216 | + {table: "life_event_types", displayCol: "label", keyCol: "label_translation_key", ownerCol: "vault_id", parentTable: "life_event_categories", parentJoinCol: "life_event_category_id"}, |
| 217 | + {table: "vault_quick_facts_templates", displayCol: "label", keyCol: "label_translation_key", ownerCol: "vault_id"}, |
| 218 | +} |
| 219 | + |
| 220 | +func (s *PersonalizeService) SyncAllTranslations(accountID, locale string) error { |
| 221 | + return s.db.Transaction(func(tx *gorm.DB) error { |
| 222 | + if err := syncEntities(tx, accountSyncEntities, accountID, locale); err != nil { |
| 223 | + return err |
| 224 | + } |
| 225 | + |
| 226 | + var vaultIDs []string |
| 227 | + if err := tx.Table("vaults").Where("account_id = ?", accountID).Pluck("id", &vaultIDs).Error; err != nil { |
| 228 | + return err |
| 229 | + } |
| 230 | + for _, vaultID := range vaultIDs { |
| 231 | + if err := syncEntities(tx, vaultSyncEntities, vaultID, locale); err != nil { |
| 232 | + return err |
| 233 | + } |
| 234 | + } |
| 235 | + return nil |
| 236 | + }) |
| 237 | +} |
| 238 | + |
| 239 | +func syncEntities(tx *gorm.DB, entities []syncableEntity, ownerID, locale string) error { |
| 240 | + for _, e := range entities { |
| 241 | + var rows []struct { |
| 242 | + ID uint `gorm:"column:id"` |
| 243 | + Key string `gorm:"column:key"` |
| 244 | + } |
| 245 | + |
| 246 | + query := tx.Table(e.table).Select("id, " + e.keyCol + " AS key"). |
| 247 | + Where(e.keyCol + " IS NOT NULL AND " + e.keyCol + " != ''") |
| 248 | + |
| 249 | + if e.parentTable != "" { |
| 250 | + query = query.Where( |
| 251 | + fmt.Sprintf("%s IN (SELECT id FROM %s WHERE %s = ?)", e.parentJoinCol, e.parentTable, e.ownerCol), |
| 252 | + ownerID, |
| 253 | + ) |
| 254 | + } else { |
| 255 | + query = query.Where(e.ownerCol+" = ?", ownerID) |
| 256 | + } |
| 257 | + |
| 258 | + if err := query.Find(&rows).Error; err != nil { |
| 259 | + return err |
| 260 | + } |
| 261 | + |
| 262 | + for _, row := range rows { |
| 263 | + translated := i18n.T(locale, row.Key) |
| 264 | + if err := tx.Table(e.table).Where("id = ?", row.ID). |
| 265 | + Update(e.displayCol, translated).Error; err != nil { |
| 266 | + return err |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + return nil |
| 271 | +} |
0 commit comments