Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
fix(server): fix webhook trigger of geospatialjpv3
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Feb 7, 2024
1 parent 3bba1ba commit a69893a
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions server/cmsintegration/cmsintegrationv3/geospatialjpv3/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ func (h *handler) Webhook(conf Config) (cmswebhook.Handler, error) {

log.Debugfc(ctx, "geospatialjpv3 webhook")

if getChangedBool(ctx, w, prepareFieldKey) {
if b := getChangedBool(ctx, w, prepareFieldKey); b != nil && *b {
if err := Prepare(ctx, item, w.ProjectID(), conf.JobName); err != nil {
log.Errorfc(ctx, "geospatialjpv3 webhook: failed to prepare: %v", err)
}
} else {
log.Debugfc(ctx, "geospatialjpv3 webhook: prepare field not changed or not true")
}

if getChangedBool(ctx, w, publishFieldKey) {
if b := getChangedBool(ctx, w, publishFieldKey); b != nil && *b {
if err := h.Publish(ctx, item); err != nil {
log.Errorfc(ctx, "geospatialjpv3 webhook: failed to publish: %v", err)
}
Expand All @@ -107,23 +107,25 @@ func (h *handler) Webhook(conf Config) (cmswebhook.Handler, error) {
}, nil
}

func getChangedBool(ctx context.Context, w *cmswebhook.Payload, key string) bool {
if f := w.ItemData.Item.MetadataFieldByKey(key); f != nil {
func getChangedBool(ctx context.Context, w *cmswebhook.Payload, key string) *bool {
// w.ItemData.Item is a metadata item, so we need to use FieldByKey instead of MetadataFieldByKey
if f := w.ItemData.Item.FieldByKey(key); f != nil {
changed, ok := lo.Find(w.ItemData.Changes, func(c cms.FieldChange) bool {
return c.ID == f.ID
})

if ok {
if lo.FromPtr(changed.GetCurrentValue().Bool()) {
return true
b := changed.GetCurrentValue().Bool()
if b != nil {
return b
}

// workaround for bool array
if res := changed.GetCurrentValue().Bools(); len(res) > 0 && res[0] {
return true
// workaround for bool array: value is [true]
if res := changed.GetCurrentValue().Bools(); len(res) > 0 {
return lo.ToPtr(res[0])
}
}
}

return false
return nil
}

0 comments on commit a69893a

Please sign in to comment.