Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add document_status filter to all find queries #725

Merged
merged 8 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions datastore/mongo/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,29 @@ func (db *apiKeyRepo) UpdateAPIKey(ctx context.Context, apiKey *datastore.APIKey

func (db *apiKeyRepo) FindAPIKeyByID(ctx context.Context, uid string) (*datastore.APIKey, error) {
apiKey := &datastore.APIKey{}
err := db.client.FindOne(ctx, bson.M{"uid": uid}).Decode(apiKey)

filter := bson.M{
"uid": uid,
"document_status": datastore.ActiveDocumentStatus,
}

err := db.client.FindOne(ctx, filter).Decode(apiKey)
if errors.Is(err, mongo.ErrNoDocuments) {
err = datastore.ErrAPIKeyNotFound
}

return apiKey, err
}

func (db *apiKeyRepo) FindAPIKeyByMaskID(ctx context.Context, maskID string) (*datastore.APIKey, error) {
apiKey := new(datastore.APIKey)
err := db.client.FindOne(ctx, bson.M{"mask_id": maskID}).Decode(apiKey)

filter := bson.M{
"mask_id": maskID,
"document_status": datastore.ActiveDocumentStatus,
}

err := db.client.FindOne(ctx, filter).Decode(apiKey)

if errors.Is(err, mongo.ErrNoDocuments) {
err = datastore.ErrAPIKeyNotFound
Expand All @@ -85,16 +101,24 @@ func (db *apiKeyRepo) RevokeAPIKeys(ctx context.Context, uids []string) error {

func (db *apiKeyRepo) FindAPIKeyByHash(ctx context.Context, hash string) (*datastore.APIKey, error) {
apiKey := &datastore.APIKey{}
err := db.client.FindOne(ctx, bson.M{"hash": hash}).Decode(apiKey)

filter := bson.M{
"hash": hash,
"document_status": datastore.ActiveDocumentStatus,
}

err := db.client.FindOne(ctx, filter).Decode(apiKey)
if errors.Is(err, mongo.ErrNoDocuments) {
err = datastore.ErrAPIKeyNotFound
}

return apiKey, err
}

func (db *apiKeyRepo) LoadAPIKeysPaged(ctx context.Context, pageable *datastore.Pageable) ([]datastore.APIKey, datastore.PaginationData, error) {
var apiKeys []datastore.APIKey

filter := bson.M{"$or": bson.A{
bson.M{"document_status": datastore.ActiveDocumentStatus},
}}
filter := bson.M{"document_status": datastore.ActiveDocumentStatus}

paginatedData, err := pager.
New(db.client).
Expand Down
2 changes: 1 addition & 1 deletion datastore/mongo/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (db *appRepo) FindApplicationByID(ctx context.Context,
filter = bson.M{"app_id": app.UID, "document_status": datastore.ActiveDocumentStatus}
count, err := msgCollection.CountDocuments(ctx, filter)
if err != nil {
log.Errorf("failed to count events in %s. Reason: %s", app.UID, err)
log.WithError(err).Errorf("failed to count events in %s", app.UID)
return app, err
}
app.Events = count
Expand Down
21 changes: 8 additions & 13 deletions datastore/mongo/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,20 @@ func (db *groupRepo) UpdateGroup(ctx context.Context, o *datastore.Group) error
return err
}

func (db *groupRepo) FetchGroupByID(ctx context.Context,
id string) (*datastore.Group, error) {
org := new(datastore.Group)

filter := bson.D{
primitive.E{
Key: "uid",
Value: id,
},
}
func (db *groupRepo) FetchGroupByID(ctx context.Context, id string) (*datastore.Group, error) {
group := new(datastore.Group)

err := db.inner.FindOne(ctx, filter).
Decode(&org)
filter := bson.M{
"uid": id,
"document_status": datastore.ActiveDocumentStatus,
}

err := db.inner.FindOne(ctx, filter).Decode(group)
if errors.Is(err, mongo.ErrNoDocuments) {
err = datastore.ErrGroupNotFound
}

return org, err
return group, err
}

func (db *groupRepo) FillGroupsStatistics(ctx context.Context, groups []*datastore.Group) error {
Expand Down
9 changes: 3 additions & 6 deletions datastore/mongo/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func Test_FetchGroupByID(t *testing.T) {
groupRepo := NewGroupRepo(db)

newOrg := &datastore.Group{
Name: "Yet another group",
UID: uuid.NewString(),
Name: "Yet another group",
UID: uuid.NewString(),
DocumentStatus: datastore.ActiveDocumentStatus,
}

require.NoError(t, groupRepo.CreateGroup(context.Background(), newOrg))
Expand Down Expand Up @@ -124,10 +125,6 @@ func Test_CreateGroup(t *testing.T) {

if i == 0 {
require.NoError(t, groupRepo.CreateGroup(context.Background(), newGroup))

g, err := groupRepo.FetchGroupByID(context.Background(), newGroup.UID)
require.NoError(t, err)
require.Equal(t, g.UID, newGroup.UID)
}

if i > 0 && tc.isDuplicate {
Expand Down
2 changes: 1 addition & 1 deletion datastore/mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package mongo

import (
"context"
"os"
"os"
"testing"

"github.com/frain-dev/convoy/config"
Expand Down
4 changes: 3 additions & 1 deletion datastore/mongo/organisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func (db *orgRepo) DeleteOrganisation(ctx context.Context, uid string) error {
func (db *orgRepo) FetchOrganisationByID(ctx context.Context, id string) (*datastore.Organisation, error) {
org := new(datastore.Organisation)

err := db.inner.FindOne(ctx, bson.M{"uid": id}).Decode(&org)
filter := bson.M{"uid": id, "document_status": datastore.ActiveDocumentStatus}

err := db.inner.FindOne(ctx, filter).Decode(&org)
if errors.Is(err, mongo.ErrNoDocuments) {
err = datastore.ErrOrgNotFound
}
Expand Down
34 changes: 17 additions & 17 deletions datastore/mongo/organisation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ func TestUpdateOrganisation(t *testing.T) {

orgRepo := NewOrgRepo(db)
org := &datastore.Organisation{
UID: uuid.NewString(),
Name: fmt.Sprintf("new org"),
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
UpdatedAt: primitive.NewDateTimeFromTime(time.Now()),
UID: uuid.NewString(),
Name: fmt.Sprintf("new org"),
DocumentStatus: datastore.ActiveDocumentStatus,
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
UpdatedAt: primitive.NewDateTimeFromTime(time.Now()),
}

err := orgRepo.CreateOrganisation(context.Background(), org)
Expand All @@ -93,10 +94,11 @@ func TestFetchOrganisationByID(t *testing.T) {

orgRepo := NewOrgRepo(db)
org := &datastore.Organisation{
UID: uuid.NewString(),
Name: fmt.Sprintf("new org"),
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
UpdatedAt: primitive.NewDateTimeFromTime(time.Now()),
UID: uuid.NewString(),
Name: fmt.Sprintf("new org"),
DocumentStatus: datastore.ActiveDocumentStatus,
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
UpdatedAt: primitive.NewDateTimeFromTime(time.Now()),
}

err := orgRepo.CreateOrganisation(context.Background(), org)
Expand All @@ -114,10 +116,11 @@ func TestDeleteOrganisation(t *testing.T) {

orgRepo := NewOrgRepo(db)
org := &datastore.Organisation{
UID: uuid.NewString(),
Name: fmt.Sprintf("new org"),
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
UpdatedAt: primitive.NewDateTimeFromTime(time.Now()),
UID: uuid.NewString(),
Name: fmt.Sprintf("new org"),
DocumentStatus: datastore.ActiveDocumentStatus,
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
UpdatedAt: primitive.NewDateTimeFromTime(time.Now()),
}

err := orgRepo.CreateOrganisation(context.Background(), org)
Expand All @@ -126,9 +129,6 @@ func TestDeleteOrganisation(t *testing.T) {
err = orgRepo.DeleteOrganisation(context.Background(), org.UID)
require.NoError(t, err)

organisation, err := orgRepo.FetchOrganisationByID(context.Background(), org.UID)
require.NoError(t, err)

require.True(t, organisation.DeletedAt > 0)
require.Equal(t, datastore.DeletedDocumentStatus, organisation.DocumentStatus)
_, err = orgRepo.FetchOrganisationByID(context.Background(), org.UID)
require.Equal(t, datastore.ErrOrgNotFound, err)
}
1 change: 0 additions & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2022-06-17 00:47:00.481242 +0100 WAT m=+69.110158792

package docs

import (
Expand Down
7 changes: 2 additions & 5 deletions server/group_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,8 @@ func (s *GroupIntegrationTestSuite) TestDeleteGroup() {

// Assert.
require.Equal(s.T(), expectedStatusCode, w.Code)
g, err := s.DB.GroupRepo().FetchGroupByID(context.Background(), group.UID)
require.NoError(s.T(), err)

require.Equal(s.T(), datastore.DeletedDocumentStatus, g.DocumentStatus)
require.True(s.T(), g.DeletedAt > 0)
_, err = s.DB.GroupRepo().FetchGroupByID(context.Background(), group.UID)
require.Equal(s.T(), datastore.ErrGroupNotFound, err)
}

func (s *GroupIntegrationTestSuite) TestDeleteGroup_GroupNotFound() {
Expand Down
7 changes: 2 additions & 5 deletions server/organisation_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,8 @@ func (s *OrganisationIntegrationTestSuite) Test_DeleteOrganisation() {
// Assert.
require.Equal(s.T(), expectedStatusCode, w.Code)

org, err := s.DB.OrganisationRepo().FetchOrganisationByID(context.Background(), uid)
require.NoError(s.T(), err)

require.NotEmpty(s.T(), org.DeletedAt)
require.Equal(s.T(), datastore.DeletedDocumentStatus, org.DocumentStatus)
_, err = s.DB.OrganisationRepo().FetchOrganisationByID(context.Background(), uid)
require.Equal(s.T(), datastore.ErrOrgNotFound, err)
}

func TestOrganisationIntegrationTestSuite(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions server/security_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ package server
import (
"context"
"fmt"
"github.com/frain-dev/convoy/auth"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/frain-dev/convoy/auth"

"github.com/frain-dev/convoy/config"
"github.com/frain-dev/convoy/datastore"
"github.com/frain-dev/convoy/server/models"
Expand Down Expand Up @@ -206,10 +207,8 @@ func (s *SecurityIntegrationTestSuite) Test_RevokeAPIKey() {
require.Equal(s.T(), expectedStatusCode, w.Code)

// Deep assert
a, err := s.DB.APIRepo().FindAPIKeyByID(context.Background(), apiKey.UID)
require.NoError(s.T(), err)
require.Equal(s.T(), datastore.DeletedDocumentStatus, a.DocumentStatus)
require.True(s.T(), a.DeletedAt > 0)
_, err = s.DB.APIRepo().FindAPIKeyByID(context.Background(), apiKey.UID)
require.Equal(s.T(), datastore.ErrAPIKeyNotFound, err)
}

func (s *SecurityIntegrationTestSuite) Test_GetAPIKeyByID() {
Expand Down