Skip to content

Commit

Permalink
Add #190 Align APIs to GovStack: Change URL paths for config endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa committed Oct 4, 2023
1 parent a6a7491 commit bd15465
Show file tree
Hide file tree
Showing 65 changed files with 3,311 additions and 26 deletions.
14 changes: 12 additions & 2 deletions src/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ const (

// Application mode
const (
SingleTenat = "single-tenant"
MultiTenant = "multi-tenant"
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
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

0 comments on commit bd15465

Please sign in to comment.