Skip to content

Commit

Permalink
Merge branch 'main' into jeffdaley/forms
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdaley committed Oct 25, 2023
2 parents 422dfcb + cb38bd2 commit d5187bb
Show file tree
Hide file tree
Showing 61 changed files with 8,095 additions and 365 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
node_modules
/web/.pnp.*
/web/.yarn/*
/web/.eslintcache
/web/dist

# Terraform related
Expand Down
8 changes: 8 additions & 0 deletions configs/config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ email {
from_address = "[email protected]"
}

// FeatureFlags contain available feature flags.
feature_flags {
// api_v2 enables v2 of the API.
flag "api_v2" {
enabled = false
}
}

// google_workspace configures Hermes to work with Google Workspace.
google_workspace {
// create_doc_shortcuts enables creating a shortcut in the shortcuts_folder
Expand Down
8 changes: 6 additions & 2 deletions internal/api/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ Hermes

// Summary.
if req.Summary != nil {
model.Summary = *req.Summary
model.Summary = req.Summary
}

// Title.
Expand All @@ -739,7 +739,11 @@ Hermes
}

w.WriteHeader(http.StatusOK)
l.Info("patched document", "doc_id", docID)
l.Info("patched document",
"method", r.Method,
"path", r.URL.Path,
"doc_id", docID,
)

// Compare Algolia and database documents to find data inconsistencies.
// Get document object from Algolia.
Expand Down
18 changes: 13 additions & 5 deletions internal/api/documents_related_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ type externalLinkRelatedResourceGetResponse struct {
}

type hermesDocumentRelatedResourceGetResponse struct {
GoogleFileID string `json:"googleFileID"`
Title string `json:"title"`
DocumentType string `json:"documentType"`
DocumentNumber string `json:"documentNumber"`
SortOrder int `json:"sortOrder"`
GoogleFileID string `json:"googleFileID"`
Title string `json:"title"`
DocumentType string `json:"documentType"`
DocumentNumber string `json:"documentNumber"`
SortOrder int `json:"sortOrder"`
Status string `json:"status"`
Owners []string `json:"owners"`
OwnerPhotos []string `json:"ownerPhotos"`
Product string `json:"product"`
}

func documentsResourceRelatedResourcesHandler(
Expand Down Expand Up @@ -153,6 +157,10 @@ func documentsResourceRelatedResourcesHandler(
DocumentType: doc.DocType,
DocumentNumber: doc.DocNumber,
SortOrder: hdrr.RelatedResource.SortOrder,
Status: doc.Status,
Owners: doc.Owners,
OwnerPhotos: doc.OwnerPhotos,
Product: doc.Product,
})
}

Expand Down
10 changes: 7 additions & 3 deletions internal/api/drafts.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func DraftsHandler(
Name: req.Product,
},
Status: models.WIPDocumentStatus,
Summary: req.Summary,
Summary: &req.Summary,
Title: req.Title,
}
if err := model.Create(db); err != nil {
Expand Down Expand Up @@ -1104,7 +1104,7 @@ func DraftsDocumentHandler(
// Summary.
if req.Summary != nil {
doc.Summary = *req.Summary
model.Summary = *req.Summary
model.Summary = req.Summary
}

// Title.
Expand Down Expand Up @@ -1176,7 +1176,11 @@ func DraftsDocumentHandler(
fmt.Sprintf("[%s] %s", doc.DocNumber, doc.Title))

w.WriteHeader(http.StatusOK)
l.Info("patched draft document", "doc_id", docId)
l.Info("patched draft document",
"method", r.Method,
"path", r.URL.Path,
"doc_id", docId,
)

// Compare Algolia and database documents to find data inconsistencies.
// Get document object from Algolia.
Expand Down
63 changes: 55 additions & 8 deletions internal/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror"
"github.com/iancoleman/strcase"
"github.com/stretchr/testify/assert"
)

// contains returns true if a string is present in a slice of strings.
Expand Down Expand Up @@ -115,6 +116,12 @@ func respondError(
http.Error(w, userErrMsg, httpCode)
}

// fakeT fulfills the assert.TestingT interface so we can use
// assert.ElementsMatch.
type fakeT struct{}

func (t fakeT) Errorf(string, ...interface{}) {}

// compareAlgoliaAndDatabaseDocument compares data for a document stored in
// Algolia and the database to determine any inconsistencies, which are returned
// back as a (multierror) error.
Expand Down Expand Up @@ -224,7 +231,7 @@ func compareAlgoliaAndDatabaseDocument(
dbApprovedBy = append(dbApprovedBy, r.User.EmailAddress)
}
}
if !reflect.DeepEqual(algoApprovedBy, dbApprovedBy) {
if !assert.ElementsMatch(fakeT{}, algoApprovedBy, dbApprovedBy) {
result = multierror.Append(result,
fmt.Errorf(
"approvedBy not equal, algolia=%v, db=%v",
Expand All @@ -242,7 +249,7 @@ func compareAlgoliaAndDatabaseDocument(
for _, a := range dbDoc.Approvers {
dbApprovers = append(dbApprovers, a.EmailAddress)
}
if !reflect.DeepEqual(algoApprovers, dbApprovers) {
if !assert.ElementsMatch(fakeT{}, algoApprovers, dbApprovers) {
result = multierror.Append(result,
fmt.Errorf(
"approvers not equal, algolia=%v, db=%v",
Expand All @@ -263,7 +270,7 @@ func compareAlgoliaAndDatabaseDocument(
dbChangesRequestedBy = append(dbChangesRequestedBy, r.User.EmailAddress)
}
}
if !reflect.DeepEqual(algoChangesRequestedBy, dbChangesRequestedBy) {
if !assert.ElementsMatch(fakeT{}, algoChangesRequestedBy, dbChangesRequestedBy) {
result = multierror.Append(result,
fmt.Errorf(
"changesRequestedBy not equal, algolia=%v, db=%v",
Expand All @@ -281,7 +288,7 @@ func compareAlgoliaAndDatabaseDocument(
for _, c := range dbDoc.Contributors {
dbContributors = append(dbContributors, c.EmailAddress)
}
if !reflect.DeepEqual(algoContributors, dbContributors) {
if !assert.ElementsMatch(fakeT{}, algoContributors, dbContributors) {
result = multierror.Append(result,
fmt.Errorf(
"contributors not equal, algolia=%v, db=%v",
Expand Down Expand Up @@ -353,7 +360,7 @@ func compareAlgoliaAndDatabaseDocument(
)
}

if !reflect.DeepEqual(algoCFVal, dbCFVal) {
if !assert.ElementsMatch(fakeT{}, algoCFVal, dbCFVal) {
result = multierror.Append(result,
fmt.Errorf(
"custom field %s not equal, algolia=%v, db=%v",
Expand All @@ -379,8 +386,24 @@ func compareAlgoliaAndDatabaseDocument(
"doc type %q not found", algoDocType))
}

// Compare file revisions.
// TODO: need to store this in the database first.
// Compare fileRevisions.
algoFileRevisions, err := getMapStringStringValue(algoDoc, "fileRevisions")
if err != nil {
result = multierror.Append(
result, fmt.Errorf("error getting fileRevisions value: %w", err))
} else {
dbFileRevisions := make(map[string]string)
for _, fr := range dbDoc.FileRevisions {
dbFileRevisions[fr.GoogleDriveFileRevisionID] = fr.Name
}
if !reflect.DeepEqual(algoFileRevisions, dbFileRevisions) {
result = multierror.Append(result,
fmt.Errorf(
"fileRevisions not equal, algolia=%v, db=%v",
algoFileRevisions, dbFileRevisions),
)
}
}

// Compare modifiedTime.
algoModifiedTime, err := getInt64Value(algoDoc, "modifiedTime")
Expand Down Expand Up @@ -473,7 +496,7 @@ func compareAlgoliaAndDatabaseDocument(
result, fmt.Errorf("error getting summary value: %w", err))
} else {
dbSummary := dbDoc.Summary
if algoSummary != dbSummary {
if dbSummary != nil && algoSummary != *dbSummary {
result = multierror.Append(result,
fmt.Errorf(
"summary not equal, algolia=%v, db=%v",
Expand Down Expand Up @@ -517,6 +540,30 @@ func getInt64Value(in map[string]any, key string) (int64, error) {
return result, nil
}

func getMapStringStringValue(in map[string]any, key string) (
map[string]string, error,
) {
result := make(map[string]string)

if v, ok := in[key]; ok {
if reflect.TypeOf(v).Kind() == reflect.Map {
for vk, vv := range v.(map[string]any) {
if vv, ok := vv.(string); ok {
result[vk] = vv
} else {
return nil, fmt.Errorf(
"invalid type: map value element is not a string")
}
}
return result, nil
} else {
return nil, fmt.Errorf("invalid type: value is not a map")
}
}

return result, nil
}

func getStringValue(in map[string]any, key string) (string, error) {
var result string

Expand Down
Loading

0 comments on commit d5187bb

Please sign in to comment.