Skip to content

Commit

Permalink
correctif pour les utilisateurs non wekan (#179)
Browse files Browse the repository at this point in the history
Co-authored-by: Christophe Ninucci <[email protected]>
  • Loading branch information
chrnin and Christophe Ninucci authored Jun 9, 2023
1 parent 01b9d94 commit 0389d8b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 53 deletions.
57 changes: 40 additions & 17 deletions pkg/core/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,24 @@ func (kanbanExport KanbanExport) docx(head ExportHeader) (Docx, error) {
if len(outErr.Bytes()) > 0 {
fmt.Println(outErr.String())
}
return Docx{
filename: fmt.Sprintf("%s-%s-%s.docx",

var filename string
if kanbanExport.Board == "" {
filename = fmt.Sprintf("ETABLISSEMENT-%s-%s.docx",
strings.Replace(kanbanExport.RaisonSociale, " ", "-", -1),
kanbanExport.Siret,
)
} else {
filename = fmt.Sprintf("%s-%s-%s.docx",
strings.Replace(kanbanExport.Board, " ", "-", -1),
strings.Replace(kanbanExport.RaisonSociale, " ", "-", -1),
kanbanExport.Siret,
),
data: file,
)
}

return Docx{
filename: filename,
data: file,
}, nil
}

Expand Down Expand Up @@ -183,14 +194,19 @@ func getXLSXFollowedByCurrentUser(c *gin.Context) {
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
}
var ok bool
params.User, ok = kanban.GetUser(libwekan.Username(s.Username))
if !ok {
c.JSON(http.StatusForbidden, "le nom d'utilisateur n'est pas reconnu")
return
if s.hasRole("wekan") {
var ok bool
params.User, ok = kanban.GetUser(libwekan.Username(s.Username))
if !ok {
c.JSON(500, "utilisateur non reconnu")
return
}
params.BoardIDs = kanban.ClearBoardIDs(params.BoardIDs, params.User)
} else {
params.User = libwekan.User{
Username: libwekan.Username(s.Username),
}
}
params.BoardIDs = kanban.ClearBoardIDs(params.BoardIDs, params.User)

exports, err := kanban.ExportFollowsForUser(c, params, db.Get(), s.roles)
if err != nil {
utils.AbortWithError(c, err)
Expand Down Expand Up @@ -245,13 +261,20 @@ func getDOCXFollowedByCurrentUser(c *gin.Context) {
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
}
var ok bool
params.User, ok = kanban.GetUser(libwekan.Username(s.Username))
if !ok {
c.JSON(http.StatusForbidden, "le nom d'utilisateur n'est pas reconnu")
return

if s.hasRole("wekan") {
var ok bool
params.User, ok = kanban.GetUser(libwekan.Username(s.Username))
if !ok {
c.JSON(500, "utilisateur non reconnu")
return
}
params.BoardIDs = kanban.ClearBoardIDs(params.BoardIDs, params.User)
} else {
params.User = libwekan.User{
Username: libwekan.Username(s.Username),
}
}
params.BoardIDs = kanban.ClearBoardIDs(params.BoardIDs, params.User)

exports, err := kanban.ExportFollowsForUser(c, params, db.Get(), s.roles)
if err != nil {
Expand Down
76 changes: 41 additions & 35 deletions pkg/wekan/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ func libelleCA(val float64, valPrec float64, variationCA float64) string {
func selectKanbanDBExportsWithSirets(ctx context.Context,
sirets []string,
db *pgxpool.Pool,
user libwekan.User,
username string,
roles []string,
zone []string,
raisonSociale *string) (core.KanbanDBExports, error) {
rows, err := db.Query(ctx, sqlDbExport, roles, user.Username, sirets, zone, raisonSocialeLike(raisonSociale))
rows, err := db.Query(ctx, sqlDbExport, roles, username, sirets, zone, raisonSocialeLike(raisonSociale))
defer rows.Close()
if err != nil {
return core.KanbanDBExports{}, err
Expand Down Expand Up @@ -206,7 +206,6 @@ func selectKanbanDBExportsWithoutCard(
if err != nil {
return core.KanbanDBExports{}, err
}

var kanbanDBExports core.KanbanDBExports
for rows.Next() {
var s []interface{}
Expand Down Expand Up @@ -236,49 +235,56 @@ func buildCardToCardAndCommentsPipeline() libwekan.Pipeline {

func (service wekanService) ExportFollowsForUser(ctx context.Context, params core.KanbanSelectCardsForUserParams, db *pgxpool.Pool, roles []string) (core.KanbanExports, error) {
wc := WekanConfig.Copy()
pipeline := buildCardsForUserPipeline(wc, params)
pipeline.AppendPipeline(buildCardToCardAndCommentsPipeline())

if params.Type == "no-card" {
params.Lists = nil
params.Labels = nil
params.BoardIDs = nil
params.Since = nil
}

var err error
var cards []libwekan.CardWithComments
var sirets []string
if utils.Contains(roles, "wekan") {
cards, err = wekan.SelectCardsWithCommentsFromPipeline(ctx, "boards", pipeline)
}
if err != nil {
return core.KanbanExports{}, err
}
sirets := utils.Convert(cards, cardWithCommentsToSiret(wc))
pipeline := buildCardsForUserPipeline(wc, params)
pipeline.AppendPipeline(buildCardToCardAndCommentsPipeline())

if params.Type == "no-card" {
params.Lists = nil
params.Labels = nil
params.BoardIDs = nil
params.Since = nil
}

// my-cards et all-cards utilisent la même méthode
if utils.Contains([]string{"my-cards", "all-cards"}, params.Type) {
kanbanDBExports, err := selectKanbanDBExportsWithSirets(ctx, sirets, db, params.User, roles, params.Zone, params.RaisonSociale)
return joinCardsWithKanbanDBExports(kanbanDBExports, cards), err
}
var err error
var cards []libwekan.CardWithComments
if utils.Contains(roles, "wekan") {
cards, err = wekan.SelectCardsWithCommentsFromPipeline(ctx, "boards", pipeline)
}
if err != nil {
return core.KanbanExports{}, err
}
sirets = utils.Convert(cards, cardWithCommentsToSiret(wc))

// my-cards et all-cards utilisent la même méthode
if utils.Contains([]string{"my-cards", "all-cards"}, params.Type) {
kanbanDBExports, err := selectKanbanDBExportsWithSirets(ctx, sirets, db, string(params.User.Username), roles, params.Zone, params.RaisonSociale)
return joinCardsWithKanbanDBExports(kanbanDBExports, cards), err
}
}
// alors que no-card retourne les suivis datapi sans carte kanban
kanbanDBExports, err := selectKanbanDBExportsWithoutCard(ctx, sirets, db, params.User, roles, params.Zone, params.RaisonSociale)
return kanbanDBExportToKanbanExports(kanbanDBExports), err
}

func (service wekanService) SelectKanbanExportsWithSiret(ctx context.Context, siret string, username string, db *pgxpool.Pool, roles []string) (core.KanbanExports, error) {
user, ok := WekanConfig.GetUserByUsername(libwekan.Username(username))
if !ok {
return nil, errors.New("utilisateur non trouvé")
}
pipeline := buildSelectCardsFromSiretPipeline(siret)
pipeline.AppendPipeline(buildCardToCardAndCommentsPipeline())
cardsWithComments, err := wekan.SelectCardsWithCommentsFromPipeline(ctx, "customFields", pipeline)
if err != nil {
return nil, err
var cardsWithComments []libwekan.CardWithComments
if utils.Contains(roles, "wekan") {
_, ok := WekanConfig.GetUserByUsername(libwekan.Username(username))
if !ok {
return nil, errors.New("utilisateur non trouvé")
}
pipeline := buildSelectCardsFromSiretPipeline(siret)
pipeline.AppendPipeline(buildCardToCardAndCommentsPipeline())
var err error
cardsWithComments, err = wekan.SelectCardsWithCommentsFromPipeline(ctx, "customFields", pipeline)
if err != nil {
return nil, err
}
}
kanbanDBExports, err := selectKanbanDBExportsWithSirets(ctx, []string{siret}, db, user, roles, nil, nil)
kanbanDBExports, err := selectKanbanDBExportsWithSirets(ctx, []string{siret}, db, username, roles, nil, nil)
kanbanExports := joinCardsWithKanbanDBExports(kanbanDBExports, cardsWithComments)

if len(kanbanExports) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/wekan/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ coalesce(v.periode_urssaf, '0001-01-01'), v.last_procol, coalesce(v.date_last_pr
coalesce(f.comment, ''), (permissions($1, v.roles, v.first_list_entreprise, v.code_departement, f.siret is not null)).in_zone
from v_summaries v
inner join etablissement_follow f on f.siret = v.siret and f.username = $2 and active
where (v.code_departement = any($4) or coalesce($4, '{}') = '{}') and v.siret != any($3) and (v.raison_sociale ilike $5 or $5 is null)
where (v.code_departement = any($4) or coalesce($4, '{}') = '{}') and (not (v.siret = any($3)) or $3 is null) and (v.raison_sociale ilike $5 or $5 is null)
order by f.id, v.siret`

0 comments on commit 0389d8b

Please sign in to comment.