From 8831b3e808f89a0b0de5b46b1f3f5e49a15c3bb1 Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Tue, 7 Nov 2023 09:04:15 +0530 Subject: [PATCH] Fix #468 Sort list apikey api based on timestamp field --- internal/apikey/apikey.go | 1 + internal/apikey/db.go | 4 ++++ .../v2/config/apikey/config_create_apikey.go | 2 +- .../v2/config/apikey/config_list_apikeys.go | 12 +++++++++--- .../v2/config/apikey/config_update_apikey.go | 2 +- internal/migrate/migrate.go | 15 +++++++++++++++ 6 files changed, 31 insertions(+), 5 deletions(-) diff --git a/internal/apikey/apikey.go b/internal/apikey/apikey.go index d2c4bb2..da8756c 100644 --- a/internal/apikey/apikey.go +++ b/internal/apikey/apikey.go @@ -18,6 +18,7 @@ type ApiKey struct { ExpiryInDays int `json:"expiryInDays"` OrganisationId string `json:"-"` IsDeleted bool `json:"-"` + Timestamp string `json:"-"` } var ApiSecretKey string diff --git a/internal/apikey/db.go b/internal/apikey/db.go index 9ad33b1..88d4c6b 100644 --- a/internal/apikey/db.go +++ b/internal/apikey/db.go @@ -2,6 +2,7 @@ package apikey import ( "context" + "time" "github.com/bb-consent/api/internal/common" "github.com/bb-consent/api/internal/database" @@ -25,6 +26,7 @@ func (apiKeyRepo *ApiKeyRepository) Init(organisationId string) { // Add Adds the data agreement to the db func (apiKeyRepo *ApiKeyRepository) Add(apiKey ApiKey) (ApiKey, error) { + apiKey.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05Z") _, err := Collection().InsertOne(context.TODO(), apiKey) if err != nil { @@ -51,6 +53,8 @@ func (apiKeyRepo *ApiKeyRepository) Get(apiKeyID string) (ApiKey, error) { // Update Updates the data agreement func (apiKeyRepo *ApiKeyRepository) Update(apiKey ApiKey) (ApiKey, error) { + apiKey.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05Z") + filter := common.CombineFilters(apiKeyRepo.DefaultFilter, bson.M{"_id": apiKey.Id}) update := bson.M{"$set": apiKey} diff --git a/internal/handler/v2/config/apikey/config_create_apikey.go b/internal/handler/v2/config/apikey/config_create_apikey.go index 6f56142..1fad9f2 100644 --- a/internal/handler/v2/config/apikey/config_create_apikey.go +++ b/internal/handler/v2/config/apikey/config_create_apikey.go @@ -48,7 +48,7 @@ func ConfigCreateApiKey(w http.ResponseWriter, r *http.Request) { valid, err := govalidator.ValidateStruct(apiKeyReq) if !valid { m := "missing mandatory params for creating api key" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } diff --git a/internal/handler/v2/config/apikey/config_list_apikeys.go b/internal/handler/v2/config/apikey/config_list_apikeys.go index 4ddba55..4f66c9b 100644 --- a/internal/handler/v2/config/apikey/config_list_apikeys.go +++ b/internal/handler/v2/config/apikey/config_list_apikeys.go @@ -10,6 +10,7 @@ import ( "github.com/bb-consent/api/internal/common" "github.com/bb-consent/api/internal/config" "github.com/bb-consent/api/internal/paginate" + "go.mongodb.org/mongo-driver/bson" ) type listApiKeyResp struct { @@ -34,8 +35,13 @@ func ConfigListApiKey(w http.ResponseWriter, r *http.Request) { // Return all api keys var apikeys []apikey.ApiKey - query := paginate.PaginateDBObjectsQuery{ - Filter: apiKeyRepo.DefaultFilter, + + var pipeline []bson.M + pipeline = append(pipeline, bson.M{"$match": bson.M{"organisationid": organisationId, "isdeleted": false}}) + pipeline = append(pipeline, bson.M{"$sort": bson.M{"timestamp": -1}}) + + query := paginate.PaginateDBObjectsQueryUsingPipeline{ + Pipeline: pipeline, Collection: apikey.Collection(), Context: context.Background(), Limit: limit, @@ -43,7 +49,7 @@ func ConfigListApiKey(w http.ResponseWriter, r *http.Request) { } var resp listApiKeyResp - result, err := paginate.PaginateDBObjects(query, &apikeys) + result, err := paginate.PaginateDBObjectsUsingPipeline(query, &apikeys) if err != nil { if errors.Is(err, paginate.EmptyDBError) { emptyApikeys := make([]interface{}, 0) diff --git a/internal/handler/v2/config/apikey/config_update_apikey.go b/internal/handler/v2/config/apikey/config_update_apikey.go index a606282..f93e385 100644 --- a/internal/handler/v2/config/apikey/config_update_apikey.go +++ b/internal/handler/v2/config/apikey/config_update_apikey.go @@ -50,7 +50,7 @@ func ConfigUpdateApiKey(w http.ResponseWriter, r *http.Request) { valid, err := govalidator.ValidateStruct(apiKeyReq) if !valid { m := "missing mandatory params for updating api key" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } diff --git a/internal/migrate/migrate.go b/internal/migrate/migrate.go index fbb2255..3af6f6d 100644 --- a/internal/migrate/migrate.go +++ b/internal/migrate/migrate.go @@ -18,6 +18,7 @@ func Migrate() { migrateThirdPartyDataSharingToTrueInDataAgreementsCollection() migrateNameInApiKeyCollection() migrateTimestampInDataAgreementsCollection() + migrateTimestampInApiKeyCollection() } func migrateThirdPartyDataSharingToTrueInPolicyCollection() { @@ -103,3 +104,17 @@ func migrateTimestampInDataAgreementsCollection() { } } + +func migrateTimestampInApiKeyCollection() { + apiKeyCollection := apikey.Collection() + + timestamp := time.Now().UTC().Format("2006-01-02T15:04:05Z") + + filter := bson.M{"timestamp": bson.M{"$exists": false}} + update := bson.M{"$set": bson.M{"timestamp": timestamp}} + + _, err := apiKeyCollection.UpdateMany(context.TODO(), filter, update) + if err != nil { + fmt.Println(err) + } +}