Skip to content

Commit

Permalink
Merge pull request #1245 from bcgov/dev-rook-RQ-FOIMOD-3551
Browse files Browse the repository at this point in the history
call foiflow api to update open info state for a request
  • Loading branch information
richard-aot authored Dec 24, 2024
2 parents 02d5a1d + 2b0948e commit 59a93eb
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 52 deletions.
45 changes: 36 additions & 9 deletions computingservices/OpenInfoServices/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@ var (
secretKey string
s3host string

env string

onceDB sync.Once
onceRedis sync.Once
onceS3 sync.Once
onceS3Path sync.Once
onceOthers sync.Once
//Keycloak
keycloakurl string
keycloakrealm string
keycloakclientid string
keycloakclientsecret string
keycloakuser string
keycloakpassword string

//Other
env string
foiflowapi string

onceDB sync.Once
onceRedis sync.Once
onceS3 sync.Once
onceS3Path sync.Once
onceKeycloak sync.Once
onceOthers sync.Once
)

// use viper package to read .env file
Expand Down Expand Up @@ -110,9 +121,19 @@ func loadConfigS3Path() {
}
}

func loadConfigKeycloak() {
keycloakurl = getEnv("KEYCLOAK_URL")
keycloakrealm = getEnv("KEYCLOAK_URL_REALM")
keycloakclientid = getEnv("KEYCLOAK_CLIENT_ID")
keycloakclientsecret = getEnv("KEYCLOAK_CLIENT_SECRET")
keycloakuser = getEnv("KEYCLOAK_USER")
keycloakpassword = getEnv("KEYCLOAK_PASS")
}

func loadConfigOther() {
env = getEnv("OI_S3_ENV")
queue = getEnv("OI_QUEUE_NAME")
foiflowapi = getEnv("FOIFLOW_BASE_API_URL")
}

// Helper function to get environment variables
Expand Down Expand Up @@ -148,8 +169,14 @@ func GetS3Path() (string, string, string, string, int) {
return s3url, oibucket, oiprefix, sitemapprefix, sitemaplimit
}

// GetKeycloak retrieves the Keycloak variables with lazy initialization
func GetKeycloak() (string, string, string, string, string, string) {
onceKeycloak.Do(loadConfigKeycloak) // Ensures loadConfig is called only once
return keycloakurl, keycloakrealm, keycloakclientid, keycloakclientsecret, keycloakuser, keycloakpassword
}

// GetS3 retrieves the S3 variables with lazy initialization
func GetOthers() (string, string) {
func GetOthers() (string, string, string) {
onceOthers.Do(loadConfigOther) // Ensures loadConfig is called only once
return env, queue
return env, queue, foiflowapi
}
2 changes: 1 addition & 1 deletion computingservices/OpenInfoServices/lib/awslib/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func ScanS3(openInfoBucket string, openInfoPrefix string, urlPrefix string, file

func CopyS3(sourceBucket string, sourcePrefix string, filemappings []AdditionalFile) {
s3url, oibucket, oiprefix, sitemapprefix, sitemaplimit = myconfig.GetS3Path()
env, _ := myconfig.GetOthers()
env, _, _ := myconfig.GetOthers()

// bucket := "dev-openinfopub"
bucket := sourceBucket
Expand Down
43 changes: 40 additions & 3 deletions computingservices/OpenInfoServices/lib/db/dbservices.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dbservice

import (
httpservice "OpenInfoServices/lib/http"
"database/sql"
"fmt"
"log"
Expand Down Expand Up @@ -40,6 +41,7 @@ type OpenInfoRecord struct {
Sitemap_pages string
Type string
Additionalfiles []AdditionalFile
Foirequestid int
}

func Conn(dsn string) (*sql.DB, error) {
Expand Down Expand Up @@ -109,6 +111,7 @@ func GetOIRecordsForPrePublishing(db *sql.DB) ([]OpenInfoRecord, error) {
SELECT
oi.foiopeninforequestid,
mr.foiministryrequestid,
r.foirequestid,
mr.axisrequestid,
mr.description,
oi.publicationdate,
Expand Down Expand Up @@ -148,7 +151,7 @@ func GetOIRecordsForPrePublishing(db *sql.DB) ([]OpenInfoRecord, error) {

oiRecordsMap := make(map[int]*OpenInfoRecord)
for rows.Next() {
var openinfoid, foiministryrequestid, additionalfileid sql.NullInt64
var openinfoid, foiministryrequestid, foirequestid, additionalfileid sql.NullInt64
var axisrequestid, description, published_date, contributor, applicant_type, bcgovcode, sitemap_pages, queuetype, filename, s3uripath sql.NullString
var fees sql.NullFloat64
var isactive bool
Expand All @@ -169,6 +172,7 @@ func GetOIRecordsForPrePublishing(db *sql.DB) ([]OpenInfoRecord, error) {
err := rows.Scan(
&openinfoid,
&foiministryrequestid,
&foirequestid,
&axisrequestid,
&description,
&published_date,
Expand All @@ -187,11 +191,12 @@ func GetOIRecordsForPrePublishing(db *sql.DB) ([]OpenInfoRecord, error) {
return records, fmt.Errorf("failed to retrieve query result for prepublish: %w", err)
}

if openinfoid.Valid && foiministryrequestid.Valid && axisrequestid.Valid && description.Valid && published_date.Valid && contributor.Valid && applicant_type.Valid && fees.Valid && bcgovcode.Valid && sitemap_pages.Valid && queuetype.Valid {
if openinfoid.Valid && foiministryrequestid.Valid && foirequestid.Valid && axisrequestid.Valid && description.Valid && published_date.Valid && contributor.Valid && applicant_type.Valid && fees.Valid && bcgovcode.Valid && sitemap_pages.Valid && queuetype.Valid {
if _, ok := oiRecordsMap[int(foiministryrequestid.Int64)]; !ok {
oiRecordsMap[int(foiministryrequestid.Int64)] = &OpenInfoRecord{
Openinfoid: int(openinfoid.Int64),
Foiministryrequestid: int(foiministryrequestid.Int64),
Foirequestid: int(foirequestid.Int64),
Axisrequestid: axisrequestid.String,
Description: description.String,
Published_date: published_date.String,
Expand Down Expand Up @@ -238,6 +243,7 @@ func GetOIRecordsForPublishing(db *sql.DB) ([]OpenInfoRecord, error) {
SELECT
oi.foiopeninforequestid,
mr.foiministryrequestid,
r.foirequestid,
mr.axisrequestid,
mr.description,
oi.publicationdate,
Expand Down Expand Up @@ -270,6 +276,7 @@ func GetOIRecordsForPublishing(db *sql.DB) ([]OpenInfoRecord, error) {
err := rows.Scan(
&record.Openinfoid,
&record.Foiministryrequestid,
&record.Foirequestid,
&record.Axisrequestid,
&record.Description,
&record.Published_date,
Expand Down Expand Up @@ -337,7 +344,7 @@ func GetOIRecordsForUnpublishing(db *sql.DB) ([]OpenInfoRecord, error) {
return records, nil
}

func UpdateOIRecordState(db *sql.DB, foiministryrequestid int, publishingstatus string, message string, sitemap_pages string) error {
func UpdateOIRecordState(db *sql.DB, foiflowapi string, foiministryrequestid int, foirequestid int, publishingstatus string, message string, sitemap_pages string, oistatusid int) error {

// Begin a transaction
tx, err := db.Begin()
Expand Down Expand Up @@ -380,9 +387,39 @@ func UpdateOIRecordState(db *sql.DB, foiministryrequestid int, publishingstatus
log.Fatalf("Error committing transaction: %v", err)
}

// Update request state in FOIMinistryRequests
// endpoint
section := "oistatusid"
endpoint := fmt.Sprintf("%s/foirequests/%d/ministryrequest/%d/section/%s", foiflowapi, foirequestid, foiministryrequestid, section)

// payload
payload := map[string]int{"oistatusid": oistatusid}
_, err = httpservice.HttpPost(endpoint, payload)

return err
}

func GetFoirequestID(db *sql.DB, foiministryrequestid int) (int, error) {

// Query to get foirequestid
var foirequestid int
query := `
SELECT foirequest_id
FROM public."FOIMinistryRequests"
WHERE foiministryrequestid=$1
ORDER BY version DESC
LIMIT 1
`

// Execute the query
err := db.QueryRow(query, foiministryrequestid).Scan(&foirequestid)
if err != nil {
return 0, fmt.Errorf("failed to retrieve foirequestid: %w", err)
}

return foirequestid, err
}

func LogError(db *sql.DB, foiministryrequestid int, publishingstatus string, message string) error {

// Begin a transaction
Expand Down
118 changes: 118 additions & 0 deletions computingservices/OpenInfoServices/lib/http/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package httpservice

import (
myconfig "OpenInfoServices/config"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)

type KeycloakTokenResponse struct {
AccessToken string `json:"access_token"`
}

func getBearerToken() (string, error) {
keycloakurl, keycloakrealm, keycloakclientid, keycloakclientsecret, keycloakuser, keycloakpassword := myconfig.GetKeycloak()

url := keycloakurl + "/auth/realms/" + keycloakrealm + "/protocol/openid-connect/token"
// data := "client_id=" + keycloakclientid + "&client_secret=" + keycloakclientsecret + "&grant_type=client_credentials"
data := fmt.Sprintf("client_id=%s&client_secret=%s&grant_type=password&username=%s&password=%s", keycloakclientid, keycloakclientsecret, keycloakuser, keycloakpassword)

req, err := http.NewRequest("POST", url, bytes.NewBufferString(data))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}

var tokenResponse KeycloakTokenResponse
err = json.Unmarshal(body, &tokenResponse)
if err != nil {
return "", err
}

return tokenResponse.AccessToken, nil
}

func HttpPost(endpoint string, payload map[string]int) ([]byte, error) {
bearerToken, err0 := getBearerToken()
if err0 != nil {
log.Fatalf("Failed to get Bearer token: %v", err0)
}

jsonPayload, err1 := json.Marshal(payload)
if err1 != nil {
return nil, err1
}

req, err2 := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonPayload))
if err2 != nil {
return nil, err2
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerToken))

client := &http.Client{}
resp, err3 := client.Do(req)
if err3 != nil {
return nil, err3
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return body, fmt.Errorf("received non-200 response code: %d", resp.StatusCode)
}

return body, nil
}

func HttpGet(endpoint string) ([]byte, error) {
bearerToken, err0 := getBearerToken()
if err0 != nil {
log.Fatalf("Failed to get Bearer token: %v", err0)
}
fmt.Printf("token: %s", bearerToken)

req, err1 := http.NewRequest("GET", endpoint, nil)
if err1 != nil {
return nil, err1
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerToken))

client := &http.Client{}
resp, err2 := client.Do(req)
if err2 != nil {
return nil, err2
}
defer resp.Body.Close()

body, err3 := io.ReadAll(resp.Body)
if err3 != nil {
return nil, err3
}

if resp.StatusCode != http.StatusOK {
return body, fmt.Errorf("received non-200 response code: %d", resp.StatusCode)
}

return body, nil
}
Loading

0 comments on commit 59a93eb

Please sign in to comment.