• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vocdoni / saas-backend / 18900277662

29 Oct 2025 07:25AM UTC coverage: 60.11% (+0.03%) from 60.079%
18900277662

Pull #301

github

altergui
avoid unsafe type cast
Pull Request #301: Refactor Process model: separate database ID from blockchain address

46 of 55 new or added lines in 3 files covered. (83.64%)

7 existing lines in 2 files now uncovered.

6124 of 10188 relevant lines covered (60.11%)

36.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.06
/api/process.go
1
package api
2

3
import (
4
        "encoding/json"
5
        "net/http"
6

7
        "github.com/go-chi/chi/v5"
8
        "github.com/vocdoni/saas-backend/api/apicommon"
9
        "github.com/vocdoni/saas-backend/db"
10
        "github.com/vocdoni/saas-backend/errors"
11
        "go.mongodb.org/mongo-driver/bson/primitive"
12
)
13

14
// createProcessHandler godoc
15
//
16
//        @Summary                Create a new voting process
17
//        @Description        Create a new voting process. Requires Manager/Admin role.
18
//        @Tags                        process
19
//        @Accept                        json
20
//        @Produce                json
21
//        @Security                BearerAuth
22
//        @Param                        processId        path                string                                                        true        "Process ID"
23
//        @Param                        request                body                apicommon.CreateProcessRequest        true        "Process creation information"
24
//        @Success                200                        {string}        string                                                        "OK"
25
//        @Failure                400                        {object}        errors.Error                                        "Invalid input data"
26
//        @Failure                401                        {object}        errors.Error                                        "Unauthorized"
27
//        @Failure                404                        {object}        errors.Error                                        "Published census not found"
28
//        @Failure                409                        {object}        errors.Error                                        "Process already exists"
29
//        @Failure                500                        {object}        errors.Error                                        "Internal server error"
30
//        @Router                        /process [post]
31
func (a *API) createProcessHandler(w http.ResponseWriter, r *http.Request) {
4✔
32
        // parse the process info from the request body
4✔
33
        processInfo := &apicommon.CreateProcessRequest{}
4✔
34
        if err := json.NewDecoder(r.Body).Decode(&processInfo); err != nil {
4✔
35
                errors.ErrMalformedBody.Write(w)
×
36
                return
×
37
        }
×
38

39
        if processInfo.CensusID == nil {
5✔
40
                errors.ErrMalformedBody.Withf("missing published census root or ID").Write(w)
1✔
41
                return
1✔
42
        }
1✔
43

44
        // get the user from the request context
45
        user, ok := apicommon.UserFromContext(r.Context())
3✔
46
        if !ok {
3✔
47
                errors.ErrUnauthorized.Write(w)
×
48
                return
×
49
        }
×
50

51
        census, err := a.db.Census(processInfo.CensusID.String())
3✔
52
        if err != nil {
4✔
53
                if err == db.ErrNotFound {
1✔
54
                        errors.ErrMalformedURLParam.Withf("census not found").Write(w)
×
55
                        return
×
56
                }
×
57
                errors.ErrGenericInternalServerError.WithErr(err).Write(w)
1✔
58
                return
1✔
59
        }
60

61
        // check the user has the necessary permissions
62
        if !user.HasRoleFor(census.OrgAddress, db.ManagerRole) && !user.HasRoleFor(census.OrgAddress, db.AdminRole) {
2✔
63
                errors.ErrUnauthorized.Withf("user is not admin of organization").Write(w)
×
64
                return
×
65
        }
×
66

67
        // finally create the process
68
        process := &db.Process{
2✔
69
                Census:     *census,
2✔
70
                Metadata:   processInfo.Metadata,
2✔
71
                OrgAddress: census.OrgAddress,
2✔
72
        }
2✔
73
        if len(processInfo.Address) > 0 {
2✔
NEW
74
                process.Address = processInfo.Address
×
NEW
75
        }
×
76

77
        processID, err := a.db.SetProcess(process)
2✔
78
        if err != nil {
2✔
79
                errors.ErrGenericInternalServerError.WithErr(err).Write(w)
×
80
                return
×
81
        }
×
82

83
        apicommon.HTTPWriteJSON(w, processID)
2✔
84
}
85

86
// processInfoHandler godoc
87
//
88
//        @Summary                Get process information
89
//        @Description        Retrieve voting process information by ID. Returns process details including census and metadata.
90
//        @Tags                        process
91
//        @Accept                        json
92
//        @Produce                json
93
//        @Param                        processId        path                string        true        "Process ID"
94
//        @Success                200                        {object}        db.Process
95
//        @Failure                400                        {object}        errors.Error        "Invalid process ID"
96
//        @Failure                404                        {object}        errors.Error        "Process not found"
97
//        @Failure                500                        {object}        errors.Error        "Internal server error"
98
//        @Router                        /process/{processId} [get]
99
func (a *API) processInfoHandler(w http.ResponseWriter, r *http.Request) {
2✔
100
        processID := chi.URLParam(r, "processId")
2✔
101
        if len(processID) == 0 {
2✔
102
                errors.ErrMalformedURLParam.Withf("missing process ID").Write(w)
×
103
                return
×
104
        }
×
105
        parsedID, err := primitive.ObjectIDFromHex(processID)
2✔
106
        if err != nil {
3✔
107
                errors.ErrMalformedURLParam.Withf("invalid process ID").Write(w)
1✔
108
                return
1✔
109
        }
1✔
110

111
        process, err := a.db.Process(parsedID)
1✔
112
        if err != nil {
1✔
UNCOV
113
                if err == db.ErrNotFound {
×
114
                        errors.ErrMalformedURLParam.Withf("process not found").Write(w)
×
115
                        return
×
116
                }
×
UNCOV
117
                errors.ErrGenericInternalServerError.WithErr(err).Write(w)
×
UNCOV
118
                return
×
119
        }
120

121
        apicommon.HTTPWriteJSON(w, process)
1✔
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc