Skip to content
Snippets Groups Projects
vote.go 8.39 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis POYEN's avatar
    Alexis POYEN committed
    package models
    
    import (
    	"encoding/json"
    	"errors"
    	"net/http"
    	"strconv"
    	"strings"
    	"time"
    
    	"forge.grandlyon.com/apoyen/elections/internal/auth"
    	"github.com/jinzhu/gorm"
    )
    
    func (d *DataHandler) handleVote(w http.ResponseWriter, r *http.Request) {
    	id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/Vote/"))
    	switch method := r.Method; method {
    	case "GET":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    		case "ADMIN", "CAPTURER", "VISUALIZER":
    			d.getVote(w, r)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    	case "POST":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    
    		case "ADMIN":
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    			d.postVote(w, r)
    
    		case "CAPTURER":
    			d.postVoteCapturer(w, r)
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		case "VISUALIZER":
    			http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    
    	case "PUT":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    
    		case "ADMIN":
    
    		case "CAPTURER":
    			d.putVoteCapturer(w, r)
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		case "VISUALIZER":
    			http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    	case "DELETE":
    		switch auth.GetLoggedUserTechnical(w, r).Role {
    
    		case "ADMIN":
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    			d.deleteVote(w, r, id)
    
    		case "CAPTURER":
    			d.deleteVoteCapturer(w, r, id)
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    		case "VISUALIZER":
    			http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
    		default:
    			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
    		}
    	default:
    		http.Error(w, "method not allowed", 400)
    	}
    }
    
    func (d *DataHandler) getVote(w http.ResponseWriter, r *http.Request) {
    	var o []Vote
    	d.db.Find(&o)
    	json.NewEncoder(w).Encode(o)
    }
    
    func (d *DataHandler) postVote(w http.ResponseWriter, r *http.Request) {
    	var o Vote
    	err := json.NewDecoder(r.Body).Decode(&o)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    
    
    	d.addVote(w, r, o)
    
    }
    
    func (d *DataHandler) postVoteCapturer(w http.ResponseWriter, r *http.Request) {
    	var o Vote
    	err := json.NewDecoder(r.Body).Decode(&o)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    
    	var flag = false
    	var capturer = d.getLoggedUser(w, r).(Capturer)
    	for _, deskRound := range capturer.DeskRounds {
    		if deskRound.ID == o.DeskRoundID {
    			flag = true
    		}
    	}
    	if flag {
    		d.addVote(w, r, o)
    	} else {
    		http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
    		return
    	}
    
    }
    
    func (d *DataHandler) addVote(w http.ResponseWriter, r *http.Request, o Vote) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	var voteFound Vote
    	if o.Blank {
    		d.db.First(&voteFound, "desk_round_id = ? AND blank = ?", o.DeskRoundID, o.Blank)
    		if voteFound.ID != 0 {
    			http.Error(w, ErrorVoteExist, http.StatusInternalServerError)
    			return
    		}
    	} else if o.NullVote {
    		d.db.First(&voteFound, "desk_round_id = ? AND null_vote = ?", o.DeskRoundID, o.NullVote)
    		if voteFound.ID != 0 {
    			http.Error(w, ErrorVoteExist, http.StatusInternalServerError)
    			return
    		}
    	} else {
    		d.db.First(&voteFound, "desk_round_id = ? AND candidate_list_id = ?", o.DeskRoundID, o.CandidateListID)
    		if voteFound.ID != 0 {
    			http.Error(w, ErrorVoteExist, http.StatusInternalServerError)
    			return
    		}
    	}
    
    	if !o.Blank && !o.NullVote {
    		// Check that CandidateListID exist
    		var candidateList CandidateList
    		if err := d.db.First(&candidateList, o.CandidateListID).Error; err != nil {
    			http.Error(w, ErrorParentNotFound, http.StatusInternalServerError)
    			return
    		}
    	}
    
    	// Check that deskRound exist
    	var deskRound DeskRound
    	if err := d.db.Preload("Votes").First(&deskRound, o.DeskRoundID).Error; err != nil {
    		http.Error(w, ErrorParentNotFound, http.StatusInternalServerError)
    		return
    	}
    
    	if deskRound.Validated {
    		http.Error(w, ErrorValidatedVote, http.StatusInternalServerError)
    		return
    	}
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    
    	d.db.Create(&o)
    	d.db.Last(&o)
    	json.NewEncoder(w).Encode(o)
    }
    
    
    func (d *DataHandler) putVote(w http.ResponseWriter, r *http.Request) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	var vote Vote
    	err := json.NewDecoder(r.Body).Decode(&vote)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    
    	d.updateVote(w, r, vote)
    
    }
    
    func (d *DataHandler) putVoteCapturer(w http.ResponseWriter, r *http.Request) {
    	var o Vote
    	err := json.NewDecoder(r.Body).Decode(&o)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
    	}
    
    	var flag = false
    	var capturer = d.getLoggedUser(w, r).(Capturer)
    	for _, deskRound := range capturer.DeskRounds {
    		if deskRound.ID == o.DeskRoundID {
    			flag = true
    		}
    	}
    	if flag {
    		d.updateVote(w, r, o)
    	} else {
    		http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
    		return
    	}
    }
    
    func (d *DataHandler) updateVote(w http.ResponseWriter, r *http.Request, vote Vote) {
    
    	var deskRound DeskRound
    	if err := d.db.First(&deskRound, vote.DeskRoundID).Error; err != nil {
    		http.Error(w, ErrorParentNotFound, http.StatusNotFound)
    		return
    	}
    	if deskRound.Validated {
    		http.Error(w, ErrorValidatedVote, http.StatusInternalServerError)
    		return
    	}
    
    
    	if vote.Blank {
    		if err := d.db.Where("blank = true  and desk_round_id = ?", vote.DeskRoundID).Find(&o).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    	} else if vote.NullVote {
    		if err := d.db.Where("null_vote = true  and desk_round_id = ?", vote.DeskRoundID).Find(&o).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    	} else {
    		if err := d.db.Where("candidate_list_id = ?  and desk_round_id = ?", vote.CandidateListID, vote.DeskRoundID).Find(&o).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	o.VoiceNumber = vote.VoiceNumber
    	d.db.Save(&o)
    	json.NewEncoder(w).Encode(o)
    
    }
    
    func (d *DataHandler) deleteVote(w http.ResponseWriter, r *http.Request, id int) {
    	if id != 0 {
    		var o Vote
    		if err := d.db.First(&o, id).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    
    		// Set completed to false for deskRound if deskRound is not validated
    
    		var deskRound DeskRound
    		if err := d.db.First(&deskRound, o.DeskRoundID).Error; err != nil {
    			http.Error(w, ErrorParentNotFound, http.StatusNotFound)
    			return
    		}
    
    		if deskRound.Validated {
    			http.Error(w, ErrorValidatedVote, http.StatusInternalServerError)
    			return
    		}
    
    		deskRound.Completed = false
    		d.db.Save(&deskRound)
    
    
    		d.db.Delete(&o)
    
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    	} else {
    		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    	}
    }
    
    
    func (d *DataHandler) deleteVoteCapturer(w http.ResponseWriter, r *http.Request, id int) {
    	if id != 0 {
    		var o Vote
    		if err := d.db.First(&o, id).Error; err != nil {
    			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    			return
    		}
    
    		var flag = false
    		var capturer = d.getLoggedUser(w, r).(Capturer)
    		for _, deskRound := range capturer.DeskRounds {
    			if deskRound.ID == o.DeskRoundID {
    				flag = true
    			}
    		}
    		if flag {
    			d.deleteVote(w, r, id)
    		} else {
    			http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
    			return
    		}
    
    	} else {
    		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
    	}
    }
    
    
    Alexis POYEN's avatar
    Alexis POYEN committed
    func (vote *Vote) AfterSave(scope *gorm.Scope) error {
    	var deskRound DeskRound
    	if err := scope.DB().First(&deskRound, vote.DeskRoundID).Error; err != nil {
    		return errors.New(ErrorValidateVote)
    	}
    
    	// Check deskCompletion
    	var desk Desk
    	if err := scope.DB().First(&desk, deskRound.DeskID).Error; err != nil {
    		return errors.New(ErrorValidateVote)
    	}
    	var section Section
    	if err := scope.DB().First(&section, desk.SectionID).Error; err != nil {
    		return errors.New(ErrorValidateVote)
    	}
    	var area Area
    	if err := scope.DB().First(&area, section.AreaID).Error; err != nil {
    		return errors.New(ErrorValidateVote)
    	}
    	var round Round
    	if err := scope.DB().First(&round, deskRound.RoundID).Error; err != nil {
    		return errors.New(ErrorValidateVote)
    	}
    
    	var candidateLists []CandidateList
    	if err := scope.DB().Where("area_id = ? and round_id = ?", area.ID, round.ID).Find(&candidateLists).Error; err != nil {
    		return errors.New(ErrorValidateVote)
    	}
    	var listNumberPerArea = len(candidateLists)
    
    	var votes []Vote
    	if err := scope.DB().Where("desk_round_id = ?", deskRound.ID).Find(&votes).Error; err != nil {
    		return errors.New(ErrorValidateVote)
    	}
    	var votesNumberPerDesk = len(votes)
    
    	if votesNumberPerDesk == (listNumberPerArea + 2) {
    		deskRound.Completed = true
    		deskRound.DateCompletion = time.Now()
    		scope.DB().Save(&deskRound)
    	}
    	return nil
    }