Put the user's role and branch on GET /profile, upsert role_user and branch_user on POST, and drop their list endpoints.

Also accept flexible card-order form values, move mock seed under db/mock, and regenerate Swagger.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
guwandykandCursor committed 2026-08-22 17:56:07 +05:00
1 parent 7225dc0399
commit 8795ae2195
47 files changed
+1308 -1785

No files matched your search

+73 -35
View File
@@ -15,53 +15,87 @@ import (
"github.com/gin-gonic/gin"
)
// sensitiveFieldSubstrings marks request body keys that must never be
// persisted verbatim (login passwords, branch billing credentials, etc).
var sensitiveFieldSubstrings = []string{"password", "token", "secret"}
const writeTimeout = 5 * time.Second
const (
writeTimeout = 5 * time.Second
queueSize = 1024
workers = 2
)
type recorder struct {
repository Repository
logger *logging.Logger
queue chan CreateActivityLogDTO
}
// Record returns a global gin middleware that persists every mutating
// (POST/PUT/PATCH/DELETE) request made by an authenticated user into
// activity_logs - e.g. creating a card or attaching a user to a branch.
// Read-only (GET) requests are not recorded. It must run after
// appresult.JwtTokenCheck has set "id" in the context, which happens
// naturally here since c.Next() runs the rest of the chain first.
// Read-only (GET) requests are not recorded. After the handler finishes
// the entry is queued (the client is not blocked) and a small worker
// pool writes it. JwtTokenCheck sets "id" during c.Next(), which is why
// the user is read afterwards.
func Record(repository Repository, logger *logging.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
if !shouldRecord(c.Request.Method) {
c.Next()
return
}
return newRecorder(repository, logger).handle
}
body := readAndRestoreBody(c)
func newRecorder(repository Repository, logger *logging.Logger) *recorder {
r := &recorder{
repository: repository,
logger: logger,
queue: make(chan CreateActivityLogDTO, queueSize),
}
for i := 0; i < workers; i++ {
go r.worker()
}
return r
}
func (r *recorder) handle(c *gin.Context) {
if !shouldRecord(c.Request.Method) {
c.Next()
return
}
userID := currentUserID(c)
if userID == nil {
return
body := readAndRestoreBody(c)
c.Next()
userID := currentUserID(c)
if userID == nil {
return
}
id := *userID
module, action := describeRoute(c)
r.enqueue(CreateActivityLogDTO{
UserId: &id,
Method: c.Request.Method,
Path: c.Request.URL.Path,
Module: module,
Action: action,
StatusCode: c.Writer.Status(),
RequestBody: redact(body),
Ip: c.ClientIP(),
})
}
func (r *recorder) enqueue(entry CreateActivityLogDTO) {
select {
case r.queue <- entry:
default:
r.logger.Errorf("activity log queue full, dropping %s %s", entry.Method, entry.Path)
}
}
func (r *recorder) worker() {
for entry := range r.queue {
ctx, cancel := context.WithTimeout(context.Background(), writeTimeout)
if _, err := r.repository.Create(ctx, entry); err != nil {
r.logger.Errorf("record activity log failed: %v", err)
}
module, action := describeRoute(c)
entry := CreateActivityLogDTO{
UserId: userID,
Method: c.Request.Method,
Path: c.Request.URL.Path,
Module: module,
Action: action,
StatusCode: c.Writer.Status(),
RequestBody: redact(body),
Ip: c.ClientIP(),
}
go func() {
ctx, cancel := context.WithTimeout(context.Background(), writeTimeout)
defer cancel()
if _, err := repository.Create(ctx, entry); err != nil {
logger.Errorf("record activity log failed: %v", err)
}
}()
cancel()
}
}
@@ -80,9 +114,13 @@ func readAndRestoreBody(c *gin.Context) []byte {
}
data, err := io.ReadAll(c.Request.Body)
if err != nil {
c.Request.Body = io.NopCloser(bytes.NewReader(nil))
return nil
}
// Restore Body for ShouldBindJSON and cache it so ShouldBindBodyWithJSON
// still works after this middleware has already consumed the stream.
c.Request.Body = io.NopCloser(bytes.NewReader(data))
c.Set(gin.BodyBytesKey, data)
return data
}