Add exports

This commit is contained in:
Arnaud Delcasse 2023-04-12 22:21:01 +02:00
parent bcddbc0d58
commit 8e2b0ada32
6 changed files with 367 additions and 960 deletions

32
go.mod
View File

@ -23,7 +23,7 @@ require (
github.com/spf13/viper v1.13.0
gitlab.scity.coop/maas/navitia-golang v0.0.0-20220429110621-5c22d6efdd0c
go.etcd.io/etcd/client/v3 v3.5.4
golang.org/x/image v0.0.0-20220722155232-062f8c9fd539
golang.org/x/image v0.5.0
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.1
@ -37,10 +37,11 @@ require (
git.coopgo.io/coopgo-platform/mobility-accounts v0.0.0-20230329105908-a76c0412a386
github.com/gorilla/securecookie v1.1.1
github.com/minio/minio-go/v7 v7.0.43
github.com/ory/viper v1.7.5
github.com/xuri/excelize/v2 v2.7.1
)
require (
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
@ -53,16 +54,12 @@ require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/gorilla/csrf v1.7.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/cpuid/v2 v2.1.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/goveralls v0.0.6 // indirect
github.com/mb0/wkt v0.0.0-20170420051526-a30afd545ee1 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
@ -71,29 +68,27 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/ory/fosite v0.42.2 // indirect
github.com/ory/go-acc v0.2.6 // indirect
github.com/ory/go-convenience v0.1.0 // indirect
github.com/ory/viper v1.7.5 // indirect
github.com/ory/x v0.0.214 // indirect
github.com/pborman/uuid v1.2.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pquerna/cachecontrol v0.1.0 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/rs/xid v1.4.0 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.0.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/tidwall/pretty v1.1.0 // indirect
github.com/twpayne/go-geom v1.3.6 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.etcd.io/etcd/api/v3 v3.5.4 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.4 // indirect
@ -101,12 +96,11 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a // indirect
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.2 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect

983
go.sum

File diff suppressed because it is too large Load Diff

137
handlers/exports/agenda.go Normal file
View File

@ -0,0 +1,137 @@
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"
"github.com/xuri/excelize/v2"
)
func (h *ExportsHandler) Agenda(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
}
events := []agendastorage.Event{}
groupids := []string{}
beneficiaries_ids := []string{}
for _, e := range resp.Events {
groupids = append(groupids, e.Owners...)
events = append(events, e.ToStorageType())
for _, subscriptions := range e.Subscriptions {
beneficiaries_ids = append(beneficiaries_ids, subscriptions.Subscriber)
}
}
sort.Sort(sorting.EventsByStartdate(events))
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()
}
/////////////// Generate file
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")
f.SetCellValue("Sheet1", "I1", "Gestionnaire événement")
// 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]]
subscribedbygroup := ""
subscribedbyuser := ""
if v, ok := e.Data["subscribed_by"].(map[string]any); ok {
if v2, ok := v["group"].(map[string]any); ok {
if v3, ok := v2["id"].(string); ok {
subscribedbygroup = v3
}
}
if v4, ok := v["user"].(map[string]any); ok {
if v5, ok := v4["display_name"].(string); ok {
subscribedbyuser = v5
}
}
}
for _, s := range e.Subscriptions {
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"])
f.SetCellValue("Sheet1", fmt.Sprintf("G%d", i), groups[subscribedbygroup].Data["name"])
f.SetCellValue("Sheet1", fmt.Sprintf("H%d", i), subscribedbyuser)
f.SetCellValue("Sheet1", fmt.Sprintf("I%d", i), admin.Data["name"])
i = i + 1
}
}
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")
f.Write(w)
}

View File

@ -0,0 +1,21 @@
package exports
import (
"git.coopgo.io/coopgo-apps/parcoursmob/services"
"git.coopgo.io/coopgo-platform/emailing"
"github.com/spf13/viper"
)
type ExportsHandler struct {
config *viper.Viper
services *services.ServicesHandler
emailing *emailing.Mailer
}
func NewExportsHandler(cfg *viper.Viper, svc *services.ServicesHandler, emailing *emailing.Mailer) (*ExportsHandler, error) {
return &ExportsHandler{
config: cfg,
services: svc,
emailing: emailing,
}, nil
}

145
handlers/exports/fleets.go Normal file
View File

@ -0,0 +1,145 @@
package exports
import (
"context"
"fmt"
"net/http"
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
fleetsstorage "git.coopgo.io/coopgo-platform/fleets/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"
"github.com/xuri/excelize/v2"
)
func (h *ExportsHandler) Bookings(w http.ResponseWriter, r *http.Request) {
vehicles := map[string]fleetsstorage.Vehicle{}
bookings := []fleetsstorage.Booking{}
reequest := &fleets.GetVehiclesRequest{
Namespaces: []string{"parcoursmob"},
}
reesp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), reequest)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
beneficiaries_ids := []string{}
for _, vehicle := range reesp.Vehicles {
v := vehicle.ToStorageType()
fmt.Println(v)
for _, b := range v.Bookings {
bookings = append(bookings, b)
beneficiaries_ids = append(beneficiaries_ids, b.Driver)
}
vehicles[vehicle.Id] = v
}
groups := map[string]groupsstorage.Group{}
admingroups, err := h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), &groupsmanagement.GetGroupsRequest{
Namespaces: []string{"parcoursmob_organizations"},
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
for _, g := range admingroups.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()
}
/////////////// Generate file
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
f.SetCellValue("Sheet1", "A1", "Numéro")
f.SetCellValue("Sheet1", "B1", "Type")
f.SetCellValue("Sheet1", "C1", "Gestionnaire")
f.SetCellValue("Sheet1", "D1", "Prescripteur")
f.SetCellValue("Sheet1", "E1", "Bénéficiaire")
f.SetCellValue("Sheet1", "F1", "Numéro allocataire / Pole emploi")
f.SetCellValue("Sheet1", "G1", "Début de Mise à disposition")
f.SetCellValue("Sheet1", "H1", "Fin de mise à disposition")
f.SetCellValue("Sheet1", "I1", "Début indisponibilité")
f.SetCellValue("Sheet1", "J1", "Fin indisponibilité")
f.SetCellValue("Sheet1", "K1", "Retrait par le gestionnaire")
f.SetCellValue("Sheet1", "L1", "Commentaire")
i := 2
for _, b := range bookings {
vehicle := vehicles[b.Vehicleid]
if len(vehicle.Administrators) == 0 {
continue
}
admin := groups[vehicle.Administrators[0]]
bookedby := ""
if v, ok := b.Data["booked_by"].(map[string]any); ok {
if v2, ok := v["group"].(map[string]any); ok {
if v3, ok := v2["name"].(string); ok {
bookedby = v3
}
}
}
beneficiary := beneficiaries_map[b.Driver]
adminunavailability := false
if av, ok := b.Data["administrator_unavailability"].(bool); ok && av {
adminunavailability = true
}
f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i), vehicle.Data["licence_plate"])
f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i), vehicle.Type)
f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i), admin.Data["name"])
f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i), bookedby)
f.SetCellValue("Sheet1", fmt.Sprintf("E%d", i), fmt.Sprintf("%v %v", beneficiary.Data["first_name"], beneficiary.Data["last_name"]))
f.SetCellValue("Sheet1", fmt.Sprintf("F%d", i), beneficiary.Data["file_number"])
f.SetCellValue("Sheet1", fmt.Sprintf("G%d", i), b.Startdate.Format("2006-01-02"))
f.SetCellValue("Sheet1", fmt.Sprintf("H%d", i), b.Enddate.Format("2006-01-02"))
f.SetCellValue("Sheet1", fmt.Sprintf("I%d", i), b.Unavailablefrom.Format("2006-01-02"))
f.SetCellValue("Sheet1", fmt.Sprintf("J%d", i), b.Unavailableto.Format("2006-01-02"))
f.SetCellValue("Sheet1", fmt.Sprintf("K%d", i), adminunavailability)
f.SetCellValue("Sheet1", fmt.Sprintf("L%d", i), b.Data["comment"])
i = i + 1
}
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")
f.Write(w)
}

View File

@ -9,6 +9,7 @@ import (
"git.coopgo.io/coopgo-apps/parcoursmob/handlers/api"
"git.coopgo.io/coopgo-apps/parcoursmob/handlers/application"
"git.coopgo.io/coopgo-apps/parcoursmob/handlers/auth"
"git.coopgo.io/coopgo-apps/parcoursmob/handlers/exports"
"git.coopgo.io/coopgo-apps/parcoursmob/renderer"
"git.coopgo.io/coopgo-apps/parcoursmob/services"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
@ -53,6 +54,7 @@ func main() {
apiHandler, _ := api.NewAPIHandler(cfg, idp, svc, kv)
applicationHandler, _ := application.NewApplicationHandler(cfg, svc, kv, filestorage, emailing)
exportsHandler, _ := exports.NewExportsHandler(cfg, svc, emailing)
authHandler, _ := auth.NewAuthHandler(cfg, idp, svc, kv, emailing)
fmt.Println("Running", service_name, ":")
@ -158,6 +160,13 @@ func main() {
application.HandleFunc("/agenda/{eventid}/{subscribeid}/delete", applicationHandler.AgendaDeleteSubscribeEvent)
application.HandleFunc("/agenda/{eventid}/history", applicationHandler.AgendaHistoryEvent)
/////////////////////////////////////////////////////////////////////////////////////////////////////
export := r.PathPrefix("/exports").Subrouter()
export.HandleFunc("/fleets/bookings", exportsHandler.Bookings)
export.HandleFunc("/agenda/subscriptions", exportsHandler.Agenda)
export.Use(idp.Middleware)
export.Use(idp.GroupsMiddleware)
fmt.Println("-> HTTP server listening on", address)
srv := &http.Server{