2025-11-15 17:43:23 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-19 04:18:19 +00:00
|
|
|
"context"
|
2025-11-15 17:43:23 +00:00
|
|
|
"errors"
|
2025-11-19 04:18:19 +00:00
|
|
|
"fmt"
|
2025-11-15 17:43:23 +00:00
|
|
|
"jaecoo-be/app/module/sales_agents/mapper"
|
|
|
|
|
"jaecoo-be/app/module/sales_agents/repository"
|
|
|
|
|
"jaecoo-be/app/module/sales_agents/request"
|
|
|
|
|
"jaecoo-be/app/module/sales_agents/response"
|
|
|
|
|
"jaecoo-be/config/config"
|
2025-11-19 04:18:19 +00:00
|
|
|
minioStorage "jaecoo-be/config/config"
|
2025-11-15 17:43:23 +00:00
|
|
|
"jaecoo-be/utils/paginator"
|
2025-11-19 04:18:19 +00:00
|
|
|
"math/rand"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
2025-11-15 17:43:23 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type salesAgentsService struct {
|
2025-11-19 04:18:19 +00:00
|
|
|
Repo repository.SalesAgentsRepository
|
|
|
|
|
Log zerolog.Logger
|
|
|
|
|
Cfg *config.Config
|
|
|
|
|
MinioStorage *minioStorage.MinioStorage
|
2025-11-15 17:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SalesAgentsService interface {
|
|
|
|
|
GetAll(req request.SalesAgentsQueryRequest) (agents []*response.SalesAgentsResponse, paging paginator.Pagination, err error)
|
|
|
|
|
GetOne(id uint) (agent *response.SalesAgentsResponse, err error)
|
2025-11-19 04:18:19 +00:00
|
|
|
Create(c *fiber.Ctx, req request.SalesAgentsCreateRequest) (agent *response.SalesAgentsResponse, err error)
|
|
|
|
|
Update(c *fiber.Ctx, id uint, req request.SalesAgentsUpdateRequest) (agent *response.SalesAgentsResponse, err error)
|
2025-11-15 17:43:23 +00:00
|
|
|
Delete(id uint) (err error)
|
2025-11-19 04:18:19 +00:00
|
|
|
UploadFileToMinio(c *fiber.Ctx, fileKey string) (filePath *string, err error)
|
2025-11-15 17:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-19 04:18:19 +00:00
|
|
|
func NewSalesAgentsService(repo repository.SalesAgentsRepository, log zerolog.Logger, cfg *config.Config, minioStorage *minioStorage.MinioStorage) SalesAgentsService {
|
2025-11-15 17:43:23 +00:00
|
|
|
return &salesAgentsService{
|
2025-11-19 04:18:19 +00:00
|
|
|
Repo: repo,
|
|
|
|
|
Log: log,
|
|
|
|
|
Cfg: cfg,
|
|
|
|
|
MinioStorage: minioStorage,
|
2025-11-15 17:43:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *salesAgentsService) GetAll(req request.SalesAgentsQueryRequest) (agents []*response.SalesAgentsResponse, paging paginator.Pagination, err error) {
|
|
|
|
|
agentsEntity, paging, err := _i.Repo.GetAll(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
|
|
|
|
|
|
for _, agent := range agentsEntity {
|
|
|
|
|
agents = append(agents, mapper.SalesAgentsResponseMapper(agent, host))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *salesAgentsService) GetOne(id uint) (agent *response.SalesAgentsResponse, err error) {
|
|
|
|
|
agentEntity, err := _i.Repo.FindOne(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if agentEntity == nil {
|
|
|
|
|
err = errors.New("sales agent not found")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
|
|
|
|
|
|
agent = mapper.SalesAgentsResponseMapper(agentEntity, host)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-19 04:18:19 +00:00
|
|
|
func (_i *salesAgentsService) Create(c *fiber.Ctx, req request.SalesAgentsCreateRequest) (agent *response.SalesAgentsResponse, err error) {
|
|
|
|
|
// Handle file upload if exists
|
|
|
|
|
if filePath, uploadErr := _i.UploadFileToMinio(c, "file"); uploadErr == nil && filePath != nil {
|
|
|
|
|
req.ProfilePicturePath = filePath
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-15 17:43:23 +00:00
|
|
|
agentEntity := req.ToEntity()
|
|
|
|
|
isActive := true
|
|
|
|
|
agentEntity.IsActive = &isActive
|
|
|
|
|
|
|
|
|
|
agentEntity, err = _i.Repo.Create(agentEntity)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
|
|
|
|
|
|
agent = mapper.SalesAgentsResponseMapper(agentEntity, host)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-19 04:18:19 +00:00
|
|
|
func (_i *salesAgentsService) Update(c *fiber.Ctx, id uint, req request.SalesAgentsUpdateRequest) (agent *response.SalesAgentsResponse, err error) {
|
|
|
|
|
// Handle file upload if exists
|
|
|
|
|
if filePath, uploadErr := _i.UploadFileToMinio(c, "file"); uploadErr == nil && filePath != nil {
|
|
|
|
|
req.ProfilePicturePath = filePath
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-15 17:43:23 +00:00
|
|
|
agentEntity := req.ToEntity()
|
|
|
|
|
|
|
|
|
|
err = _i.Repo.Update(id, agentEntity)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
agentEntity, err = _i.Repo.FindOne(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
|
|
|
|
|
|
agent = mapper.SalesAgentsResponseMapper(agentEntity, host)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-19 04:18:19 +00:00
|
|
|
func (_i *salesAgentsService) UploadFileToMinio(c *fiber.Ctx, fileKey string) (filePath *string, err error) {
|
|
|
|
|
form, err := c.MultipartForm()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
files := form.File[fileKey]
|
|
|
|
|
if len(files) == 0 {
|
|
|
|
|
return nil, nil // No file uploaded, return nil without error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileHeader := files[0]
|
|
|
|
|
|
|
|
|
|
// Create minio connection
|
|
|
|
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
|
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
|
src, err := fileHeader.Open()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
|
|
// Process filename
|
|
|
|
|
filename := filepath.Base(fileHeader.Filename)
|
|
|
|
|
filename = strings.ReplaceAll(filename, " ", "")
|
|
|
|
|
filenameWithoutExt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
|
|
|
|
extension := filepath.Ext(fileHeader.Filename)[1:]
|
|
|
|
|
|
|
|
|
|
// Generate unique filename
|
|
|
|
|
now := time.Now()
|
|
|
|
|
rand.New(rand.NewSource(now.UnixNano()))
|
|
|
|
|
randUniqueId := rand.Intn(1000000)
|
|
|
|
|
|
|
|
|
|
newFilenameWithoutExt := filenameWithoutExt + "_" + strconv.Itoa(randUniqueId)
|
|
|
|
|
newFilename := newFilenameWithoutExt + "." + extension
|
|
|
|
|
|
|
|
|
|
// Create object name with path structure
|
|
|
|
|
objectName := fmt.Sprintf("sales_agents/upload/%d/%d/%s", now.Year(), now.Month(), newFilename)
|
|
|
|
|
|
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "SalesAgents:UploadFileToMinio").
|
|
|
|
|
Interface("Uploading file", objectName).Msg("")
|
|
|
|
|
|
|
|
|
|
// Upload file to MinIO
|
|
|
|
|
_, err = minioClient.PutObject(context.Background(), bucketName, objectName, src, fileHeader.Size, minio.PutObjectOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
_i.Log.Error().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "SalesAgents:UploadFileToMinio").
|
|
|
|
|
Interface("Error uploading file", err).Msg("")
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "SalesAgents:UploadFileToMinio").
|
|
|
|
|
Interface("Successfully uploaded", objectName).Msg("")
|
|
|
|
|
|
|
|
|
|
return &objectName, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-15 17:43:23 +00:00
|
|
|
func (_i *salesAgentsService) Delete(id uint) (err error) {
|
|
|
|
|
err = _i.Repo.Delete(id)
|
|
|
|
|
return
|
|
|
|
|
}
|