admin: Insert $team assignee in db automatically

This commit is contained in:
nemunaire 2021-09-03 12:18:02 +02:00
parent f5941dcece
commit abf0715dbf
4 changed files with 37 additions and 32 deletions

View File

@ -171,11 +171,11 @@ func clearClaims(_ httprouter.Params, _ []byte) (interface{}, error) {
func generateTeamIssuesFile(team fic.Team) error { func generateTeamIssuesFile(team fic.Team) error {
if my, err := team.MyIssueFile(); err != nil { if my, err := team.MyIssueFile(); err != nil {
return err return fmt.Errorf("Unable to generate issue FILE (tid=%d): %w", team.Id, err)
} else if j, err := json.Marshal(my); err != nil { } else if j, err := json.Marshal(my); err != nil {
return err return fmt.Errorf("Unable to encode issues' file JSON: %w", err)
} else if err = ioutil.WriteFile(path.Join(TeamsDir, fmt.Sprintf("%d", team.Id), "issues.json"), j, 0644); err != nil { } else if err = ioutil.WriteFile(path.Join(TeamsDir, fmt.Sprintf("%d", team.Id), "issues.json"), j, 0644); err != nil {
return err return fmt.Errorf("Unable to write issues' file: %w", err)
} }
return nil return nil
} }

View File

@ -283,9 +283,9 @@ func eventHandler(f func(fic.Event, []byte) (interface{}, error)) func(httproute
func claimHandler(f func(fic.Claim, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) { func claimHandler(f func(fic.Claim, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) { return func(ps httprouter.Params, body []byte) (interface{}, error) {
if cid, err := strconv.ParseInt(string(ps.ByName("cid")), 10, 64); err != nil { if cid, err := strconv.ParseInt(string(ps.ByName("cid")), 10, 64); err != nil {
return nil, err return nil, fmt.Errorf("Invalid claim id: %w", err)
} else if claim, err := fic.GetClaim(cid); err != nil { } else if claim, err := fic.GetClaim(cid); err != nil {
return nil, err return nil, fmt.Errorf("Unable to find requested claim (id=%d): %w", cid, err)
} else { } else {
return f(claim, body) return f(claim, body)
} }

View File

@ -400,6 +400,8 @@ CREATE TABLE IF NOT EXISTS claim_assignees(
`); err != nil { `); err != nil {
return err return err
} }
db.Exec(`INSERT INTO claim_assignees VALUES (0, "$team");`)
db.Exec(`UPDATE claim_assignees SET id_assignee = 0 WHERE name = "$team";`)
if _, err := db.Exec(` if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS claims( CREATE TABLE IF NOT EXISTS claims(
id_claim INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, id_claim INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,

View File

@ -2,20 +2,21 @@ package fic
import ( import (
"database/sql" "database/sql"
"fmt"
"path" "path"
"time" "time"
) )
// Claim represents an issue, a bug or a ToDo item. // Claim represents an issue, a bug or a ToDo item.
type Claim struct { type Claim struct {
Id int64 `json:"id"` Id int64 `json:"id"`
Subject string `json:"subject"` Subject string `json:"subject"`
IdTeam *int64 `json:"id_team"` IdTeam *int64 `json:"id_team"`
IdExercice *int64 `json:"id_exercice"` IdExercice *int64 `json:"id_exercice"`
IdAssignee *int64 `json:"id_assignee"` IdAssignee *int64 `json:"id_assignee"`
Creation time.Time `json:"creation"` Creation time.Time `json:"creation"`
State string `json:"state"` State string `json:"state"`
Priority string `json:"priority"` Priority string `json:"priority"`
} }
// GetClaim retrieves the claim with the given identifier. // GetClaim retrieves the claim with the given identifier.
@ -25,7 +26,7 @@ func GetClaim(id int64) (c Claim, err error) {
} }
// GetClaims returns a list of all Claim registered in the database. // GetClaims returns a list of all Claim registered in the database.
func GetClaims() (res []Claim, err error) { func GetClaims() (res []Claim, err error) {
var rows *sql.Rows var rows *sql.Rows
if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims"); err != nil { if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims"); err != nil {
return return
@ -51,7 +52,7 @@ func (t Team) GetClaim(id int64) (c Claim, err error) {
} }
// GetClaims returns a list of all Claim registered for the Team. // GetClaims returns a list of all Claim registered for the Team.
func (t Team) GetClaims() (res []Claim, err error) { func (t Team) GetClaims() (res []Claim, err error) {
var rows *sql.Rows var rows *sql.Rows
if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims WHERE id_team = ?", t.Id); err != nil { if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims WHERE id_team = ?", t.Id); err != nil {
return nil, err return nil, err
@ -71,7 +72,7 @@ func (t Team) GetClaims() (res []Claim, err error) {
} }
// GetExercices returns a list of all Claim registered for the Exercice. // GetExercices returns a list of all Claim registered for the Exercice.
func (e Exercice) GetClaims() (res []Claim, err error) { func (e Exercice) GetClaims() (res []Claim, err error) {
var rows *sql.Rows var rows *sql.Rows
if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims WHERE id_exercice = ?", e.Id); err != nil { if rows, err = DBQuery("SELECT id_claim, subject, id_team, id_exercice, id_assignee, creation, state, priority FROM claims WHERE id_exercice = ?", e.Id); err != nil {
return nil, err return nil, err
@ -189,15 +190,15 @@ func ClearClaims() (int64, error) {
// ClaimDescription represents some text describing an issue. // ClaimDescription represents some text describing an issue.
type ClaimDescription struct { type ClaimDescription struct {
Id int64 `json:"id"` Id int64 `json:"id"`
// IdAssignee stores the user who handle the claim (or 0 if nobody handles it). // IdAssignee stores the user who handle the claim (or 0 if nobody handles it).
IdAssignee int64 `json:"id_assignee"` IdAssignee int64 `json:"id_assignee"`
// Content is the raw description. // Content is the raw description.
Content string `json:"content"` Content string `json:"content"`
// Date is the timestamp when the description was written. // Date is the timestamp when the description was written.
Date time.Time `json:"date"` Date time.Time `json:"date"`
// Publish indicates wether it is shown back to the team. // Publish indicates wether it is shown back to the team.
Publish bool `json:"publish"` Publish bool `json:"publish"`
} }
// GetLastUpdate returns the date of the latest message written for the given Claim. // GetLastUpdate returns the date of the latest message written for the given Claim.
@ -394,28 +395,28 @@ func (t Team) MyIssueFile() (ret []teamIssueFile, err error) {
} }
if descriptions, err := claim.GetDescriptions(); err != nil { if descriptions, err := claim.GetDescriptions(); err != nil {
return nil, err return nil, fmt.Errorf("Error occurs during description retrieval (cid=%d): %w", claim.Id, err)
} else { } else {
tif := teamIssueFile{ tif := teamIssueFile{
Id: claim.Id, Id: claim.Id,
Subject: claim.Subject, Subject: claim.Subject,
Exercice: exercice, Exercice: exercice,
ExerciceURL: url, ExerciceURL: url,
Assignee: assignee, Assignee: assignee,
State: claim.State, State: claim.State,
Priority: claim.Priority, Priority: claim.Priority,
Texts: []teamIssueText{}, Texts: []teamIssueText{},
} }
for _, description := range descriptions { for _, description := range descriptions {
if description.Publish { if description.Publish {
if people, err := description.GetAssignee(); err != nil { if people, err := description.GetAssignee(); err != nil {
return nil, err return nil, fmt.Errorf("Error ocurs during assignee retrieval (aid=%d): %w", description.IdAssignee, err)
} else { } else {
tif.Texts = append(tif.Texts, teamIssueText{ tif.Texts = append(tif.Texts, teamIssueText{
Content: description.Content, Content: description.Content,
Assignee: people.Name, Assignee: people.Name,
Date: description.Date, Date: description.Date,
}) })
} }
} }
@ -424,6 +425,8 @@ func (t Team) MyIssueFile() (ret []teamIssueFile, err error) {
ret = append(ret, tif) ret = append(ret, tif)
} }
} }
} else {
err = fmt.Errorf("Error occurs during claim retrieval: %w", err)
} }
return return