Skip to content

Commit

Permalink
Fix #468 Sort list apikey api based on timestamp field
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 7, 2023
1 parent 64e9606 commit 8831b3e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ApiKey struct {
ExpiryInDays int `json:"expiryInDays"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
Timestamp string `json:"-"`
}

var ApiSecretKey string
Expand Down
4 changes: 4 additions & 0 deletions internal/apikey/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apikey

import (
"context"
"time"

"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/database"
Expand All @@ -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 {
Expand All @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion internal/handler/v2/config/apikey/config_create_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 9 additions & 3 deletions internal/handler/v2/config/apikey/config_list_apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,16 +35,21 @@ 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,
Offset: offset,
}

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)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/v2/config/apikey/config_update_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
15 changes: 15 additions & 0 deletions internal/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Migrate() {
migrateThirdPartyDataSharingToTrueInDataAgreementsCollection()
migrateNameInApiKeyCollection()
migrateTimestampInDataAgreementsCollection()
migrateTimestampInApiKeyCollection()
}

func migrateThirdPartyDataSharingToTrueInPolicyCollection() {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 8831b3e

Please sign in to comment.