2023-04-12 20:21:01 +00:00
|
|
|
package exports
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
|
|
|
|
agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi"
|
|
|
|
agendastorage "git.coopgo.io/coopgo-platform/agenda/storage"
|
|
|
|
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
|
|
|
groupsstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
|
|
|
|
accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
|
|
|
accountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
2023-06-01 12:11:03 +00:00
|
|
|
"github.com/gorilla/mux"
|
2023-04-12 20:21:01 +00:00
|
|
|
"github.com/xuri/excelize/v2"
|
|
|
|
)
|
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
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{
|
|
|
|
Namespaces: []string{"parcoursmob_dispositifs"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
events := []agendastorage.Event{}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
groupids := []string{}
|
|
|
|
beneficiaries_ids := []string{}
|
|
|
|
for _, e := range resp.Events {
|
|
|
|
groupids = append(groupids, e.Owners...)
|
|
|
|
events = append(events, e.ToStorageType())
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
for _, subscriptions := range e.Subscriptions {
|
|
|
|
beneficiaries_ids = append(beneficiaries_ids, subscriptions.Subscriber)
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
sort.Sort(sorting.EventsByStartdate(events))
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
|
|
|
|
Groupids: groupids,
|
|
|
|
})
|
|
|
|
groups := map[string]groupsstorage.Group{}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
if err == nil {
|
|
|
|
for _, g := range groupsresp.Groups {
|
|
|
|
groups[g.Id] = g.ToStorageType()
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
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
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
beneficiaries_map := map[string]accountsstorage.Account{}
|
|
|
|
for _, ben := range beneficiaries.Accounts {
|
|
|
|
beneficiaries_map[ben.Id] = ben.ToStorageType()
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
f := h.generateExcel(events, groups, beneficiaries_map)
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
h.writeFileResponse(f, w)
|
2023-04-12 20:21:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
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()
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
f := h.generateExcel([]agendastorage.Event{resp.Event.ToStorageType()}, groups, beneficiaries_map)
|
|
|
|
h.writeFileResponse(f, w)
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
func (h *ExportsHandler) generateExcel(events []agendastorage.Event, groups map[string]groupsstorage.Group,
|
|
|
|
beneficiaries_map map[string]accountsstorage.Account) *excelize.File {
|
2023-04-12 20:21:01 +00:00
|
|
|
f := excelize.NewFile()
|
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
f.SetCellValue("Sheet1", "A1", "Evénement")
|
|
|
|
f.SetCellValue("Sheet1", "B1", "Date de début")
|
|
|
|
f.SetCellValue("Sheet1", "C1", "Date de fin")
|
|
|
|
f.SetCellValue("Sheet1", "D1", "Nom bénéficiaire")
|
|
|
|
f.SetCellValue("Sheet1", "E1", "Prenom bénéficiaire")
|
|
|
|
f.SetCellValue("Sheet1", "F1", "Numéro allocataire / Pole emploi")
|
|
|
|
f.SetCellValue("Sheet1", "G1", "Prescipteur")
|
|
|
|
f.SetCellValue("Sheet1", "H1", "Prescipteur Nom")
|
2023-11-27 06:26:21 +00:00
|
|
|
f.SetCellValue("Sheet1", "I1", "Prescipteur Email")
|
|
|
|
f.SetCellValue("Sheet1", "J1", "Gestionnaire événement")
|
2023-04-12 20:21:01 +00:00
|
|
|
// f.SetCellValue("Sheet1", "I1", "Prescripteur téléphone")
|
|
|
|
|
|
|
|
i := 2
|
|
|
|
for _, e := range events {
|
|
|
|
if len(e.Owners) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
admin := groups[e.Owners[0]]
|
|
|
|
|
2023-11-27 06:26:21 +00:00
|
|
|
for _, s := range e.Subscriptions {
|
|
|
|
subscribedbygroup := ""
|
|
|
|
subscribedbyuser := ""
|
|
|
|
subscribedbyemail := ""
|
|
|
|
if v, ok := s.Data["subscribed_by"].(map[string]any); ok {
|
|
|
|
if v2, ok := v["group"].(map[string]any); ok {
|
|
|
|
if v3, ok := v2["name"].(string); ok {
|
|
|
|
subscribedbygroup = v3
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
|
|
|
}
|
2023-11-27 06:26:21 +00:00
|
|
|
if v4, ok := v["user"].(map[string]any); ok {
|
|
|
|
if v5, ok := v4["display_name"].(string); ok {
|
|
|
|
subscribedbyuser = v5
|
|
|
|
}
|
|
|
|
if v6, ok := v4["email"].(string); ok {
|
|
|
|
subscribedbyemail = v6
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-11-27 06:26:21 +00:00
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
beneficiary := beneficiaries_map[s.Subscriber]
|
|
|
|
|
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i), e.Name)
|
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i), e.Startdate.Format("2006-01-02"))
|
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i), e.Enddate.Format("2006-01-02"))
|
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i), beneficiary.Data["last_name"])
|
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("E%d", i), beneficiary.Data["first_name"])
|
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("F%d", i), beneficiary.Data["file_number"])
|
2023-11-27 06:26:21 +00:00
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("G%d", i), subscribedbygroup)
|
2023-04-12 20:21:01 +00:00
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("H%d", i), subscribedbyuser)
|
2023-11-27 06:26:21 +00:00
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("I%d", i), subscribedbyemail)
|
|
|
|
f.SetCellValue("Sheet1", fmt.Sprintf("J%d", i), admin.Data["name"])
|
2023-04-12 20:21:01 +00:00
|
|
|
i = i + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-06-01 12:11:03 +00:00
|
|
|
return f
|
|
|
|
|
|
|
|
}
|
2023-04-12 20:21:01 +00:00
|
|
|
|
2023-06-01 12:11:03 +00:00
|
|
|
func (h *ExportsHandler) writeFileResponse(file *excelize.File, w http.ResponseWriter) {
|
2023-04-12 20:21:01 +00:00
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
|
|
|
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
|
|
|
w.Header().Set("Expires", "0")
|
2023-06-01 12:11:03 +00:00
|
|
|
file.Write(w)
|
2023-04-12 20:21:01 +00:00
|
|
|
}
|