Skip to content

Commit

Permalink
Fix #573 CSV file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 21, 2023
1 parent 3eff808 commit 9863cee
Showing 1 changed file with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package individual
import (
"encoding/csv"
"fmt"
"io"
"log"
"net/http"
"strings"

"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
Expand All @@ -29,29 +31,73 @@ func ConfigCreateIndividualsInBulk(w http.ResponseWriter, r *http.Request) {

reader := csv.NewReader(file)

// Read the header row
header, err := reader.Read()
if err != nil {
m := "Failed to read CSV header"
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
}

if len(header) == 2 {
// Validate header columns
expectedColumns := []string{"Name", "Email"}
for i, col := range expectedColumns {
if header[i] != col {
m := fmt.Sprintf("Invalid column name in CSV header. Expected %s, got %s", col, header[i])
common.HandleErrorV2(w, http.StatusBadRequest, m, nil)
return
}
}
} else if len(header) == 3 {
// Validate header columns
expectedColumns := []string{"Name", "Email", "Phone"}
for i, col := range expectedColumns {
if header[i] != col {
m := fmt.Sprintf("Invalid column name in CSV header. Expected %s, got %s", col, header[i])
common.HandleErrorV2(w, http.StatusBadRequest, m, nil)
return
}
}
} else {
m := "Invalid number of columns in CSV header"
common.HandleErrorV2(w, http.StatusBadRequest, m, nil)
return
}

var individuals []individual.Individual

// Read and parse the CSV data
for {
record, err := reader.Read()
if err != nil {
if err == io.EOF {
break
} else if err != nil {
m := "Error reading CSV data"
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
}

if len(record) != 3 {
if len(record) != 2 && len(record) != 3 {
fmt.Println("Invalid CSV format")
continue
}

individual := individual.Individual{
Name: record[0],
Email: record[1],
Phone: record[2],
}

if len(record) == 3 {
individual.Phone = record[2]
}

individuals = append(individuals, individual)

}
go createIndividuals(individuals, organisationId)
if len(individuals) > 0 {
go createIndividuals(individuals, organisationId)
}

w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.WriteHeader(http.StatusOK)
Expand All @@ -60,18 +106,18 @@ func ConfigCreateIndividualsInBulk(w http.ResponseWriter, r *http.Request) {
func createIndividuals(individuals []individual.Individual, organisationId string) {
// Repository
individualRepo := individual.IndividualRepository{}
individualRepo.Init(organisationId)

for _, individual := range individuals {
// Check if individual exists If exist update individual
// If individual doesn't exist, create individual
individualRepo.Init(organisationId)
u, err := individualRepo.GetIndividualByEmail(individual.Email)
if err != nil {
if err == mongo.ErrNoDocuments {
// Register individual to keycloak
iamId, err := iam.RegisterUser(individual.Email, individual.Name)
if err != nil {
log.Printf("Unable to register individual to keycloak: %v", u.Name)
log.Printf("Unable to register individual to keycloak: %v", individual.Name)
}

individual.IsDeleted = false
Expand All @@ -83,7 +129,7 @@ func createIndividuals(individuals []individual.Individual, organisationId strin
log.Printf("Unable to save individual in db: %v", individual.Email)
}
} else {
log.Printf("Unable to fetchindividual in db: %v", individual.Name)
log.Printf("Unable to fetch individual in db: %v", individual.Name)
}
} else {
// Update individual in keycloak
Expand All @@ -92,7 +138,9 @@ func createIndividuals(individuals []individual.Individual, organisationId strin
log.Printf("Unable to update individual to keycloak: %v", u.IamId)
}
u.Name = individual.Name
u.Phone = individual.Phone
if len(strings.TrimSpace(individual.Phone)) > 0 {
u.Phone = individual.Phone
}
// Update individual to db
_, err = individualRepo.Update(u)
if err != nil {
Expand Down

0 comments on commit 9863cee

Please sign in to comment.