diff --git a/internal/actionlog/actionlog.go b/internal/actionlog/actionlog.go index 33e6d0d..2f753ad 100644 --- a/internal/actionlog/actionlog.go +++ b/internal/actionlog/actionlog.go @@ -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" ) @@ -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 { diff --git a/internal/apikey/apikey.go b/internal/apikey/apikey.go index ee4f2ff..badb67b 100644 --- a/internal/apikey/apikey.go +++ b/internal/apikey/apikey.go @@ -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 diff --git a/internal/apikey/db.go b/internal/apikey/db.go index 88d4c6b..e73d154 100644 --- a/internal/apikey/db.go +++ b/internal/apikey/db.go @@ -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" ) @@ -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 } diff --git a/internal/cmd/start_api.go b/internal/cmd/start_api.go index 85d4f0e..deeac75 100644 --- a/internal/cmd/start_api.go +++ b/internal/cmd/start_api.go @@ -80,6 +80,12 @@ func StartApiCmdHandler(cmd *cobra.Command, args []string) { panic(err) } + // Applying migration to user, policy, orgtype and organisation + migrate.MigrateIdToStringInUsersCollection() + migrate.MigrateIdToStringInPoliciesCollection() + migrate.MigrateIdToStringInOrgTypesCollection() + migrate.MigrateIdToStringInOrganisationCollection() + // Execute actions based on application mode switch loadedConfig.ApplicationMode { case config.SingleTenant: diff --git a/internal/dataagreement/dataagreements.go b/internal/dataagreement/dataagreements.go index 5d2039d..bc776df 100644 --- a/internal/dataagreement/dataagreements.go +++ b/internal/dataagreement/dataagreements.go @@ -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 { @@ -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 } @@ -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 } @@ -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, diff --git a/internal/dataagreement_record/dataagreement_record.go b/internal/dataagreement_record/dataagreement_record.go index e92cf55..ea12aee 100644 --- a/internal/dataagreement_record/dataagreement_record.go +++ b/internal/dataagreement_record/dataagreement_record.go @@ -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 { @@ -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 } diff --git a/internal/dataagreement_record/db.go b/internal/dataagreement_record/db.go index a87d178..5c99c28 100644 --- a/internal/dataagreement_record/db.go +++ b/internal/dataagreement_record/db.go @@ -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 } @@ -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, }}) diff --git a/internal/dataagreement_record_history/dataagreement_record_history.go b/internal/dataagreement_record_history/dataagreement_record_history.go index 370ee76..74f96ef 100644 --- a/internal/dataagreement_record_history/dataagreement_record_history.go +++ b/internal/dataagreement_record_history/dataagreement_record_history.go @@ -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 @@ -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 diff --git a/internal/fixture/fixture.go b/internal/fixture/fixture.go index f51b564..064dec9 100644 --- a/internal/fixture/fixture.go +++ b/internal/fixture/fixture.go @@ -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 } @@ -128,7 +128,7 @@ 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 } @@ -136,7 +136,7 @@ func LoadImageAssetsForSingleTenantConfiguration() error { // 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 } diff --git a/internal/handler/v2/audit/audit_list_dataagreement_records.go b/internal/handler/v2/audit/audit_list_dataagreement_records.go index 377cfa2..9dd0a14 100644 --- a/internal/handler/v2/audit/audit_list_dataagreement_records.go +++ b/internal/handler/v2/audit/audit_list_dataagreement_records.go @@ -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 { @@ -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"` diff --git a/internal/handler/v2/config/apikey/config_create_apikey.go b/internal/handler/v2/config/apikey/config_create_apikey.go index 29713a2..6f5d99d 100644 --- a/internal/handler/v2/config/apikey/config_create_apikey.go +++ b/internal/handler/v2/config/apikey/config_create_apikey.go @@ -85,7 +85,7 @@ func ConfigCreateApiKey(w http.ResponseWriter, r *http.Request) { expiryTimestamp := expiryTime.UTC().Format("2006-01-02T15:04:05Z") var newApiKey apikey.ApiKey - newApiKey.Id = primitive.NewObjectID() + newApiKey.Id = primitive.NewObjectID().Hex() newApiKey.Name = apiKeyReq.Apikey.Name newApiKey.Scopes = apiKeyReq.Apikey.Scopes newApiKey.Apikey = key diff --git a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go index d902ea7..08d24ad 100644 --- a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go @@ -67,25 +67,18 @@ var MethodOfUseMappings = []MethodOfUseMapping{ type policyForDataAgreement struct { policy.Policy - Id string `json:"id"` Name string `json:"name" validate:"required_if=Active true"` Url string `json:"url" validate:"required_if=Active true"` } type dataAttributeForDataAgreement struct { dataagreement.DataAttribute - Id string `json:"id"` Name string `json:"name" validate:"required_if=Active true"` Description string `json:"description" validate:"required_if=Active true,max=500"` } -type signatureForDataAgreement struct { - dataagreement.Signature - Id string `json:"id"` -} - type dataAgreement struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + Id string `json:"id" bson:"_id,omitempty"` Version string `json:"version"` ControllerId string `json:"controllerId"` ControllerUrl string `json:"controllerUrl" validate:"required_if=Active true"` @@ -97,7 +90,7 @@ type dataAgreement struct { MethodOfUse string `json:"methodOfUse" validate:"required_if=Active true"` DpiaDate string `json:"dpiaDate"` DpiaSummaryUrl string `json:"dpiaSummaryUrl"` - Signature signatureForDataAgreement `json:"signature"` + Signature dataagreement.Signature `json:"signature"` Active bool `json:"active"` Forgettable bool `json:"forgettable"` CompatibleWithVersionId string `json:"compatibleWithVersionId"` @@ -177,7 +170,7 @@ func setDataAttributesFromReq(requestBody addDataAgreementReq) []dataagreement.D for _, dA := range requestBody.DataAgreement.DataAttributes { var dataAttribute dataagreement.DataAttribute - dataAttribute.Id = primitive.NewObjectID() + dataAttribute.Id = primitive.NewObjectID().Hex() dataAttribute.Name = dA.Name dataAttribute.Description = dA.Description dataAttribute.Category = dA.Category @@ -191,7 +184,7 @@ func setDataAttributesFromReq(requestBody addDataAgreementReq) []dataagreement.D func setDataAgreementFromReq(requestBody addDataAgreementReq, newDataAgreement dataagreement.DataAgreement) dataagreement.DataAgreement { // Policy - newDataAgreement.Policy.Id = primitive.NewObjectID() + newDataAgreement.Policy.Id = primitive.NewObjectID().Hex() newDataAgreement.Policy.Name = requestBody.DataAgreement.Policy.Name newDataAgreement.Policy.Version = requestBody.DataAgreement.Policy.Version newDataAgreement.Policy.Url = requestBody.DataAgreement.Policy.Url @@ -203,9 +196,9 @@ func setDataAgreementFromReq(requestBody addDataAgreementReq, newDataAgreement d newDataAgreement.Policy.ThirdPartyDataSharing = requestBody.DataAgreement.Policy.ThirdPartyDataSharing // Signature - newDataAgreement.Signature.Id = primitive.NewObjectID() + newDataAgreement.Signature.Id = primitive.NewObjectID().Hex() newDataAgreement.Signature.Payload = requestBody.DataAgreement.Signature.Payload - newDataAgreement.Signature.Signature = requestBody.DataAgreement.Signature.Signature.Signature + newDataAgreement.Signature.Signature = requestBody.DataAgreement.Signature.Signature newDataAgreement.Signature.VerificationMethod = requestBody.DataAgreement.Signature.VerificationMethod newDataAgreement.Signature.VerificationPayload = requestBody.DataAgreement.Signature.VerificationPayload newDataAgreement.Signature.VerificationPayloadHash = requestBody.DataAgreement.Signature.VerificationPayloadHash @@ -235,8 +228,8 @@ func setDataAgreementFromReq(requestBody addDataAgreementReq, newDataAgreement d } func setControllerFromReq(o org.Organization, newDataAgreement dataagreement.DataAgreement) dataagreement.DataAgreement { - newDataAgreement.OrganisationId = o.ID.Hex() - newDataAgreement.ControllerId = o.ID.Hex() + newDataAgreement.OrganisationId = o.ID + newDataAgreement.ControllerId = o.ID newDataAgreement.ControllerName = o.Name newDataAgreement.ControllerUrl = o.EulaURL return newDataAgreement @@ -289,7 +282,7 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) { // Initialise data agreement var newDataAgreement dataagreement.DataAgreement - newDataAgreement.Id = primitive.NewObjectID() + newDataAgreement.Id = primitive.NewObjectID().Hex() // Set data agreement details from request body newDataAgreement = setDataAgreementFromReq(dataAgreementReq, newDataAgreement) diff --git a/internal/handler/v2/config/dataagreement/config_list_dataagreements.go b/internal/handler/v2/config/dataagreement/config_list_dataagreements.go index 4462212..bf845da 100644 --- a/internal/handler/v2/config/dataagreement/config_list_dataagreements.go +++ b/internal/handler/v2/config/dataagreement/config_list_dataagreements.go @@ -17,7 +17,6 @@ import ( "github.com/bb-consent/api/internal/policy" "github.com/bb-consent/api/internal/revision" "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" ) // ListDataAgreementsError is an error enumeration for list data agreement API. @@ -178,7 +177,7 @@ func getDataAgreementsWithRevisions(organisationId string, lifecycle string) ([] for _, dataAgreement := range dataAgreements { // list all revisions for data agreement - revisions, err := revision.ListAllByDataAgreementId(dataAgreement.Id.Hex()) + revisions, err := revision.ListAllByDataAgreementId(dataAgreement.Id) if err != nil { return tempDataAgreements, err } @@ -215,7 +214,7 @@ func getDataAgreementsWithRevisions(organisationId string, lifecycle string) ([] } type dataAgreementWithRevisions struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + Id string `json:"id" bson:"_id,omitempty"` Version string `json:"version"` ControllerId string `json:"controllerId"` ControllerUrl string `json:"controllerUrl" valid:"required"` diff --git a/internal/handler/v2/config/dataagreement/config_list_dataattributes_for_dataagreement.go b/internal/handler/v2/config/dataagreement/config_list_dataattributes_for_dataagreement.go index 6d1b7bb..bd4f881 100644 --- a/internal/handler/v2/config/dataagreement/config_list_dataattributes_for_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_list_dataattributes_for_dataagreement.go @@ -11,7 +11,6 @@ import ( "github.com/bb-consent/api/internal/dataagreement" "github.com/bb-consent/api/internal/paginate" "github.com/gorilla/mux" - "go.mongodb.org/mongo-driver/bson/primitive" ) type dataAgreementForDataAttribute struct { @@ -20,7 +19,7 @@ type dataAgreementForDataAttribute struct { } type dataAttributeForLists struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + Id string `json:"id" bson:"_id,omitempty"` Name string `json:"name" valid:"required"` Description string `json:"description" valid:"required"` Sensitivity bool `json:"sensitivity"` @@ -44,7 +43,7 @@ func dataAttributesForList(dA dataagreement.DataAgreement) []dataAttributeForLis tempDataAttribute.Description = dataAttribute.Description tempDataAttribute.Sensitivity = dataAttribute.Sensitivity tempDataAttribute.Category = dataAttribute.Category - tempDataAttribute.DataAgreement.Id = dA.Id.Hex() + tempDataAttribute.DataAgreement.Id = dA.Id tempDataAttribute.DataAgreement.Purpose = dA.Purpose dataAttributes = append(dataAttributes, tempDataAttribute) diff --git a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go index 6125210..7279d02 100644 --- a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go @@ -49,7 +49,7 @@ func validateUpdateDataAgreementRequestBody(dataAgreementReq updateDataAgreement return nil } -func dataAttributeIdExists(dataAttributeId primitive.ObjectID, currentDataAttributes []dataagreement.DataAttribute) bool { +func dataAttributeIdExists(dataAttributeId string, currentDataAttributes []dataagreement.DataAttribute) bool { for _, dataAttribute := range currentDataAttributes { if dataAttributeId == dataAttribute.Id { return true @@ -64,12 +64,11 @@ func updateDataAttributeFromUpdateDataAgreementRequestBody(requestBody updateDat for _, dA := range requestBody.DataAgreement.DataAttributes { var dataAttribute dataagreement.DataAttribute - dataAttributeId, _ := primitive.ObjectIDFromHex(dA.Id) - isExistingDataAttribute := dataAttributeIdExists(dataAttributeId, currentDataAttributes) + isExistingDataAttribute := dataAttributeIdExists(dA.Id, currentDataAttributes) if isExistingDataAttribute { - dataAttribute.Id = dataAttributeId + dataAttribute.Id = dA.Id } else { - dataAttribute.Id = primitive.NewObjectID() + dataAttribute.Id = primitive.NewObjectID().Hex() } dataAttribute.Name = dA.Name @@ -103,7 +102,7 @@ func updateDataAgreementFromRequestBody(requestBody updateDataAgreementReq, toBe toBeUpdatedDataAgreement.DpiaSummaryUrl = requestBody.DataAgreement.DpiaSummaryUrl toBeUpdatedDataAgreement.Signature.Payload = requestBody.DataAgreement.Signature.Payload - toBeUpdatedDataAgreement.Signature.Signature = requestBody.DataAgreement.Signature.Signature.Signature + toBeUpdatedDataAgreement.Signature.Signature = requestBody.DataAgreement.Signature.Signature toBeUpdatedDataAgreement.Signature.VerificationMethod = requestBody.DataAgreement.Signature.VerificationMethod toBeUpdatedDataAgreement.Signature.VerificationPayload = requestBody.DataAgreement.Signature.VerificationPayload toBeUpdatedDataAgreement.Signature.VerificationPayloadHash = requestBody.DataAgreement.Signature.VerificationPayloadHash diff --git a/internal/handler/v2/config/dataattribute/config_list_dataattributes.go b/internal/handler/v2/config/dataattribute/config_list_dataattributes.go index d91d5c7..51adf02 100644 --- a/internal/handler/v2/config/dataattribute/config_list_dataattributes.go +++ b/internal/handler/v2/config/dataattribute/config_list_dataattributes.go @@ -12,7 +12,6 @@ import ( "github.com/bb-consent/api/internal/dataagreement" "github.com/bb-consent/api/internal/paginate" "github.com/bb-consent/api/internal/revision" - "go.mongodb.org/mongo-driver/bson/primitive" ) // ListDataAttributesError is an error enumeration for list data attribute API. @@ -47,7 +46,7 @@ func dataAttributesForList(res []dataagreement.DataAgreement) []dataAttributeFor dataAttribute.Description = dA.Description dataAttribute.Sensitivity = dA.Sensitivity dataAttribute.Category = dA.Category - dataAttribute.DataAgreement.Id = res[i].Id.Hex() + dataAttribute.DataAgreement.Id = res[i].Id dataAttribute.DataAgreement.Purpose = res[i].Purpose dataAttributes = append(dataAttributes, dataAttribute) @@ -108,7 +107,7 @@ type dataAgreementForDataAttribute struct { } type dataAttributeForLists struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + Id string `json:"id" bson:"_id,omitempty"` Name string `json:"name" valid:"required"` Description string `json:"description" valid:"required"` Sensitivity bool `json:"sensitivity"` @@ -224,7 +223,7 @@ func ConfigListDataAttributes(w http.ResponseWriter, r *http.Request) { dA.Description = a.Description dA.Sensitivity = a.Sensitivity dA.Category = a.Category - dA.DataAgreement.Id = da.Id.Hex() + dA.DataAgreement.Id = da.Id dA.DataAgreement.Purpose = da.Purpose dAttributes = append(dAttributes, dA) } diff --git a/internal/handler/v2/config/dataattribute/config_update_dataattribute.go b/internal/handler/v2/config/dataattribute/config_update_dataattribute.go index 6639f12..fc5c822 100644 --- a/internal/handler/v2/config/dataattribute/config_update_dataattribute.go +++ b/internal/handler/v2/config/dataattribute/config_update_dataattribute.go @@ -30,7 +30,7 @@ func updateDataAttributeFromReq(dataAttributeId string, requestBody updateDataAt updatedDataAttributes = dataAttributes for i, dataAttribute := range dataAttributes { - if dataAttribute.Id.Hex() == dataAttributeId { + if dataAttribute.Id == dataAttributeId { updatedDataAttributes[i].Name = requestBody.DataAttribute.Name updatedDataAttributes[i].Description = requestBody.DataAttribute.Description updatedDataAttributes[i].Sensitivity = requestBody.DataAttribute.Sensitivity @@ -97,7 +97,7 @@ func ConfigUpdateDataAttribute(w http.ResponseWriter, r *http.Request) { // Bump major version for data agreement updatedVersion, err := common.BumpMajorVersion(toBeUpdatedDataAgreement.Version) if err != nil { - m := fmt.Sprintf("Failed to bump major version for data agreement: %v", toBeUpdatedDataAgreement.Id.Hex()) + m := fmt.Sprintf("Failed to bump major version for data agreement: %v", toBeUpdatedDataAgreement.Id) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) return } @@ -119,7 +119,7 @@ func ConfigUpdateDataAttribute(w http.ResponseWriter, r *http.Request) { // Update revision _, err = revision.UpdateRevisionForDataAgreement(toBeUpdatedDataAgreement, orgAdminId) if err != nil { - m := fmt.Sprintf("Failed to update data agreement: %v", toBeUpdatedDataAgreement.Id.Hex()) + m := fmt.Sprintf("Failed to update data agreement: %v", toBeUpdatedDataAgreement.Id) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) return } diff --git a/internal/handler/v2/config/idp/config_create_idp.go b/internal/handler/v2/config/idp/config_create_idp.go index 5f34fa0..aefe3b3 100644 --- a/internal/handler/v2/config/idp/config_create_idp.go +++ b/internal/handler/v2/config/idp/config_create_idp.go @@ -55,7 +55,7 @@ func ConfigCreateIdp(w http.ResponseWriter, r *http.Request) { var newIdentityProvider idp.IdentityProvider // OpenID config - newIdentityProvider.Id = primitive.NewObjectID() + newIdentityProvider.Id = primitive.NewObjectID().Hex() newIdentityProvider.AuthorizationURL = idpReq.Idp.AuthorizationURL newIdentityProvider.TokenURL = idpReq.Idp.TokenURL newIdentityProvider.LogoutURL = idpReq.Idp.LogoutURL diff --git a/internal/handler/v2/config/individual/config_create_individual.go b/internal/handler/v2/config/individual/config_create_individual.go index 56b6c30..97b33d9 100644 --- a/internal/handler/v2/config/individual/config_create_individual.go +++ b/internal/handler/v2/config/individual/config_create_individual.go @@ -66,7 +66,7 @@ func ConfigCreateIndividual(w http.ResponseWriter, r *http.Request) { } newIndividual := updateIndividualFromRequestBody(individualReq) - newIndividual.Id = primitive.NewObjectID() + newIndividual.Id = primitive.NewObjectID().Hex() newIndividual.OrganisationId = organisationId newIndividual.IsDeleted = false newIndividual.IsOnboardedFromIdp = false diff --git a/internal/handler/v2/config/policy/config_create_policy.go b/internal/handler/v2/config/policy/config_create_policy.go index 9f1018b..f09c801 100644 --- a/internal/handler/v2/config/policy/config_create_policy.go +++ b/internal/handler/v2/config/policy/config_create_policy.go @@ -113,7 +113,7 @@ func ConfigCreatePolicy(w http.ResponseWriter, r *http.Request) { // Initialise policy var newPolicy policy.Policy - newPolicy.Id = primitive.NewObjectID() + newPolicy.Id = primitive.NewObjectID().Hex() // Update policy from request body newPolicy = updatePolicyFromAddPolicyRequestBody(policyReq, newPolicy) newPolicy.OrganisationId = organisationId diff --git a/internal/handler/v2/config/webhook/config_create_webhook.go b/internal/handler/v2/config/webhook/config_create_webhook.go index a6193e5..1abe700 100644 --- a/internal/handler/v2/config/webhook/config_create_webhook.go +++ b/internal/handler/v2/config/webhook/config_create_webhook.go @@ -137,7 +137,7 @@ func ConfigCreateWebhook(w http.ResponseWriter, r *http.Request) { } var newWebhook wh.Webhook - newWebhook.ID = primitive.NewObjectID() + newWebhook.ID = primitive.NewObjectID().Hex() newWebhook = updateWebhookFromAddWebhookRequestBody(webhookReq, newWebhook) newWebhook.OrganisationId = organisationId newWebhook.IsDeleted = false diff --git a/internal/handler/v2/config/webhook/config_list_recent_webhook_deliveries.go b/internal/handler/v2/config/webhook/config_list_recent_webhook_deliveries.go index 59793c4..fdc0264 100644 --- a/internal/handler/v2/config/webhook/config_list_recent_webhook_deliveries.go +++ b/internal/handler/v2/config/webhook/config_list_recent_webhook_deliveries.go @@ -11,18 +11,17 @@ import ( "github.com/bb-consent/api/internal/paginate" wh "github.com/bb-consent/api/internal/webhook" "github.com/gorilla/mux" - "go.mongodb.org/mongo-driver/bson/primitive" ) // recentWebhookDelivery Defines the structure for recent webhook delivery type recentWebhookDelivery struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` // Webhook delivery ID - WebhookId string `json:"webhookId"` // Webhook ID - ResponseStatusCode int `json:"responseStatusCode"` // HTTP response status code - ResponseStatusStr string `json:"responseStatusStr"` // HTTP response status string - TimeStamp string `json:"timestamp"` // UTC timestamp when webhook execution started - Status string `json:"status"` // Status of webhook delivery for e.g. failed or completed - StatusDescription string `json:"statusDescription"` // Describe the status for e.g. Reason for failure + Id string `json:"id" bson:"_id,omitempty"` // Webhook delivery ID + WebhookId string `json:"webhookId"` // Webhook ID + ResponseStatusCode int `json:"responseStatusCode"` // HTTP response status code + ResponseStatusStr string `json:"responseStatusStr"` // HTTP response status string + TimeStamp string `json:"timestamp"` // UTC timestamp when webhook execution started + Status string `json:"status"` // Status of webhook delivery for e.g. failed or completed + StatusDescription string `json:"statusDescription"` // Describe the status for e.g. Reason for failure } func webhookDeliveriesToInterfaceSlice(webhookdeliveries []recentWebhookDelivery) []interface{} { @@ -64,7 +63,7 @@ func ConfigListRecentWebhookDeliveries(w http.ResponseWriter, r *http.Request) { } // Get all recent webhook deliveries - recentWebhookDeliveries, err := wh.GetAllDeliveryByWebhookId(webhook.ID.Hex()) + recentWebhookDeliveries, err := wh.GetAllDeliveryByWebhookId(webhook.ID) if err != nil { m := fmt.Sprintf("Failed to fetch recent payload deliveries for webhook:%v for organisation: %v", webhookId, organisationId) common.HandleError(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/config/webhook/config_list_webhooks.go b/internal/handler/v2/config/webhook/config_list_webhooks.go index 37c0270..1817106 100644 --- a/internal/handler/v2/config/webhook/config_list_webhooks.go +++ b/internal/handler/v2/config/webhook/config_list_webhooks.go @@ -65,7 +65,7 @@ func ConfigListWebhooks(w http.ResponseWriter, r *http.Request) { // Fetching the last delivery to the webhook and retrieving the delivery status isLastDeliverySuccess := false - lastDelivery, err := wh.GetLastWebhookDelivery(webhook.ID.Hex()) + lastDelivery, err := wh.GetLastWebhookDelivery(webhook.ID) if err != nil { // There is no payload delivery yet ! isLastDeliverySuccess = true @@ -80,7 +80,7 @@ func ConfigListWebhooks(w http.ResponseWriter, r *http.Request) { } updatedWebhook := WebhookWithLastDeliveryStatus{ - ID: webhook.ID.Hex(), + ID: webhook.ID, OrganisationId: webhook.OrganisationId, PayloadURL: webhook.PayloadURL, ContentType: webhook.ContentType, diff --git a/internal/handler/v2/config/webhook/config_read_webhook_delivery.go b/internal/handler/v2/config/webhook/config_read_webhook_delivery.go index edb0087..b8d89cc 100644 --- a/internal/handler/v2/config/webhook/config_read_webhook_delivery.go +++ b/internal/handler/v2/config/webhook/config_read_webhook_delivery.go @@ -9,17 +9,16 @@ import ( "github.com/bb-consent/api/internal/config" wh "github.com/bb-consent/api/internal/webhook" "github.com/gorilla/mux" - "go.mongodb.org/mongo-driver/bson/primitive" ) type readWebhookDeliveryResp struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` // Webhook delivery ID - WebhookId string `json:"webhookId"` // Webhook ID - ResponseStatusCode int `json:"responseStatusCode"` // HTTP response status code - ResponseStatusStr string `json:"responseStatusStr"` // HTTP response status string - TimeStamp string `json:"timestamp"` // UTC timestamp when webhook execution started - Status string `json:"status"` // Status of webhook delivery for e.g. failed or completed - StatusDescription string `json:"statusDescription"` // Describe the status for e.g. Reason for failure + Id string `json:"id" bson:"_id,omitempty"` // Webhook delivery ID + WebhookId string `json:"webhookId"` // Webhook ID + ResponseStatusCode int `json:"responseStatusCode"` // HTTP response status code + ResponseStatusStr string `json:"responseStatusStr"` // HTTP response status string + TimeStamp string `json:"timestamp"` // UTC timestamp when webhook execution started + Status string `json:"status"` // Status of webhook delivery for e.g. failed or completed + StatusDescription string `json:"statusDescription"` // Describe the status for e.g. Reason for failure } // GetRecentWebhookDeliveryById Gets the payload delivery details for a webhook by ID @@ -47,7 +46,7 @@ func ConfigReadRecentWebhookDelivery(w http.ResponseWriter, r *http.Request) { } // Get the webhook delivery by ID - webhookDelivery, err := wh.GetWebhookDeliveryByID(webhook.ID.Hex(), deliveryId) + webhookDelivery, err := wh.GetWebhookDeliveryByID(webhook.ID, deliveryId) if err != nil { m := fmt.Sprintf("Failed to get delivery details by ID:%v for webhook:%v for organisation: %v", deliveryId, webhookId, organisationId) common.HandleError(w, http.StatusBadRequest, m, err) diff --git a/internal/handler/v2/config/webhook/config_redeliver_webhook_payload_by_deliveryid.go b/internal/handler/v2/config/webhook/config_redeliver_webhook_payload_by_deliveryid.go index 0ae73f8..3de5783 100644 --- a/internal/handler/v2/config/webhook/config_redeliver_webhook_payload_by_deliveryid.go +++ b/internal/handler/v2/config/webhook/config_redeliver_webhook_payload_by_deliveryid.go @@ -8,7 +8,7 @@ import ( "github.com/bb-consent/api/internal/actionlog" "github.com/bb-consent/api/internal/common" "github.com/bb-consent/api/internal/config" - "github.com/bb-consent/api/internal/user" + "github.com/bb-consent/api/internal/individual" wh "github.com/bb-consent/api/internal/webhook" "github.com/bb-consent/api/internal/webhook_dispatcher" "github.com/gorilla/mux" @@ -39,7 +39,7 @@ func ConfigRedeliverWebhookPayloadByDeliveryID(w http.ResponseWriter, r *http.Re } // Validating the given delivery ID for a webhook - webhookDelivery, err := wh.GetWebhookDeliveryByID(webhook.ID.Hex(), deliveryId) + webhookDelivery, err := webhook_dispatcher.GetWebhookDeliveryByID(webhook.ID, deliveryId) if err != nil { m := fmt.Sprintf("Failed to get delivery details by ID:%v for webhook:%v for organisation: %v", deliveryId, webhookId, organisationId) common.HandleError(w, http.StatusBadRequest, m, err) @@ -54,8 +54,12 @@ func ConfigRedeliverWebhookPayloadByDeliveryID(w http.ResponseWriter, r *http.Re return } + // Repository + individualRepo := individual.IndividualRepository{} + individualRepo.Init(organisationId) + // Get the details of who triggered webhook - u, err := user.Get(webhookDelivery.UserID) + u, err := individualRepo.Get(webhookDelivery.UserID) if err != nil { m := fmt.Sprintf("Failed to get user, error:%v; Failed to redeliver payload for webhook for event:<%s>, user:<%s>, org:<%s>", err.Error(), webhookDelivery.WebhookEventType, webhookDelivery.UserID, organisationId) common.HandleError(w, http.StatusInternalServerError, m, err) @@ -66,7 +70,7 @@ func ConfigRedeliverWebhookPayloadByDeliveryID(w http.ResponseWriter, r *http.Re // Log webhook calls in webhooks category aLog := fmt.Sprintf("Organization webhook: %v triggered by user: %v by event: %v", webhook.PayloadURL, u.Email, webhookDelivery.WebhookEventType) - actionlog.LogOrgWebhookCalls(u.ID.Hex(), u.Email, organisationId, aLog) + actionlog.LogOrgWebhookCalls(u.Id, u.Email, organisationId, aLog) w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON) w.WriteHeader(http.StatusOK) diff --git a/internal/handler/v2/config/webhook/config_update_webhook.go b/internal/handler/v2/config/webhook/config_update_webhook.go index 0ef2ca4..b67565b 100644 --- a/internal/handler/v2/config/webhook/config_update_webhook.go +++ b/internal/handler/v2/config/webhook/config_update_webhook.go @@ -30,7 +30,7 @@ func validateUpdatewebhookRequestBody(webhookReq updateWebhookReq, currentWebhoo // Check if webhook with provided payload URL already exists tempWebhook, err := webhhokRepo.GetWebhookByPayloadURL(webhookReq.Webhook.PayloadURL) if err == nil { - if tempWebhook.ID.Hex() != webhookId { + if tempWebhook.ID != webhookId { return errors.New("webhook with provided payload URL already exists") } } diff --git a/internal/handler/v2/onboard/getorganizationcoverimage_handler.go b/internal/handler/v2/onboard/getorganizationcoverimage_handler.go index 85f95a7..9dd4999 100644 --- a/internal/handler/v2/onboard/getorganizationcoverimage_handler.go +++ b/internal/handler/v2/onboard/getorganizationcoverimage_handler.go @@ -12,7 +12,7 @@ import ( // GetOrganizationCoverImage Retrieves the organization cover image func GetOrganizationCoverImage(w http.ResponseWriter, r *http.Request) { - organizationID := r.Header.Get(config.OrganizationId) + organizationID := common.Sanitize(r.Header.Get(config.OrganizationId)) o, err := org.Get(organizationID) if err != nil { m := fmt.Sprintf("Failed to get organization by ID :%v", organizationID) diff --git a/internal/handler/v2/onboard/getorganizationlogoimage_handler.go b/internal/handler/v2/onboard/getorganizationlogoimage_handler.go index 63b6824..9c8ae4a 100644 --- a/internal/handler/v2/onboard/getorganizationlogoimage_handler.go +++ b/internal/handler/v2/onboard/getorganizationlogoimage_handler.go @@ -12,7 +12,7 @@ import ( // GetOrganizationLogoImage Retrieves the organization logo image func GetOrganizationLogoImage(w http.ResponseWriter, r *http.Request) { - organizationID := r.Header.Get(config.OrganizationId) + organizationID := common.Sanitize(r.Header.Get(config.OrganizationId)) o, err := org.Get(organizationID) if err != nil { m := fmt.Sprintf("Failed to get organization by ID :%v", organizationID) diff --git a/internal/handler/v2/onboard/loginadminuser_handler.go b/internal/handler/v2/onboard/loginadminuser_handler.go index ecd4dac..eb983ea 100644 --- a/internal/handler/v2/onboard/loginadminuser_handler.go +++ b/internal/handler/v2/onboard/loginadminuser_handler.go @@ -84,7 +84,7 @@ func LoginAdminUser(w http.ResponseWriter, r *http.Request) { } actionLog := fmt.Sprintf("%v logged in", u.Email) - actionlog.LogOrgSecurityCalls(u.ID.Hex(), u.Email, u.Roles[0].OrgID, actionLog) + actionlog.LogOrgSecurityCalls(u.ID, u.Email, u.Roles[0].OrgID, actionLog) lResp := loginResp{ AccessToken: t.AccessToken, ExpiresIn: t.ExpiresIn, diff --git a/internal/handler/v2/onboard/onboard_exchange_authorization_code.go b/internal/handler/v2/onboard/onboard_exchange_authorization_code.go index 7e25ea2..107bf4c 100644 --- a/internal/handler/v2/onboard/onboard_exchange_authorization_code.go +++ b/internal/handler/v2/onboard/onboard_exchange_authorization_code.go @@ -152,7 +152,7 @@ func ExchangeAuthorizationCode(w http.ResponseWriter, r *http.Request) { if err != nil { if err == mongo.ErrNoDocuments { // Create individual if not present - createIndividualFromIdp(individualEmail, individualExternalId, organisationId, idp.Id.Hex()) + createIndividualFromIdp(individualEmail, individualExternalId, organisationId, idp.Id) } else { m := fmt.Sprintf("Failed to fetch individual: %v", individualExternalId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) @@ -183,7 +183,7 @@ func createIndividualFromIdp(email string, externalId string, organisationId str individualRepo.Init(organisationId) var newIndividual individual.Individual - newIndividual.Id = primitive.NewObjectID() + newIndividual.Id = primitive.NewObjectID().Hex() newIndividual.Email = email newIndividual.ExternalId = externalId newIndividual.OrganisationId = organisationId diff --git a/internal/handler/v2/onboard/onboard_logout_user.go b/internal/handler/v2/onboard/onboard_logout_user.go index ee7af69..342bed7 100644 --- a/internal/handler/v2/onboard/onboard_logout_user.go +++ b/internal/handler/v2/onboard/onboard_logout_user.go @@ -35,7 +35,7 @@ func logoutUser(accessToken string, refreshToken string, iamId string, organisat // log security calls if len(orgAdmin.Roles) > 0 { actionLog := fmt.Sprintf("%v logged out", orgAdmin.Email) - actionlog.LogOrgSecurityCalls(orgAdmin.ID.Hex(), orgAdmin.Email, organisationId, actionLog) + actionlog.LogOrgSecurityCalls(orgAdmin.ID, orgAdmin.Email, organisationId, actionLog) } return nil } diff --git a/internal/handler/v2/onboard/onboard_read_organisation.go b/internal/handler/v2/onboard/onboard_read_organisation.go index a67057b..a3315d8 100644 --- a/internal/handler/v2/onboard/onboard_read_organisation.go +++ b/internal/handler/v2/onboard/onboard_read_organisation.go @@ -8,16 +8,15 @@ import ( "github.com/bb-consent/api/internal/common" "github.com/bb-consent/api/internal/config" "github.com/bb-consent/api/internal/org" - "go.mongodb.org/mongo-driver/bson/primitive" ) type organizationResp struct { - ID primitive.ObjectID `bson:"_id,omitempty" json:"id"` - Name string `json:"name"` - Description string `json:"description"` - Sector string `json:"sector"` - Location string `json:"location"` - PolicyURL string `json:"policyUrl"` + ID string `bson:"_id,omitempty" json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Sector string `json:"sector"` + Location string `json:"location"` + PolicyURL string `json:"policyUrl"` } type getOrgResp struct { @@ -26,7 +25,7 @@ type getOrgResp struct { // OnboardReadOrganisation Gets a single organisation by given id func OnboardReadOrganisation(w http.ResponseWriter, r *http.Request) { - organizationID := r.Header.Get(config.OrganizationId) + organizationID := common.Sanitize(r.Header.Get(config.OrganizationId)) o, err := org.Get(organizationID) if err != nil { m := fmt.Sprintf("Failed to get organization by ID :%v", organizationID) diff --git a/internal/handler/v2/onboard/onboard_read_organisation_admin.go b/internal/handler/v2/onboard/onboard_read_organisation_admin.go index 5d02ef2..b663724 100644 --- a/internal/handler/v2/onboard/onboard_read_organisation_admin.go +++ b/internal/handler/v2/onboard/onboard_read_organisation_admin.go @@ -40,7 +40,7 @@ func OnboardReadOrganisationAdmin(w http.ResponseWriter, r *http.Request) { panic(err) } resp := readOrgAdminResp{ - Id: u.ID.Hex(), + Id: u.ID, Email: u.Email, Name: u.Name, AvatarImageId: u.ImageID, diff --git a/internal/handler/v2/onboard/onboard_refresh_token.go b/internal/handler/v2/onboard/onboard_refresh_token.go index 186684d..b665fcb 100644 --- a/internal/handler/v2/onboard/onboard_refresh_token.go +++ b/internal/handler/v2/onboard/onboard_refresh_token.go @@ -27,7 +27,7 @@ func refreshTokenForExternalIdpIssuedToken(refreshToken string) (*oauth2.Token, // Repository idpRepo := idp.IdentityProviderRepository{} - idpRepo.Init(organisation.ID.Hex()) + idpRepo.Init(organisation.ID) // Fetch IDP details based on org Id idp, err := idpRepo.GetByOrgId() diff --git a/internal/handler/v2/onboard/onboard_update_org_admin_avathar.go b/internal/handler/v2/onboard/onboard_update_org_admin_avathar.go index 5d6a05f..b9df90a 100644 --- a/internal/handler/v2/onboard/onboard_update_org_admin_avathar.go +++ b/internal/handler/v2/onboard/onboard_update_org_admin_avathar.go @@ -45,7 +45,7 @@ func OnboardUpdateOrganisationAdminAvatar(w http.ResponseWriter, r *http.Request file, _, err := r.FormFile("avatarimage") if err != nil { - m := fmt.Sprintf("Failed to extract image user: %v", u.ID.Hex()) + m := fmt.Sprintf("Failed to extract image user: %v", u.ID) common.HandleError(w, http.StatusInternalServerError, m, err) return } @@ -54,14 +54,14 @@ func OnboardUpdateOrganisationAdminAvatar(w http.ResponseWriter, r *http.Request buf := bytes.NewBuffer(nil) _, err = io.Copy(buf, file) if err != nil { - m := fmt.Sprintf("Failed to copy image user: %v", u.ID.Hex()) + m := fmt.Sprintf("Failed to copy image user: %v", u.ID) common.HandleError(w, http.StatusInternalServerError, m, err) return } imageID, err := image.Add(buf.Bytes()) if err != nil { - m := fmt.Sprintf("Failed to store image in data store user: %v", u.ID.Hex()) + m := fmt.Sprintf("Failed to store image in data store user: %v", u.ID) common.HandleError(w, http.StatusInternalServerError, m, err) return } @@ -69,7 +69,7 @@ func OnboardUpdateOrganisationAdminAvatar(w http.ResponseWriter, r *http.Request imageURL := "https://" + r.Host + "/onboard/admin/avatarimage" u.ImageID = imageID u.ImageURL = imageURL - u, err = user.Update(u.ID.Hex(), u) + u, err = user.Update(u.ID, u) if err != nil { m := fmt.Sprintf("Failed to update user: %v with image: %v details", u.ID, imageID) common.HandleError(w, http.StatusInternalServerError, m, err) @@ -77,7 +77,7 @@ func OnboardUpdateOrganisationAdminAvatar(w http.ResponseWriter, r *http.Request } resp := updateOrgAdminImageResp{ - Id: u.ID.Hex(), + Id: u.ID, Email: u.Email, Name: u.Name, AvatarImageId: u.ImageID, diff --git a/internal/handler/v2/onboard/onboard_update_organisation_admin.go b/internal/handler/v2/onboard/onboard_update_organisation_admin.go index 21f40c0..c3980d9 100644 --- a/internal/handler/v2/onboard/onboard_update_organisation_admin.go +++ b/internal/handler/v2/onboard/onboard_update_organisation_admin.go @@ -78,15 +78,15 @@ func OnboardUpdateOrganisationAdmin(w http.ResponseWriter, r *http.Request) { } - u, err = user.Update(u.ID.Hex(), u) + u, err = user.Update(u.ID, u) if err != nil { - m := fmt.Sprintf("Failed to update user by id:%v", u.ID.Hex()) + m := fmt.Sprintf("Failed to update user by id:%v", u.ID) common.HandleError(w, http.StatusInternalServerError, m, err) return } orgAdmin := orgAdmin{ - Id: u.ID.Hex(), + Id: u.ID, Email: u.Email, Name: u.Name, AvatarImageId: u.ImageID, diff --git a/internal/handler/v2/onboard/updateorganizationcoverimage_handler.go b/internal/handler/v2/onboard/updateorganizationcoverimage_handler.go index 479f570..b0109cd 100644 --- a/internal/handler/v2/onboard/updateorganizationcoverimage_handler.go +++ b/internal/handler/v2/onboard/updateorganizationcoverimage_handler.go @@ -14,7 +14,7 @@ import ( // UpdateOrganizationCoverImage Inserts the image and update the id to user func UpdateOrganizationCoverImage(w http.ResponseWriter, r *http.Request) { - organizationID := r.Header.Get(config.OrganizationId) + organizationID := common.Sanitize(r.Header.Get(config.OrganizationId)) file, _, err := r.FormFile("orgimage") if err != nil { diff --git a/internal/handler/v2/onboard/updateorganizationlogoimage_handler.go b/internal/handler/v2/onboard/updateorganizationlogoimage_handler.go index 13f6a64..580a631 100644 --- a/internal/handler/v2/onboard/updateorganizationlogoimage_handler.go +++ b/internal/handler/v2/onboard/updateorganizationlogoimage_handler.go @@ -14,7 +14,7 @@ import ( // UpdateOrganizationLogoImage Inserts the image and update the id to user func UpdateOrganizationLogoImage(w http.ResponseWriter, r *http.Request) { - organizationID := r.Header.Get(config.OrganizationId) + organizationID := common.Sanitize(r.Header.Get(config.OrganizationId)) file, _, err := r.FormFile("orgimage") if err != nil { diff --git a/internal/handler/v2/onboard/validatephonenumber_handler.go b/internal/handler/v2/onboard/validatephonenumber_handler.go index 8cd42d5..dd49c8c 100644 --- a/internal/handler/v2/onboard/validatephonenumber_handler.go +++ b/internal/handler/v2/onboard/validatephonenumber_handler.go @@ -67,8 +67,15 @@ func ValidatePhoneNumber(w http.ResponseWriter, r *http.Request) { } if o != (otp.Otp{}) { - if primitive.NewObjectID().Timestamp().Sub(o.ID.Timestamp()) > 2*time.Minute { - err = otp.Delete(o.ID.Hex()) + otpId, err := primitive.ObjectIDFromHex(o.ID) + if err != nil { + m := "Failed to convert otp id to objectId" + common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + return + } + + if primitive.NewObjectID().Timestamp().Sub(otpId.Timestamp()) > 2*time.Minute { + err = otp.Delete(o.ID) if err != nil { m := "Failed to clear expired otp" common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/onboard/verifyphonenumber_handler.go b/internal/handler/v2/onboard/verifyphonenumber_handler.go index e664296..901b295 100644 --- a/internal/handler/v2/onboard/verifyphonenumber_handler.go +++ b/internal/handler/v2/onboard/verifyphonenumber_handler.go @@ -123,7 +123,7 @@ func verifyPhoneNumber(w http.ResponseWriter, r *http.Request, clientType int) { common.HandleErrorV2(w, http.StatusInternalServerError, m, err) return } - var o otp.OtpV2 + var o otp.Otp o.Phone = verifyReq.Phone o.Otp = vCode @@ -131,10 +131,10 @@ func verifyPhoneNumber(w http.ResponseWriter, r *http.Request, clientType int) { oldOtp, err := otp.SearchPhone(sanitizedPhoneNumber) if err == nil { - otp.Delete(oldOtp.ID.Hex()) + otp.Delete(oldOtp.ID) } - o, err = otp.AddV2(o) + o, err = otp.Add(o) if err != nil { m := fmt.Sprintf("Failed to store otp details") common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/individual/service_create_individual.go b/internal/handler/v2/service/individual/service_create_individual.go index 9dab278..45397fd 100644 --- a/internal/handler/v2/service/individual/service_create_individual.go +++ b/internal/handler/v2/service/individual/service_create_individual.go @@ -28,19 +28,8 @@ func updateIndividualFromAddRequestBody(requestBody addServiceIndividualReq) ind return newIndividual } -type individualReq struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` - ExternalId string `json:"externalId"` - ExternalIdType string `json:"externalIdType"` - IdentityProviderId string `json:"identityProviderId"` - Name string `json:"name"` - IamId string `json:"iamId"` - Email string `json:"email"` - Phone string `json:"phone"` -} - type addServiceIndividualReq struct { - Individual individualReq `json:"individual"` + Individual individual.Individual `json:"individual"` } type addServiceIndividualResp struct { @@ -67,7 +56,7 @@ func ServiceCreateIndividual(w http.ResponseWriter, r *http.Request) { } newIndividual := updateIndividualFromAddRequestBody(individualReq) - newIndividual.Id = primitive.NewObjectID() + newIndividual.Id = primitive.NewObjectID().Hex() newIndividual.IsDeleted = false newIndividual.IsOnboardedFromIdp = false newIndividual.OrganisationId = organisationId diff --git a/internal/handler/v2/service/service_create_blank_signature_object.go b/internal/handler/v2/service/service_create_blank_signature_object.go index 153048d..2370285 100644 --- a/internal/handler/v2/service/service_create_blank_signature_object.go +++ b/internal/handler/v2/service/service_create_blank_signature_object.go @@ -36,7 +36,7 @@ func ServiceCreateBlankSignature(w http.ResponseWriter, r *http.Request) { return } // create signature for data agreement record - toBeCreatedSignature, err := signature.CreateSignatureForObject("revision", daRecordRevision.Id.Hex(), false, daRecordRevision, false, signature.Signature{}) + toBeCreatedSignature, err := signature.CreateSignatureForObject("revision", daRecordRevision.Id, false, daRecordRevision, false, signature.Signature{}) if err != nil { m := fmt.Sprintf("Failed to create signature for data agreement record: %v", dataAgreementRecordId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_create_dataagreement_record.go b/internal/handler/v2/service/service_create_dataagreement_record.go index d671447..3ea364c 100644 --- a/internal/handler/v2/service/service_create_dataagreement_record.go +++ b/internal/handler/v2/service/service_create_dataagreement_record.go @@ -20,10 +20,10 @@ import ( func createDataAgreementRecord(dataAgreementId string, rev revision.Revision, individualId string) daRecord.DataAgreementRecord { var newDaRecord daRecord.DataAgreementRecord - newDaRecord.Id = primitive.NewObjectID() + newDaRecord.Id = primitive.NewObjectID().Hex() newDaRecord.DataAgreementId = dataAgreementId newDaRecord.DataAgreementRevisionHash = rev.SerializedHash - newDaRecord.DataAgreementRevisionId = rev.Id.Hex() + newDaRecord.DataAgreementRevisionId = rev.Id newDaRecord.IndividualId = individualId newDaRecord.OptIn = true newDaRecord.State = config.Unsigned @@ -118,7 +118,7 @@ func ServiceCreateDataAgreementRecord(w http.ResponseWriter, r *http.Request) { darH := daRecordHistory.DataAgreementRecordsHistory{} darH.DataAgreementId = dataAgreementId darH.OrganisationId = organisationId - darH.ConsentRecordId = savedDaRecord.Id.Hex() + darH.ConsentRecordId = savedDaRecord.Id darH.IndividualId = individualId err = daRecordHistory.DataAgreementRecordHistoryAdd(darH, savedDaRecord.OptIn) if err != nil { diff --git a/internal/handler/v2/service/service_create_draft_consent_record.go b/internal/handler/v2/service/service_create_draft_consent_record.go index 519aacc..fa6f8ff 100644 --- a/internal/handler/v2/service/service_create_draft_consent_record.go +++ b/internal/handler/v2/service/service_create_draft_consent_record.go @@ -20,7 +20,7 @@ func createDraftDataAgreementRecord(dataAgreementId string, rev revision.Revisio newDaRecord.DataAgreementId = dataAgreementId newDaRecord.DataAgreementRevisionHash = rev.SerializedHash - newDaRecord.DataAgreementRevisionId = rev.Id.Hex() + newDaRecord.DataAgreementRevisionId = rev.Id newDaRecord.IndividualId = individualId newDaRecord.OptIn = true newDaRecord.State = config.Unsigned diff --git a/internal/handler/v2/service/service_create_paired_dataagreement_record.go b/internal/handler/v2/service/service_create_paired_dataagreement_record.go index 0c1a8b5..a33a669 100644 --- a/internal/handler/v2/service/service_create_paired_dataagreement_record.go +++ b/internal/handler/v2/service/service_create_paired_dataagreement_record.go @@ -25,7 +25,7 @@ func createPairedDataAgreementRecord(dataAgreementId string, rev revision.Revisi newDaRecord.DataAgreementId = dataAgreementId newDaRecord.DataAgreementRevisionHash = rev.SerializedHash - newDaRecord.DataAgreementRevisionId = rev.Id.Hex() + newDaRecord.DataAgreementRevisionId = rev.Id newDaRecord.IndividualId = individualId newDaRecord.OptIn = true newDaRecord.State = config.Unsigned @@ -34,14 +34,14 @@ func createPairedDataAgreementRecord(dataAgreementId string, rev revision.Revisi } type dataAgreementRecordReq struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` - DataAgreementId string `json:"dataAgreementId" valid:"required"` - DataAgreementRevisionId string `json:"dataAgreementRevisionId" valid:"required"` - DataAgreementRevisionHash string `json:"dataAgreementRevisionHash"` - IndividualId string `json:"individualId" valid:"required"` - OptIn bool `json:"optIn"` - State string `json:"state"` - SignatureId string `json:"signatureId"` + Id string `json:"id" bson:"_id,omitempty"` + DataAgreementId string `json:"dataAgreementId" valid:"required"` + DataAgreementRevisionId string `json:"dataAgreementRevisionId" valid:"required"` + DataAgreementRevisionHash string `json:"dataAgreementRevisionHash"` + IndividualId string `json:"individualId" valid:"required"` + OptIn bool `json:"optIn"` + State string `json:"state"` + SignatureId string `json:"signatureId"` } type createPairedDataAgreementRecordReq struct { @@ -90,14 +90,14 @@ func ServiceCreatePairedDataAgreementRecord(w http.ResponseWriter, r *http.Reque darRepo.Init(organisationId) // Check for existing data agreement record with same data agreement id and individual id - count, err := darRepo.CountDataAgreementRecords(dataAgreementRecordReq.DataAgreementRecord.DataAgreementId, individual.Id.Hex()) + count, err := darRepo.CountDataAgreementRecords(dataAgreementRecordReq.DataAgreementRecord.DataAgreementId, individual.Id) if err != nil { m := fmt.Sprintf("Failed to fetch data agreement record for data agreement: %v", dataAgreementRecordReq.DataAgreementRecord.DataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) return } if count > 0 { - m := fmt.Sprintf("Data agreement record for data agreement: %v and individual id : %s exists", dataAgreementRecordReq.DataAgreementRecord.DataAgreementId, individual.Id.Hex()) + m := fmt.Sprintf("Data agreement record for data agreement: %v and individual id : %s exists", dataAgreementRecordReq.DataAgreementRecord.DataAgreementId, individual.Id) common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -114,32 +114,32 @@ func ServiceCreatePairedDataAgreementRecord(w http.ResponseWriter, r *http.Reque } // fetch revision based on id and schema name - dataAgreementRevision, err := revision.GetByRevisionIdAndSchema(dataAgreementRecordReq.DataAgreementRecord.DataAgreementRevisionId, config.DataAgreement) + dataAgreementRevision, err := revision.GetByRevisionIdAndSchema(common.Sanitize(dataAgreementRecordReq.DataAgreementRecord.DataAgreementRevisionId), config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementRecordReq.DataAgreementRecord.DataAgreementRevisionId) common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } - newDataAgreementRecord := createPairedDataAgreementRecord(dataAgreement.Id.Hex(), dataAgreementRevision, individual.Id.Hex()) + newDataAgreementRecord := createPairedDataAgreementRecord(dataAgreement.Id, dataAgreementRevision, individual.Id) dataAgreementRecord := newDataAgreementRecord dataAgreementRecord.OrganisationId = organisationId currentSignature := dataAgreementRecordReq.Signature - dataAgreementRecord.Id = primitive.NewObjectID() - currentSignature.Id = primitive.NewObjectID() - dataAgreementRecord.SignatureId = currentSignature.Id.Hex() + dataAgreementRecord.Id = primitive.NewObjectID().Hex() + currentSignature.Id = primitive.NewObjectID().Hex() + dataAgreementRecord.SignatureId = currentSignature.Id newRecordRevision, err := revision.CreateRevisionForDataAgreementRecord(dataAgreementRecord, individualId) if err != nil { - m := fmt.Sprintf("Failed to create new revision for dataAgreementRecord: %v", dataAgreementRecord.Id.Hex()) + m := fmt.Sprintf("Failed to create new revision for dataAgreementRecord: %v", dataAgreementRecord.Id) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) return } // create signature for data agreement record - toBeCreatedSignature, err := signature.CreateSignatureForObject("revision", newRecordRevision.Id.Hex(), false, newRecordRevision, true, currentSignature) + toBeCreatedSignature, err := signature.CreateSignatureForObject("revision", newRecordRevision.Id, false, newRecordRevision, true, currentSignature) if err != nil { - m := fmt.Sprintf("Failed to create signature for data agreement record: %v", dataAgreementRecord.Id.Hex()) + m := fmt.Sprintf("Failed to create signature for data agreement record: %v", dataAgreementRecord.Id) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) return } @@ -180,8 +180,8 @@ func ServiceCreatePairedDataAgreementRecord(w http.ResponseWriter, r *http.Reque darH := daRecordHistory.DataAgreementRecordsHistory{} darH.DataAgreementId = dataAgreementRecord.DataAgreementId darH.OrganisationId = organisationId - darH.ConsentRecordId = savedDataAgreementRecord.Id.Hex() - darH.IndividualId = individual.Id.Hex() + darH.ConsentRecordId = savedDataAgreementRecord.Id + darH.IndividualId = individual.Id err = daRecordHistory.DataAgreementRecordHistoryAdd(darH, savedDataAgreementRecord.OptIn) if err != nil { m := "Failed to add data agreement record history" diff --git a/internal/handler/v2/service/service_list_dataattributes_for_dataagreement.go b/internal/handler/v2/service/service_list_dataattributes_for_dataagreement.go index 04eb5c7..b06cae9 100644 --- a/internal/handler/v2/service/service_list_dataattributes_for_dataagreement.go +++ b/internal/handler/v2/service/service_list_dataattributes_for_dataagreement.go @@ -52,7 +52,7 @@ func ServiceListDataAttributesForDataAgreement(w http.ResponseWriter, r *http.Re var daRevision revision.Revision if err != nil && errors.Is(err, RevisionIDIsMissingError) { - daRevision, err = revision.GetLatestByDataAgreementId(da.Id.Hex()) + daRevision, err = revision.GetLatestByDataAgreementId(da.Id) if err != nil { m := fmt.Sprintf("Failed to fetch data agreement revision: %v", dataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_read_dataagreement.go b/internal/handler/v2/service/service_read_dataagreement.go index 1a0b3ba..179da5a 100644 --- a/internal/handler/v2/service/service_read_dataagreement.go +++ b/internal/handler/v2/service/service_read_dataagreement.go @@ -37,7 +37,7 @@ func ServiceReadDataAgreement(w http.ResponseWriter, r *http.Request) { } var revisionResp revision.Revision - revisionResp, err = revision.GetLatestByDataAgreementId(da.Id.Hex()) + revisionResp, err = revision.GetLatestByDataAgreementId(da.Id) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_read_idp.go b/internal/handler/v2/service/service_read_idp.go index 0729b53..b648077 100644 --- a/internal/handler/v2/service/service_read_idp.go +++ b/internal/handler/v2/service/service_read_idp.go @@ -44,7 +44,7 @@ func ServiceReadIdp(w http.ResponseWriter, r *http.Request) { return } idpResp := serviceIdp{ - Id: idp.Id.Hex(), + Id: idp.Id, IssuerUrl: idp.IssuerUrl, AuthorizationURL: idp.AuthorizationURL, TokenURL: idp.TokenURL, diff --git a/internal/handler/v2/service/service_read_organisation.go b/internal/handler/v2/service/service_read_organisation.go index 976e58c..efa7c4e 100644 --- a/internal/handler/v2/service/service_read_organisation.go +++ b/internal/handler/v2/service/service_read_organisation.go @@ -8,20 +8,19 @@ import ( "github.com/bb-consent/api/internal/common" "github.com/bb-consent/api/internal/config" "github.com/bb-consent/api/internal/org" - "go.mongodb.org/mongo-driver/bson/primitive" ) type organizationResp struct { - ID primitive.ObjectID `bson:"_id,omitempty" json:"id"` - Name string `json:"name"` - Description string `json:"description"` - Sector string `json:"sector"` - Location string `json:"location"` - PolicyURL string `json:"policyUrl"` - CoverImageId string `json:"coverImageId"` - CoverImageUrl string `json:"coverImageUrl"` - LogoImageId string `json:"logoImageId"` - LogoImageUrl string `json:"logoImageUrl"` + ID string `bson:"_id,omitempty" json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Sector string `json:"sector"` + Location string `json:"location"` + PolicyURL string `json:"policyUrl"` + CoverImageId string `json:"coverImageId"` + CoverImageUrl string `json:"coverImageUrl"` + LogoImageId string `json:"logoImageId"` + LogoImageUrl string `json:"logoImageUrl"` } type getOrgResp struct { @@ -30,7 +29,7 @@ type getOrgResp struct { // ServiceReadOrganisation func ServiceReadOrganisation(w http.ResponseWriter, r *http.Request) { - organizationID := r.Header.Get(config.OrganizationId) + organizationID := common.Sanitize(r.Header.Get(config.OrganizationId)) o, err := org.Get(organizationID) if err != nil { m := fmt.Sprintf("Failed to get organization by ID :%v", organizationID) diff --git a/internal/handler/v2/service/service_read_organisation_image.go b/internal/handler/v2/service/service_read_organisation_image.go index 8dc69bc..f61a183 100644 --- a/internal/handler/v2/service/service_read_organisation_image.go +++ b/internal/handler/v2/service/service_read_organisation_image.go @@ -14,7 +14,7 @@ import ( func ServiceReadOrganisationImage(w http.ResponseWriter, r *http.Request) { organizationID := r.Header.Get(config.OrganizationId) // Path params - imageId := mux.Vars(r)["imageId"] + imageId := common.Sanitize(mux.Vars(r)["imageId"]) image, err := image.Get(imageId) if err != nil { diff --git a/internal/handler/v2/service/service_read_organisation_logo_image.go b/internal/handler/v2/service/service_read_organisation_logo_image.go index aadb1a1..533f01c 100644 --- a/internal/handler/v2/service/service_read_organisation_logo_image.go +++ b/internal/handler/v2/service/service_read_organisation_logo_image.go @@ -12,7 +12,7 @@ import ( // ServiceReadOrganisationLogoImage Retrieves the organization logo image func ServiceReadOrganisationLogoImage(w http.ResponseWriter, r *http.Request) { - organizationID := r.Header.Get(config.OrganizationId) + organizationID := common.Sanitize(r.Header.Get(config.OrganizationId)) o, err := org.Get(organizationID) if err != nil { m := fmt.Sprintf("Failed to get organization by ID :%v", organizationID) diff --git a/internal/handler/v2/service/service_update_dataagreement_record.go b/internal/handler/v2/service/service_update_dataagreement_record.go index 76b4420..faa0415 100644 --- a/internal/handler/v2/service/service_update_dataagreement_record.go +++ b/internal/handler/v2/service/service_update_dataagreement_record.go @@ -151,7 +151,7 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) { darH := daRecordHistory.DataAgreementRecordsHistory{} darH.DataAgreementId = savedDaRecord.DataAgreementId darH.OrganisationId = organisationId - darH.ConsentRecordId = savedDaRecord.Id.Hex() + darH.ConsentRecordId = savedDaRecord.Id darH.IndividualId = individualId err = daRecordHistory.DataAgreementRecordHistoryAdd(darH, savedDaRecord.OptIn) if err != nil { diff --git a/internal/handler/v2/service/service_verification_fetch_dataagreement_record.go b/internal/handler/v2/service/service_verification_fetch_dataagreement_record.go index 13550dd..91fabbc 100644 --- a/internal/handler/v2/service/service_verification_fetch_dataagreement_record.go +++ b/internal/handler/v2/service/service_verification_fetch_dataagreement_record.go @@ -34,7 +34,7 @@ func ServiceVerificationFetchDataAgreementRecord(w http.ResponseWriter, r *http. return } - currentRevision, err := revision.GetLatestByObjectId(daRecord.Id.Hex()) + currentRevision, err := revision.GetLatestByObjectId(daRecord.Id) if err != nil { m := "Failed to fetch revision for data agreement record" common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/idp/idp.go b/internal/idp/idp.go index 2d00134..381d741 100644 --- a/internal/idp/idp.go +++ b/internal/idp/idp.go @@ -6,7 +6,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" ) @@ -15,18 +14,18 @@ func Collection() *mongo.Collection { } type IdentityProvider struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` - IssuerUrl string `json:"issuerUrl"` - AuthorizationURL string `json:"authorisationUrl" valid:"required"` - TokenURL string `json:"tokenUrl" valid:"required"` - LogoutURL string `json:"logoutUrl" valid:"required"` - ClientID string `json:"clientId" valid:"required"` - ClientSecret string `json:"clientSecret" valid:"required"` - JWKSURL string `json:"jwksUrl" valid:"required"` - UserInfoURL string `json:"userInfoUrl" valid:"required"` - DefaultScope string `json:"defaultScope" valid:"required"` - OrganisationId string `json:"-"` - IsDeleted bool `json:"-"` + Id string `json:"id" bson:"_id,omitempty"` + IssuerUrl string `json:"issuerUrl"` + AuthorizationURL string `json:"authorisationUrl" valid:"required"` + TokenURL string `json:"tokenUrl" valid:"required"` + LogoutURL string `json:"logoutUrl" valid:"required"` + ClientID string `json:"clientId" valid:"required"` + ClientSecret string `json:"clientSecret" valid:"required"` + JWKSURL string `json:"jwksUrl" valid:"required"` + UserInfoURL string `json:"userInfoUrl" valid:"required"` + DefaultScope string `json:"defaultScope" valid:"required"` + OrganisationId string `json:"-"` + IsDeleted bool `json:"-"` } type IdentityProviderRepository struct { @@ -75,16 +74,12 @@ func (idpRepo *IdentityProviderRepository) Update(idp IdentityProvider) (Identit } // Get Gets a single identity provider by given id -func (idpRepo *IdentityProviderRepository) Get(idpID string) (IdentityProvider, error) { +func (idpRepo *IdentityProviderRepository) Get(idpId string) (IdentityProvider, error) { var result IdentityProvider - idpId, err := primitive.ObjectIDFromHex(idpID) - if err != nil { - return result, err - } filter := common.CombineFilters(idpRepo.DefaultFilter, bson.M{"_id": idpId}) - err = Collection().FindOne(context.TODO(), filter).Decode(&result) + err := Collection().FindOne(context.TODO(), filter).Decode(&result) return result, err } diff --git a/internal/image/images.go b/internal/image/images.go index 2cc2f12..d5604bf 100644 --- a/internal/image/images.go +++ b/internal/image/images.go @@ -11,35 +11,30 @@ import ( // Image data type type Image struct { - ID primitive.ObjectID `bson:"_id,omitempty"` + ID string `bson:"_id,omitempty"` Data []byte } -func collection() *mongo.Collection { +func Collection() *mongo.Collection { return database.DB.Client.Database(database.DB.Name).Collection("images") } // Add Adds an image to image store func Add(image []byte) (imageID string, err error) { - i := Image{primitive.NewObjectID(), image} - _, err = collection().InsertOne(context.TODO(), &i) + i := Image{primitive.NewObjectID().Hex(), image} + _, err = Collection().InsertOne(context.TODO(), &i) if err != nil { return "", err } - return i.ID.Hex(), err + return i.ID, err } // Get Fetches the image by ID -func Get(imageID string) (Image, error) { +func Get(imageId string) (Image, error) { var image Image - imageId, err := primitive.ObjectIDFromHex(imageID) - if err != nil { - return image, err - } - - err = collection().FindOne(context.TODO(), bson.M{"_id": imageId}).Decode(&image) + err := Collection().FindOne(context.TODO(), bson.M{"_id": imageId}).Decode(&image) return image, err } diff --git a/internal/individual/individuals.go b/internal/individual/individuals.go index 4f46374..1d3f7e1 100644 --- a/internal/individual/individuals.go +++ b/internal/individual/individuals.go @@ -6,7 +6,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" ) @@ -15,17 +14,17 @@ func Collection() *mongo.Collection { } type Individual struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` - ExternalId string `json:"externalId"` - ExternalIdType string `json:"externalIdType"` - IdentityProviderId string `json:"identityProviderId"` - Name string `json:"name"` - IamId string `json:"iamId"` - Email string `json:"email"` - Phone string `json:"phone"` - IsOnboardedFromIdp bool `json:"-"` - OrganisationId string `json:"-"` - IsDeleted bool `json:"-"` + Id string `json:"id" bson:"_id,omitempty"` + ExternalId string `json:"externalId"` + ExternalIdType string `json:"externalIdType"` + IdentityProviderId string `json:"identityProviderId"` + Name string `json:"name"` + IamId string `json:"iamId"` + Email string `json:"email"` + Phone string `json:"phone"` + IsOnboardedFromIdp bool `json:"-"` + OrganisationId string `json:"-"` + IsDeleted bool `json:"-"` } type IndividualRepository struct { @@ -49,16 +48,12 @@ func (iRepo *IndividualRepository) Add(individual Individual) (Individual, error } // Get Gets a single individual by given id -func (iRepo *IndividualRepository) Get(individualID string) (Individual, error) { +func (iRepo *IndividualRepository) Get(individualId string) (Individual, error) { var result Individual - individualId, err := primitive.ObjectIDFromHex(individualID) - if err != nil { - return result, err - } filter := common.CombineFilters(iRepo.DefaultFilter, bson.M{"_id": individualId}) - err = Collection().FindOne(context.TODO(), filter).Decode(&result) + err := Collection().FindOne(context.TODO(), filter).Decode(&result) return result, err } diff --git a/internal/middleware/application_mode.go b/internal/middleware/application_mode.go index 8c263a5..0abf76f 100644 --- a/internal/middleware/application_mode.go +++ b/internal/middleware/application_mode.go @@ -54,7 +54,7 @@ func singleTenantConfig(r *http.Request) error { if err != nil { return err } - organizationId := organization.ID.Hex() + organizationId := organization.ID r.Header.Set(config.OrganizationId, organizationId) return nil } diff --git a/internal/middleware/authenticate.go b/internal/middleware/authenticate.go index 724ffb6..423ac4b 100644 --- a/internal/middleware/authenticate.go +++ b/internal/middleware/authenticate.go @@ -22,7 +22,7 @@ func createIndividual(externalId string, r *http.Request, organisationId string, individualRepo := individual.IndividualRepository{} individualRepo.Init(organisationId) - newIndividual.Id = primitive.NewObjectID() + newIndividual.Id = primitive.NewObjectID().Hex() newIndividual.Email = token.GetUserName(r) newIndividual.ExternalId = externalId newIndividual.Name = token.GetName(r) @@ -74,7 +74,7 @@ func verifyTokenAndIdentifyRole(accessToken string, r *http.Request) error { // Repository individualRepo := individual.IndividualRepository{} - individualRepo.Init(organization.ID.Hex()) + individualRepo.Init(organization.ID) if err != nil { // Individual doesn't belong to Consent BB IDP @@ -83,7 +83,7 @@ func verifyTokenAndIdentifyRole(accessToken string, r *http.Request) error { // Repository idpRepo := idp.IdentityProviderRepository{} - idpRepo.Init(organization.ID.Hex()) + idpRepo.Init(organization.ID) // Fetch IDP for the org idp, err := idpRepo.GetByOrgId() @@ -110,7 +110,7 @@ func verifyTokenAndIdentifyRole(accessToken string, r *http.Request) error { individual, err = individualRepo.GetByExternalId(externalId) if err != nil { log.Println("Creating individual") - individual, err = createIndividual(externalId, r, organization.ID.Hex(), idp.Id.Hex()) + individual, err = createIndividual(externalId, r, organization.ID, idp.Id) if err != nil { m := "User does not exist, Authorization failed" error_handler.Exit(http.StatusBadRequest, m) @@ -118,7 +118,7 @@ func verifyTokenAndIdentifyRole(accessToken string, r *http.Request) error { } // Set user Id and user roles to request context - token.SetUserToRequestContext(r, individual.Id.Hex(), rbac.ROLE_USER) + token.SetUserToRequestContext(r, individual.Id, rbac.ROLE_USER) return nil @@ -138,14 +138,14 @@ func verifyTokenAndIdentifyRole(accessToken string, r *http.Request) error { } // Set user Id and user roles to request context - token.SetUserToRequestContext(r, individual.Id.Hex(), rbac.ROLE_USER) + token.SetUserToRequestContext(r, individual.Id, rbac.ROLE_USER) } // Set user Id and user roles to request context if len(user.Roles) > 0 { - token.SetUserToRequestContext(r, user.ID.Hex(), rbac.ROLE_ADMIN) + token.SetUserToRequestContext(r, user.ID, rbac.ROLE_ADMIN) } else { - token.SetUserToRequestContext(r, user.ID.Hex(), rbac.ROLE_USER) + token.SetUserToRequestContext(r, user.ID, rbac.ROLE_USER) } return nil diff --git a/internal/migrate/migrate.go b/internal/migrate/migrate.go index 8852f47..2c81065 100644 --- a/internal/migrate/migrate.go +++ b/internal/migrate/migrate.go @@ -5,13 +5,22 @@ import ( "fmt" "time" + "github.com/bb-consent/api/internal/actionlog" "github.com/bb-consent/api/internal/apikey" "github.com/bb-consent/api/internal/dataagreement" + dataagreementrecord "github.com/bb-consent/api/internal/dataagreement_record" dataagreementrecordhistory "github.com/bb-consent/api/internal/dataagreement_record_history" "github.com/bb-consent/api/internal/idp" + "github.com/bb-consent/api/internal/image" "github.com/bb-consent/api/internal/individual" "github.com/bb-consent/api/internal/org" + "github.com/bb-consent/api/internal/orgtype" + "github.com/bb-consent/api/internal/otp" "github.com/bb-consent/api/internal/policy" + "github.com/bb-consent/api/internal/revision" + "github.com/bb-consent/api/internal/signature" + "github.com/bb-consent/api/internal/user" + "github.com/bb-consent/api/internal/webhook" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -27,6 +36,19 @@ func Migrate() { migrateUnusedFieldsFromOrganistaionColloction() migrateIsOnboardedFromIDPInindividualCollection() migrateConsentRecordIdAndIndividualIdInConsentHistoryCollection() + migrateIdToStringInApiKeyCollection() + migrateIdToStringInActionLogsCollection() + migrateIdToStringInIdentityProvidersCollection() + migrateIdToStringInImagesCollection() + migrateIdToStringInIndividualsCollection() + migrateIdToStringInWebhooksCollection() + migrateIdToStringInWebhookDeliveriesCollection() + migrateIdToStringInOtpsCollection() + migrateIdToStringInConsentRecordHistoriesCollection() + migrateIdToStringInConsentRecordsCollection() + migrateIdToStringInDataAgreementsCollection() + migrateIdToStringInSignaturesCollection() + migrateIdToStringInRevisionsCollection() } func migrateThirdPartyDataSharingToTrueInPolicyCollection() { @@ -79,7 +101,7 @@ func migrateTimestampInDataAgreementsCollection() { panic(err) } - pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreements(o.ID.Hex(), true) + pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreements(o.ID, true) if err != nil { fmt.Println(err) } @@ -132,7 +154,7 @@ func migrateOrganisationIdInIDPCollection() { if err != nil { fmt.Println(err) } - organizationId := organization.ID.Hex() + organizationId := organization.ID idpCollection := idp.Collection() @@ -231,3 +253,768 @@ func migrateConsentRecordIdAndIndividualIdInConsentHistoryCollection() { fmt.Println(err) } } + +func migrateIdToStringInApiKeyCollection() { + apiKeyCollection := apikey.Collection() + + var results []apikey.ApiKey + + cursor, err := apiKeyCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, apiKey := range results { + + apiKeyId, err := primitive.ObjectIDFromHex(apiKey.Id) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": apiKeyId} + + exists, err := apiKeyCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = apiKeyCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := apiKeyCollection.InsertOne(context.TODO(), apiKey) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInActionLogsCollection() { + actionLogCollection := actionlog.Collection() + + var results []actionlog.ActionLog + + cursor, err := actionLogCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, actionLog := range results { + + actionLogId, err := primitive.ObjectIDFromHex(actionLog.ID) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": actionLogId} + + exists, err := actionLogCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = actionLogCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := actionLogCollection.InsertOne(context.TODO(), actionLog) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInIdentityProvidersCollection() { + idpCollection := idp.Collection() + + var results []idp.IdentityProvider + + cursor, err := idpCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, idp := range results { + + idpId, err := primitive.ObjectIDFromHex(idp.Id) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": idpId} + + exists, err := idpCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = idpCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := idpCollection.InsertOne(context.TODO(), idp) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInImagesCollection() { + imageCollection := image.Collection() + + var results []image.Image + + cursor, err := imageCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, image := range results { + + imageId, err := primitive.ObjectIDFromHex(image.ID) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": imageId} + + exists, err := imageCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = imageCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := imageCollection.InsertOne(context.TODO(), image) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInIndividualsCollection() { + individualCollection := individual.Collection() + + var results []individual.Individual + + cursor, err := individualCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, individual := range results { + + individualId, err := primitive.ObjectIDFromHex(individual.Id) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": individualId} + + exists, err := individualCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = individualCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := individualCollection.InsertOne(context.TODO(), individual) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInWebhooksCollection() { + webhookCollection := webhook.WebhookCollection() + + var results []webhook.Webhook + + cursor, err := webhookCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, webhook := range results { + + webhookId, err := primitive.ObjectIDFromHex(webhook.ID) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": webhookId} + + exists, err := webhookCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = webhookCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := webhookCollection.InsertOne(context.TODO(), webhook) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInWebhookDeliveriesCollection() { + webhookDeliveryCollection := webhook.WebhookDeliveryCollection() + + var results []webhook.WebhookDelivery + + cursor, err := webhookDeliveryCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, webhookDelivery := range results { + + webhookDeliveryId, err := primitive.ObjectIDFromHex(webhookDelivery.ID) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": webhookDeliveryId} + + exists, err := webhookDeliveryCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = webhookDeliveryCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := webhookDeliveryCollection.InsertOne(context.TODO(), webhookDelivery) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInOtpsCollection() { + otpCollection := otp.Collection() + + var results []otp.Otp + + cursor, err := otpCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, otp := range results { + + otpId, err := primitive.ObjectIDFromHex(otp.ID) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": otpId} + + exists, err := otpCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = otpCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := otpCollection.InsertOne(context.TODO(), otp) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func MigrateIdToStringInPoliciesCollection() { + policyCollection := policy.Collection() + + var results []policy.Policy + + cursor, err := policyCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, policy := range results { + + policyId, err := primitive.ObjectIDFromHex(policy.Id) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": policyId} + + exists, err := policyCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = policyCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := policyCollection.InsertOne(context.TODO(), policy) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInConsentRecordHistoriesCollection() { + consentRecordHistoriesCollection := dataagreementrecordhistory.Collection() + + var results []dataagreementrecordhistory.DataAgreementRecordsHistory + + cursor, err := consentRecordHistoriesCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, consentRecordHistory := range results { + + consentRecordHistoryId, err := primitive.ObjectIDFromHex(consentRecordHistory.Id) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": consentRecordHistoryId} + + exists, err := consentRecordHistoriesCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = consentRecordHistoriesCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := consentRecordHistoriesCollection.InsertOne(context.TODO(), consentRecordHistory) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInConsentRecordsCollection() { + consentRecordCollection := dataagreementrecord.Collection() + + var results []dataagreementrecord.DataAgreementRecord + + cursor, err := consentRecordCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, consentRecord := range results { + + consentRecordId, err := primitive.ObjectIDFromHex(consentRecord.Id) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": consentRecordId} + + exists, err := consentRecordCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = consentRecordCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := consentRecordCollection.InsertOne(context.TODO(), consentRecord) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInDataAgreementsCollection() { + dataAgreementCollection := dataagreement.Collection() + + var results []dataagreement.DataAgreement + + cursor, err := dataAgreementCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, dataAgreement := range results { + + dataAgreementId, err := primitive.ObjectIDFromHex(dataAgreement.Id) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": dataAgreementId} + + exists, err := dataAgreementCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = dataAgreementCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := dataAgreementCollection.InsertOne(context.TODO(), dataAgreement) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInSignaturesCollection() { + signatureCollection := signature.Collection() + + var results []signature.Signature + + cursor, err := signatureCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, signature := range results { + + signatureId, err := primitive.ObjectIDFromHex(signature.Id) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": signatureId} + + exists, err := signatureCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = signatureCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := signatureCollection.InsertOne(context.TODO(), signature) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func migrateIdToStringInRevisionsCollection() { + revisionCollection := revision.Collection() + + var results []revision.Revision + + cursor, err := revisionCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, revision := range results { + + revisionId, err := primitive.ObjectIDFromHex(revision.Id) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": revisionId} + + exists, err := revisionCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = revisionCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := revisionCollection.InsertOne(context.TODO(), revision) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func MigrateIdToStringInUsersCollection() { + userCollection := user.Collection() + + var results []user.User + + cursor, err := userCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, user := range results { + + userId, err := primitive.ObjectIDFromHex(user.ID) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": userId} + + exists, err := userCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = userCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := userCollection.InsertOne(context.TODO(), user) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func MigrateIdToStringInOrgTypesCollection() { + orgTypeCollection := orgtype.Collection() + + var results []orgtype.OrgType + + cursor, err := orgTypeCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, orgType := range results { + + orgTypeId, err := primitive.ObjectIDFromHex(orgType.ID) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": orgTypeId} + + exists, err := orgTypeCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = orgTypeCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := orgTypeCollection.InsertOne(context.TODO(), orgType) + if err != nil { + fmt.Println(err) + } + } + } + +} + +func MigrateIdToStringInOrganisationCollection() { + orgCollection := org.Collection() + + var results []org.Organization + + cursor, err := orgCollection.Find(context.TODO(), bson.M{}) + if err != nil { + fmt.Println(err) + } + defer cursor.Close(context.TODO()) + + if err := cursor.All(context.TODO(), &results); err != nil { + fmt.Println(err) + } + + for _, org := range results { + + orgId, err := primitive.ObjectIDFromHex(org.ID) + if err != nil { + fmt.Println(err) + } + + filter := bson.M{"_id": orgId} + + exists, err := orgCollection.CountDocuments(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + if exists > 0 { + + _, err = orgCollection.DeleteOne(context.TODO(), filter) + if err != nil { + fmt.Println(err) + } + + _, err := orgCollection.InsertOne(context.TODO(), org) + if err != nil { + fmt.Println(err) + } + } + } + +} diff --git a/internal/org/organizations.go b/internal/org/organizations.go index e8ac6a2..4d8ade6 100644 --- a/internal/org/organizations.go +++ b/internal/org/organizations.go @@ -26,7 +26,7 @@ type Admin struct { // Organization organization data type type Organization struct { - ID primitive.ObjectID `bson:"_id,omitempty"` + ID string `bson:"_id,omitempty"` Name string CoverImageID string CoverImageURL string @@ -55,7 +55,7 @@ func Collection() *mongo.Collection { // Add Adds an organization func Add(org Organization) (Organization, error) { - org.ID = primitive.NewObjectID() + org.ID = primitive.NewObjectID().Hex() _, err := Collection().InsertOne(context.TODO(), &org) if err != nil { return org, err @@ -64,14 +64,10 @@ func Add(org Organization) (Organization, error) { } // Get Gets a single organization by given id -func Get(organizationID string) (Organization, error) { - orgID, err := primitive.ObjectIDFromHex(organizationID) - if err != nil { - return Organization{}, err - } +func Get(organizationId string) (Organization, error) { var result Organization - err = Collection().FindOne(context.TODO(), bson.M{"_id": orgID}).Decode(&result) + err := Collection().FindOne(context.TODO(), bson.M{"_id": organizationId}).Decode(&result) return result, err } @@ -100,12 +96,8 @@ func Update(org Organization) (Organization, error) { // UpdateCoverImage Update the organization image func UpdateCoverImage(organizationID string, imageID string) (Organization, error) { - orgID, err := primitive.ObjectIDFromHex(organizationID) - if err != nil { - return Organization{}, err - } - _, err = Collection().UpdateOne(context.TODO(), bson.M{"_id": orgID}, bson.M{"$set": bson.M{"coverimageid": imageID}}) + _, err := Collection().UpdateOne(context.TODO(), bson.M{"_id": organizationID}, bson.M{"$set": bson.M{"coverimageid": imageID}}) if err != nil { return Organization{}, err } @@ -115,12 +107,8 @@ func UpdateCoverImage(organizationID string, imageID string) (Organization, erro // UpdateLogoImage Update the organization image func UpdateLogoImage(organizationID string, imageID string) (Organization, error) { - orgID, err := primitive.ObjectIDFromHex(organizationID) - if err != nil { - return Organization{}, err - } - _, err = Collection().UpdateOne(context.TODO(), bson.M{"_id": orgID}, bson.M{"$set": bson.M{"logoimageid": imageID}}) + _, err := Collection().UpdateOne(context.TODO(), bson.M{"_id": organizationID}, bson.M{"$set": bson.M{"logoimageid": imageID}}) if err != nil { return Organization{}, err } @@ -130,12 +118,8 @@ func UpdateLogoImage(organizationID string, imageID string) (Organization, error // AddAdminUsers Add admin users to organization func AddAdminUsers(organizationID string, admin Admin) (Organization, error) { - orgID, err := primitive.ObjectIDFromHex(organizationID) - if err != nil { - return Organization{}, err - } - _, err = Collection().UpdateOne(context.TODO(), bson.M{"_id": orgID}, bson.M{"$push": bson.M{"admins": admin}}) + _, err := Collection().UpdateOne(context.TODO(), bson.M{"_id": organizationID}, bson.M{"$push": bson.M{"admins": admin}}) if err != nil { return Organization{}, err } @@ -145,30 +129,22 @@ func AddAdminUsers(organizationID string, admin Admin) (Organization, error) { // GetAdminUsers Get admin users of organization func GetAdminUsers(organizationID string) (Organization, error) { - orgID, err := primitive.ObjectIDFromHex(organizationID) - if err != nil { - return Organization{}, err - } - filter := bson.M{"_id": orgID} + filter := bson.M{"_id": organizationID} projection := bson.M{"admins": 1} findOptions := options.FindOne().SetProjection(projection) var result Organization - err = Collection().FindOne(context.TODO(), filter, findOptions).Decode(&result) + err := Collection().FindOne(context.TODO(), filter, findOptions).Decode(&result) return result, err } // DeleteAdminUsers Delete admin users from organization func DeleteAdminUsers(organizationID string, admin Admin) (Organization, error) { - orgID, err := primitive.ObjectIDFromHex(organizationID) - if err != nil { - return Organization{}, err - } - _, err = Collection().UpdateOne(context.TODO(), bson.M{"_id": orgID}, bson.M{"$pull": bson.M{"admins": admin}}) + _, err := Collection().UpdateOne(context.TODO(), bson.M{"_id": organizationID}, bson.M{"$pull": bson.M{"admins": admin}}) if err != nil { return Organization{}, err } @@ -193,12 +169,8 @@ func UpdateOrganizationsOrgType(oType orgtype.OrgType) error { // SetEnabled Sets the enabled status to true/false func SetEnabled(organizationID string, enabled bool) (Organization, error) { - orgID, err := primitive.ObjectIDFromHex(organizationID) - if err != nil { - return Organization{}, err - } - _, err = Collection().UpdateOne(context.TODO(), bson.M{"_id": orgID}, bson.M{"$set": bson.M{"enabled": enabled}}) + _, err := Collection().UpdateOne(context.TODO(), bson.M{"_id": organizationID}, bson.M{"$set": bson.M{"enabled": enabled}}) if err != nil { return Organization{}, err } @@ -207,35 +179,26 @@ func SetEnabled(organizationID string, enabled bool) (Organization, error) { } // GetSubscribeMethod Get org subscribe method -func GetSubscribeMethod(orgID string) (int, error) { +func GetSubscribeMethod(orgId string) (int, error) { var result Organization - orgId, err := primitive.ObjectIDFromHex(orgID) - if err != nil { - return result.Subs.Method, err - } - filter := bson.M{"_id": orgId} projection := bson.M{"subs.method": 1} findOptions := options.FindOne().SetProjection(projection) - err = Collection().FindOne(context.TODO(), filter, findOptions).Decode(&result) + err := Collection().FindOne(context.TODO(), filter, findOptions).Decode(&result) return result.Subs.Method, err } // UpdateSubscribeMethod Update subscription method -func UpdateSubscribeMethod(orgID string, method int) error { - orgId, err := primitive.ObjectIDFromHex(orgID) - if err != nil { - return err - } +func UpdateSubscribeMethod(orgId string, method int) error { filter := bson.M{"_id": orgId} update := bson.M{"$set": bson.M{"subs.method": method}} - _, err = Collection().UpdateOne(context.TODO(), filter, update) + _, err := Collection().UpdateOne(context.TODO(), filter, update) if err != nil { return err } @@ -244,16 +207,12 @@ func UpdateSubscribeMethod(orgID string, method int) error { } // UpdateSubscribeKey Update subscription key -func UpdateSubscribeKey(orgID string, key string) error { - orgId, err := primitive.ObjectIDFromHex(orgID) - if err != nil { - return err - } +func UpdateSubscribeKey(orgId string, key string) error { filter := bson.M{"_id": orgId} update := bson.M{"$set": bson.M{"subs.key": key}} - _, err = Collection().UpdateOne(context.TODO(), filter, update) + _, err := Collection().UpdateOne(context.TODO(), filter, update) if err != nil { return err } @@ -262,19 +221,14 @@ func UpdateSubscribeKey(orgID string, key string) error { } // GetSubscribeKey Update subscription token -func GetSubscribeKey(orgID string) (string, error) { +func GetSubscribeKey(orgId string) (string, error) { var result Organization - orgId, err := primitive.ObjectIDFromHex(orgID) - if err != nil { - return result.Subs.Key, err - } - filter := bson.M{"_id": orgId} projection := bson.M{"subs.key": 1} findOptions := options.FindOne().SetProjection(projection) - err = Collection().FindOne(context.TODO(), filter, findOptions).Decode(&result) + err := Collection().FindOne(context.TODO(), filter, findOptions).Decode(&result) return result.Subs.Key, err } @@ -283,16 +237,11 @@ func GetSubscribeKey(orgID string) (string, error) { func GetName(organizationID string) (string, error) { var result Organization - orgID, err := primitive.ObjectIDFromHex(organizationID) - if err != nil { - return result.Name, err - } - - filter := bson.M{"_id": orgID} + filter := bson.M{"_id": organizationID} projection := bson.M{"name": 1} findOptions := options.FindOne().SetProjection(projection) - err = Collection().FindOne(context.TODO(), filter, findOptions).Decode(&result) + err := Collection().FindOne(context.TODO(), filter, findOptions).Decode(&result) return result.Name, err } @@ -341,7 +290,7 @@ func AddOrganization(orgReq config.Organization, typeId string, userId string) ( } description := orgReq.Description - defaultDescription := "is committed to safeguarding your privacy. We process your personal data in line with data agreements, ensuring adherence to ISO27560 standards and legal frameworks like GDPR. For every personal data we process, you can view its usage purpose and make informed choices to opt in or out. For inquiries, contact our Data Protection Officer at dpo@" + defaultDescription := orgReq.Name + "is committed to safeguarding your privacy. We process your personal data in line with data agreements, ensuring adherence to ISO27560 standards and legal frameworks like GDPR. For every personal data we process, you can view its usage purpose and make informed choices to opt in or out." if strings.TrimSpace(description) == "" { description = defaultDescription diff --git a/internal/orgtype/orgType.go b/internal/orgtype/orgType.go index f374338..0d2dc0c 100644 --- a/internal/orgtype/orgType.go +++ b/internal/orgtype/orgType.go @@ -13,21 +13,21 @@ import ( // OrgType Type related information type OrgType struct { - ID primitive.ObjectID `bson:"_id,omitempty"` + ID string `bson:"_id,omitempty"` Type string ImageID string ImageURL string } -func collection() *mongo.Collection { +func Collection() *mongo.Collection { return database.DB.Client.Database(database.DB.Name).Collection("orgTypes") } // Add Adds an organization func Add(ot OrgType) (OrgType, error) { - ot.ID = primitive.NewObjectID() - _, err := collection().InsertOne(context.TODO(), &ot) + ot.ID = primitive.NewObjectID().Hex() + _, err := Collection().InsertOne(context.TODO(), &ot) return ot, err } @@ -36,12 +36,7 @@ func Add(ot OrgType) (OrgType, error) { func Get(organizationTypeID string) (OrgType, error) { var result OrgType - orgTypeID, err := primitive.ObjectIDFromHex(organizationTypeID) - if err != nil { - return result, err - } - - err = collection().FindOne(context.Background(), bson.M{"_id": orgTypeID}).Decode(&result) + err := Collection().FindOne(context.Background(), bson.M{"_id": organizationTypeID}).Decode(&result) return result, err } @@ -51,7 +46,7 @@ func GetAll() ([]OrgType, error) { var results []OrgType - cursor, err := collection().Find(context.TODO(), bson.M{}) + cursor, err := Collection().Find(context.TODO(), bson.M{}) if err != nil { return nil, err } @@ -66,12 +61,8 @@ func GetAll() ([]OrgType, error) { // Update Update the organization type func Update(organizationTypeID string, typeName string) (OrgType, error) { - orgTypeID, err := primitive.ObjectIDFromHex(organizationTypeID) - if err != nil { - return OrgType{}, err - } - _, err = collection().UpdateOne(context.TODO(), bson.M{"_id": orgTypeID}, bson.M{"$set": bson.M{"type": typeName}}) + _, err := Collection().UpdateOne(context.TODO(), bson.M{"_id": organizationTypeID}, bson.M{"$set": bson.M{"type": typeName}}) if err == nil { return Get(organizationTypeID) } @@ -80,31 +71,23 @@ func Update(organizationTypeID string, typeName string) (OrgType, error) { // Delete Deletes an organization func Delete(organizationTypeID string) error { - orgTypeID, err := primitive.ObjectIDFromHex(organizationTypeID) - if err != nil { - return err - } - _, err = collection().DeleteOne(context.TODO(), bson.M{"_id": orgTypeID}) + _, err := Collection().DeleteOne(context.TODO(), bson.M{"_id": organizationTypeID}) return err } // UpdateImage Update the org type image func UpdateImage(organizationTypeID string, imageID string, imageURL string) error { - orgTypeID, err := primitive.ObjectIDFromHex(organizationTypeID) - if err != nil { - return err - } - _, err = collection().UpdateOne(context.TODO(), bson.M{"_id": orgTypeID}, + _, err := Collection().UpdateOne(context.TODO(), bson.M{"_id": organizationTypeID}, bson.M{"$set": bson.M{"imageid": imageID, "imageurl": imageURL}}) return err } // GetTypesCount Gets types count func GetTypesCount() (int64, error) { - count, err := collection().CountDocuments(context.TODO(), bson.D{}) + count, err := Collection().CountDocuments(context.TODO(), bson.D{}) if err != nil { return count, err } @@ -116,7 +99,7 @@ func GetTypesCount() (int64, error) { func GetFirstType() (OrgType, error) { var result OrgType - err := collection().FindOne(context.TODO(), bson.M{}).Decode(&result) + err := Collection().FindOne(context.TODO(), bson.M{}).Decode(&result) return result, err } @@ -124,7 +107,7 @@ func GetFirstType() (OrgType, error) { // DeleteAllTypes delete all types func DeleteAllTypes() (*mongo.DeleteResult, error) { - result, err := collection().DeleteMany(context.TODO(), bson.D{}) + result, err := Collection().DeleteMany(context.TODO(), bson.D{}) if err != nil { return result, err } diff --git a/internal/otp/otps.go b/internal/otp/otps.go index ef17dfa..a2812aa 100644 --- a/internal/otp/otps.go +++ b/internal/otp/otps.go @@ -9,32 +9,23 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) -// Otp Otp holds the generated OTP info type Otp struct { - ID primitive.ObjectID `bson:"_id,omitempty"` - Name string - Email string - Phone string - Otp string - Verified bool -} -type OtpV2 struct { - ID primitive.ObjectID `bson:"_id,omitempty"` + ID string `bson:"_id,omitempty"` Phone string Otp string Verified bool } -func collection() *mongo.Collection { +func Collection() *mongo.Collection { return database.DB.Client.Database(database.DB.Name).Collection("otps") } // Add Adds the otp to the db func Add(otp Otp) (Otp, error) { - otp.ID = primitive.NewObjectID() + otp.ID = primitive.NewObjectID().Hex() - _, err := collection().InsertOne(context.TODO(), otp) + _, err := Collection().InsertOne(context.TODO(), otp) if err != nil { return Otp{}, err } @@ -42,27 +33,10 @@ func Add(otp Otp) (Otp, error) { return otp, nil } -// Add Adds the otp to the db -func AddV2(otp OtpV2) (OtpV2, error) { - - otp.ID = primitive.NewObjectID() - - _, err := collection().InsertOne(context.TODO(), otp) - if err != nil { - return OtpV2{}, err - } - - return otp, nil -} - // Delete Deletes the otp entry by ID -func Delete(otpID string) error { - otpId, err := primitive.ObjectIDFromHex(otpID) - if err != nil { - return err - } +func Delete(otpId string) error { - _, err = collection().DeleteOne(context.TODO(), bson.M{"_id": otpId}) + _, err := Collection().DeleteOne(context.TODO(), bson.M{"_id": otpId}) if err != nil { return err } @@ -75,7 +49,7 @@ func UpdateVerified(o Otp) error { filter := bson.M{"_id": o.ID} update := bson.M{"$set": bson.M{"verified": o.Verified}} - _, err := collection().UpdateOne(context.TODO(), filter, update) + _, err := Collection().UpdateOne(context.TODO(), filter, update) return err } @@ -84,7 +58,7 @@ func UpdateVerified(o Otp) error { func PhoneNumberExist(phone string) (o Otp, err error) { filter := bson.M{"phone": phone} - err = collection().FindOne(context.TODO(), filter).Decode(&o) + err = Collection().FindOne(context.TODO(), filter).Decode(&o) if err == mongo.ErrNoDocuments { return o, err } else if err != nil { @@ -99,7 +73,7 @@ func SearchPhone(phone string) (Otp, error) { filter := bson.M{"phone": phone} var result Otp - err := collection().FindOne(context.TODO(), filter).Decode(&result) + err := Collection().FindOne(context.TODO(), filter).Decode(&result) if err != nil { return result, err } diff --git a/internal/policy/policy.go b/internal/policy/policy.go index a7249eb..7c09988 100644 --- a/internal/policy/policy.go +++ b/internal/policy/policy.go @@ -15,18 +15,18 @@ func Collection() *mongo.Collection { } type Policy struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` - Name string `json:"name" valid:"required"` - Version string `json:"version"` - Url string `json:"url" valid:"required"` - Jurisdiction string `json:"jurisdiction"` - IndustrySector string `json:"industrySector"` - DataRetentionPeriodDays int `json:"dataRetentionPeriodDays"` - GeographicRestriction string `json:"geographicRestriction"` - StorageLocation string `json:"storageLocation"` - ThirdPartyDataSharing bool `json:"thirdPartyDataSharing"` - OrganisationId string `json:"-"` - IsDeleted bool `json:"-"` + Id string `json:"id" bson:"_id,omitempty"` + Name string `json:"name" valid:"required"` + Version string `json:"version"` + Url string `json:"url" valid:"required"` + Jurisdiction string `json:"jurisdiction"` + IndustrySector string `json:"industrySector"` + DataRetentionPeriodDays int `json:"dataRetentionPeriodDays"` + GeographicRestriction string `json:"geographicRestriction"` + StorageLocation string `json:"storageLocation"` + ThirdPartyDataSharing bool `json:"thirdPartyDataSharing"` + OrganisationId string `json:"-"` + IsDeleted bool `json:"-"` } func CombineFilters(filter1 bson.M, filter2 bson.M) bson.M { @@ -55,16 +55,12 @@ func (prepo *PolicyRepository) Add(policy Policy) (Policy, error) { } // Get Gets a single policy by given id -func (prepo *PolicyRepository) Get(policyID string) (Policy, error) { - policyId, err := primitive.ObjectIDFromHex(policyID) - if err != nil { - return Policy{}, err - } +func (prepo *PolicyRepository) Get(policyId string) (Policy, error) { filter := CombineFilters(prepo.DefaultFilter, bson.M{"_id": policyId}) var result Policy - err = Collection().FindOne(context.TODO(), filter).Decode(&result) + err := Collection().FindOne(context.TODO(), filter).Decode(&result) return result, err } diff --git a/internal/revision/db.go b/internal/revision/db.go index aa8aa12..eb0f7f3 100644 --- a/internal/revision/db.go +++ b/internal/revision/db.go @@ -5,7 +5,6 @@ import ( "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" ) @@ -71,14 +70,10 @@ func ListAllByPolicyId(policyId string) ([]Revision, error) { } // GetByRevisionId Get revision by id -func GetByRevisionId(revisionID string) (Revision, error) { +func GetByRevisionId(revisionId string) (Revision, error) { var result Revision - revisionId, err := primitive.ObjectIDFromHex(revisionID) - if err != nil { - return result, err - } - err = Collection().FindOne(context.TODO(), bson.M{"_id": revisionId}).Decode(&result) + err := Collection().FindOne(context.TODO(), bson.M{"_id": revisionId}).Decode(&result) if err != nil { return Revision{}, err } @@ -150,15 +145,10 @@ func ListAllByDataAttributeId(dataAttributeId string) ([]Revision, error) { } // GetByRevisionIdAndSchema gets revision by id and schema -func GetByRevisionIdAndSchema(revisionID string, schemaName string) (Revision, error) { +func GetByRevisionIdAndSchema(revisionId string, schemaName string) (Revision, error) { var result Revision - revisionId, err := primitive.ObjectIDFromHex(revisionID) - if err != nil { - return result, err - } - - err = Collection().FindOne(context.TODO(), bson.M{"_id": revisionId, "schemaname": schemaName}).Decode(&result) + err := Collection().FindOne(context.TODO(), bson.M{"_id": revisionId, "schemaname": schemaName}).Decode(&result) if err != nil { return result, err } diff --git a/internal/revision/revisions.go b/internal/revision/revisions.go index 5c4d45a..96e1480 100644 --- a/internal/revision/revisions.go +++ b/internal/revision/revisions.go @@ -2,6 +2,7 @@ package revision import ( "encoding/json" + "strings" "time" "github.com/bb-consent/api/internal/common" @@ -14,24 +15,24 @@ import ( // Revision type Revision struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` - SchemaName string `json:"schemaName"` - ObjectId string `json:"objectId"` - SignedWithoutObjectId bool `json:"signedWithoutObjectId"` - Timestamp string `json:"timestamp"` - AuthorizedByIndividualId string `json:"authorizedByIndividualId"` - AuthorizedByOtherId string `json:"authorizedByOtherId"` - PredecessorHash string `json:"predecessorHash"` - PredecessorSignature string `json:"predecessorSignature"` - ObjectData string `json:"objectData"` - SuccessorId string `json:"-"` - SerializedHash string `json:"-"` - SerializedSnapshot string `json:"-"` + Id string `json:"id" bson:"_id,omitempty"` + SchemaName string `json:"schemaName"` + ObjectId string `json:"objectId"` + SignedWithoutObjectId bool `json:"signedWithoutObjectId"` + Timestamp string `json:"timestamp"` + AuthorizedByIndividualId string `json:"authorizedByIndividualId"` + AuthorizedByOtherId string `json:"authorizedByOtherId"` + PredecessorHash string `json:"predecessorHash"` + PredecessorSignature string `json:"predecessorSignature"` + ObjectData string `json:"objectData"` + SuccessorId string `json:"-"` + SerializedHash string `json:"-"` + SerializedSnapshot string `json:"-"` } // Init func (r *Revision) Init(objectId string, authorisedByOtherId string, schemaName string) { - r.Id = primitive.NewObjectID() + r.Id = primitive.NewObjectID().Hex() r.SchemaName = schemaName r.ObjectId = objectId r.SignedWithoutObjectId = false @@ -84,7 +85,7 @@ func (r *Revision) UpdateRevision(previousRevision *Revision, objectData interfa if previousRevision != nil { // Update successor for previous revision - previousRevision.updateSuccessorId(r.Id.Hex()) + previousRevision.updateSuccessorId(r.Id) // Predecessor hash r.updatePredecessorHash(previousRevision.SerializedHash) @@ -105,15 +106,15 @@ func (r *Revision) UpdateRevision(previousRevision *Revision, objectData interfa } type policyForObjectData struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` - Name string `json:"name" valid:"required"` - Version string `json:"version"` - Url string `json:"url" valid:"required"` - Jurisdiction string `json:"jurisdiction"` - IndustrySector string `json:"industrySector"` - DataRetentionPeriodDays int `json:"dataRetentionPeriodDays"` - GeographicRestriction string `json:"geographicRestriction"` - StorageLocation string `json:"storageLocation"` + Id string `json:"id" bson:"_id,omitempty"` + Name string `json:"name" valid:"required"` + Version string `json:"version"` + Url string `json:"url" valid:"required"` + Jurisdiction string `json:"jurisdiction"` + IndustrySector string `json:"industrySector"` + DataRetentionPeriodDays int `json:"dataRetentionPeriodDays"` + GeographicRestriction string `json:"geographicRestriction"` + StorageLocation string `json:"storageLocation"` } // CreateRevisionForPolicy @@ -133,7 +134,7 @@ func CreateRevisionForPolicy(newPolicy policy.Policy, orgAdminId string) (Revisi // Create revision revision := Revision{} - revision.Init(objectData.Id.Hex(), orgAdminId, config.Policy) + revision.Init(objectData.Id, orgAdminId, config.Policy) err := revision.CreateRevision(objectData) return revision, err @@ -156,9 +157,9 @@ func UpdateRevisionForPolicy(updatedPolicy policy.Policy, orgAdminId string) (Re // Update revision r := Revision{} - r.Init(objectData.Id.Hex(), orgAdminId, config.Policy) + r.Init(objectData.Id, orgAdminId, config.Policy) // Query for previous revisions - previousRevision, err := GetLatestByPolicyId(updatedPolicy.Id.Hex()) + previousRevision, err := GetLatestByPolicyId(updatedPolicy.Id) if err != nil { // Previous revision is not present err = r.UpdateRevision(nil, objectData) @@ -210,7 +211,6 @@ func RecreatePolicyFromRevision(revision Revision) (policy.Policy, error) { // RevisionForHTTPResponse type RevisionForHTTPResponse struct { Revision - Id string `json:"id"` SuccessorId string `json:"successorId"` SerializedHash string `json:"serializedHash"` SerializedSnapshot string `json:"serizalizedSnapshot"` @@ -218,10 +218,10 @@ type RevisionForHTTPResponse struct { // Init func (r *RevisionForHTTPResponse) Init(revision Revision) { - if revision.Id.IsZero() { + if len(strings.TrimSpace(revision.Id)) < 1 { r.Id = "" } else { - r.Id = revision.Id.Hex() + r.Id = revision.Id } r.SchemaName = revision.SchemaName r.ObjectId = revision.ObjectId @@ -262,7 +262,7 @@ type dataAgreementForObjectData struct { // InitForDraftDataAgreement func (r *Revision) InitForDraftDataAgreement(objectId string, authorisedByOtherId string, schemaName string) { - r.Id = primitive.NilObjectID + r.Id = "" r.SchemaName = schemaName r.ObjectId = objectId r.SignedWithoutObjectId = false @@ -275,7 +275,7 @@ func (r *Revision) InitForDraftDataAgreement(objectId string, authorisedByOtherI func CreateRevisionForDataAgreement(newDataAgreement dataagreement.DataAgreement, orgAdminId string) (Revision, error) { // Object data objectData := dataAgreementForObjectData{ - Id: newDataAgreement.Id.Hex(), + Id: newDataAgreement.Id, Version: newDataAgreement.Version, ControllerId: newDataAgreement.ControllerId, ControllerUrl: newDataAgreement.ControllerUrl, @@ -306,7 +306,7 @@ func CreateRevisionForDataAgreement(newDataAgreement dataagreement.DataAgreement func UpdateRevisionForDataAgreement(updatedDataAgreement dataagreement.DataAgreement, orgAdminId string) (Revision, error) { // Object data objectData := dataAgreementForObjectData{ - Id: updatedDataAgreement.Id.Hex(), + Id: updatedDataAgreement.Id, Version: updatedDataAgreement.Version, ControllerId: updatedDataAgreement.ControllerId, ControllerUrl: updatedDataAgreement.ControllerUrl, @@ -330,7 +330,7 @@ func UpdateRevisionForDataAgreement(updatedDataAgreement dataagreement.DataAgree r.Init(objectData.Id, orgAdminId, config.DataAgreement) // Query for previous revisions - previousRevision, err := GetLatestByDataAgreementId(updatedDataAgreement.Id.Hex()) + previousRevision, err := GetLatestByDataAgreementId(updatedDataAgreement.Id) if err != nil { // Previous revision is not present err = r.UpdateRevision(nil, objectData) @@ -383,7 +383,7 @@ func RecreateDataAgreementFromRevision(revision Revision) (dataagreement.DataAgr func CreateRevisionForDraftDataAgreement(newDataAgreement dataagreement.DataAgreement, orgAdminId string) (Revision, error) { // Object data objectData := dataAgreementForObjectData{ - Id: newDataAgreement.Id.Hex(), + Id: newDataAgreement.Id, Version: newDataAgreement.Version, ControllerId: newDataAgreement.ControllerId, ControllerUrl: newDataAgreement.ControllerUrl, @@ -423,14 +423,14 @@ func RecreateDataAgreementFromObjectData(objectData string) (interface{}, error) } type dataAgreementRecordForObjectData 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"` + 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"` } // CreateRevisionForDataAgreementRecord @@ -449,7 +449,7 @@ func CreateRevisionForDataAgreementRecord(newDataAgreementRecord daRecord.DataAg // Create revision revision := Revision{} - revision.Init(objectData.Id.Hex(), orgAdminId, config.DataAgreementRecord) + revision.Init(objectData.Id, orgAdminId, config.DataAgreementRecord) err := revision.CreateRevision(objectData) return revision, err @@ -461,7 +461,7 @@ func UpdateRevisionForDataAgreementRecord(updatedDataAgreementRecord daRecord.Da objectData := dataAgreementRecordForObjectData{ Id: updatedDataAgreementRecord.Id, DataAgreementId: updatedDataAgreementRecord.DataAgreementId, - DataAgreementRevisionId: dataAgreementRevision.Id.Hex(), + DataAgreementRevisionId: dataAgreementRevision.Id, DataAgreementRevisionHash: dataAgreementRevision.SerializedHash, IndividualId: updatedDataAgreementRecord.IndividualId, OptIn: updatedDataAgreementRecord.OptIn, @@ -471,9 +471,9 @@ func UpdateRevisionForDataAgreementRecord(updatedDataAgreementRecord daRecord.Da // Update revision revision := Revision{} - revision.Init(objectData.Id.Hex(), orgAdminId, config.DataAgreementRecord) + revision.Init(objectData.Id, orgAdminId, config.DataAgreementRecord) // Query for previous revisions - previousRevision, err := GetLatestByObjectId(updatedDataAgreementRecord.Id.Hex()) + previousRevision, err := GetLatestByObjectId(updatedDataAgreementRecord.Id) if err != nil { return revision, err } diff --git a/internal/signature/db.go b/internal/signature/db.go index 7487b65..5ade689 100644 --- a/internal/signature/db.go +++ b/internal/signature/db.go @@ -5,7 +5,6 @@ import ( "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" ) @@ -25,16 +24,12 @@ func Add(signature Signature) (Signature, error) { } // Get Gets a signature by given id -func Get(signatureID string) (Signature, error) { - signatureId, err := primitive.ObjectIDFromHex(signatureID) - if err != nil { - return Signature{}, err - } +func Get(signatureId string) (Signature, error) { filter := bson.M{"_id": signatureId} var result Signature - err = Collection().FindOne(context.TODO(), filter).Decode(&result) + err := Collection().FindOne(context.TODO(), filter).Decode(&result) return result, err } diff --git a/internal/signature/signature.go b/internal/signature/signature.go index 8267830..dcd92d5 100644 --- a/internal/signature/signature.go +++ b/internal/signature/signature.go @@ -5,24 +5,23 @@ import ( "time" "github.com/bb-consent/api/internal/common" - "go.mongodb.org/mongo-driver/bson/primitive" ) type Signature struct { - Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` - 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"` + Id string `json:"id" bson:"_id,omitempty"` + 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"` } // Init diff --git a/internal/tenant/single.go b/internal/tenant/single.go index ddf78d0..fc0bbc0 100644 --- a/internal/tenant/single.go +++ b/internal/tenant/single.go @@ -69,13 +69,13 @@ func createOrganisation(config *config.Configuration, orgType orgtype.OrgType, o if err != nil { if err == mongo.ErrNoDocuments { log.Printf("Organization doesn't exist, creating organization.") - organization, err = org.AddOrganization(config.Organization, orgType.ID.Hex(), organisationAdminId) + organization, err = org.AddOrganization(config.Organization, orgType.ID, organisationAdminId) if err != nil { log.Println("failed to add organization") panic(err) } // Add roles to organisation admin user - addOrganisationAdminRole(organisationAdminId, organization.ID.Hex()) + addOrganisationAdminRole(organisationAdminId, organization.ID) } else { log.Println("failed to find organization") @@ -127,7 +127,7 @@ func deleteAllPolicies() { // Repository prepo := policy.PolicyRepository{} - prepo.Init(o.ID.Hex()) + prepo.Init(o.ID) count, err := prepo.GetPolicyCountByOrganisation() if err != nil { @@ -135,7 +135,7 @@ func deleteAllPolicies() { panic(err) } if count > 1 { - err := policy.DeleteAllPolicies(o.ID.Hex()) + err := policy.DeleteAllPolicies(o.ID) if err != nil { log.Println("failed to delete policies") panic(err) @@ -147,7 +147,7 @@ func deleteAllPolicies() { func createDefaultPolicy(config *config.Configuration, org org.Organization, orgAdminId string) (policy.Policy, error) { var newPolicy policy.Policy - newPolicy.Id = primitive.NewObjectID() + newPolicy.Id = primitive.NewObjectID().Hex() newPolicy.Name = config.Policy.Name newPolicy.Url = org.PolicyURL newPolicy.Jurisdiction = org.Location @@ -156,7 +156,7 @@ func createDefaultPolicy(config *config.Configuration, org org.Organization, org newPolicy.GeographicRestriction = config.Policy.GeographicRestriction newPolicy.StorageLocation = config.Policy.StorageLocation newPolicy.ThirdPartyDataSharing = true - newPolicy.OrganisationId = org.ID.Hex() + newPolicy.OrganisationId = org.ID newPolicy.IsDeleted = false version := common.IntegerToSemver(1) @@ -194,7 +194,7 @@ func createGlobalPolicy(config *config.Configuration, orgAdminId string) { // Repository prepo := policy.PolicyRepository{} - prepo.Init(o.ID.Hex()) + prepo.Init(o.ID) policyCount, _ := prepo.GetPolicyCountByOrganisation() if policyCount == 0 { @@ -228,7 +228,7 @@ func SingleTenantConfiguration(config *config.Configuration) { // Create an organisation admin organisationAdmin := createOrganisationAdmin(config) - organisationAdminId := organisationAdmin.ID.Hex() + organisationAdminId := organisationAdmin.ID // Create organisation type orgType := createOrganisationType(config) diff --git a/internal/user/users.go b/internal/user/users.go index 823f570..f4b6e95 100644 --- a/internal/user/users.go +++ b/internal/user/users.go @@ -16,7 +16,6 @@ import ( "github.com/bb-consent/api/internal/config" "github.com/bb-consent/api/internal/database" "github.com/bb-consent/api/internal/org" - "github.com/bb-consent/api/internal/orgtype" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" @@ -25,26 +24,12 @@ import ( // Org Organization snippet stored as part of user type Org struct { - OrgID primitive.ObjectID `bson:"orgid,omitempty"` - Name string - Location string - Type string - TypeID primitive.ObjectID - EulaAccepted bool -} -type OrgV2 struct { - OrgID primitive.ObjectID `bson:"orgid,omitempty" json:"id"` - Name string `json:"name"` - Location string `json:"location"` - Type string `json:"type"` - TypeID primitive.ObjectID `bson:"typeid,omitempty" json:"typeId"` - EulaAccepted bool `json:"eulaAccepted"` -} - -// ClientInfo The client device details. -type ClientInfo struct { - Token string `json:"token"` - Type int `json:"type"` + OrgID string `bson:"orgid,omitempty" json:"id"` + Name string `json:"name"` + Location string `json:"location"` + Type string `json:"type"` + TypeID string `bson:"typeid,omitempty" json:"typeId"` + EulaAccepted bool `json:"eulaAccepted"` } // Role Role assignment to user @@ -52,86 +37,59 @@ type Role struct { RoleID int `json:"roleId"` OrgID string `json:"orgId"` } -type RoleV2 struct { - RoleID int `json:"roleId"` - OrgID string `json:"orgId"` -} // User data type type User struct { - ID primitive.ObjectID `bson:"_id,omitempty"` - Name string `json:"Name"` - IamID string `json:"IamID"` - Email string `json:"Email"` - Phone string `json:"Phone"` - ImageID string `json:"ImageID"` - ImageURL string `json:"ImageURL"` - LastVisit string `json:"LastVisit"` - Client ClientInfo `json:"Client"` - Orgs []Org `json:"Orgs"` - APIKey string `json:"APIKey"` - Roles []Role `json:"roles"` - IncompleteProfile bool `json:"IncompleteProfile"` -} - -type UserV2 struct { - ID primitive.ObjectID `bson:"_id,omitempty" json:"id"` - Name string `json:"name"` - ExternalId string `json:"externalId"` - ExternalIdType string `json:"externalIdType"` - IdentityProviderId string `json:"identityProviderId"` - IamID string `json:"iamId"` - Email string `json:"email"` - Phone string `json:"phone"` - ImageID string `json:"imageId"` - ImageURL string `json:"imageUrl"` - LastVisit string `json:"lastVisit"` //TODO Replace with ISODate() - Orgs []OrgV2 `json:"orgs"` - APIKey string `json:"apiKey"` - Roles []RoleV2 `json:"roles"` - IncompleteProfile bool `json:"incompleteProfile"` -} - -func collection() *mongo.Collection { + ID string `bson:"_id,omitempty" json:"id"` + Name string `json:"name"` + ExternalId string `json:"externalId"` + ExternalIdType string `json:"externalIdType"` + IdentityProviderId string `json:"identityProviderId"` + IamID string `json:"iamId"` + Email string `json:"email"` + Phone string `json:"phone"` + ImageID string `json:"imageId"` + ImageURL string `json:"imageUrl"` + LastVisit string `json:"lastVisit"` //TODO Replace with ISODate() + Orgs []Org `json:"orgs"` + APIKey string `json:"apiKey"` + Roles []Role `json:"roles"` + IncompleteProfile bool `json:"incompleteProfile"` +} + +func Collection() *mongo.Collection { return database.DB.Client.Database(database.DB.Name).Collection("users") } // Add Adds an user to the collection func Add(user User) (User, error) { - user.ID = primitive.NewObjectID() + user.ID = primitive.NewObjectID().Hex() user.LastVisit = time.Now().Format(time.RFC3339) - _, err := collection().InsertOne(context.TODO(), &user) + _, err := Collection().InsertOne(context.TODO(), &user) return user, err } // Update Update the user details -func Update(userID string, u User) (User, error) { - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return User{}, err - } +func Update(userId string, u User) (User, error) { - _, err = collection().UpdateOne(context.TODO(), bson.M{"_id": userId}, bson.M{"$set": u}) + _, err := Collection().UpdateOne(context.TODO(), bson.M{"_id": userId}, bson.M{"$set": u}) if err != nil { return User{}, err } - u, err = Get(userID) + u, err = Get(userId) return u, err } // Delete Deletes the user by ID -func Delete(userID string) error { - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return err - } +func Delete(userId string) error { + filter := bson.M{"_id": userId} - _, err = collection().DeleteOne(context.TODO(), filter) + _, err := Collection().DeleteOne(context.TODO(), filter) return err } @@ -140,19 +98,7 @@ func Delete(userID string) error { func GetByIamID(iamID string) (User, error) { var result User - err := collection().FindOne(context.TODO(), bson.M{"iamid": iamID}).Decode(&result) - if err != nil { - log.Printf("Failed to find user id:%v err:%v", iamID, err) - return result, err - } - - return result, err -} - -func GetByIamIDV2(iamID string) (UserV2, error) { - var result UserV2 - - err := collection().FindOne(context.TODO(), bson.M{"iamid": iamID}).Decode(&result) + err := Collection().FindOne(context.TODO(), bson.M{"iamid": iamID}).Decode(&result) if err != nil { log.Printf("Failed to find user id:%v err:%v", iamID, err) return result, err @@ -162,21 +108,16 @@ func GetByIamIDV2(iamID string) (UserV2, error) { } // Get Gets a single user by given id -func Get(userID string) (User, error) { - c := collection() - - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return User{}, err - } +func Get(userId string) (User, error) { + c := Collection() var result User // Find the user by ID filter := bson.M{"_id": userId} - err = c.FindOne(context.TODO(), filter).Decode(&result) + err := c.FindOne(context.TODO(), filter).Decode(&result) if err != nil { - log.Printf("Failed to find user ID: %v, error: %v", userID, err) + log.Printf("Failed to find user ID: %v, error: %v", userId, err) return result, err } @@ -186,7 +127,7 @@ func Get(userID string) (User, error) { updateOptions := options.Update().SetUpsert(false) _, err = c.UpdateOne(context.TODO(), filter, update, updateOptions) if err != nil { - log.Printf("Failed to update LastVisit field for id:%v \n", userID) + log.Printf("Failed to update LastVisit field for id:%v", userId) } return result, err @@ -202,7 +143,7 @@ func GetByEmail(email string) (User, error) { findOptions := options.FindOne().SetProjection(projection) - err := collection().FindOne(context.TODO(), filter, findOptions).Decode(&u) + err := Collection().FindOne(context.TODO(), filter, findOptions).Decode(&u) return u, err } @@ -213,7 +154,7 @@ func EmailExist(email string) (bool, error) { countOptions := options.Count().SetLimit(1) - count, err := collection().CountDocuments(context.TODO(), filter, countOptions) + count, err := Collection().CountDocuments(context.TODO(), filter, countOptions) if err != nil { return false, err } @@ -227,7 +168,7 @@ func PhoneNumberExist(phone string) (bool, error) { countOptions := options.Count().SetLimit(1) - count, err := collection().CountDocuments(context.TODO(), filter, countOptions) + count, err := Collection().CountDocuments(context.TODO(), filter, countOptions) if err != nil { return false, err } @@ -235,155 +176,21 @@ func PhoneNumberExist(phone string) (bool, error) { return count > 0, nil } -// UpdateClientDeviceInfo Update the client device info -func UpdateClientDeviceInfo(userID string, client ClientInfo) (User, error) { - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return User{}, err - } - - filter := bson.M{"_id": userId} - update := bson.M{"$set": bson.M{"client": client}} - - _, err = collection().UpdateOne(context.TODO(), filter, update) - if err != nil { - return User{}, err - } - - //TODO: Is this DB get necessary? - u, err := Get(userID) - return u, err -} - // AddRole Add roles to users -func AddRole(userID string, role Role) (User, error) { - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return User{}, err - } +func AddRole(userId string, role Role) (User, error) { - _, err = collection().UpdateOne(context.TODO(), bson.M{"_id": userId}, bson.M{"$push": bson.M{"roles": role}}) + _, err := Collection().UpdateOne(context.TODO(), bson.M{"_id": userId}, bson.M{"$push": bson.M{"roles": role}}) if err != nil { return User{}, err } - u, err := Get(userID) + u, err := Get(userId) return u, err } -// GetOrgSubscribeUsers Get list of users subscribed to an organizations -func GetOrgSubscribeUsers(orgID string, startID string, limit int) ([]User, string, error) { - var results []User - var err error - limit = 10000 - - findOptions := options.Find() - findOptions.SetSort(bson.D{{Key: "_id", Value: -1}}) - findOptions.SetLimit(int64(limit)) - - orgId, err := primitive.ObjectIDFromHex(orgID) - if err != nil { - return results, "", err - } - - filter := bson.M{"orgs.orgid": orgId} - if startID != "" { - startId, err := primitive.ObjectIDFromHex(startID) - if err != nil { - return results, "", err - } - - filter["_id"] = bson.M{"$lt": startId} - } - - cursor, err := collection().Find(context.TODO(), filter, findOptions) - if err != nil { - return results, "", err - } - defer cursor.Close(context.TODO()) - - err = cursor.All(context.TODO(), &results) - - lastID := "" - if err == nil { - if len(results) != 0 && len(results) == (limit) { - lastID = results[len(results)-1].ID.Hex() - } - } - - return results, lastID, nil - -} - -// GetOrgSubscribeIter Get Iterator to users subscribed to an organizations -func GetOrgSubscribeIter(orgID string) (*mongo.Cursor, error) { - orgId, err := primitive.ObjectIDFromHex(orgID) - if err != nil { - return nil, err - } - - filter := bson.M{"orgs.orgid": orgId} - cursor, err := collection().Find(context.TODO(), filter) - if err != nil { - return nil, err - } - - return cursor, nil -} - -// GetOrgSubscribeCount Get count of users subscribed to an organizations -func GetOrgSubscribeCount(orgID string) (int64, error) { - orgId, err := primitive.ObjectIDFromHex(orgID) - if err != nil { - return 0, err - } - filter := bson.M{"orgs.orgid": orgId} - - count, err := collection().CountDocuments(context.TODO(), filter) - if err != nil { - log.Printf("Failed to find user count by org id:%v err:%v", orgID, err) - return 0, err - } - - return count, err -} - -// UpdateOrgTypeOfSubscribedUsers Updates the embedded organization type snippet for all users -func UpdateOrgTypeOfSubscribedUsers(orgType orgtype.OrgType) error { - filter := bson.M{"orgs.typeid": orgType.ID} - - cursor, err := collection().Find(context.TODO(), filter) - if err != nil { - return err - } - defer cursor.Close(context.TODO()) - - for cursor.Next(context.TODO()) { - var u User - err := cursor.Decode(&u) - if err != nil { - return err - } - - for i := range u.Orgs { - if u.Orgs[i].TypeID == orgType.ID { - u.Orgs[i].Type = orgType.Type - } - } - - _, err = collection().ReplaceOne(context.TODO(), bson.M{"_id": u.ID}, u) - if err != nil { - return err - } - } - - log.Println("Successfully updated users for organization type name change") - return nil -} - // UpdateOrganizationsSubscribedUsers Updates the embedded organization snippet for all users func UpdateOrganizationsSubscribedUsers(org org.Organization) error { filter := bson.M{"orgs.orgid": org.ID} - cursor, err := collection().Find(context.TODO(), filter) + cursor, err := Collection().Find(context.TODO(), filter) if err != nil { return err } @@ -403,7 +210,7 @@ func UpdateOrganizationsSubscribedUsers(org org.Organization) error { } } - _, err = collection().ReplaceOne(context.TODO(), bson.M{"_id": result.ID}, result) + _, err = Collection().ReplaceOne(context.TODO(), bson.M{"_id": result.ID}, result) if err != nil { return err } @@ -412,122 +219,6 @@ func UpdateOrganizationsSubscribedUsers(org org.Organization) error { return nil } -// UpdateOrganization Updates organization to user collection -func UpdateOrganization(userID string, org Org) (User, error) { - var result User - - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return User{}, err - } - - filter := bson.M{"_id": userId} - - update := bson.M{"$push": bson.M{"orgs": org}} - - _, err = collection().UpdateOne(context.TODO(), filter, update) - if err != nil { - return result, err - } - - err = collection().FindOne(context.TODO(), filter).Decode(&result) - if err != nil { - return result, err - } - - return result, err -} - -func GetUserOrgDetails(u User, oID string) (org Org, found bool) { - for _, o := range u.Orgs { - if o.OrgID.Hex() == oID { - return o, true - } - } - return org, false -} - -// DeleteOrganization Remove user from an organization -func DeleteOrganization(userID string, orgID string) (User, error) { - - u, err := Get(userID) - if err != nil { - return User{}, err - } - org, _ := GetUserOrgDetails(u, orgID) - //Check found == true - - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return User{}, err - } - - filter := bson.M{"_id": userId} - update := bson.M{"$pull": bson.M{"orgs": org}} - - var result User - - _, err = collection().UpdateOne(context.TODO(), filter, update) - if err != nil { - return result, err - } - - err = collection().FindOne(context.TODO(), filter).Decode(&result) - - return result, err -} - -// RemoveRole Remove role of an user -func RemoveRole(userID string, role Role) (User, error) { - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return User{}, err - } - filter := bson.M{"_id": userId} - update := bson.M{"$pull": bson.M{"roles": role}} - - _, err = collection().UpdateOne(context.TODO(), filter, update) - if err != nil { - return User{}, err - } - - u, err := Get(userID) - return u, err -} - -// UpdateAPIKey update apikey to user -func UpdateAPIKey(userID string, apiKey string) error { - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return err - } - filter := bson.M{"_id": userId} - update := bson.M{"$set": bson.M{"apikey": apiKey}} - - _, err = collection().UpdateOne(context.TODO(), filter, update) - return err -} - -// GetAPIKey Gets the API key of the user -func GetAPIKey(userID string) (string, error) { - userId, err := primitive.ObjectIDFromHex(userID) - if err != nil { - return "", err - } - - projection := bson.M{"apikey": 1} - opts := options.FindOne().SetProjection(projection) - - var result User - err = collection().FindOne(context.TODO(), bson.M{"_id": userId}, opts).Decode(&result) - if err != nil { - log.Printf("Failed to find user by id:%v err:%v", userID, err) - return "", err - } - - return result.APIKey, err -} - type iamCredentials struct { Type string `json:"type"` Value string `json:"value"` diff --git a/internal/webhook/db.go b/internal/webhook/db.go index 7ca2fed..8a0e5e8 100644 --- a/internal/webhook/db.go +++ b/internal/webhook/db.go @@ -6,28 +6,27 @@ 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" ) // Webhook Defines the structure for an organisation webhook type Webhook struct { - ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // Webhook ID - OrganisationId string `json:"orgId" bson:"orgid"` // Organisation ID - PayloadURL string `json:"payloadUrl" valid:"required"` // Webhook payload URL - ContentType string `json:"contentType" valid:"required"` // Webhook payload content type for e.g application/json - SubscribedEvents []string `json:"subscribedEvents" valid:"required"` // Events subscribed for e.g. user.data.delete - Disabled bool `json:"disabled"` // Disabled or not - SecretKey string `json:"secretKey" valid:"required"` // For calculating SHA256 HMAC to verify data integrity and authenticity - SkipSSLVerification bool `json:"skipSslVerification"` // Skip SSL certificate verification or not (expiry is checked) - TimeStamp string `json:"timestamp"` // UTC timestamp - IsDeleted bool `json:"-"` + ID string `json:"id" bson:"_id,omitempty"` // Webhook ID + OrganisationId string `json:"orgId" bson:"orgid"` // Organisation ID + PayloadURL string `json:"payloadUrl" valid:"required"` // Webhook payload URL + ContentType string `json:"contentType" valid:"required"` // Webhook payload content type for e.g application/json + SubscribedEvents []string `json:"subscribedEvents" valid:"required"` // Events subscribed for e.g. user.data.delete + Disabled bool `json:"disabled"` // Disabled or not + SecretKey string `json:"secretKey" valid:"required"` // For calculating SHA256 HMAC to verify data integrity and authenticity + SkipSSLVerification bool `json:"skipSslVerification"` // Skip SSL certificate verification or not (expiry is checked) + TimeStamp string `json:"timestamp"` // UTC timestamp + IsDeleted bool `json:"-"` } // WebhookDelivery Details of payload delivery to webhook endpoint type WebhookDelivery struct { - ID primitive.ObjectID `bson:"_id,omitempty"` // Webhook delivery ID + ID string `bson:"_id,omitempty"` // Webhook delivery ID WebhookID string // Webhook ID UserID string // ID of user who triggered the webhook event WebhookEventType string // Webhook event type for e.g. data.delete.initiated @@ -43,18 +42,18 @@ type WebhookDelivery struct { StatusDescription string // Describe the status for e.g. Reason for failure } -func webhookCollection() *mongo.Collection { +func WebhookCollection() *mongo.Collection { return database.DB.Client.Database(database.DB.Name).Collection("webhooks") } -func webhookDeliveryCollection() *mongo.Collection { +func WebhookDeliveryCollection() *mongo.Collection { return database.DB.Client.Database(database.DB.Name).Collection("webhookDeliveries") } // CreateWebhook Adds a webhook for an organisation func CreateWebhook(webhook Webhook) (Webhook, error) { - _, err := webhookCollection().InsertOne(context.TODO(), &webhook) + _, err := WebhookCollection().InsertOne(context.TODO(), &webhook) if err != nil { return webhook, err } @@ -62,27 +61,19 @@ func CreateWebhook(webhook Webhook) (Webhook, error) { } // GetByOrgID Gets a webhook by organisation ID and webhook ID -func GetByOrgID(webhookID, orgID string) (result Webhook, err error) { - webhookId, err := primitive.ObjectIDFromHex(webhookID) - if err != nil { - return result, err - } +func GetByOrgID(webhookId, orgID string) (result Webhook, err error) { - err = webhookCollection().FindOne(context.TODO(), bson.M{"_id": webhookId, "orgid": orgID}).Decode(&result) + err = WebhookCollection().FindOne(context.TODO(), bson.M{"_id": webhookId, "orgid": orgID}).Decode(&result) return result, err } // DeleteWebhook Deletes a webhook for an organisation -func DeleteWebhook(webhookID string) error { - webhookId, err := primitive.ObjectIDFromHex(webhookID) - if err != nil { - return err - } +func DeleteWebhook(webhookId string) error { filter := bson.M{"_id": webhookId} - _, err = webhookCollection().DeleteOne(context.TODO(), filter) + _, err := WebhookCollection().DeleteOne(context.TODO(), filter) if err != nil { return err } @@ -96,7 +87,7 @@ func UpdateWebhook(webhook Webhook) (Webhook, error) { filter := bson.M{"_id": webhook.ID} update := bson.M{"$set": webhook} - _, err := webhookCollection().UpdateOne(context.TODO(), filter, update) + _, err := WebhookCollection().UpdateOne(context.TODO(), filter, update) return webhook, err } @@ -104,7 +95,7 @@ func UpdateWebhook(webhook Webhook) (Webhook, error) { func GetActiveWebhooksByOrgID(orgID string) (results []Webhook, err error) { filter := bson.M{"orgid": orgID, "disabled": false, "isdeleted": false} - cursor, err := webhookCollection().Find(context.TODO(), filter) + cursor, err := WebhookCollection().Find(context.TODO(), filter) if err != nil { return nil, err } @@ -120,7 +111,7 @@ func GetActiveWebhooksByOrgID(orgID string) (results []Webhook, err error) { // GetWebhookCountByPayloadURL Gets the count of webhooks with same payload URL for an organisation func GetWebhookCountByPayloadURL(orgID string, payloadURL string) (count int64, err error) { - count, err = webhookCollection().CountDocuments(context.TODO(), bson.M{"orgid": orgID, "payloadurl": payloadURL}) + count, err = WebhookCollection().CountDocuments(context.TODO(), bson.M{"orgid": orgID, "payloadurl": payloadURL}) return count, err } @@ -131,7 +122,7 @@ func GetAllWebhooksByOrgID(orgID string) (results []Webhook, err error) { options := options.Find().SetSort(bson.D{{Key: "timestamp", Value: -1}}) - cursor, err := webhookCollection().Find(context.TODO(), filter, options) + cursor, err := WebhookCollection().Find(context.TODO(), filter, options) if err != nil { return nil, err } @@ -150,7 +141,7 @@ func GetLastWebhookDelivery(webhookID string) (result WebhookDelivery, err error options := options.FindOne().SetSort(bson.D{{Key: "executionstarttimestamp", Value: -1}}) - err = webhookDeliveryCollection().FindOne(context.TODO(), filter, options).Decode(&result) + err = WebhookDeliveryCollection().FindOne(context.TODO(), filter, options).Decode(&result) if err != nil { return WebhookDelivery{}, err } @@ -161,19 +152,15 @@ func GetLastWebhookDelivery(webhookID string) (result WebhookDelivery, err error // GetWebhookByPayloadURL Get the webhook for an organisation by payload URL func GetWebhookByPayloadURL(orgID string, payloadURL string) (result Webhook, err error) { - err = webhookCollection().FindOne(context.TODO(), bson.M{"orgid": orgID, "payloadurl": payloadURL}).Decode(&result) + err = WebhookCollection().FindOne(context.TODO(), bson.M{"orgid": orgID, "payloadurl": payloadURL}).Decode(&result) return result, err } // GetWebhookDeliveryByID Gets payload delivery details by ID -func GetWebhookDeliveryByID(webhookID string, webhookDeliveryID string) (result WebhookDelivery, err error) { - webhookDeliveryId, err := primitive.ObjectIDFromHex(webhookDeliveryID) - if err != nil { - return result, err - } +func GetWebhookDeliveryByID(webhookID string, webhookDeliveryId string) (result WebhookDelivery, err error) { - err = webhookDeliveryCollection().FindOne(context.TODO(), bson.M{"webhookid": webhookID, "_id": webhookDeliveryId}).Decode(&result) + err = WebhookDeliveryCollection().FindOne(context.TODO(), bson.M{"webhookid": webhookID, "_id": webhookDeliveryId}).Decode(&result) return result, err } @@ -185,7 +172,7 @@ func GetAllDeliveryByWebhookId(webhookId string) (results []WebhookDelivery, err options := options.Find() options.SetSort(bson.D{{Key: "executionstarttimestamp", Value: -1}}) - cursor, err := webhookDeliveryCollection().Find(context.TODO(), filter, options) + cursor, err := WebhookDeliveryCollection().Find(context.TODO(), filter, options) if err != nil { return results, err } @@ -210,7 +197,7 @@ func (whRepo *WebhookRepository) Init(organisationId string) { // CreateWebhook Adds a webhook for an organisation func (whRepo *WebhookRepository) CreateWebhook(webhook Webhook) (Webhook, error) { - _, err := webhookCollection().InsertOne(context.TODO(), &webhook) + _, err := WebhookCollection().InsertOne(context.TODO(), &webhook) if err != nil { return webhook, err } @@ -221,22 +208,18 @@ func (whRepo *WebhookRepository) CreateWebhook(webhook Webhook) (Webhook, error) func (whRepo *WebhookRepository) GetWebhookCountByPayloadURL(payloadURL string) (count int64, err error) { filter := common.CombineFilters(whRepo.DefaultFilter, bson.M{"payloadurl": payloadURL}) - count, err = webhookCollection().CountDocuments(context.TODO(), filter) + count, err = WebhookCollection().CountDocuments(context.TODO(), filter) return count, err } // GetByOrgID Gets a webhook by organisation ID and webhook ID -func (whRepo *WebhookRepository) GetByOrgID(webhookID string) (Webhook, error) { +func (whRepo *WebhookRepository) GetByOrgID(webhookId string) (Webhook, error) { var result Webhook - webhookId, err := primitive.ObjectIDFromHex(webhookID) - if err != nil { - return result, err - } filter := common.CombineFilters(whRepo.DefaultFilter, bson.M{"_id": webhookId}) - err = webhookCollection().FindOne(context.TODO(), filter).Decode(&result) + err := WebhookCollection().FindOne(context.TODO(), filter).Decode(&result) return result, err } @@ -245,7 +228,7 @@ func (whRepo *WebhookRepository) GetByOrgID(webhookID string) (Webhook, error) { func (whRepo *WebhookRepository) GetWebhookByPayloadURL(payloadURL string) (result Webhook, err error) { filter := common.CombineFilters(whRepo.DefaultFilter, bson.M{"payloadurl": payloadURL}) - err = webhookCollection().FindOne(context.TODO(), filter).Decode(&result) + err = WebhookCollection().FindOne(context.TODO(), filter).Decode(&result) return result, err } @@ -255,7 +238,7 @@ func (whRepo *WebhookRepository) UpdateWebhook(webhook Webhook) (Webhook, error) filter := bson.M{"_id": webhook.ID, "orgid": webhook.OrganisationId, "isdeleted": false} update := bson.M{"$set": webhook} - _, err := webhookCollection().UpdateOne(context.TODO(), filter, update) + _, err := WebhookCollection().UpdateOne(context.TODO(), filter, update) return webhook, err } @@ -265,7 +248,7 @@ func (whRepo *WebhookRepository) GetAllWebhooksByOrgID() (results []Webhook, err options := options.Find().SetSort(bson.D{{Key: "timestamp", Value: -1}}) - cursor, err := webhookCollection().Find(context.TODO(), filter, options) + cursor, err := WebhookCollection().Find(context.TODO(), filter, options) if err != nil { return nil, err } diff --git a/internal/webhook/webhook.go b/internal/webhook/webhook.go index 3de092c..0171d07 100644 --- a/internal/webhook/webhook.go +++ b/internal/webhook/webhook.go @@ -178,7 +178,7 @@ func TriggerWebhooks(webhookEventData WebhookEventData, webhookEventType string) // Get the active webhooks for the organisation activeWebhooks, err := GetActiveWebhooksByOrgID(webhookEventData.GetOrganisationID()) if err != nil { - log.Printf("Failed to fetch active webhooks;Failed to trigger webhook for event:<%s>, user:<%s>, org:<%s>", webhookEventType, individual.Id.Hex(), webhookEventData.GetOrganisationID()) + log.Printf("Failed to fetch active webhooks;Failed to trigger webhook for event:<%s>, user:<%s>, org:<%s>", webhookEventType, individual.Id, webhookEventData.GetOrganisationID()) return } @@ -196,7 +196,7 @@ func TriggerWebhooks(webhookEventData WebhookEventData, webhookEventType string) for _, toBeProcessedWebhook := range toBeProcessedWebhooks { // Constructing webhook payload we := WebhookEvent{ - WebhookID: toBeProcessedWebhook.ID.Hex(), + WebhookID: toBeProcessedWebhook.ID, Timestamp: time.Now().UTC().Format("2006-01-02T15:04:05Z"), Data: webhookEventData, Type: webhookEventType, @@ -205,7 +205,7 @@ func TriggerWebhooks(webhookEventData WebhookEventData, webhookEventType string) // Converting the webhook event data to bytes b, err := json.Marshal(we) if err != nil { - log.Printf("Failed to convert webhook event data to bytes, error:%v, Failed to trigger webhook for event:<%s>, user:<%s>, org:<%s>", err.Error(), webhookEventType, individual.Id.Hex(), webhookEventData.GetOrganisationID()) + log.Printf("Failed to convert webhook event data to bytes, error:%v, Failed to trigger webhook for event:<%s>, user:<%s>, org:<%s>", err.Error(), webhookEventType, individual.Id, webhookEventData.GetOrganisationID()) return } @@ -213,7 +213,7 @@ func TriggerWebhooks(webhookEventData WebhookEventData, webhookEventType string) // Log webhook calls in webhooks category aLog := fmt.Sprintf("Organization webhook: %v triggered by user: %v by event: %v", toBeProcessedWebhook.PayloadURL, individual.Email, webhookEventType) - actionlog.LogOrgWebhookCalls(individual.Id.Hex(), individual.Email, webhookEventData.GetOrganisationID(), aLog) + actionlog.LogOrgWebhookCalls(individual.Id, individual.Email, webhookEventData.GetOrganisationID(), aLog) } } @@ -236,7 +236,7 @@ func TriggerConsentWebhookEvent(consentRecord daRecord.DataAgreementRecord, orga // Constructing webhook event data attribute consentRecordWebhookEvent := ConsentRecordWebhookEvent{ - ConsentRecordId: consentRecord.Id.Hex(), + ConsentRecordId: consentRecord.Id, DataAgreementId: consentRecord.DataAgreementId, DataAgreementRevisionId: consentRecord.DataAgreementRevisionId, DataAgreementRevisionHash: consentRecord.DataAgreementRevisionHash, diff --git a/internal/webhook_dispatcher/db.go b/internal/webhook_dispatcher/db.go index 2b101d4..011d1d9 100644 --- a/internal/webhook_dispatcher/db.go +++ b/internal/webhook_dispatcher/db.go @@ -18,11 +18,7 @@ func webhookDeliveryCollection() *mongo.Collection { } // GetWebhookByOrgID Gets a webhook by organisation ID and webhook ID -func GetWebhookByOrgID(webhookID, orgID string) (result Webhook, err error) { - webhookId, err := primitive.ObjectIDFromHex(webhookID) - if err != nil { - return result, err - } +func GetWebhookByOrgID(webhookId, orgID string) (result Webhook, err error) { err = webhookCollection().FindOne(context.TODO(), bson.M{"_id": webhookId, "orgid": orgID}).Decode(&result) @@ -32,11 +28,19 @@ func GetWebhookByOrgID(webhookID, orgID string) (result Webhook, err error) { // AddWebhookDelivery Adds payload delivery details to database for a webhook event func AddWebhookDelivery(webhookDelivery WebhookDelivery) (WebhookDelivery, error) { - if webhookDelivery.ID == primitive.NilObjectID { - webhookDelivery.ID = primitive.NewObjectID() + if webhookDelivery.ID == primitive.NilObjectID.Hex() { + webhookDelivery.ID = primitive.NewObjectID().Hex() } _, err := webhookDeliveryCollection().InsertOne(context.TODO(), &webhookDelivery) return webhookDelivery, err } + +// GetWebhookDeliveryByID Gets payload delivery details by ID +func GetWebhookDeliveryByID(webhookID string, webhookDeliveryId string) (result WebhookDelivery, err error) { + + err = webhookDeliveryCollection().FindOne(context.TODO(), bson.M{"webhookid": webhookID, "_id": webhookDeliveryId}).Decode(&result) + + return result, err +} diff --git a/internal/webhook_dispatcher/webhookdispatcher.go b/internal/webhook_dispatcher/webhookdispatcher.go index ef317e9..6cc47b9 100644 --- a/internal/webhook_dispatcher/webhookdispatcher.go +++ b/internal/webhook_dispatcher/webhookdispatcher.go @@ -20,11 +20,23 @@ import ( // WebhookEvent Webhook event wrapper type WebhookEvent struct { - DeliveryID string `json:"deliveryID"` // Webhook delivery ID - WebhookID string `json:"webhookID"` // Webhook endpoint ID - Timestamp string `json:"timestamp"` // UTC timestamp of webhook triggered data time - Data interface{} `json:"data"` // Event data attribute - Type string `json:"type"` // Event type for e.g. data.delete.initiated + DeliveryID string `json:"deliveryID"` // Webhook delivery ID + WebhookID string `json:"webhookID"` // Webhook endpoint ID + Timestamp string `json:"timestamp"` // UTC timestamp of webhook triggered data time + Data ConsentRecordWebhookEvent `json:"data"` // Event data attribute + Type string `json:"type"` // Event type for e.g. data.delete.initiated +} + +type ConsentRecordWebhookEvent struct { + ConsentRecordId string `json:"consentRecordId"` + DataAgreementId string `json:"dataAgreementId"` + DataAgreementRevisionId string `json:"dataAgreementRevisionId"` + DataAgreementRevisionHash string `json:"dataAgreementRevisionHash"` + IndividualId string `json:"individualId"` + OptIn bool `json:"optIn"` + State string `json:"state"` + SignatureId string `json:"signatureId"` + OrganisationId string `json:"organisationId"` } // Payload content type const @@ -69,12 +81,12 @@ type Webhook struct { // WebhookDelivery Details of payload delivery to webhook endpoint type WebhookDelivery struct { - ID primitive.ObjectID `bson:"_id,omitempty"` // Webhook delivery ID + ID string `bson:"_id,omitempty"` // Webhook delivery ID WebhookID string // Webhook ID UserID string // ID of user who triggered the webhook event WebhookEventType string // Webhook event type for e.g. data.delete.initiated RequestHeaders map[string][]string // HTTP headers posted to webhook endpoint - RequestPayload interface{} // JSON payload posted to webhook endpoint + RequestPayload WebhookEvent // JSON payload posted to webhook endpoint ResponseHeaders map[string][]string // HTTP response headers received from webhook endpoint ResponseBody string // HTTP response body received from webhook endpoint in bytes ResponseStatusCode int // HTTP response status code @@ -109,22 +121,18 @@ func ProcessWebhooks(webhookEventType string, value []byte) { // Webhook event data attribute // Converting data attribute to appropriate webhook event struct - webhookEventData, ok := webhookEvent.Data.(map[string]interface{}) - if !ok { - log.Printf("Invalid incoming webhook recieved !") - return - } + webhookEventData := webhookEvent.Data // Quick fix // Retrieving user and organisation ID from webhook data attribute - userID := webhookEventData["individualId"].(string) - orgID := webhookEventData["organisationId"].(string) + userID := webhookEventData.IndividualId + orgID := webhookEventData.OrganisationId log.Printf("Processing webhook:%s triggered by user:%s of org:%s for event:%s", webhookEvent.WebhookID, userID, orgID, webhookEventType) // Instantiating webhook delivery webhookDelivery = WebhookDelivery{ - ID: primitive.NewObjectID(), + ID: primitive.NewObjectID().Hex(), WebhookID: webhookEvent.WebhookID, UserID: userID, WebhookEventType: webhookEventType, @@ -148,7 +156,7 @@ func ProcessWebhooks(webhookEventType string, value []byte) { secretKey := webhook.SecretKey // Updating webhook event payload with delivery ID - webhookEvent.DeliveryID = webhookDelivery.ID.Hex() + webhookEvent.DeliveryID = webhookDelivery.ID // Constructing webhook payload bytes requestPayload, _ := json.Marshal(&webhookEvent)