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 6, 2024
1 parent c8ce528 commit 754cae4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
58 changes: 43 additions & 15 deletions server/cmsintegration/cmsintegrationv3/geospatialjpv3/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package geospatialjpv3

import (
"context"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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
})
Expand Down

0 comments on commit 754cae4

Please sign in to comment.