Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Feature/189 align apis to govstack #194

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/config
11 changes: 6 additions & 5 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ type Configuration struct {
UserName string
Password string
}
Iam Iam
Twilio Twilio
Firebase Firebase
Smtp SmtpConfig
Webhooks WebhooksConfig
ApplicationMode string
Iam Iam
Twilio Twilio
Firebase Firebase
Smtp SmtpConfig
Webhooks WebhooksConfig
}

// Load the config file
Expand Down
16 changes: 16 additions & 0 deletions src/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,19 @@ const (
ContentTypeImage = "image/jpeg"
ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"
)

// Application mode
const (
SingleTenant = "single-tenant"
MultiTenant = "multi-tenant"
)

// All http path url variables
const (
OrganizationId = "organizationId"
DataAgreementId = "dataAgreementId"
DataAttributeId = "dataAttributeId"
WebhookId = "webhookId"
WebhookDeliveryId = "deliveryId"
PolicyId = "policyId"
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"time"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import "github.com/bb-consent/api/src/org"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"bytes"
Expand Down Expand Up @@ -134,6 +134,16 @@ func GetOrganizationByID(w http.ResponseWriter, r *http.Request) {
w.Write(response)
}

// GetOrganizationId Gets an organization Id.
func GetOrganizationId() (string, error) {
org, err := org.GetOrganization()
if err != nil {
log.Printf("Failed to get organization")
return "", err
}
return org.ID.Hex(), err
}

type orgUpdateReq struct {
Name string
Location string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package handlerv1

import (
"encoding/json"
Expand Down
126 changes: 126 additions & 0 deletions src/handlerv2/adddataagreement_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package handlerv2

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/asaskevich/govalidator"
"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
"github.com/bb-consent/api/src/org"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type purpose struct {
Name string `valid:"required"`
Description string `valid:"required"`
LawfulBasisOfProcessing int
PolicyURL string `valid:"required"`
AttributeType int
Jurisdiction string
Disclosure string
IndustryScope string
DataRetention org.DataRetention
Restriction string
Shared3PP bool
SSIID string
}

type addPurposeReq struct {
purpose
}

// Check if the lawful usage ID provided is valid
func isValidLawfulBasisOfProcessing(lawfulBasis int) bool {
isFound := false
for _, lawfulBasisOfProcessingMapping := range org.LawfulBasisOfProcessingMappings {
if lawfulBasisOfProcessingMapping.ID == lawfulBasis {
isFound = true
break
}
}

return isFound
}

// Fetch the lawful usage based on the lawful basis ID
func getLawfulUsageByLawfulBasis(lawfulBasis int) bool {
if lawfulBasis == org.ConsentBasis {
return false
} else {
return true
}
}

// AddDataAgreement Adds a single data agreement to the organization
func AddDataAgreement(w http.ResponseWriter, r *http.Request) {
organizationID := r.Header.Get(config.OrganizationId)

o, err := org.Get(organizationID)
if err != nil {
m := fmt.Sprintf("Failed to fetch organization: %v", organizationID)
common.HandleError(w, http.StatusInternalServerError, m, err)
return
}

var pReq addPurposeReq
b, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()

json.Unmarshal(b, &pReq)

// validating request payload
valid, err := govalidator.ValidateStruct(pReq)
if !valid {
log.Printf("Missing mandatory fields for a adding consent purpose to org")
common.HandleError(w, http.StatusBadRequest, err.Error(), err)
return
}

// Proceed if lawful basis of processing provided is valid
if !isValidLawfulBasisOfProcessing(pReq.LawfulBasisOfProcessing) {
m := fmt.Sprintf("Invalid lawful basis of processing provided")
common.HandleError(w, http.StatusBadRequest, m, err)
return
}

tempLawfulUsage := getLawfulUsageByLawfulBasis(pReq.LawfulBasisOfProcessing)

newPurpose := org.Purpose{
ID: primitive.NewObjectID().Hex(),
Name: pReq.Name,
Description: pReq.Description,
LawfulUsage: tempLawfulUsage,
LawfulBasisOfProcessing: pReq.LawfulBasisOfProcessing,
PolicyURL: pReq.PolicyURL,
AttributeType: pReq.AttributeType,
Jurisdiction: pReq.Jurisdiction,
Disclosure: pReq.Disclosure,
IndustryScope: pReq.IndustryScope,
DataRetention: pReq.DataRetention,
Restriction: pReq.Restriction,
Shared3PP: pReq.Shared3PP,
SSIID: pReq.SSIID,
}
o.Purposes = append(o.Purposes, newPurpose)

_, err = org.UpdatePurposes(o.ID.Hex(), o.Purposes)
if err != nil {
m := fmt.Sprintf("Failed to update purpose to organization: %v", o.Name)
common.HandleError(w, http.StatusInternalServerError, m, err)
return
}
/*
u, err := user.Get(token.GetUserID(r))
if err != nil {
//notifications.SendPurposeUpdateNotification(u, o.ID.Hex(), )
}
*/
response, _ := json.Marshal(newPurpose)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(response)
}
81 changes: 81 additions & 0 deletions src/handlerv2/adddataattribute_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package handlerv2

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/asaskevich/govalidator"
"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
"github.com/bb-consent/api/src/org"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type template struct {
Consent string `valid:"required"`
PurposeIDs []string `valid:"required"`
}
type templateReq struct {
Templates []template
}

// AddDataAttribute Adds an organization data attribute
func AddDataAttribute(w http.ResponseWriter, r *http.Request) {
organizationID := r.Header.Get(config.OrganizationId)

o, err := org.Get(organizationID)
if err != nil {
m := fmt.Sprintf("Failed to fetch organization: %v", organizationID)
common.HandleError(w, http.StatusInternalServerError, m, err)
return
}

var tReq templateReq
b, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()

json.Unmarshal(b, &tReq)

// validating request payload
valid, err := govalidator.ValidateStruct(tReq)
if !valid {
log.Printf("Missing mandatory fields for adding consent template to org: %v", organizationID)
common.HandleError(w, http.StatusBadRequest, err.Error(), err)
return
}

// validating purposeIDs provided
for _, t := range tReq.Templates {
// checking if purposeID provided exist in the org
for _, p := range t.PurposeIDs {
_, err = org.GetPurpose(organizationID, p)
if err != nil {
m := fmt.Sprintf("Invalid purposeID:%v provided;Failed to update templates to organization: %v", p, o.Name)
common.HandleError(w, http.StatusBadRequest, m, err)
return
}
}

// Appending the new template to existing org templates
o.Templates = append(o.Templates, org.Template{
ID: primitive.NewObjectID().Hex(),
Consent: t.Consent,
PurposeIDs: t.PurposeIDs,
})
}

orgResp, err := org.UpdateTemplates(o.ID.Hex(), o.Templates)
if err != nil {
m := fmt.Sprintf("Failed to update templates to organization: %v", o.Name)
common.HandleError(w, http.StatusNotFound, m, err)
return
}

response, _ := json.Marshal(organization{orgResp})
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.WriteHeader(http.StatusCreated)
w.Write(response)
}
Loading