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

vocdoni / saas-backend / 18745664014

23 Oct 2025 10:37AM UTC coverage: 60.112% (+0.03%) from 60.079%
18745664014

Pull #301

github

emmdim
refactor(api): change process creation to use address-based identification

- Remove processId URL parameter from process creation endpoint
- Replace process ID with address field in CreateProcessRequest
- Update process data structure to use Address instead of ID throughout
- Change metadata field from byte array to key-value map
- Add organization context validation to process creation handler
- Update all related tests and database operations to use address-based identification
Pull Request #301: Refactor Process model: separate database ID from blockchain address

41 of 53 new or added lines in 3 files covered. (77.36%)

3 existing lines in 1 file now uncovered.

6123 of 10186 relevant lines covered (60.11%)

36.84 hits per line

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

59.65
/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
)
12

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

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

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

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

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

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

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

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

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

105
        process, err := a.db.Process(processID)
2✔
106
        if err != nil {
3✔
107
                if err == db.ErrNotFound {
1✔
108
                        errors.ErrMalformedURLParam.Withf("process not found").Write(w)
×
109
                        return
×
110
                }
×
111
                errors.ErrGenericInternalServerError.WithErr(err).Write(w)
1✔
112
                return
1✔
113
        }
114

115
        apicommon.HTTPWriteJSON(w, process)
1✔
116
}
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