-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_list_account_entries.go
70 lines (60 loc) · 2.19 KB
/
rpc_list_account_entries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package gapi
import (
"context"
"errors"
db "github.com/aradwann/eenergy/db/store"
"github.com/aradwann/eenergy/pb"
"github.com/aradwann/eenergy/util"
"github.com/aradwann/eenergy/val"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (server *Server) ListAccountEntries(ctx context.Context, req *pb.ListAccountEntriesRequest) (*pb.ListAccountEntriesResponse, error) {
authPayload, err := server.authorizeUser(ctx, []string{util.AdminRole, util.UserRole})
if err != nil {
return nil, unauthenticatedError(err)
}
account, err := server.store.GetAccount(ctx, req.AccountId)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
return nil, status.Errorf(codes.NotFound, "acc not found")
}
return nil, status.Errorf(codes.Internal, "failed to get get account: %s", err)
}
if authPayload.Role == util.UserRole && authPayload.Username != account.Owner {
return nil, status.Error(codes.PermissionDenied, "cannot get other user's account entries")
}
violations := validateListAccountEntriesRequest(req)
if violations != nil {
return nil, invalidArgumentError(violations)
}
arg := db.ListEntriesParams{
AccountID: req.GetAccountId(),
Limit: req.GetLimit(),
Offset: req.GetOffset(),
}
entries, err := server.store.ListEntries(ctx, arg)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
return nil, status.Errorf(codes.NotFound, "acc not found")
}
return nil, status.Errorf(codes.Internal, "failed to get user accounts: %s", err)
}
rsp := &pb.ListAccountEntriesResponse{
Entries: convertEntries(entries),
}
return rsp, nil
}
func validateListAccountEntriesRequest(req *pb.ListAccountEntriesRequest) (violations []*errdetails.BadRequest_FieldViolation) {
if err := val.ValidateID(int64(req.GetAccountId())); err != nil {
violations = append(violations, fieldViolation("account_id", err))
}
if err := val.ValidateIntNotNegative(int64(req.GetLimit())); err != nil {
violations = append(violations, fieldViolation("limit", err))
}
if err := val.ValidateIntNotNegative(int64(req.GetOffset())); err != nil {
violations = append(violations, fieldViolation("offset", err))
}
return
}