Can restrict survey to group

This commit is contained in:
nemunaire 2022-02-21 08:53:27 +01:00
parent 588089d11b
commit f0d5296a31
4 changed files with 36 additions and 11 deletions

View File

@ -51,6 +51,15 @@
</div>
</div>
<div class="row">
<div class="col-sm-3 text-sm-end">
<label for="group" class="col-form-label col-form-label-sm">Restreindre au groupe</label>
</div>
<div class="col-sm-8 col-md-4 col-lg-2">
<input class="form-control form-control-sm" id="group" bind:value={survey.group}>
</div>
</div>
<div class="row">
<div class="col-sm-3 text-sm-end">
<label for="start_availability" class="col-form-label col-form-label-sm">Date de début</label>

View File

@ -8,10 +8,11 @@ class Survey {
}
}
update({ id, title, promo, shown, corrected, start_availability, end_availability }) {
update({ id, title, promo, group, shown, corrected, start_availability, end_availability }) {
this.id = id;
this.title = title;
this.promo = promo;
this.group = group;
this.shown = shown;
this.corrected = corrected;
if (this.start_availability != start_availability) {

1
db.go
View File

@ -82,6 +82,7 @@ CREATE TABLE IF NOT EXISTS surveys(
id_survey INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255),
promo MEDIUMINT NOT NULL,
grp VARCHAR(255) NOT NULL,
shown BOOLEAN NOT NULL DEFAULT FALSE,
corrected BOOLEAN NOT NULL DEFAULT FALSE,
start_availability TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

View File

@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/julienschmidt/httprouter"
@ -19,7 +20,19 @@ func init() {
} else if u.IsAdmin {
return formatApiResponse(getSurveys("ORDER BY promo DESC, start_availability ASC"))
} else {
return formatApiResponse(getSurveys(fmt.Sprintf("WHERE shown = TRUE AND promo = %d ORDER BY start_availability ASC", u.Promo)))
surveys, err := getSurveys(fmt.Sprintf("WHERE shown = TRUE AND promo = %d ORDER BY start_availability ASC", u.Promo))
if err != nil {
return APIErrorResponse{err: err}
}
var response []Survey
for _, s := range surveys {
if s.Group == "" || strings.Contains(u.Groups, ","+s.Group+",") {
response = append(response, s)
}
}
return formatApiResponse(response, nil)
}
}))
router.POST("/api/surveys", apiHandler(func(_ httprouter.Params, body []byte) HTTPResponse {
@ -32,11 +45,11 @@ func init() {
new.Promo = currentPromo
}
return formatApiResponse(NewSurvey(new.Title, new.Promo, new.Shown, new.StartAvailability, new.EndAvailability))
return formatApiResponse(NewSurvey(new.Title, new.Promo, new.Group, new.Shown, new.StartAvailability, new.EndAvailability))
}, adminRestricted))
router.GET("/api/surveys/:sid", apiAuthHandler(surveyAuthHandler(
func(s Survey, u *User, _ []byte) HTTPResponse {
if (s.Promo == u.Promo && s.Shown) || (u != nil && u.IsAdmin) {
if (s.Promo == u.Promo && (s.Group == "" || (u != nil && strings.Contains(u.Groups, ","+s.Group+",")) && s.Shown)) || (u != nil && u.IsAdmin) {
return APIResponse{s}
} else {
return APIErrorResponse{
@ -125,6 +138,7 @@ type Survey struct {
Id int64 `json:"id"`
Title string `json:"title"`
Promo uint `json:"promo"`
Group string `json:"group"`
Shown bool `json:"shown"`
Corrected bool `json:"corrected"`
StartAvailability time.Time `json:"start_availability"`
@ -132,14 +146,14 @@ type Survey struct {
}
func getSurveys(cnd string, param ...interface{}) (surveys []Survey, err error) {
if rows, errr := DBQuery("SELECT id_survey, title, promo, shown, corrected, start_availability, end_availability FROM surveys "+cnd, param...); errr != nil {
if rows, errr := DBQuery("SELECT id_survey, title, promo, grp, shown, corrected, start_availability, end_availability FROM surveys "+cnd, param...); errr != nil {
return nil, errr
} else {
defer rows.Close()
for rows.Next() {
var s Survey
if err = rows.Scan(&s.Id, &s.Title, &s.Promo, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability); err != nil {
if err = rows.Scan(&s.Id, &s.Title, &s.Promo, &s.Group, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability); err != nil {
return
}
surveys = append(surveys, s)
@ -153,17 +167,17 @@ func getSurveys(cnd string, param ...interface{}) (surveys []Survey, err error)
}
func getSurvey(id int) (s Survey, err error) {
err = DBQueryRow("SELECT id_survey, title, promo, shown, corrected, start_availability, end_availability FROM surveys WHERE id_survey=?", id).Scan(&s.Id, &s.Title, &s.Promo, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability)
err = DBQueryRow("SELECT id_survey, title, promo, grp, shown, corrected, start_availability, end_availability FROM surveys WHERE id_survey=?", id).Scan(&s.Id, &s.Title, &s.Promo, &s.Group, &s.Shown, &s.Corrected, &s.StartAvailability, &s.EndAvailability)
return
}
func NewSurvey(title string, promo uint, shown bool, startAvailability time.Time, endAvailability time.Time) (*Survey, error) {
if res, err := DBExec("INSERT INTO surveys (title, promo, shown, start_availability, end_availability) VALUES (?, ?, ?, ?, ?)", title, promo, shown, startAvailability, endAvailability); err != nil {
func NewSurvey(title string, promo uint, group string, shown bool, startAvailability time.Time, endAvailability time.Time) (*Survey, error) {
if res, err := DBExec("INSERT INTO surveys (title, promo, grp, shown, start_availability, end_availability) VALUES (?, ?, ?, ?, ?, ?)", title, promo, group, shown, startAvailability, endAvailability); err != nil {
return nil, err
} else if sid, err := res.LastInsertId(); err != nil {
return nil, err
} else {
return &Survey{sid, title, promo, shown, false, startAvailability, endAvailability}, nil
return &Survey{sid, title, promo, group, shown, false, startAvailability, endAvailability}, nil
}
}
@ -201,7 +215,7 @@ func (s Survey) GetScores() (scores map[int64]*float64, err error) {
}
func (s *Survey) Update() (*Survey, error) {
if _, err := DBExec("UPDATE surveys SET title = ?, promo = ?, shown = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_survey = ?", s.Title, s.Promo, s.Shown, s.Corrected, s.StartAvailability, s.EndAvailability, s.Id); err != nil {
if _, err := DBExec("UPDATE surveys SET title = ?, promo = ?, grp = ?, shown = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_survey = ?", s.Title, s.Promo, s.Group, s.Shown, s.Corrected, s.StartAvailability, s.EndAvailability, s.Id); err != nil {
return nil, err
} else {
return s, err