Skip to content

Commit

Permalink
Add #597 Replace the usage of primitive.ObjectID with string
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa committed Dec 14, 2023
1 parent 4f4fa0d commit 5d5dd77
Show file tree
Hide file tree
Showing 75 changed files with 1,295 additions and 1,008 deletions.
17 changes: 8 additions & 9 deletions internal/actionlog/actionlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand Down Expand Up @@ -44,14 +43,14 @@ func GetTypeStr(logType int) string {

// ActionLog All access logs
type ActionLog struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Type int `json:"type"`
TypeStr string `json:"typeStr"`
OrgID string `json:"orgId"`
UserID string `json:"userId"`
UserName string `json:"userName"`
Action string `json:"action"` //Free string storing the real log
Timestamp string `json:"timestamp"`
ID string `json:"id" bson:"_id,omitempty"`
Type int `json:"type"`
TypeStr string `json:"typeStr"`
OrgID string `json:"orgId"`
UserID string `json:"userId"`
UserName string `json:"userName"`
Action string `json:"action"` //Free string storing the real log
Timestamp string `json:"timestamp"`
}

type ActionLogRepository struct {
Expand Down
19 changes: 9 additions & 10 deletions internal/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import (

"github.com/bb-consent/api/internal/config"
"github.com/dgrijalva/jwt-go"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type ApiKey struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Name string `json:"name"`
Scopes []string `json:"scopes" valid:"required"`
Apikey string `json:"apiKey"`
ExpiryInDays int `json:"expiryInDays"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
Timestamp string `json:"timestamp"`
ExpiryTimestamp string `json:"expiryTimestamp"`
Id string `json:"id" bson:"_id,omitempty"`
Name string `json:"name"`
Scopes []string `json:"scopes" valid:"required"`
Apikey string `json:"apiKey"`
ExpiryInDays int `json:"expiryInDays"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
Timestamp string `json:"timestamp"`
ExpiryTimestamp string `json:"expiryTimestamp"`
}

var ApiSecretKey string
Expand Down
9 changes: 2 additions & 7 deletions internal/apikey/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)

Expand Down Expand Up @@ -37,16 +36,12 @@ func (apiKeyRepo *ApiKeyRepository) Add(apiKey ApiKey) (ApiKey, error) {
}

// Get Gets a single data agreement by given id
func (apiKeyRepo *ApiKeyRepository) Get(apiKeyID string) (ApiKey, error) {
apiKeyId, err := primitive.ObjectIDFromHex(apiKeyID)
if err != nil {
return ApiKey{}, err
}
func (apiKeyRepo *ApiKeyRepository) Get(apiKeyId string) (ApiKey, error) {

filter := common.CombineFilters(apiKeyRepo.DefaultFilter, bson.M{"_id": apiKeyId})

var result ApiKey
err = Collection().FindOne(context.TODO(), filter).Decode(&result)
err := Collection().FindOne(context.TODO(), filter).Decode(&result)
return result, err
}

Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/start_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func StartApiCmdHandler(cmd *cobra.Command, args []string) {
}
log.Println("Data base session opened")

// Applying migration
log.Println("Applying migrate")
migrate.Migrate()

// Webhooks
webhook.Init(loadedConfig)
log.Println("Webhooks configuration initialized")
Expand Down Expand Up @@ -89,10 +93,6 @@ func StartApiCmdHandler(cmd *cobra.Command, args []string) {
tenant.SingleTenantConfiguration(loadedConfig)
}

// Applying migration
log.Println("Applying migrate")
migrate.Migrate()

// Router
router := mux.NewRouter()
if loadedConfig.TestMode {
Expand Down
110 changes: 46 additions & 64 deletions internal/dataagreement/dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,52 @@ func Collection() *mongo.Collection {
}

type DataAttribute struct {
Id primitive.ObjectID `json:"id" bson:"id,omitempty"`
Name string `json:"name" valid:"required"`
Description string `json:"description" valid:"required"`
Sensitivity bool `json:"sensitivity"`
Category string `json:"category"`
Id string `json:"id" bson:"id,omitempty"`
Name string `json:"name" valid:"required"`
Description string `json:"description" valid:"required"`
Sensitivity bool `json:"sensitivity"`
Category string `json:"category"`
}

type Signature struct {
Id primitive.ObjectID `json:"id"`
Payload string `json:"payload"`
Signature string `json:"signature"`
VerificationMethod string `json:"verificationMethod"`
VerificationPayload string `json:"verificationPayload"`
VerificationPayloadHash string `json:"verificationPayloadHash"`
VerificationArtifact string `json:"verificationArtifact"`
VerificationSignedBy string `json:"verificationSignedBy"`
VerificationSignedAs string `json:"verificationSignedAs"`
VerificationJwsHeader string `json:"verificationJwsHeader"`
Timestamp string `json:"timestamp"`
SignedWithoutObjectReference bool `json:"signedWithoutObjectReference"`
ObjectType string `json:"objectType"`
ObjectReference string `json:"objectReference"`
}

type PolicyForDataAgreement struct {
policy.Policy
Id string `json:"id"`
Id string `json:"id"`
Payload string `json:"payload"`
Signature string `json:"signature"`
VerificationMethod string `json:"verificationMethod"`
VerificationPayload string `json:"verificationPayload"`
VerificationPayloadHash string `json:"verificationPayloadHash"`
VerificationArtifact string `json:"verificationArtifact"`
VerificationSignedBy string `json:"verificationSignedBy"`
VerificationSignedAs string `json:"verificationSignedAs"`
VerificationJwsHeader string `json:"verificationJwsHeader"`
Timestamp string `json:"timestamp"`
SignedWithoutObjectReference bool `json:"signedWithoutObjectReference"`
ObjectType string `json:"objectType"`
ObjectReference string `json:"objectReference"`
}

type DataAgreement struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Version string `json:"version"`
ControllerId string `json:"controllerId"`
ControllerUrl string `json:"controllerUrl" valid:"required"`
ControllerName string `json:"controllerName" valid:"required"`
Policy policy.Policy `json:"policy" valid:"required"`
Purpose string `json:"purpose" valid:"required"`
PurposeDescription string `json:"purposeDescription" valid:"required"`
LawfulBasis string `json:"lawfulBasis" valid:"required"`
MethodOfUse string `json:"methodOfUse" valid:"required"`
DpiaDate string `json:"dpiaDate"`
DpiaSummaryUrl string `json:"dpiaSummaryUrl"`
Signature Signature `json:"signature"`
Active bool `json:"active"`
Forgettable bool `json:"forgettable"`
CompatibleWithVersionId string `json:"compatibleWithVersionId"`
Lifecycle string `json:"lifecycle" valid:"required"`
DataAttributes []DataAttribute `json:"dataAttributes" valid:"required"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
Timestamp string `json:"-"`
Id string `json:"id" bson:"_id,omitempty"`
Version string `json:"version"`
ControllerId string `json:"controllerId"`
ControllerUrl string `json:"controllerUrl" valid:"required"`
ControllerName string `json:"controllerName" valid:"required"`
Policy policy.Policy `json:"policy" valid:"required"`
Purpose string `json:"purpose" valid:"required"`
PurposeDescription string `json:"purposeDescription" valid:"required"`
LawfulBasis string `json:"lawfulBasis" valid:"required"`
MethodOfUse string `json:"methodOfUse" valid:"required"`
DpiaDate string `json:"dpiaDate"`
DpiaSummaryUrl string `json:"dpiaSummaryUrl"`
Signature Signature `json:"signature"`
Active bool `json:"active"`
Forgettable bool `json:"forgettable"`
CompatibleWithVersionId string `json:"compatibleWithVersionId"`
Lifecycle string `json:"lifecycle" valid:"required"`
DataAttributes []DataAttribute `json:"dataAttributes" valid:"required"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
Timestamp string `json:"-"`
}

type DataAgreementWithObjectData struct {
Expand Down Expand Up @@ -99,16 +94,12 @@ func (darepo *DataAgreementRepository) Add(dataAgreement DataAgreement) (DataAgr
}

// Get Gets a single data agreement by given id
func (darepo *DataAgreementRepository) Get(dataAgreementID string) (DataAgreement, error) {
dataAgreementId, err := primitive.ObjectIDFromHex(dataAgreementID)
if err != nil {
return DataAgreement{}, err
}
func (darepo *DataAgreementRepository) Get(dataAgreementId string) (DataAgreement, error) {

filter := common.CombineFilters(darepo.DefaultFilter, bson.M{"_id": dataAgreementId})

var result DataAgreement
err = Collection().FindOne(context.TODO(), filter).Decode(&result)
err := Collection().FindOne(context.TODO(), filter).Decode(&result)
return result, err
}

Expand Down Expand Up @@ -248,16 +239,12 @@ func CreatePipelineForFilteringDataAgreementsUsingLifecycle(organisationId strin
}

// GetDataAttributeById Gets a single data agreement by data attribute id
func (darepo *DataAgreementRepository) GetByDataAttributeId(dataAttributeID string) (DataAgreement, error) {
dataAgreementId, err := primitive.ObjectIDFromHex(dataAttributeID)
if err != nil {
return DataAgreement{}, err
}
func (darepo *DataAgreementRepository) GetByDataAttributeId(dataAttributeId string) (DataAgreement, error) {

filter := common.CombineFilters(darepo.DefaultFilter, bson.M{"dataattributes.id": dataAgreementId})
filter := common.CombineFilters(darepo.DefaultFilter, bson.M{"dataattributes.id": dataAttributeId})

var result DataAgreement
err = Collection().FindOne(context.TODO(), filter).Decode(&result)
err := Collection().FindOne(context.TODO(), filter).Decode(&result)
return result, err
}

Expand Down Expand Up @@ -364,12 +351,7 @@ func (darepo *DataAgreementRepository) CountDocumentsByPurpose(purpose string) (
return exists, nil
}

func (darepo *DataAgreementRepository) CountDocumentsByPurposeExeptOneDataAgreement(purpose string, dataAgreementID string) (int64, error) {

dataAgreementId, err := primitive.ObjectIDFromHex(dataAgreementID)
if err != nil {
return 0, err
}
func (darepo *DataAgreementRepository) CountDocumentsByPurposeExeptOneDataAgreement(purpose string, dataAgreementId string) (int64, error) {

filter := common.CombineFilters(darepo.DefaultFilter, bson.M{
"purpose": purpose,
Expand Down
24 changes: 11 additions & 13 deletions internal/dataagreement_record/dataagreement_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ package dataagreementrecord
import (
"net/http"
"strings"

"go.mongodb.org/mongo-driver/bson/primitive"
)

type DataAgreementRecord struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
DataAgreementId string `json:"dataAgreementId"`
DataAgreementRevisionId string `json:"dataAgreementRevisionId"`
DataAgreementRevisionHash string `json:"dataAgreementRevisionHash"`
IndividualId string `json:"individualId"`
OptIn bool `json:"optIn"`
State string `json:"state" valid:"required"`
SignatureId string `json:"signatureId"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
Id string `json:"id" bson:"_id,omitempty"`
DataAgreementId string `json:"dataAgreementId"`
DataAgreementRevisionId string `json:"dataAgreementRevisionId"`
DataAgreementRevisionHash string `json:"dataAgreementRevisionHash"`
IndividualId string `json:"individualId"`
OptIn bool `json:"optIn"`
State string `json:"state" valid:"required"`
SignatureId string `json:"signatureId"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
}

type RevisionForListDataAgreementRecord struct {
Expand All @@ -26,7 +24,7 @@ type RevisionForListDataAgreementRecord struct {
}

type DataAgreementRecordForAuditList struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Id string `json:"id" bson:"_id,omitempty"`
Revisions []RevisionForListDataAgreementRecord
}

Expand Down
16 changes: 3 additions & 13 deletions internal/dataagreement_record/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,12 @@ func (darRepo *DataAgreementRecordRepository) Add(dataAgreementRecord DataAgreem
}

// Get Gets a single data agreement record
func (darRepo *DataAgreementRecordRepository) Get(dataAgreementRecordID string) (DataAgreementRecord, error) {
dataAgreementRecordId, err := primitive.ObjectIDFromHex(dataAgreementRecordID)
if err != nil {
return DataAgreementRecord{}, err
}
func (darRepo *DataAgreementRecordRepository) Get(dataAgreementRecordId string) (DataAgreementRecord, error) {

filter := common.CombineFilters(darRepo.DefaultFilter, bson.M{"_id": dataAgreementRecordId})

var result DataAgreementRecord
err = Collection().FindOne(context.TODO(), filter).Decode(&result)
err := Collection().FindOne(context.TODO(), filter).Decode(&result)

return result, err
}
Expand Down Expand Up @@ -109,16 +105,10 @@ func DataAgreementRecordsWithRevisionsFilteredById(organisationId string, id str
or := []bson.M{
{"dataagreementid": id},
{"individualid": id},
{"_id": id},
}

// Stage 2 - Match `id` against `dataAgreementRecordId`, `dataAgreementId`, `individualId`
convertIdtoObjectId, err := primitive.ObjectIDFromHex(id)
if err == nil {
// Append `dataAgreementRecordId` `or` statements only if
// string is converted to objectId without errors
or = append(or, bson.M{"_id": convertIdtoObjectId})
}

pipeline = append(pipeline, bson.M{"$match": bson.M{
"$or": or,
}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/bb-consent/api/internal/dataagreement"
"github.com/bb-consent/api/internal/org"
"go.mongodb.org/mongo-driver/bson/primitive"
)

// DataAgreementRecordsHistory
Expand Down Expand Up @@ -44,6 +45,8 @@ func DataAgreementRecordHistoryAdd(darH DataAgreementRecordsHistory, optIn bool)
}
log.Printf("The log is: %s", darH.Log)

darH.Id = primitive.NewObjectID().Hex()

_, err = Add(darH)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions internal/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func LoadOrganisationAdminAvatarImageAssets(u user.User, hostUrl string) (user.U
// Update avatar image for organisation
u.ImageURL = "https://" + hostUrl + "/v2/onboard/admin/avatarimage"
u.ImageID = avatarImageId
u, err = user.Update(u.ID.Hex(), u)
u, err = user.Update(u.ID, u)
if err != nil {
return user.User{}, err
}
Expand All @@ -128,15 +128,15 @@ func LoadImageAssetsForSingleTenantConfiguration() error {

// Check if cover image is present
if len(strings.TrimSpace(o.CoverImageID)) == 0 {
err = loadCoverImageAssets(o.ID.Hex())
err = loadCoverImageAssets(o.ID)
if err != nil {
return err
}
}

// Check if logo image is present
if len(strings.TrimSpace(o.LogoImageID)) == 0 {
err = loadLogoImageAssets(o.ID.Hex())
err = loadLogoImageAssets(o.ID)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
daRecord "github.com/bb-consent/api/internal/dataagreement_record"
"github.com/bb-consent/api/internal/paginate"
"github.com/bb-consent/api/internal/revision"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type dataAgreementForListDataAgreementRecord struct {
Expand All @@ -23,7 +22,7 @@ type dataAgreementForListDataAgreementRecord struct {
}

type listDataAgreementRecord struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Id string `json:"id" bson:"_id,omitempty"`
DataAgreementId string `json:"dataAgreementId"`
DataAgreementRevisionId string `json:"dataAgreementRevisionId"`
DataAgreementRevisionHash string `json:"dataAgreementRevisionHash"`
Expand Down
Loading

0 comments on commit 5d5dd77

Please sign in to comment.