New search for members + contacts on list + csv export
This commit is contained in:
parent
8044cb975a
commit
e6c698c98d
5 changed files with 133 additions and 271 deletions
|
|
@ -2,7 +2,6 @@ package controllers
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -11,9 +10,9 @@ import (
|
|||
"git.readonly.ch/bouzoure/pop-camarades/helpers"
|
||||
"git.readonly.ch/bouzoure/pop-camarades/helpers/database"
|
||||
"git.readonly.ch/bouzoure/pop-camarades/models"
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PersonValidation struct {
|
||||
|
|
@ -82,6 +81,16 @@ func Members(c *fiber.Ctx) error {
|
|||
db.Order("name collate nocase asc").Find(§ions, "contains_members = ? AND id IN ?", true, allowedSections)
|
||||
params.AllowedSections = allowedSections
|
||||
|
||||
// Security for active contacts
|
||||
if !permShow {
|
||||
params.Active = false
|
||||
}
|
||||
|
||||
// Security for archived contacts
|
||||
if !permShowArchived {
|
||||
params.Archive = false
|
||||
}
|
||||
|
||||
results, err := database.PeopleSearch(params)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -135,75 +144,37 @@ func MembersExport(c *fiber.Ctx) error {
|
|||
return err
|
||||
}
|
||||
|
||||
filterSection := c.Query("se")
|
||||
filterArchive := c.Query("a")
|
||||
filterSearch := c.Query("s")
|
||||
searchJSON := c.Query("s")
|
||||
var params database.PeopleSearchParams
|
||||
|
||||
sqlFilterSections := allowedSections
|
||||
sqlFilterAppend := "IS NULL"
|
||||
if filterArchive == "1" {
|
||||
sqlFilterAppend = "IS NOT NULL"
|
||||
sqlFilterSections = allowedSectionsArchived
|
||||
}
|
||||
|
||||
var filterSectionUint uint
|
||||
if len(filterSection) > 0 {
|
||||
filterSectionUint64, err := strconv.ParseUint(filterSection, 10, 0)
|
||||
if err == nil {
|
||||
for _, s := range sqlFilterSections {
|
||||
if s == uint(filterSectionUint64) {
|
||||
filterSectionUint = uint(filterSectionUint64)
|
||||
}
|
||||
}
|
||||
if len(searchJSON) > 0 {
|
||||
err = json.Unmarshal([]byte(searchJSON), ¶ms)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
searchJSON = ""
|
||||
}
|
||||
}
|
||||
|
||||
if filterSectionUint > 0 {
|
||||
sqlFilterAppend = fmt.Sprintf(
|
||||
"%s AND section_id = %d",
|
||||
sqlFilterAppend,
|
||||
filterSectionUint,
|
||||
)
|
||||
params.PageSize = 0
|
||||
params.PersonType = "members"
|
||||
|
||||
var sections []models.Section
|
||||
db.Order("name collate nocase asc").Find(§ions, "contains_members = ? AND id IN ?", true, allowedSections)
|
||||
params.AllowedSections = allowedSections
|
||||
|
||||
// Security for active contacts
|
||||
if !permShow {
|
||||
params.Active = false
|
||||
}
|
||||
|
||||
var sqlFilterSearch string
|
||||
if len(filterSearch) > 0 {
|
||||
sqlFilterSearch = fmt.Sprintf(
|
||||
"%%%s%%", filterSearch,
|
||||
)
|
||||
// Security for archived contacts
|
||||
if !permShowArchived {
|
||||
params.Archive = false
|
||||
}
|
||||
|
||||
var people []models.Person
|
||||
if len(filterSearch) > 0 {
|
||||
result := db.Unscoped().Order(
|
||||
"last_name collate nocase asc, first_name collate nocase asc",
|
||||
).Preload("Section").Find(
|
||||
&people,
|
||||
fmt.Sprintf(
|
||||
"is_member = ? AND section_id IN ? AND (first_name LIKE ? OR last_name LIKE ?) AND deleted_at %s",
|
||||
sqlFilterAppend,
|
||||
),
|
||||
true, sqlFilterSections, sqlFilterSearch, sqlFilterSearch,
|
||||
)
|
||||
|
||||
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
result := db.Unscoped().Order(
|
||||
"last_name collate nocase asc, first_name collate nocase asc",
|
||||
).Preload("Section").Find(
|
||||
&people,
|
||||
fmt.Sprintf(
|
||||
"is_member = ? AND section_id IN ? AND deleted_at %s",
|
||||
sqlFilterAppend,
|
||||
),
|
||||
true, sqlFilterSections,
|
||||
)
|
||||
|
||||
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
results, err := database.PeopleSearch(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var fields []models.Field
|
||||
|
|
@ -253,7 +224,7 @@ func MembersExport(c *fiber.Ctx) error {
|
|||
}
|
||||
}
|
||||
|
||||
for _, person := range people {
|
||||
for _, person := range results.Results {
|
||||
c.Writef("\"%s\";", person.FirstName)
|
||||
c.Writef("\"%s\";", person.LastName)
|
||||
c.Writef("\"%s\";", person.Email)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue