fix: improvement log record ads tih ip, useragent, and uer_id
This commit is contained in:
@@ -7,30 +7,22 @@ import (
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func CreateLogAds(
|
||||
router chi.Router,
|
||||
userSvc usersvc.User,
|
||||
logsSvc logssvc.Log,
|
||||
validate *validator.Validate,
|
||||
) {
|
||||
router.Post("/logs/ads", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
// userDetail, err := utils.GetTokenDetail(r)
|
||||
// if err != nil {
|
||||
// response.RespondJsonErrorWithCode(
|
||||
// ctx,
|
||||
// w,
|
||||
// err,
|
||||
// response.ErrUnauthorized.Code,
|
||||
// response.ErrUnauthorized.HttpCode,
|
||||
// "unauthorized",
|
||||
// )
|
||||
// return
|
||||
// }
|
||||
var specReq logsdomain.LogsSpec
|
||||
|
||||
var spec logsdomain.LogsRequest
|
||||
|
||||
@@ -46,7 +38,41 @@ func CreateLogAds(
|
||||
return
|
||||
}
|
||||
|
||||
if err := logsSvc.CreateLogAds(spec.AdsID); err != nil {
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
specReq.ContentID = spec.AdsID
|
||||
|
||||
userDetail, _ := utils.GetTokenDetail(r)
|
||||
|
||||
if userDetail.ID != "" {
|
||||
specReq.UserID = &userDetail.ID
|
||||
}
|
||||
|
||||
ip := r.RemoteAddr
|
||||
|
||||
if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
|
||||
ip = strings.Split(forwarded, ",")[0]
|
||||
}
|
||||
|
||||
if ip != "" {
|
||||
specReq.IP = &ip
|
||||
}
|
||||
|
||||
if userAgent := r.UserAgent(); userAgent != "" {
|
||||
specReq.UserAgent = &userAgent
|
||||
}
|
||||
|
||||
if err := logsSvc.CreateLogAds(specReq); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
|
||||
Reference in New Issue
Block a user