Beneficiaries export as XLSX
This commit is contained in:
63
servers/web/exports/beneficiaries.go
Normal file
63
servers/web/exports/beneficiaries.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package exports
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
xlsxrenderer "git.coopgo.io/coopgo-apps/parcoursmob/renderer/xlsx"
|
||||
"github.com/paulmach/orb/geojson"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (h *Handler) Beneficiaries(w http.ResponseWriter, r *http.Request) {
|
||||
archivedFilter := r.URL.Query().Get("archived") == "true"
|
||||
beneficiaryAddressGeo := r.URL.Query().Get("beneficiary_address_geo")
|
||||
|
||||
addressGeoLayer, addressGeoCode := "", ""
|
||||
if beneficiaryAddressGeo != "" {
|
||||
parts := strings.SplitN(beneficiaryAddressGeo, ":", 2)
|
||||
if len(parts) == 2 {
|
||||
addressGeoLayer, addressGeoCode = parts[0], parts[1]
|
||||
}
|
||||
}
|
||||
|
||||
result, err := h.applicationHandler.GetBeneficiaries(r.Context(), "", archivedFilter, addressGeoLayer, addressGeoCode)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get beneficiaries")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Resolve geographic layers (EPCI, Département, Région) for each beneficiary
|
||||
geoInfoMap := map[string]xlsxrenderer.BeneficiaryGeoInfo{}
|
||||
for _, account := range result.Accounts {
|
||||
if addr, ok := account.Data["address"]; ok {
|
||||
jsonAddr, err := json.Marshal(addr)
|
||||
if err == nil {
|
||||
addrFeature, err := geojson.UnmarshalFeature(jsonAddr)
|
||||
if err == nil && addrFeature.Geometry != nil {
|
||||
geo, err := h.services.Geography.GeoSearch(addrFeature)
|
||||
if err == nil {
|
||||
info := xlsxrenderer.BeneficiaryGeoInfo{}
|
||||
if commune, ok := geo["communes"]; ok {
|
||||
info.Commune = commune.Properties.MustString("nom")
|
||||
}
|
||||
if epci, ok := geo["epci"]; ok {
|
||||
info.EPCI = epci.Properties.MustString("nom")
|
||||
}
|
||||
if dept, ok := geo["departements"]; ok {
|
||||
info.Departement = dept.Properties.MustString("nom")
|
||||
}
|
||||
if region, ok := geo["regions"]; ok {
|
||||
info.Region = region.Properties.MustString("nom")
|
||||
}
|
||||
geoInfoMap[account.ID] = info
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h.renderer.XLSX.Beneficiaries(w, result.Accounts, geoInfoMap)
|
||||
}
|
||||
Reference in New Issue
Block a user