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

vocdoni / saas-backend / 18936111830

30 Oct 2025 09:29AM UTC coverage: 60.11% (+0.03%) from 60.079%
18936111830

Pull #301

github

altergui
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

47 of 56 new or added lines in 3 files covered. (83.93%)

7 existing lines in 2 files now uncovered.

6124 of 10188 relevant lines covered (60.11%)

36.85 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                        request        body                apicommon.CreateProcessRequest        true        "Process creation information"
23
//        @Success                200                {object}        primitive.ObjectID                                "Process ID"
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 census 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
        parsedID, err := primitive.ObjectIDFromHex(processID)
2✔
105
        if err != nil {
3✔
106
                errors.ErrMalformedURLParam.Withf("invalid process ID").Write(w)
1✔
107
                return
1✔
108
        }
1✔
109

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

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