fix: update audit_trails, ai-chat

This commit is contained in:
hanif salafi 2025-11-04 10:15:21 +07:00
parent 5f10073f8d
commit b3bfb2bc3d
5 changed files with 49 additions and 18 deletions

View File

@ -23,22 +23,59 @@ func AuditTrailsMiddleware(db *gorm.DB) fiber.Handler {
authHeader := c.Get("Authorization") authHeader := c.Get("Authorization")
userId := utilSvc.GetUserId(authHeader) userId := utilSvc.GetUserId(authHeader)
// Execute the next handler
err := c.Next() err := c.Next()
// Get status code - ensure it's set correctly for errors
statusCode := c.Response().StatusCode()
if err != nil {
// If error occurred, ensure status code reflects the error
// The error handler should have set this, but if not, default to 500
if statusCode == fiber.StatusOK || statusCode == 0 {
statusCode = fiber.StatusInternalServerError
}
}
// Get response body
responseBody := c.Response().Body()
// If response body is empty and there's an error, create error response
if len(responseBody) == 0 && err != nil {
// Create error response JSON matching the error handler format
errorResp := map[string]interface{}{
"success": false,
"code": statusCode,
"message": err.Error(),
}
if errorJSON, marshalErr := json.Marshal(errorResp); marshalErr == nil {
responseBody = errorJSON
} else {
responseBody = []byte(err.Error())
}
}
audit := entity.AuditTrails{ audit := entity.AuditTrails{
Method: c.Method(), Method: c.Method(),
Path: c.OriginalURL(), Path: c.OriginalURL(),
IP: getIP(c), IP: getIP(c),
Status: c.Response().StatusCode(), Status: statusCode,
UserID: userId, UserID: userId,
RequestHeaders: string(headersJSON), RequestHeaders: string(headersJSON),
RequestBody: string(requestBody), RequestBody: string(requestBody),
ResponseBody: string(c.Response().Body()), ResponseBody: string(responseBody),
DurationMs: time.Since(start).Milliseconds(), DurationMs: time.Since(start).Milliseconds(),
CreatedAt: time.Now(), CreatedAt: time.Now(),
} }
go db.Create(&audit) // Save audit trail - use goroutine to avoid blocking
// IMPORTANT: Save synchronously to ensure it completes even if app crashes
// Using goroutine but with proper error handling
go func(auditRecord entity.AuditTrails) {
if saveErr := db.Create(&auditRecord).Error; saveErr != nil {
log.Printf("Failed to save audit trail for %s %s (status: %d): %v",
auditRecord.Method, auditRecord.Path, auditRecord.Status, saveErr)
}
}(audit)
return err return err
} }

View File

@ -120,7 +120,7 @@ func (_i *aiChatController) GetSession(c *fiber.Ctx) error {
// @Description API for create AI chat session // @Description API for create AI chat session
// @Tags AI Chat // @Tags AI Chat
// @Security Bearer // @Security Bearer
// @Param X-Csrf-Token header string true "Insert the X-Csrf-Token" // @Param X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param Authorization header string false "Insert your access token" default(Bearer <Add access token here>) // @Param Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param payload body request.AIChatSessionsCreateRequest true "Required payload" // @Param payload body request.AIChatSessionsCreateRequest true "Required payload"
// @Success 200 {object} response.Response // @Success 200 {object} response.Response
@ -154,7 +154,7 @@ func (_i *aiChatController) CreateSession(c *fiber.Ctx) error {
// @Tags AI Chat // @Tags AI Chat
// @Security Bearer // @Security Bearer
// @Param Authorization header string false "Insert your access token" default(Bearer <Add access token here>) // @Param Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param X-Csrf-Token header string true "Insert the X-Csrf-Token" // @Param X-Csrf-Token header string false "Insert the X-Csrf-Token"
// @Param id path int true "Session ID" // @Param id path int true "Session ID"
// @Param payload body request.AIChatSessionsUpdateRequest true "Required payload" // @Param payload body request.AIChatSessionsUpdateRequest true "Required payload"
// @Success 200 {object} response.Response // @Success 200 {object} response.Response

View File

@ -1173,8 +1173,7 @@ const docTemplate = `{
"type": "string", "type": "string",
"description": "Insert the X-Csrf-Token", "description": "Insert the X-Csrf-Token",
"name": "X-Csrf-Token", "name": "X-Csrf-Token",
"in": "header", "in": "header"
"required": true
}, },
{ {
"type": "string", "type": "string",
@ -1497,8 +1496,7 @@ const docTemplate = `{
"type": "string", "type": "string",
"description": "Insert the X-Csrf-Token", "description": "Insert the X-Csrf-Token",
"name": "X-Csrf-Token", "name": "X-Csrf-Token",
"in": "header", "in": "header"
"required": true
}, },
{ {
"type": "integer", "type": "integer",
@ -15584,12 +15582,12 @@ const docTemplate = `{
"request.ChatScheduleCreateRequest": { "request.ChatScheduleCreateRequest": {
"type": "object", "type": "object",
"required": [ "required": [
"chat_session_id",
"scheduled_at", "scheduled_at",
"title" "title"
], ],
"properties": { "properties": {
"chat_session_id": { "chat_session_id": {
"description": "Optional - if empty, will create new chat session",
"type": "integer" "type": "integer"
}, },
"description": { "description": {

View File

@ -1162,8 +1162,7 @@
"type": "string", "type": "string",
"description": "Insert the X-Csrf-Token", "description": "Insert the X-Csrf-Token",
"name": "X-Csrf-Token", "name": "X-Csrf-Token",
"in": "header", "in": "header"
"required": true
}, },
{ {
"type": "string", "type": "string",
@ -1486,8 +1485,7 @@
"type": "string", "type": "string",
"description": "Insert the X-Csrf-Token", "description": "Insert the X-Csrf-Token",
"name": "X-Csrf-Token", "name": "X-Csrf-Token",
"in": "header", "in": "header"
"required": true
}, },
{ {
"type": "integer", "type": "integer",
@ -15573,12 +15571,12 @@
"request.ChatScheduleCreateRequest": { "request.ChatScheduleCreateRequest": {
"type": "object", "type": "object",
"required": [ "required": [
"chat_session_id",
"scheduled_at", "scheduled_at",
"title" "title"
], ],
"properties": { "properties": {
"chat_session_id": { "chat_session_id": {
"description": "Optional - if empty, will create new chat session",
"type": "integer" "type": "integer"
}, },
"description": { "description": {

View File

@ -410,6 +410,7 @@ definitions:
request.ChatScheduleCreateRequest: request.ChatScheduleCreateRequest:
properties: properties:
chat_session_id: chat_session_id:
description: Optional - if empty, will create new chat session
type: integer type: integer
description: description:
maxLength: 1000 maxLength: 1000
@ -434,7 +435,6 @@ definitions:
minLength: 3 minLength: 3
type: string type: string
required: required:
- chat_session_id
- scheduled_at - scheduled_at
- title - title
type: object type: object
@ -2080,7 +2080,6 @@ paths:
- description: Insert the X-Csrf-Token - description: Insert the X-Csrf-Token
in: header in: header
name: X-Csrf-Token name: X-Csrf-Token
required: true
type: string type: string
- default: Bearer <Add access token here> - default: Bearer <Add access token here>
description: Insert your access token description: Insert your access token
@ -2202,7 +2201,6 @@ paths:
- description: Insert the X-Csrf-Token - description: Insert the X-Csrf-Token
in: header in: header
name: X-Csrf-Token name: X-Csrf-Token
required: true
type: string type: string
- description: Session ID - description: Session ID
in: path in: path