From 754cae4ab1107335dc8509cfc66a7269cb9525b1 Mon Sep 17 00:00:00 2001 From: rot1024 Date: Tue, 6 Feb 2024 21:26:30 +0900 Subject: [PATCH] fix(server): fix webhook trigger of geospatialjpv3 --- .../cmsintegrationv3/geospatialjpv3/model.go | 58 ++++++++++++++----- .../geospatialjpv3/webhook.go | 9 ++- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/server/cmsintegration/cmsintegrationv3/geospatialjpv3/model.go b/server/cmsintegration/cmsintegrationv3/geospatialjpv3/model.go index 353272e08..2897f2b36 100644 --- a/server/cmsintegration/cmsintegrationv3/geospatialjpv3/model.go +++ b/server/cmsintegration/cmsintegrationv3/geospatialjpv3/model.go @@ -1,6 +1,7 @@ package geospatialjpv3 import ( + "context" "fmt" "strconv" "strings" @@ -65,21 +66,23 @@ var featureTypes = []string{ } type CityItem struct { - ID string `json:"id,omitempty" cms:"id"` - Prefecture string `json:"prefecture,omitempty" cms:"prefecture,select"` - CityName string `json:"city_name,omitempty" cms:"city_name,text"` - CityNameEn string `json:"city_name_en,omitempty" cms:"city_name_en,text"` - CityCode string `json:"city_code,omitempty" cms:"city_code,text"` - CodeLists string `json:"codelists,omitempty" cms:"codelists,asset"` - Schemas string `json:"schemas,omitempty" cms:"schemas,asset"` - Metadata string `json:"metadata,omitempty" cms:"metadata,asset"` - Specification string `json:"specification,omitempty" cms:"specification,asset"` - Misc string `json:"misc,omitempty" cms:"misc,asset"` - Year string `json:"year,omitempty" cms:"year,select"` - References map[string]string `json:"references,omitempty" cms:"-"` - RelatedDataset string `json:"related_dataset,omitempty" cms:"related_dataset,reference"` - GeospatialjpIndex string `json:"geospatialjp-index,omitempty" cms:"geospatialjp-index,reference"` - GeospatialjpData string `json:"geospatialjp-data,omitempty" cms:"geospatialjp-data,reference"` + ID string `json:"id,omitempty" cms:"id"` + Prefecture string `json:"prefecture,omitempty" cms:"prefecture,select"` + CityName string `json:"city_name,omitempty" cms:"city_name,text"` + CityNameEn string `json:"city_name_en,omitempty" cms:"city_name_en,text"` + CityCode string `json:"city_code,omitempty" cms:"city_code,text"` + CodeLists string `json:"codelists,omitempty" cms:"codelists,asset"` + Schemas string `json:"schemas,omitempty" cms:"schemas,asset"` + Metadata string `json:"metadata,omitempty" cms:"metadata,asset"` + Specification string `json:"specification,omitempty" cms:"specification,asset"` + Misc string `json:"misc,omitempty" cms:"misc,asset"` + Year string `json:"year,omitempty" cms:"year,select"` + References map[string]string `json:"references,omitempty" cms:"-"` + RelatedDataset string `json:"related_dataset,omitempty" cms:"related_dataset,reference"` + GeospatialjpIndex string `json:"geospatialjp-index,omitempty" cms:"geospatialjp-index,reference"` + GeospatialjpData string `json:"geospatialjp-data,omitempty" cms:"geospatialjp-data,reference"` + GeospatialjpPrepare bool `json:"geospatialjp_prepare,omitempty" cms:"geospatialjp_prepare,bool,metadata"` + GeospatialjpPublish bool `json:"geospatialjp_publish,omitempty" cms:"geospatialjp_publish,bool,metadata"` } func (c *CityItem) SpecVersion() string { @@ -218,3 +221,28 @@ func (p PackageSeed) ToNewPackage() ckan.Package { Version: p.Version, } } + +func GetMainItemWithMetadata(ctx context.Context, c cms.Interface, i *cms.Item) (_ *cms.Item, err error) { + var mainItem, metadataItem *cms.Item + + if i.MetadataItemID == nil && i.OriginalItemID != nil { + // w is metadata item + metadataItem = i + mainItem, err = c.GetItem(ctx, *i.OriginalItemID, false) + if err != nil { + return nil, fmt.Errorf("failed to get main item: %w", err) + } + } else if i.OriginalItemID == nil && i.MetadataItemID != nil { + // w is main item + mainItem = i + metadataItem, err = c.GetItem(ctx, *i.MetadataItemID, false) + if err != nil { + return nil, fmt.Errorf("failed to get metadata item: %w", err) + } + } else { + return nil, fmt.Errorf("invalid webhook payload") + } + + mainItem.MetadataFields = metadataItem.Fields + return mainItem, nil +} diff --git a/server/cmsintegration/cmsintegrationv3/geospatialjpv3/webhook.go b/server/cmsintegration/cmsintegrationv3/geospatialjpv3/webhook.go index 31362ad2d..24dd43999 100644 --- a/server/cmsintegration/cmsintegrationv3/geospatialjpv3/webhook.go +++ b/server/cmsintegration/cmsintegrationv3/geospatialjpv3/webhook.go @@ -70,10 +70,15 @@ func (h *handler) Webhook(conf Config) (cmswebhook.Handler, error) { return nil } + if !w.ItemData.Item.IsMetadata { + log.Debugfc(ctx, "geospatialjpv3 webhook: not metadata item") + return nil + } + log.Debugfc(ctx, "geospatialjpv3 webhook") // prepare - if prepareField := w.ItemData.Item.MetadataFieldByKey(prepareFieldKey); prepareField != nil { + if prepareField := w.ItemData.Item.FieldByKey(prepareFieldKey); prepareField != nil { changed, ok := lo.Find(w.ItemData.Changes, func(c cms.FieldChange) bool { return c.ID == prepareField.ID }) @@ -90,7 +95,7 @@ func (h *handler) Webhook(conf Config) (cmswebhook.Handler, error) { } // publish - if publishField := w.ItemData.Item.MetadataFieldByKey(publishFieldKey); publishField != nil { + if publishField := w.ItemData.Item.FieldByKey(publishFieldKey); publishField != nil { changed, ok := lo.Find(w.ItemData.Changes, func(c cms.FieldChange) bool { return c.ID == publishField.ID })