add export agenda by Salim
This commit is contained in:
parent
4fe479c1fb
commit
ddb08cf671
|
@ -3,9 +3,6 @@ package exports
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"sort"
|
|
||||||
|
|
||||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
|
||||||
agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi"
|
agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi"
|
||||||
agendastorage "git.coopgo.io/coopgo-platform/agenda/storage"
|
agendastorage "git.coopgo.io/coopgo-platform/agenda/storage"
|
||||||
|
@ -13,14 +10,19 @@ import (
|
||||||
groupsstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
|
groupsstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||||
accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
||||||
accountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
accountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
"github.com/xuri/excelize/v2"
|
"github.com/xuri/excelize/v2"
|
||||||
|
"net/http"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
|
func (h *ExportsHandler) Agenda(filter string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch filter {
|
||||||
|
case "allEvents":
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
resp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
|
resp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
|
||||||
Namespaces: []string{"parcoursmob_dispositifs"},
|
Namespaces: []string{"parcoursmob_dispositifs"},
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
@ -67,15 +69,70 @@ func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
|
||||||
beneficiaries_map[ben.Id] = ben.ToStorageType()
|
beneficiaries_map[ben.Id] = ben.ToStorageType()
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////// Generate file
|
f := h.generateExcel(events, groups, beneficiaries_map)
|
||||||
|
|
||||||
|
h.writeFileResponse(f, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "oneEvent":
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
eventId := vars["eventid"]
|
||||||
|
resp, err := h.services.GRPC.Agenda.GetEvent(context.TODO(), &agenda.GetEventRequest{
|
||||||
|
Id: eventId,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
groupids := []string{}
|
||||||
|
beneficiaries_ids := []string{}
|
||||||
|
groupids = append(groupids, resp.Event.Owners...)
|
||||||
|
for _, subscriptions := range resp.Event.Subscriptions {
|
||||||
|
beneficiaries_ids = append(beneficiaries_ids, subscriptions.Subscriber)
|
||||||
|
}
|
||||||
|
groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
|
||||||
|
Groupids: groupids,
|
||||||
|
})
|
||||||
|
groups := map[string]groupsstorage.Group{}
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
for _, g := range groupsresp.Groups {
|
||||||
|
groups[g.Id] = g.ToStorageType()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
beneficiaries, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), &accounts.GetAccountsBatchRequest{
|
||||||
|
Accountids: beneficiaries_ids,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
beneficiaries_map := map[string]accountsstorage.Account{}
|
||||||
|
for _, ben := range beneficiaries.Accounts {
|
||||||
|
beneficiaries_map[ben.Id] = ben.ToStorageType()
|
||||||
|
}
|
||||||
|
|
||||||
|
f := h.generateExcel([]agendastorage.Event{resp.Event.ToStorageType()}, groups, beneficiaries_map)
|
||||||
|
h.writeFileResponse(f, w)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ExportsHandler) generateExcel(events []agendastorage.Event, groups map[string]groupsstorage.Group,
|
||||||
|
beneficiaries_map map[string]accountsstorage.Account) *excelize.File {
|
||||||
f := excelize.NewFile()
|
f := excelize.NewFile()
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := f.Close(); err != nil {
|
if err := f.Close(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
f.SetCellValue("Sheet1", "A1", "Evénement")
|
f.SetCellValue("Sheet1", "A1", "Evénement")
|
||||||
f.SetCellValue("Sheet1", "B1", "Date de début")
|
f.SetCellValue("Sheet1", "B1", "Date de début")
|
||||||
f.SetCellValue("Sheet1", "C1", "Date de fin")
|
f.SetCellValue("Sheet1", "C1", "Date de fin")
|
||||||
|
@ -85,8 +142,6 @@ func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
|
||||||
f.SetCellValue("Sheet1", "G1", "Prescipteur")
|
f.SetCellValue("Sheet1", "G1", "Prescipteur")
|
||||||
f.SetCellValue("Sheet1", "H1", "Prescipteur Nom")
|
f.SetCellValue("Sheet1", "H1", "Prescipteur Nom")
|
||||||
f.SetCellValue("Sheet1", "I1", "Gestionnaire événement")
|
f.SetCellValue("Sheet1", "I1", "Gestionnaire événement")
|
||||||
// f.SetCellValue("Sheet1", "I1", "Prescripteur téléphone")
|
|
||||||
|
|
||||||
i := 2
|
i := 2
|
||||||
for _, e := range events {
|
for _, e := range events {
|
||||||
if len(e.Owners) == 0 {
|
if len(e.Owners) == 0 {
|
||||||
|
@ -128,10 +183,14 @@ func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return f
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *ExportsHandler) writeFileResponse(file *excelize.File, w http.ResponseWriter) {
|
||||||
w.Header().Set("Content-Type", "application/octet-stream")
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
w.Header().Set("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
|
w.Header().Set("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
|
||||||
w.Header().Set("Content-Transfer-Encoding", "binary")
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
||||||
w.Header().Set("Expires", "0")
|
w.Header().Set("Expires", "0")
|
||||||
f.Write(w)
|
file.Write(w)
|
||||||
}
|
}
|
||||||
|
|
4
main.go
4
main.go
|
@ -166,7 +166,9 @@ func main() {
|
||||||
|
|
||||||
export := r.PathPrefix("/exports").Subrouter()
|
export := r.PathPrefix("/exports").Subrouter()
|
||||||
export.HandleFunc("/fleets/bookings", exportsHandler.Bookings)
|
export.HandleFunc("/fleets/bookings", exportsHandler.Bookings)
|
||||||
export.HandleFunc("/agenda/subscriptions", exportsHandler.Agenda)
|
export.HandleFunc("/fleets/bookings/{groupid}", exportsHandler.Bookings)
|
||||||
|
export.HandleFunc("/agenda/subscriptions", exportsHandler.Agenda("allEvents"))
|
||||||
|
export.HandleFunc("/agenda/{eventid}", exportsHandler.Agenda("oneEvent"))
|
||||||
export.Use(idp.Middleware)
|
export.Use(idp.Middleware)
|
||||||
export.Use(idp.GroupsMiddleware)
|
export.Use(idp.GroupsMiddleware)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue