Adapt count tags for managers

This commit is contained in:
Christoph Kluge
2023-01-30 13:43:12 +01:00
parent b2aed2f16b
commit 2590ce7e44
2 changed files with 15 additions and 5 deletions

View File

@@ -58,7 +58,7 @@ func (r *JobRepository) CreateTag(tagType string, tagName string) (tagId int64,
return res.LastInsertId()
}
func (r *JobRepository) CountTags(user *string) (tags []schema.Tag, counts map[string]int, err error) {
func (r *JobRepository) CountTags(user *string, project *string) (tags []schema.Tag, counts map[string]int, err error) {
tags = make([]schema.Tag, 0, 100)
xrows, err := r.DB.Queryx("SELECT * FROM tag")
if err != nil {
@@ -77,9 +77,12 @@ func (r *JobRepository) CountTags(user *string) (tags []schema.Tag, counts map[s
From("tag t").
LeftJoin("jobtag jt ON t.id = jt.tag_id").
GroupBy("t.tag_name")
if user != nil {
if (user != nil && project == nil) { // USER: Only count own jobs
q = q.Where("jt.job_id IN (SELECT id FROM job WHERE job.user = ?)", *user)
}
} else if (user != nil && project != nil) { // MANAGER: Count own jobs plus project's jobs
q = q.Where("jt.job_id IN (SELECT id FROM job WHERE job.user = ? OR job.project = ?)", *user, *project)
} // else: ADMIN: Count all jobs
rows, err := q.RunWith(r.stmtCache).Query()
if err != nil {