fix: entry generation applies an unseen address change instead of overwriting it (WR-19)
refreshEntry set entry.url to the descriptor's address unconditionally, so a slug change the job saw before any RecordSaved listener lost its 301 for good. An entry whose URL differs now goes through AddressChanged (via auto), which creates the 301 and moves the URL; an empty URL is filled.
This commit is contained in:
@@ -107,7 +107,9 @@ func (s *Service) GenerateEntries(ctx context.Context, typeKey string) (Generate
|
||||
}
|
||||
|
||||
// refreshEntry creates or refreshes the entry of one record in one
|
||||
// locale; created is true for a new row.
|
||||
// locale; created is true for a new row. The job only sees published
|
||||
// records, so an entry whose URL differs from the record's address gets
|
||||
// the address change (with its 301), never a silent overwrite.
|
||||
func (s *Service) refreshEntry(ctx context.Context, tx *gorm.DB, typ pact.MenuItemType, id string, l pact.ContentLocale, settings models.Settings, ts models.TypeSetting) (bool, error) {
|
||||
entry, found, err := LoadEntry(tx, typ.Content.Model, id, l.Locale)
|
||||
if err != nil {
|
||||
@@ -123,12 +125,27 @@ func (s *Service) refreshEntry(ctx context.Context, tx *gorm.DB, typ pact.MenuIt
|
||||
}
|
||||
return true, tx.Create(&row).Error
|
||||
}
|
||||
return false, tx.Model(&models.Entry{}).Where("id = ?", entry.ID).Updates(map[string]any{
|
||||
"url": l.URL,
|
||||
updates := map[string]any{
|
||||
"type_key": typ.Key,
|
||||
"effective_title": title,
|
||||
"effective_description": description,
|
||||
}).Error
|
||||
}
|
||||
switch {
|
||||
case entry.URL == "":
|
||||
updates["url"] = l.URL
|
||||
case entry.URL != l.URL:
|
||||
// The published record's address changed and no RecordSaved
|
||||
// listener has seen it yet (WR-19): apply it like the listener
|
||||
// would, so its 301 is not lost; AddressChanged moves the entry's
|
||||
// URL.
|
||||
if _, err := s.AddressChanged(ctx, tx, AddressChange{
|
||||
TypeKey: typ.Key, ModelType: typ.Content.Model, RecordID: id, Locale: l.Locale,
|
||||
Old: entry.URL, New: l.URL,
|
||||
}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
return false, tx.Model(&models.Entry{}).Where("id = ?", entry.ID).Updates(updates).Error
|
||||
}
|
||||
|
||||
// EffectiveValues are a record's effective title and description without
|
||||
|
||||
Reference in New Issue
Block a user