user devices
This commit is contained in:
parent
23b6293502
commit
bbd6666299
@ -105,6 +105,8 @@ func (a *App) Initialize(cfg *config.Config) error {
|
|||||||
services.customerPointsService,
|
services.customerPointsService,
|
||||||
services.spinGameService,
|
services.spinGameService,
|
||||||
middleware.customerAuthMiddleware,
|
middleware.customerAuthMiddleware,
|
||||||
|
services.userDeviceService,
|
||||||
|
validators.userDeviceValidator,
|
||||||
)
|
)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -192,6 +194,7 @@ type repositories struct {
|
|||||||
customerPointsRepo repository.CustomerPointsRepository
|
customerPointsRepo repository.CustomerPointsRepository
|
||||||
otpRepo repository.OtpRepository
|
otpRepo repository.OtpRepository
|
||||||
txManager *repository.TxManager
|
txManager *repository.TxManager
|
||||||
|
userDeviceRepo *repository.UserDeviceRepositoryImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) initRepositories() *repositories {
|
func (a *App) initRepositories() *repositories {
|
||||||
@ -238,6 +241,7 @@ func (a *App) initRepositories() *repositories {
|
|||||||
customerPointsRepo: repository.NewCustomerPointsRepository(a.db),
|
customerPointsRepo: repository.NewCustomerPointsRepository(a.db),
|
||||||
otpRepo: repository.NewOtpRepository(a.db),
|
otpRepo: repository.NewOtpRepository(a.db),
|
||||||
txManager: repository.NewTxManager(a.db),
|
txManager: repository.NewTxManager(a.db),
|
||||||
|
userDeviceRepo: repository.NewUserDeviceRepositoryImpl(a.db),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,6 +284,7 @@ type processors struct {
|
|||||||
otpProcessor processor.OtpProcessor
|
otpProcessor processor.OtpProcessor
|
||||||
fileClient processor.FileClient
|
fileClient processor.FileClient
|
||||||
inventoryMovementService service.InventoryMovementService
|
inventoryMovementService service.InventoryMovementService
|
||||||
|
userDeviceProcessor *processor.UserDeviceProcessorImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) initProcessors(cfg *config.Config, repos *repositories) *processors {
|
func (a *App) initProcessors(cfg *config.Config, repos *repositories) *processors {
|
||||||
@ -327,6 +332,7 @@ func (a *App) initProcessors(cfg *config.Config, repos *repositories) *processor
|
|||||||
otpProcessor: otpProcessor,
|
otpProcessor: otpProcessor,
|
||||||
fileClient: fileClient,
|
fileClient: fileClient,
|
||||||
inventoryMovementService: inventoryMovementService,
|
inventoryMovementService: inventoryMovementService,
|
||||||
|
userDeviceProcessor: processor.NewUserDeviceProcessorImpl(repos.userDeviceRepo),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -363,11 +369,12 @@ type services struct {
|
|||||||
customerAuthService service.CustomerAuthService
|
customerAuthService service.CustomerAuthService
|
||||||
customerPointsService service.CustomerPointsService
|
customerPointsService service.CustomerPointsService
|
||||||
spinGameService service.SpinGameService
|
spinGameService service.SpinGameService
|
||||||
|
userDeviceService service.UserDeviceService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) initServices(processors *processors, repos *repositories, cfg *config.Config) *services {
|
func (a *App) initServices(processors *processors, repos *repositories, cfg *config.Config) *services {
|
||||||
authConfig := cfg.Auth()
|
authConfig := cfg.Auth()
|
||||||
authService := service.NewAuthService(processors.userProcessor, authConfig)
|
authService := service.NewAuthService(processors.userProcessor, processors.userDeviceProcessor, authConfig)
|
||||||
organizationService := service.NewOrganizationService(processors.organizationProcessor)
|
organizationService := service.NewOrganizationService(processors.organizationProcessor)
|
||||||
outletService := service.NewOutletService(processors.outletProcessor)
|
outletService := service.NewOutletService(processors.outletProcessor)
|
||||||
outletSettingService := service.NewOutletSettingService(processors.outletSettingProcessor)
|
outletSettingService := service.NewOutletSettingService(processors.outletSettingProcessor)
|
||||||
@ -398,6 +405,7 @@ func (a *App) initServices(processors *processors, repos *repositories, cfg *con
|
|||||||
customerAuthService := service.NewCustomerAuthService(processors.customerAuthProcessor)
|
customerAuthService := service.NewCustomerAuthService(processors.customerAuthProcessor)
|
||||||
customerPointsService := service.NewCustomerPointsService(processors.customerPointsProcessor)
|
customerPointsService := service.NewCustomerPointsService(processors.customerPointsProcessor)
|
||||||
spinGameService := service.NewSpinGameService(processors.gamePlayProcessor, repos.txManager)
|
spinGameService := service.NewSpinGameService(processors.gamePlayProcessor, repos.txManager)
|
||||||
|
userDeviceService := service.NewUserDeviceService(processors.userDeviceProcessor)
|
||||||
|
|
||||||
// Update order service with order ingredient transaction service
|
// Update order service with order ingredient transaction service
|
||||||
orderService = service.NewOrderServiceImpl(processors.orderProcessor, repos.tableRepo, orderIngredientTransactionService, processors.orderIngredientTransactionProcessor, *repos.productRecipeRepo, repos.txManager)
|
orderService = service.NewOrderServiceImpl(processors.orderProcessor, repos.tableRepo, orderIngredientTransactionService, processors.orderIngredientTransactionProcessor, *repos.productRecipeRepo, repos.txManager)
|
||||||
@ -435,6 +443,7 @@ func (a *App) initServices(processors *processors, repos *repositories, cfg *con
|
|||||||
customerAuthService: customerAuthService,
|
customerAuthService: customerAuthService,
|
||||||
customerPointsService: customerPointsService,
|
customerPointsService: customerPointsService,
|
||||||
spinGameService: spinGameService,
|
spinGameService: spinGameService,
|
||||||
|
userDeviceService: userDeviceService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -474,6 +483,7 @@ type validators struct {
|
|||||||
rewardValidator validator.RewardValidator
|
rewardValidator validator.RewardValidator
|
||||||
campaignValidator validator.CampaignValidator
|
campaignValidator validator.CampaignValidator
|
||||||
customerAuthValidator validator.CustomerAuthValidator
|
customerAuthValidator validator.CustomerAuthValidator
|
||||||
|
userDeviceValidator *validator.UserDeviceValidatorImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) initValidators() *validators {
|
func (a *App) initValidators() *validators {
|
||||||
@ -501,5 +511,6 @@ func (a *App) initValidators() *validators {
|
|||||||
rewardValidator: validator.NewRewardValidator(),
|
rewardValidator: validator.NewRewardValidator(),
|
||||||
campaignValidator: validator.NewCampaignValidator(),
|
campaignValidator: validator.NewCampaignValidator(),
|
||||||
customerAuthValidator: validator.NewCustomerAuthValidator(),
|
customerAuthValidator: validator.NewCustomerAuthValidator(),
|
||||||
|
userDeviceValidator: validator.NewUserDeviceValidator(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,18 +44,19 @@ const (
|
|||||||
IngredientCompositionServiceEntity = "ingredient_composition_service"
|
IngredientCompositionServiceEntity = "ingredient_composition_service"
|
||||||
TableEntity = "table"
|
TableEntity = "table"
|
||||||
// Gamification entities
|
// Gamification entities
|
||||||
CustomerPointsEntity = "customer_points"
|
CustomerPointsEntity = "customer_points"
|
||||||
CustomerTokensEntity = "customer_tokens"
|
CustomerTokensEntity = "customer_tokens"
|
||||||
TierEntity = "tier"
|
TierEntity = "tier"
|
||||||
GameEntity = "game"
|
GameEntity = "game"
|
||||||
GamePrizeEntity = "game_prize"
|
GamePrizeEntity = "game_prize"
|
||||||
GamePlayEntity = "game_play"
|
GamePlayEntity = "game_play"
|
||||||
OmsetTrackerEntity = "omset_tracker"
|
OmsetTrackerEntity = "omset_tracker"
|
||||||
RewardEntity = "reward"
|
RewardEntity = "reward"
|
||||||
CampaignEntity = "campaign"
|
CampaignEntity = "campaign"
|
||||||
CampaignRuleEntity = "campaign_rule"
|
CampaignRuleEntity = "campaign_rule"
|
||||||
CustomerEntity = "customer"
|
CustomerEntity = "customer"
|
||||||
SpinGameHandlerEntity = "spin_game_handler"
|
SpinGameHandlerEntity = "spin_game_handler"
|
||||||
|
UserDeviceServiceEntity = "user_device_service"
|
||||||
)
|
)
|
||||||
|
|
||||||
var HttpErrorMap = map[string]int{
|
var HttpErrorMap = map[string]int{
|
||||||
|
|||||||
@ -35,16 +35,23 @@ type UpdateUserOutletRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LoginRequest struct {
|
type LoginRequest struct {
|
||||||
Email string `json:"email" validate:"required,email"`
|
Email string `json:"email" validate:"required,email"`
|
||||||
Password string `json:"password" validate:"required"`
|
Password string `json:"password" validate:"required"`
|
||||||
|
DeviceID string `json:"device_id,omitempty"`
|
||||||
|
DeviceName string `json:"device_name,omitempty"`
|
||||||
|
DeviceType string `json:"device_type,omitempty"`
|
||||||
|
Platform string `json:"platform,omitempty"`
|
||||||
|
FCMToken string `json:"fcm_token,omitempty"`
|
||||||
|
AppVersion string `json:"app_version,omitempty"`
|
||||||
|
OsVersion string `json:"os_version,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LoginResponse struct {
|
type LoginResponse struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
RefreshToken string `json:"refresh_token"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
ExpiresAt time.Time `json:"expires_at"`
|
ExpiresAt time.Time `json:"expires_at"`
|
||||||
RefreshExpiresAt time.Time `json:"refresh_expires_at"`
|
RefreshExpiresAt time.Time `json:"refresh_expires_at"`
|
||||||
User UserResponse `json:"user"`
|
User UserResponse `json:"user"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserResponse struct {
|
type UserResponse struct {
|
||||||
|
|||||||
59
internal/contract/user_device_contract.go
Normal file
59
internal/contract/user_device_contract.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package contract
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"apskel-pos-be/internal/entities"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RegisterUserDeviceRequest struct {
|
||||||
|
DeviceID string `json:"device_id" validate:"required,min=1,max=255"`
|
||||||
|
DeviceName string `json:"device_name,omitempty" validate:"omitempty,max=255"`
|
||||||
|
DeviceType entities.DeviceType `json:"device_type,omitempty" validate:"omitempty,oneof=mobile tablet desktop"`
|
||||||
|
Platform entities.DevicePlatform `json:"platform,omitempty" validate:"omitempty,oneof=android ios web"`
|
||||||
|
FCMToken string `json:"fcm_token,omitempty" validate:"omitempty,max=512"`
|
||||||
|
AppVersion string `json:"app_version,omitempty" validate:"omitempty,max=50"`
|
||||||
|
OsVersion string `json:"os_version,omitempty" validate:"omitempty,max=50"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateUserDeviceRequest struct {
|
||||||
|
DeviceName string `json:"device_name,omitempty" validate:"omitempty,max=255"`
|
||||||
|
DeviceType entities.DeviceType `json:"device_type,omitempty" validate:"omitempty,oneof=mobile tablet desktop"`
|
||||||
|
Platform entities.DevicePlatform `json:"platform,omitempty" validate:"omitempty,oneof=android ios web"`
|
||||||
|
FCMToken string `json:"fcm_token,omitempty" validate:"omitempty,max=512"`
|
||||||
|
AppVersion string `json:"app_version,omitempty" validate:"omitempty,max=50"`
|
||||||
|
OsVersion string `json:"os_version,omitempty" validate:"omitempty,max=50"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserDeviceResponse struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
DeviceID string `json:"device_id"`
|
||||||
|
DeviceName string `json:"device_name"`
|
||||||
|
DeviceType entities.DeviceType `json:"device_type"`
|
||||||
|
Platform entities.DevicePlatform `json:"platform"`
|
||||||
|
FCMToken string `json:"fcm_token"`
|
||||||
|
AppVersion string `json:"app_version"`
|
||||||
|
OsVersion string `json:"os_version"`
|
||||||
|
IPAddress string `json:"ip_address"`
|
||||||
|
LastActiveAt *time.Time `json:"last_active_at"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListUserDevicesRequest struct {
|
||||||
|
Page int `json:"page" validate:"min=1"`
|
||||||
|
Limit int `json:"limit" validate:"min=1,max=100"`
|
||||||
|
UserID string `json:"user_id,omitempty"`
|
||||||
|
Platform string `json:"platform,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListUserDevicesResponse struct {
|
||||||
|
Devices []UserDeviceResponse `json:"devices"`
|
||||||
|
TotalCount int `json:"total_count"`
|
||||||
|
Page int `json:"page"`
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
TotalPages int `json:"total_pages"`
|
||||||
|
}
|
||||||
@ -36,6 +36,7 @@ func GetAllEntities() []interface{} {
|
|||||||
&CampaignRule{},
|
&CampaignRule{},
|
||||||
&OtpSession{},
|
&OtpSession{},
|
||||||
// Analytics entities are not database tables, they are query results
|
// Analytics entities are not database tables, they are query results
|
||||||
|
&UserDevice{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
50
internal/entities/user_device.go
Normal file
50
internal/entities/user_device.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DeviceType string
|
||||||
|
type DevicePlatform string
|
||||||
|
|
||||||
|
const (
|
||||||
|
DeviceTypeMobile DeviceType = "mobile"
|
||||||
|
DeviceTypeTablet DeviceType = "tablet"
|
||||||
|
DeviceTypeDesktop DeviceType = "desktop"
|
||||||
|
|
||||||
|
DevicePlatformAndroid DevicePlatform = "android"
|
||||||
|
DevicePlatformIOS DevicePlatform = "ios"
|
||||||
|
DevicePlatformWeb DevicePlatform = "web"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserDevice struct {
|
||||||
|
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
|
||||||
|
UserID uuid.UUID `gorm:"type:uuid;not null;index" json:"user_id"`
|
||||||
|
DeviceID string `gorm:"not null;size:255;index" json:"device_id"`
|
||||||
|
DeviceName string `gorm:"size:255" json:"device_name"`
|
||||||
|
DeviceType DeviceType `gorm:"size:50" json:"device_type"`
|
||||||
|
Platform DevicePlatform `gorm:"size:50" json:"platform"`
|
||||||
|
FCMToken string `gorm:"size:512" json:"fcm_token"`
|
||||||
|
AppVersion string `gorm:"size:50" json:"app_version"`
|
||||||
|
OsVersion string `gorm:"size:50" json:"os_version"`
|
||||||
|
IPAddress string `gorm:"size:45" json:"ip_address"`
|
||||||
|
LastActiveAt *time.Time `gorm:"type:timestamptz" json:"last_active_at"`
|
||||||
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||||
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||||
|
|
||||||
|
User User `gorm:"foreignKey:UserID" json:"user,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserDevice) BeforeCreate(tx *gorm.DB) error {
|
||||||
|
if u.ID == uuid.Nil {
|
||||||
|
u.ID = uuid.New()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (UserDevice) TableName() string {
|
||||||
|
return "user_devices"
|
||||||
|
}
|
||||||
215
internal/handler/user_device_handler.go
Normal file
215
internal/handler/user_device_handler.go
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"apskel-pos-be/internal/appcontext"
|
||||||
|
"apskel-pos-be/internal/constants"
|
||||||
|
"apskel-pos-be/internal/contract"
|
||||||
|
"apskel-pos-be/internal/logger"
|
||||||
|
"apskel-pos-be/internal/service"
|
||||||
|
"apskel-pos-be/internal/util"
|
||||||
|
"apskel-pos-be/internal/validator"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserDeviceHandler struct {
|
||||||
|
userDeviceService service.UserDeviceService
|
||||||
|
userDeviceValidator validator.UserDeviceValidator
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserDeviceHandler(
|
||||||
|
userDeviceService service.UserDeviceService,
|
||||||
|
userDeviceValidator validator.UserDeviceValidator,
|
||||||
|
) *UserDeviceHandler {
|
||||||
|
return &UserDeviceHandler{
|
||||||
|
userDeviceService: userDeviceService,
|
||||||
|
userDeviceValidator: userDeviceValidator,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *UserDeviceHandler) RegisterDevice(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
contextInfo := appcontext.FromGinContext(ctx)
|
||||||
|
|
||||||
|
var req contract.RegisterUserDeviceRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
logger.FromContext(ctx).WithError(err).Error("UserDeviceHandler::RegisterDevice -> request binding failed")
|
||||||
|
validationResponseError := contract.NewResponseError(constants.MissingFieldErrorCode, constants.RequestEntity, err.Error())
|
||||||
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "UserDeviceHandler::RegisterDevice")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
validationError, validationErrorCode := h.userDeviceValidator.ValidateRegisterDeviceRequest(&req)
|
||||||
|
if validationError != nil {
|
||||||
|
validationResponseError := contract.NewResponseError(validationErrorCode, constants.RequestEntity, validationError.Error())
|
||||||
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "UserDeviceHandler::RegisterDevice")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceResponse := h.userDeviceService.RegisterDevice(ctx, contextInfo.UserID, &req)
|
||||||
|
if deviceResponse.HasErrors() {
|
||||||
|
errorResp := deviceResponse.GetErrors()[0]
|
||||||
|
logger.FromContext(ctx).WithError(errorResp).Error("UserDeviceHandler::RegisterDevice -> Failed to register device from service")
|
||||||
|
}
|
||||||
|
|
||||||
|
util.HandleResponse(c.Writer, c.Request, deviceResponse, "UserDeviceHandler::RegisterDevice")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *UserDeviceHandler) UpdateDevice(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
|
||||||
|
deviceIDStr := c.Param("id")
|
||||||
|
deviceID, err := uuid.Parse(deviceIDStr)
|
||||||
|
if err != nil {
|
||||||
|
logger.FromContext(ctx).WithError(err).Error("UserDeviceHandler::UpdateDevice -> Invalid device ID")
|
||||||
|
validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid device ID")
|
||||||
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "UserDeviceHandler::UpdateDevice")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req contract.UpdateUserDeviceRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
logger.FromContext(ctx).WithError(err).Error("UserDeviceHandler::UpdateDevice -> request binding failed")
|
||||||
|
validationResponseError := contract.NewResponseError(constants.MissingFieldErrorCode, constants.RequestEntity, "Invalid request body")
|
||||||
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "UserDeviceHandler::UpdateDevice")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
validationError, validationErrorCode := h.userDeviceValidator.ValidateUpdateDeviceRequest(&req)
|
||||||
|
if validationError != nil {
|
||||||
|
validationResponseError := contract.NewResponseError(validationErrorCode, constants.RequestEntity, validationError.Error())
|
||||||
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "UserDeviceHandler::UpdateDevice")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceResponse := h.userDeviceService.UpdateDevice(ctx, deviceID, &req)
|
||||||
|
if deviceResponse.HasErrors() {
|
||||||
|
errorResp := deviceResponse.GetErrors()[0]
|
||||||
|
logger.FromContext(ctx).WithError(errorResp).Error("UserDeviceHandler::UpdateDevice -> Failed to update device from service")
|
||||||
|
}
|
||||||
|
|
||||||
|
util.HandleResponse(c.Writer, c.Request, deviceResponse, "UserDeviceHandler::UpdateDevice")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *UserDeviceHandler) DeleteDevice(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
|
||||||
|
deviceIDStr := c.Param("id")
|
||||||
|
deviceID, err := uuid.Parse(deviceIDStr)
|
||||||
|
if err != nil {
|
||||||
|
logger.FromContext(ctx).WithError(err).Error("UserDeviceHandler::DeleteDevice -> Invalid device ID")
|
||||||
|
validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid device ID")
|
||||||
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "UserDeviceHandler::DeleteDevice")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceResponse := h.userDeviceService.DeleteDevice(ctx, deviceID)
|
||||||
|
if deviceResponse.HasErrors() {
|
||||||
|
errorResp := deviceResponse.GetErrors()[0]
|
||||||
|
logger.FromContext(ctx).WithError(errorResp).Error("UserDeviceHandler::DeleteDevice -> Failed to delete device from service")
|
||||||
|
}
|
||||||
|
|
||||||
|
util.HandleResponse(c.Writer, c.Request, deviceResponse, "UserDeviceHandler::DeleteDevice")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *UserDeviceHandler) GetDevice(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
|
||||||
|
deviceIDStr := c.Param("id")
|
||||||
|
deviceID, err := uuid.Parse(deviceIDStr)
|
||||||
|
if err != nil {
|
||||||
|
logger.FromContext(ctx).WithError(err).Error("UserDeviceHandler::GetDevice -> Invalid device ID")
|
||||||
|
validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid device ID")
|
||||||
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "UserDeviceHandler::GetDevice")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceResponse := h.userDeviceService.GetDeviceByID(ctx, deviceID)
|
||||||
|
if deviceResponse.HasErrors() {
|
||||||
|
errorResp := deviceResponse.GetErrors()[0]
|
||||||
|
logger.FromContext(ctx).WithError(errorResp).Error("UserDeviceHandler::GetDevice -> Failed to get device from service")
|
||||||
|
}
|
||||||
|
|
||||||
|
util.HandleResponse(c.Writer, c.Request, deviceResponse, "UserDeviceHandler::GetDevice")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *UserDeviceHandler) GetMyDevices(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
contextInfo := appcontext.FromGinContext(ctx)
|
||||||
|
|
||||||
|
deviceResponse := h.userDeviceService.GetDevicesByUserID(ctx, contextInfo.UserID)
|
||||||
|
if deviceResponse.HasErrors() {
|
||||||
|
errorResp := deviceResponse.GetErrors()[0]
|
||||||
|
logger.FromContext(ctx).WithError(errorResp).Error("UserDeviceHandler::GetMyDevices -> Failed to get devices from service")
|
||||||
|
}
|
||||||
|
|
||||||
|
util.HandleResponse(c.Writer, c.Request, deviceResponse, "UserDeviceHandler::GetMyDevices")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *UserDeviceHandler) GetDevicesByUser(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
|
||||||
|
userIDStr := c.Param("user_id")
|
||||||
|
userID, err := uuid.Parse(userIDStr)
|
||||||
|
if err != nil {
|
||||||
|
logger.FromContext(ctx).WithError(err).Error("UserDeviceHandler::GetDevicesByUser -> Invalid user ID")
|
||||||
|
validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid user ID")
|
||||||
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "UserDeviceHandler::GetDevicesByUser")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceResponse := h.userDeviceService.GetDevicesByUserID(ctx, userID)
|
||||||
|
if deviceResponse.HasErrors() {
|
||||||
|
errorResp := deviceResponse.GetErrors()[0]
|
||||||
|
logger.FromContext(ctx).WithError(errorResp).Error("UserDeviceHandler::GetDevicesByUser -> Failed to get devices from service")
|
||||||
|
}
|
||||||
|
|
||||||
|
util.HandleResponse(c.Writer, c.Request, deviceResponse, "UserDeviceHandler::GetDevicesByUser")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *UserDeviceHandler) ListDevices(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
|
||||||
|
req := &contract.ListUserDevicesRequest{
|
||||||
|
Page: 1,
|
||||||
|
Limit: 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
if pageStr := c.Query("page"); pageStr != "" {
|
||||||
|
if page, err := strconv.Atoi(pageStr); err == nil {
|
||||||
|
req.Page = page
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if limitStr := c.Query("limit"); limitStr != "" {
|
||||||
|
if limit, err := strconv.Atoi(limitStr); err == nil {
|
||||||
|
req.Limit = limit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if userID := c.Query("user_id"); userID != "" {
|
||||||
|
req.UserID = userID
|
||||||
|
}
|
||||||
|
|
||||||
|
if platform := c.Query("platform"); platform != "" {
|
||||||
|
req.Platform = platform
|
||||||
|
}
|
||||||
|
|
||||||
|
validationError, validationErrorCode := h.userDeviceValidator.ValidateListDevicesRequest(req)
|
||||||
|
if validationError != nil {
|
||||||
|
validationResponseError := contract.NewResponseError(validationErrorCode, constants.RequestEntity, validationError.Error())
|
||||||
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "UserDeviceHandler::ListDevices")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceResponse := h.userDeviceService.ListDevices(ctx, req)
|
||||||
|
if deviceResponse.HasErrors() {
|
||||||
|
errorResp := deviceResponse.GetErrors()[0]
|
||||||
|
logger.FromContext(ctx).WithError(errorResp).Error("UserDeviceHandler::ListDevices -> Failed to list devices from service")
|
||||||
|
}
|
||||||
|
|
||||||
|
util.HandleResponse(c.Writer, c.Request, deviceResponse, "UserDeviceHandler::ListDevices")
|
||||||
|
}
|
||||||
62
internal/mappers/user_device_mapper.go
Normal file
62
internal/mappers/user_device_mapper.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package mappers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"apskel-pos-be/internal/entities"
|
||||||
|
"apskel-pos-be/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UserDeviceEntityToModel(entity *entities.UserDevice) *models.UserDevice {
|
||||||
|
if entity == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &models.UserDevice{
|
||||||
|
ID: entity.ID,
|
||||||
|
UserID: entity.UserID,
|
||||||
|
DeviceID: entity.DeviceID,
|
||||||
|
DeviceName: entity.DeviceName,
|
||||||
|
DeviceType: entity.DeviceType,
|
||||||
|
Platform: entity.Platform,
|
||||||
|
FCMToken: entity.FCMToken,
|
||||||
|
AppVersion: entity.AppVersion,
|
||||||
|
OsVersion: entity.OsVersion,
|
||||||
|
IPAddress: entity.IPAddress,
|
||||||
|
LastActiveAt: entity.LastActiveAt,
|
||||||
|
CreatedAt: entity.CreatedAt,
|
||||||
|
UpdatedAt: entity.UpdatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UserDeviceEntityToResponse(entity *entities.UserDevice) *models.UserDeviceResponse {
|
||||||
|
if entity == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &models.UserDeviceResponse{
|
||||||
|
ID: entity.ID,
|
||||||
|
UserID: entity.UserID,
|
||||||
|
DeviceID: entity.DeviceID,
|
||||||
|
DeviceName: entity.DeviceName,
|
||||||
|
DeviceType: entity.DeviceType,
|
||||||
|
Platform: entity.Platform,
|
||||||
|
FCMToken: entity.FCMToken,
|
||||||
|
AppVersion: entity.AppVersion,
|
||||||
|
OsVersion: entity.OsVersion,
|
||||||
|
IPAddress: entity.IPAddress,
|
||||||
|
LastActiveAt: entity.LastActiveAt,
|
||||||
|
CreatedAt: entity.CreatedAt,
|
||||||
|
UpdatedAt: entity.UpdatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UserDeviceEntitiesToResponses(entities []*entities.UserDevice) []*models.UserDeviceResponse {
|
||||||
|
if entities == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
responses := make([]*models.UserDeviceResponse, len(entities))
|
||||||
|
for i, entity := range entities {
|
||||||
|
responses[i] = UserDeviceEntityToResponse(entity)
|
||||||
|
}
|
||||||
|
return responses
|
||||||
|
}
|
||||||
77
internal/models/user_device.go
Normal file
77
internal/models/user_device.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"apskel-pos-be/internal/entities"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserDevice struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
DeviceID string `json:"device_id"`
|
||||||
|
DeviceName string `json:"device_name"`
|
||||||
|
DeviceType entities.DeviceType `json:"device_type"`
|
||||||
|
Platform entities.DevicePlatform `json:"platform"`
|
||||||
|
FCMToken string `json:"fcm_token"`
|
||||||
|
AppVersion string `json:"app_version"`
|
||||||
|
OsVersion string `json:"os_version"`
|
||||||
|
IPAddress string `json:"ip_address"`
|
||||||
|
LastActiveAt *time.Time `json:"last_active_at"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserDeviceResponse struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
DeviceID string `json:"device_id"`
|
||||||
|
DeviceName string `json:"device_name"`
|
||||||
|
DeviceType entities.DeviceType `json:"device_type"`
|
||||||
|
Platform entities.DevicePlatform `json:"platform"`
|
||||||
|
FCMToken string `json:"fcm_token"`
|
||||||
|
AppVersion string `json:"app_version"`
|
||||||
|
OsVersion string `json:"os_version"`
|
||||||
|
IPAddress string `json:"ip_address"`
|
||||||
|
LastActiveAt *time.Time `json:"last_active_at"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegisterUserDeviceRequest struct {
|
||||||
|
UserID uuid.UUID `json:"user_id"`
|
||||||
|
DeviceID string `json:"device_id"`
|
||||||
|
DeviceName string `json:"device_name"`
|
||||||
|
DeviceType entities.DeviceType `json:"device_type"`
|
||||||
|
Platform entities.DevicePlatform `json:"platform"`
|
||||||
|
FCMToken string `json:"fcm_token"`
|
||||||
|
AppVersion string `json:"app_version"`
|
||||||
|
OsVersion string `json:"os_version"`
|
||||||
|
IPAddress string `json:"ip_address"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateUserDeviceRequest struct {
|
||||||
|
DeviceName string `json:"device_name"`
|
||||||
|
DeviceType entities.DeviceType `json:"device_type"`
|
||||||
|
Platform entities.DevicePlatform `json:"platform"`
|
||||||
|
FCMToken string `json:"fcm_token"`
|
||||||
|
AppVersion string `json:"app_version"`
|
||||||
|
OsVersion string `json:"os_version"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListUserDevicesRequest struct {
|
||||||
|
Page int `json:"page"`
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
UserID string `json:"user_id,omitempty"`
|
||||||
|
Platform string `json:"platform,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListUserDevicesResponse struct {
|
||||||
|
Devices []*UserDeviceResponse `json:"devices"`
|
||||||
|
TotalCount int `json:"total_count"`
|
||||||
|
Page int `json:"page"`
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
TotalPages int `json:"total_pages"`
|
||||||
|
}
|
||||||
165
internal/processor/user_device_processor.go
Normal file
165
internal/processor/user_device_processor.go
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
package processor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"apskel-pos-be/internal/entities"
|
||||||
|
"apskel-pos-be/internal/mappers"
|
||||||
|
"apskel-pos-be/internal/models"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserDeviceRepository interface {
|
||||||
|
Create(ctx context.Context, device *entities.UserDevice) error
|
||||||
|
GetByID(ctx context.Context, id uuid.UUID) (*entities.UserDevice, error)
|
||||||
|
GetByDeviceID(ctx context.Context, deviceID string, userID uuid.UUID) (*entities.UserDevice, error)
|
||||||
|
GetByUserID(ctx context.Context, userID uuid.UUID) ([]*entities.UserDevice, error)
|
||||||
|
Update(ctx context.Context, device *entities.UserDevice) error
|
||||||
|
Delete(ctx context.Context, id uuid.UUID) error
|
||||||
|
DeleteByUserID(ctx context.Context, userID uuid.UUID) error
|
||||||
|
List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.UserDevice, int64, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserDeviceProcessor interface {
|
||||||
|
RegisterDevice(ctx context.Context, req *models.RegisterUserDeviceRequest) (*models.UserDeviceResponse, error)
|
||||||
|
UpdateDevice(ctx context.Context, id uuid.UUID, req *models.UpdateUserDeviceRequest) (*models.UserDeviceResponse, error)
|
||||||
|
DeleteDevice(ctx context.Context, id uuid.UUID) error
|
||||||
|
GetDeviceByID(ctx context.Context, id uuid.UUID) (*models.UserDeviceResponse, error)
|
||||||
|
GetDevicesByUserID(ctx context.Context, userID uuid.UUID) ([]*models.UserDeviceResponse, error)
|
||||||
|
ListDevices(ctx context.Context, filters map[string]interface{}, page, limit int) ([]*models.UserDeviceResponse, int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserDeviceProcessorImpl struct {
|
||||||
|
userDeviceRepo UserDeviceRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserDeviceProcessorImpl(userDeviceRepo UserDeviceRepository) *UserDeviceProcessorImpl {
|
||||||
|
return &UserDeviceProcessorImpl{
|
||||||
|
userDeviceRepo: userDeviceRepo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *UserDeviceProcessorImpl) RegisterDevice(ctx context.Context, req *models.RegisterUserDeviceRequest) (*models.UserDeviceResponse, error) {
|
||||||
|
// Upsert: if device already registered for this user, update it
|
||||||
|
existing, err := p.userDeviceRepo.GetByDeviceID(ctx, req.DeviceID, req.UserID)
|
||||||
|
if err == nil && existing != nil {
|
||||||
|
existing.DeviceName = req.DeviceName
|
||||||
|
existing.DeviceType = req.DeviceType
|
||||||
|
existing.Platform = req.Platform
|
||||||
|
existing.FCMToken = req.FCMToken
|
||||||
|
existing.AppVersion = req.AppVersion
|
||||||
|
existing.OsVersion = req.OsVersion
|
||||||
|
existing.IPAddress = req.IPAddress
|
||||||
|
now := time.Now()
|
||||||
|
existing.LastActiveAt = &now
|
||||||
|
|
||||||
|
if err := p.userDeviceRepo.Update(ctx, existing); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to update device: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappers.UserDeviceEntityToResponse(existing), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceEntity := &entities.UserDevice{
|
||||||
|
UserID: req.UserID,
|
||||||
|
DeviceID: req.DeviceID,
|
||||||
|
DeviceName: req.DeviceName,
|
||||||
|
DeviceType: req.DeviceType,
|
||||||
|
Platform: req.Platform,
|
||||||
|
FCMToken: req.FCMToken,
|
||||||
|
AppVersion: req.AppVersion,
|
||||||
|
OsVersion: req.OsVersion,
|
||||||
|
IPAddress: req.IPAddress,
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
deviceEntity.LastActiveAt = &now
|
||||||
|
|
||||||
|
if err := p.userDeviceRepo.Create(ctx, deviceEntity); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to register device: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappers.UserDeviceEntityToResponse(deviceEntity), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *UserDeviceProcessorImpl) UpdateDevice(ctx context.Context, id uuid.UUID, req *models.UpdateUserDeviceRequest) (*models.UserDeviceResponse, error) {
|
||||||
|
deviceEntity, err := p.userDeviceRepo.GetByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("device not found: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.DeviceName != "" {
|
||||||
|
deviceEntity.DeviceName = req.DeviceName
|
||||||
|
}
|
||||||
|
if req.DeviceType != "" {
|
||||||
|
deviceEntity.DeviceType = req.DeviceType
|
||||||
|
}
|
||||||
|
if req.Platform != "" {
|
||||||
|
deviceEntity.Platform = req.Platform
|
||||||
|
}
|
||||||
|
if req.FCMToken != "" {
|
||||||
|
deviceEntity.FCMToken = req.FCMToken
|
||||||
|
}
|
||||||
|
if req.AppVersion != "" {
|
||||||
|
deviceEntity.AppVersion = req.AppVersion
|
||||||
|
}
|
||||||
|
if req.OsVersion != "" {
|
||||||
|
deviceEntity.OsVersion = req.OsVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
deviceEntity.LastActiveAt = &now
|
||||||
|
|
||||||
|
if err := p.userDeviceRepo.Update(ctx, deviceEntity); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to update device: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappers.UserDeviceEntityToResponse(deviceEntity), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *UserDeviceProcessorImpl) DeleteDevice(ctx context.Context, id uuid.UUID) error {
|
||||||
|
_, err := p.userDeviceRepo.GetByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("device not found: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := p.userDeviceRepo.Delete(ctx, id); err != nil {
|
||||||
|
return fmt.Errorf("failed to delete device: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *UserDeviceProcessorImpl) GetDeviceByID(ctx context.Context, id uuid.UUID) (*models.UserDeviceResponse, error) {
|
||||||
|
deviceEntity, err := p.userDeviceRepo.GetByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("device not found: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappers.UserDeviceEntityToResponse(deviceEntity), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *UserDeviceProcessorImpl) GetDevicesByUserID(ctx context.Context, userID uuid.UUID) ([]*models.UserDeviceResponse, error) {
|
||||||
|
deviceEntities, err := p.userDeviceRepo.GetByUserID(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get devices: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappers.UserDeviceEntitiesToResponses(deviceEntities), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *UserDeviceProcessorImpl) ListDevices(ctx context.Context, filters map[string]interface{}, page, limit int) ([]*models.UserDeviceResponse, int, error) {
|
||||||
|
offset := (page - 1) * limit
|
||||||
|
deviceEntities, total, err := p.userDeviceRepo.List(ctx, filters, limit, offset)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, fmt.Errorf("failed to list devices: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceResponses := mappers.UserDeviceEntitiesToResponses(deviceEntities)
|
||||||
|
totalPages := int((total + int64(limit) - 1) / int64(limit))
|
||||||
|
|
||||||
|
return deviceResponses, totalPages, nil
|
||||||
|
}
|
||||||
94
internal/repository/user_device_repository.go
Normal file
94
internal/repository/user_device_repository.go
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"apskel-pos-be/internal/entities"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserDeviceRepositoryImpl struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserDeviceRepositoryImpl(db *gorm.DB) *UserDeviceRepositoryImpl {
|
||||||
|
return &UserDeviceRepositoryImpl{
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *UserDeviceRepositoryImpl) Create(ctx context.Context, device *entities.UserDevice) error {
|
||||||
|
return r.db.WithContext(ctx).Create(device).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *UserDeviceRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.UserDevice, error) {
|
||||||
|
var device entities.UserDevice
|
||||||
|
err := r.db.WithContext(ctx).First(&device, "id = ?", id).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &device, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *UserDeviceRepositoryImpl) GetByDeviceID(ctx context.Context, deviceID string, userID uuid.UUID) (*entities.UserDevice, error) {
|
||||||
|
var device entities.UserDevice
|
||||||
|
err := r.db.WithContext(ctx).Where("device_id = ? AND user_id = ?", deviceID, userID).First(&device).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &device, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *UserDeviceRepositoryImpl) GetByUserID(ctx context.Context, userID uuid.UUID) ([]*entities.UserDevice, error) {
|
||||||
|
var devices []*entities.UserDevice
|
||||||
|
err := r.db.WithContext(ctx).Where("user_id = ?", userID).Order("created_at DESC").Find(&devices).Error
|
||||||
|
return devices, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *UserDeviceRepositoryImpl) Update(ctx context.Context, device *entities.UserDevice) error {
|
||||||
|
return r.db.WithContext(ctx).Save(device).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *UserDeviceRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error {
|
||||||
|
return r.db.WithContext(ctx).Delete(&entities.UserDevice{}, "id = ?", id).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *UserDeviceRepositoryImpl) DeleteByUserID(ctx context.Context, userID uuid.UUID) error {
|
||||||
|
return r.db.WithContext(ctx).Delete(&entities.UserDevice{}, "user_id = ?", userID).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *UserDeviceRepositoryImpl) List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.UserDevice, int64, error) {
|
||||||
|
var devices []*entities.UserDevice
|
||||||
|
var total int64
|
||||||
|
|
||||||
|
query := r.db.WithContext(ctx).Model(&entities.UserDevice{})
|
||||||
|
|
||||||
|
for key, value := range filters {
|
||||||
|
switch key {
|
||||||
|
case "user_id":
|
||||||
|
query = query.Where("user_id = ?", value)
|
||||||
|
case "platform":
|
||||||
|
if platform, ok := value.(string); ok && platform != "" {
|
||||||
|
query = query.Where("platform = ?", platform)
|
||||||
|
}
|
||||||
|
case "search":
|
||||||
|
if searchStr, ok := value.(string); ok && searchStr != "" {
|
||||||
|
searchPattern := "%" + strings.ToLower(searchStr) + "%"
|
||||||
|
query = query.Where("LOWER(device_name) LIKE ? OR LOWER(device_id) LIKE ?",
|
||||||
|
searchPattern, searchPattern)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
query = query.Where(key+" = ?", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := query.Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err := query.Order("created_at DESC").Limit(limit).Offset(offset).Find(&devices).Error
|
||||||
|
return devices, total, err
|
||||||
|
}
|
||||||
@ -46,11 +46,12 @@ type Router struct {
|
|||||||
customerAuthHandler *handler.CustomerAuthHandler
|
customerAuthHandler *handler.CustomerAuthHandler
|
||||||
customerPointsHandler *handler.CustomerPointsHandler
|
customerPointsHandler *handler.CustomerPointsHandler
|
||||||
spinGameHandler *handler.SpinGameHandler
|
spinGameHandler *handler.SpinGameHandler
|
||||||
|
userDeviceHandler *handler.UserDeviceHandler
|
||||||
authMiddleware *middleware.AuthMiddleware
|
authMiddleware *middleware.AuthMiddleware
|
||||||
customerAuthMiddleware *middleware.CustomerAuthMiddleware
|
customerAuthMiddleware *middleware.CustomerAuthMiddleware
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouter(cfg *config.Config, healthHandler *handler.HealthHandler, authService service.AuthService, authMiddleware *middleware.AuthMiddleware, userService *service.UserServiceImpl, userValidator *validator.UserValidatorImpl, organizationService service.OrganizationService, organizationValidator validator.OrganizationValidator, outletService service.OutletService, outletValidator validator.OutletValidator, outletSettingService service.OutletSettingService, categoryService service.CategoryService, categoryValidator validator.CategoryValidator, productService service.ProductService, productValidator validator.ProductValidator, productVariantService service.ProductVariantService, productVariantValidator validator.ProductVariantValidator, inventoryService service.InventoryService, inventoryValidator validator.InventoryValidator, orderService service.OrderService, orderValidator validator.OrderValidator, fileService service.FileService, fileValidator validator.FileValidator, customerService service.CustomerService, customerValidator validator.CustomerValidator, paymentMethodService service.PaymentMethodService, paymentMethodValidator validator.PaymentMethodValidator, analyticsService *service.AnalyticsServiceImpl, reportService service.ReportService, tableService *service.TableServiceImpl, tableValidator *validator.TableValidator, unitService handler.UnitService, ingredientService handler.IngredientService, productRecipeService service.ProductRecipeService, vendorService service.VendorService, vendorValidator validator.VendorValidator, purchaseOrderService service.PurchaseOrderService, purchaseOrderValidator validator.PurchaseOrderValidator, unitConverterService service.IngredientUnitConverterService, unitConverterValidator validator.IngredientUnitConverterValidator, chartOfAccountTypeService service.ChartOfAccountTypeService, chartOfAccountTypeValidator validator.ChartOfAccountTypeValidator, chartOfAccountService service.ChartOfAccountService, chartOfAccountValidator validator.ChartOfAccountValidator, accountService service.AccountService, accountValidator validator.AccountValidator, orderIngredientTransactionService service.OrderIngredientTransactionService, orderIngredientTransactionValidator validator.OrderIngredientTransactionValidator, gamificationService service.GamificationService, gamificationValidator validator.GamificationValidator, rewardService service.RewardService, rewardValidator validator.RewardValidator, campaignService service.CampaignService, campaignValidator validator.CampaignValidator, customerAuthService service.CustomerAuthService, customerAuthValidator validator.CustomerAuthValidator, customerPointsService service.CustomerPointsService, spinGameService service.SpinGameService, customerAuthMiddleware *middleware.CustomerAuthMiddleware) *Router {
|
func NewRouter(cfg *config.Config, healthHandler *handler.HealthHandler, authService service.AuthService, authMiddleware *middleware.AuthMiddleware, userService *service.UserServiceImpl, userValidator *validator.UserValidatorImpl, organizationService service.OrganizationService, organizationValidator validator.OrganizationValidator, outletService service.OutletService, outletValidator validator.OutletValidator, outletSettingService service.OutletSettingService, categoryService service.CategoryService, categoryValidator validator.CategoryValidator, productService service.ProductService, productValidator validator.ProductValidator, productVariantService service.ProductVariantService, productVariantValidator validator.ProductVariantValidator, inventoryService service.InventoryService, inventoryValidator validator.InventoryValidator, orderService service.OrderService, orderValidator validator.OrderValidator, fileService service.FileService, fileValidator validator.FileValidator, customerService service.CustomerService, customerValidator validator.CustomerValidator, paymentMethodService service.PaymentMethodService, paymentMethodValidator validator.PaymentMethodValidator, analyticsService *service.AnalyticsServiceImpl, reportService service.ReportService, tableService *service.TableServiceImpl, tableValidator *validator.TableValidator, unitService handler.UnitService, ingredientService handler.IngredientService, productRecipeService service.ProductRecipeService, vendorService service.VendorService, vendorValidator validator.VendorValidator, purchaseOrderService service.PurchaseOrderService, purchaseOrderValidator validator.PurchaseOrderValidator, unitConverterService service.IngredientUnitConverterService, unitConverterValidator validator.IngredientUnitConverterValidator, chartOfAccountTypeService service.ChartOfAccountTypeService, chartOfAccountTypeValidator validator.ChartOfAccountTypeValidator, chartOfAccountService service.ChartOfAccountService, chartOfAccountValidator validator.ChartOfAccountValidator, accountService service.AccountService, accountValidator validator.AccountValidator, orderIngredientTransactionService service.OrderIngredientTransactionService, orderIngredientTransactionValidator validator.OrderIngredientTransactionValidator, gamificationService service.GamificationService, gamificationValidator validator.GamificationValidator, rewardService service.RewardService, rewardValidator validator.RewardValidator, campaignService service.CampaignService, campaignValidator validator.CampaignValidator, customerAuthService service.CustomerAuthService, customerAuthValidator validator.CustomerAuthValidator, customerPointsService service.CustomerPointsService, spinGameService service.SpinGameService, customerAuthMiddleware *middleware.CustomerAuthMiddleware, userDeviceService service.UserDeviceService, userDeviceValidator validator.UserDeviceValidator) *Router {
|
||||||
|
|
||||||
return &Router{
|
return &Router{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
@ -89,6 +90,7 @@ func NewRouter(cfg *config.Config, healthHandler *handler.HealthHandler, authSer
|
|||||||
authMiddleware: authMiddleware,
|
authMiddleware: authMiddleware,
|
||||||
customerAuthMiddleware: customerAuthMiddleware,
|
customerAuthMiddleware: customerAuthMiddleware,
|
||||||
productVariantHandler: handler.NewProductVariantHandler(productVariantService, productVariantValidator),
|
productVariantHandler: handler.NewProductVariantHandler(productVariantService, productVariantValidator),
|
||||||
|
userDeviceHandler: handler.NewUserDeviceHandler(userDeviceService, userDeviceValidator),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -559,6 +561,24 @@ func (r *Router) addAppRoutes(rg *gin.Engine) {
|
|||||||
// Reports
|
// Reports
|
||||||
outlets.GET("/:outlet_id/reports/daily-transaction.pdf", r.reportHandler.GetDailyTransactionReportPDF)
|
outlets.GET("/:outlet_id/reports/daily-transaction.pdf", r.reportHandler.GetDailyTransactionReportPDF)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User device routes - accessible by authenticated users for their own devices
|
||||||
|
userDevices := protected.Group("/user-devices")
|
||||||
|
{
|
||||||
|
userDevices.POST("/register", r.userDeviceHandler.RegisterDevice)
|
||||||
|
userDevices.GET("/me", r.userDeviceHandler.GetMyDevices)
|
||||||
|
userDevices.GET("/:id", r.userDeviceHandler.GetDevice)
|
||||||
|
userDevices.PUT("/:id", r.userDeviceHandler.UpdateDevice)
|
||||||
|
userDevices.DELETE("/:id", r.userDeviceHandler.DeleteDevice)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Admin-only user device routes
|
||||||
|
adminUserDevices := protected.Group("/user-devices")
|
||||||
|
adminUserDevices.Use(r.authMiddleware.RequireAdminOrManager())
|
||||||
|
{
|
||||||
|
adminUserDevices.GET("", r.userDeviceHandler.ListDevices)
|
||||||
|
adminUserDevices.GET("/user/:user_id", r.userDeviceHandler.GetDevicesByUser)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,9 @@ import (
|
|||||||
|
|
||||||
"apskel-pos-be/config"
|
"apskel-pos-be/config"
|
||||||
"apskel-pos-be/internal/contract"
|
"apskel-pos-be/internal/contract"
|
||||||
|
"apskel-pos-be/internal/entities"
|
||||||
"apskel-pos-be/internal/models"
|
"apskel-pos-be/internal/models"
|
||||||
|
"apskel-pos-be/internal/processor"
|
||||||
"apskel-pos-be/internal/transformer"
|
"apskel-pos-be/internal/transformer"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
@ -24,11 +26,12 @@ type AuthService interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AuthServiceImpl struct {
|
type AuthServiceImpl struct {
|
||||||
userProcessor UserProcessor
|
userProcessor UserProcessor
|
||||||
jwtSecret string
|
userDeviceProcessor processor.UserDeviceProcessor
|
||||||
refreshSecret string
|
jwtSecret string
|
||||||
tokenTTL time.Duration
|
refreshSecret string
|
||||||
refreshTokenTTL time.Duration
|
tokenTTL time.Duration
|
||||||
|
refreshTokenTTL time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type Claims struct {
|
type Claims struct {
|
||||||
@ -39,13 +42,14 @@ type Claims struct {
|
|||||||
jwt.RegisteredClaims
|
jwt.RegisteredClaims
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthService(userProcessor UserProcessor, authConfig *config.AuthConfig) AuthService {
|
func NewAuthService(userProcessor UserProcessor, userDeviceProcessor processor.UserDeviceProcessor, authConfig *config.AuthConfig) AuthService {
|
||||||
return &AuthServiceImpl{
|
return &AuthServiceImpl{
|
||||||
userProcessor: userProcessor,
|
userProcessor: userProcessor,
|
||||||
jwtSecret: authConfig.AccessTokenSecret(),
|
userDeviceProcessor: userDeviceProcessor,
|
||||||
refreshSecret: authConfig.RefreshTokenSecret(),
|
jwtSecret: authConfig.AccessTokenSecret(),
|
||||||
tokenTTL: authConfig.AccessTokenTTL(),
|
refreshSecret: authConfig.RefreshTokenSecret(),
|
||||||
refreshTokenTTL: authConfig.RefreshTokenTTL(),
|
tokenTTL: authConfig.AccessTokenTTL(),
|
||||||
|
refreshTokenTTL: authConfig.RefreshTokenTTL(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,6 +85,25 @@ func (s *AuthServiceImpl) Login(ctx context.Context, req *contract.LoginRequest)
|
|||||||
return nil, fmt.Errorf("failed to generate refresh token: %w", err)
|
return nil, fmt.Errorf("failed to generate refresh token: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register or update device info if provided
|
||||||
|
if req.DeviceID != "" && s.userDeviceProcessor != nil {
|
||||||
|
deviceReq := &models.RegisterUserDeviceRequest{
|
||||||
|
UserID: userResponse.ID,
|
||||||
|
DeviceID: req.DeviceID,
|
||||||
|
DeviceName: req.DeviceName,
|
||||||
|
DeviceType: entities.DeviceType(req.DeviceType),
|
||||||
|
Platform: entities.DevicePlatform(req.Platform),
|
||||||
|
FCMToken: req.FCMToken,
|
||||||
|
AppVersion: req.AppVersion,
|
||||||
|
OsVersion: req.OsVersion,
|
||||||
|
}
|
||||||
|
// Non-blocking: log error but don't fail login
|
||||||
|
if _, err := s.userDeviceProcessor.RegisterDevice(ctx, deviceReq); err != nil {
|
||||||
|
// Log but don't fail the login
|
||||||
|
_ = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &contract.LoginResponse{
|
return &contract.LoginResponse{
|
||||||
Token: token,
|
Token: token,
|
||||||
RefreshToken: refreshToken,
|
RefreshToken: refreshToken,
|
||||||
|
|||||||
122
internal/service/user_device_service.go
Normal file
122
internal/service/user_device_service.go
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"apskel-pos-be/internal/constants"
|
||||||
|
"apskel-pos-be/internal/contract"
|
||||||
|
"apskel-pos-be/internal/processor"
|
||||||
|
"apskel-pos-be/internal/transformer"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserDeviceService interface {
|
||||||
|
RegisterDevice(ctx context.Context, userID uuid.UUID, req *contract.RegisterUserDeviceRequest) *contract.Response
|
||||||
|
UpdateDevice(ctx context.Context, id uuid.UUID, req *contract.UpdateUserDeviceRequest) *contract.Response
|
||||||
|
DeleteDevice(ctx context.Context, id uuid.UUID) *contract.Response
|
||||||
|
GetDeviceByID(ctx context.Context, id uuid.UUID) *contract.Response
|
||||||
|
GetDevicesByUserID(ctx context.Context, userID uuid.UUID) *contract.Response
|
||||||
|
ListDevices(ctx context.Context, req *contract.ListUserDevicesRequest) *contract.Response
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserDeviceServiceImpl struct {
|
||||||
|
userDeviceProcessor processor.UserDeviceProcessor
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserDeviceService(userDeviceProcessor processor.UserDeviceProcessor) *UserDeviceServiceImpl {
|
||||||
|
return &UserDeviceServiceImpl{
|
||||||
|
userDeviceProcessor: userDeviceProcessor,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *UserDeviceServiceImpl) RegisterDevice(ctx context.Context, userID uuid.UUID, req *contract.RegisterUserDeviceRequest) *contract.Response {
|
||||||
|
modelReq := transformer.RegisterUserDeviceRequestToModel(req)
|
||||||
|
modelReq.UserID = userID
|
||||||
|
|
||||||
|
deviceResponse, err := s.userDeviceProcessor.RegisterDevice(ctx, modelReq)
|
||||||
|
if err != nil {
|
||||||
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.UserDeviceServiceEntity, err.Error())
|
||||||
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
||||||
|
}
|
||||||
|
|
||||||
|
contractResponse := transformer.UserDeviceModelResponseToResponse(deviceResponse)
|
||||||
|
return contract.BuildSuccessResponse(contractResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *UserDeviceServiceImpl) UpdateDevice(ctx context.Context, id uuid.UUID, req *contract.UpdateUserDeviceRequest) *contract.Response {
|
||||||
|
modelReq := transformer.UpdateUserDeviceRequestToModel(req)
|
||||||
|
|
||||||
|
deviceResponse, err := s.userDeviceProcessor.UpdateDevice(ctx, id, modelReq)
|
||||||
|
if err != nil {
|
||||||
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.UserDeviceServiceEntity, err.Error())
|
||||||
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
||||||
|
}
|
||||||
|
|
||||||
|
contractResponse := transformer.UserDeviceModelResponseToResponse(deviceResponse)
|
||||||
|
return contract.BuildSuccessResponse(contractResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *UserDeviceServiceImpl) DeleteDevice(ctx context.Context, id uuid.UUID) *contract.Response {
|
||||||
|
err := s.userDeviceProcessor.DeleteDevice(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.UserDeviceServiceEntity, err.Error())
|
||||||
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
||||||
|
}
|
||||||
|
|
||||||
|
return contract.BuildSuccessResponse(map[string]interface{}{
|
||||||
|
"message": "Device deleted successfully",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *UserDeviceServiceImpl) GetDeviceByID(ctx context.Context, id uuid.UUID) *contract.Response {
|
||||||
|
deviceResponse, err := s.userDeviceProcessor.GetDeviceByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
errorResp := contract.NewResponseError(constants.NotFoundErrorCode, constants.UserDeviceServiceEntity, err.Error())
|
||||||
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
||||||
|
}
|
||||||
|
|
||||||
|
contractResponse := transformer.UserDeviceModelResponseToResponse(deviceResponse)
|
||||||
|
return contract.BuildSuccessResponse(contractResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *UserDeviceServiceImpl) GetDevicesByUserID(ctx context.Context, userID uuid.UUID) *contract.Response {
|
||||||
|
deviceResponses, err := s.userDeviceProcessor.GetDevicesByUserID(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.UserDeviceServiceEntity, err.Error())
|
||||||
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
||||||
|
}
|
||||||
|
|
||||||
|
contractResponses := transformer.UserDeviceModelResponsesToResponses(deviceResponses)
|
||||||
|
return contract.BuildSuccessResponse(contractResponses)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *UserDeviceServiceImpl) ListDevices(ctx context.Context, req *contract.ListUserDevicesRequest) *contract.Response {
|
||||||
|
modelReq := transformer.ListUserDevicesRequestToModel(req)
|
||||||
|
|
||||||
|
filters := make(map[string]interface{})
|
||||||
|
if modelReq.UserID != "" {
|
||||||
|
filters["user_id"] = modelReq.UserID
|
||||||
|
}
|
||||||
|
if modelReq.Platform != "" {
|
||||||
|
filters["platform"] = modelReq.Platform
|
||||||
|
}
|
||||||
|
|
||||||
|
devices, totalPages, err := s.userDeviceProcessor.ListDevices(ctx, filters, modelReq.Page, modelReq.Limit)
|
||||||
|
if err != nil {
|
||||||
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.UserDeviceServiceEntity, err.Error())
|
||||||
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
||||||
|
}
|
||||||
|
|
||||||
|
contractResponses := transformer.UserDeviceModelResponsesToResponses(devices)
|
||||||
|
|
||||||
|
response := contract.ListUserDevicesResponse{
|
||||||
|
Devices: contractResponses,
|
||||||
|
TotalCount: len(contractResponses),
|
||||||
|
Page: modelReq.Page,
|
||||||
|
Limit: modelReq.Limit,
|
||||||
|
TotalPages: totalPages,
|
||||||
|
}
|
||||||
|
|
||||||
|
return contract.BuildSuccessResponse(response)
|
||||||
|
}
|
||||||
74
internal/transformer/user_device_transformer.go
Normal file
74
internal/transformer/user_device_transformer.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package transformer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"apskel-pos-be/internal/contract"
|
||||||
|
"apskel-pos-be/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterUserDeviceRequestToModel(req *contract.RegisterUserDeviceRequest) *models.RegisterUserDeviceRequest {
|
||||||
|
return &models.RegisterUserDeviceRequest{
|
||||||
|
DeviceID: req.DeviceID,
|
||||||
|
DeviceName: req.DeviceName,
|
||||||
|
DeviceType: req.DeviceType,
|
||||||
|
Platform: req.Platform,
|
||||||
|
FCMToken: req.FCMToken,
|
||||||
|
AppVersion: req.AppVersion,
|
||||||
|
OsVersion: req.OsVersion,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateUserDeviceRequestToModel(req *contract.UpdateUserDeviceRequest) *models.UpdateUserDeviceRequest {
|
||||||
|
return &models.UpdateUserDeviceRequest{
|
||||||
|
DeviceName: req.DeviceName,
|
||||||
|
DeviceType: req.DeviceType,
|
||||||
|
Platform: req.Platform,
|
||||||
|
FCMToken: req.FCMToken,
|
||||||
|
AppVersion: req.AppVersion,
|
||||||
|
OsVersion: req.OsVersion,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUserDevicesRequestToModel(req *contract.ListUserDevicesRequest) *models.ListUserDevicesRequest {
|
||||||
|
return &models.ListUserDevicesRequest{
|
||||||
|
Page: req.Page,
|
||||||
|
Limit: req.Limit,
|
||||||
|
UserID: req.UserID,
|
||||||
|
Platform: req.Platform,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UserDeviceModelResponseToResponse(device *models.UserDeviceResponse) *contract.UserDeviceResponse {
|
||||||
|
if device == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &contract.UserDeviceResponse{
|
||||||
|
ID: device.ID,
|
||||||
|
UserID: device.UserID,
|
||||||
|
DeviceID: device.DeviceID,
|
||||||
|
DeviceName: device.DeviceName,
|
||||||
|
DeviceType: device.DeviceType,
|
||||||
|
Platform: device.Platform,
|
||||||
|
FCMToken: device.FCMToken,
|
||||||
|
AppVersion: device.AppVersion,
|
||||||
|
OsVersion: device.OsVersion,
|
||||||
|
IPAddress: device.IPAddress,
|
||||||
|
LastActiveAt: device.LastActiveAt,
|
||||||
|
CreatedAt: device.CreatedAt,
|
||||||
|
UpdatedAt: device.UpdatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UserDeviceModelResponsesToResponses(devices []*models.UserDeviceResponse) []contract.UserDeviceResponse {
|
||||||
|
if devices == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
responses := make([]contract.UserDeviceResponse, len(devices))
|
||||||
|
for i, device := range devices {
|
||||||
|
response := UserDeviceModelResponseToResponse(device)
|
||||||
|
if response != nil {
|
||||||
|
responses[i] = *response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return responses
|
||||||
|
}
|
||||||
126
internal/validator/user_device_validator.go
Normal file
126
internal/validator/user_device_validator.go
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
package validator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"apskel-pos-be/internal/constants"
|
||||||
|
"apskel-pos-be/internal/contract"
|
||||||
|
"apskel-pos-be/internal/entities"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserDeviceValidator interface {
|
||||||
|
ValidateRegisterDeviceRequest(req *contract.RegisterUserDeviceRequest) (error, string)
|
||||||
|
ValidateUpdateDeviceRequest(req *contract.UpdateUserDeviceRequest) (error, string)
|
||||||
|
ValidateListDevicesRequest(req *contract.ListUserDevicesRequest) (error, string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserDeviceValidatorImpl struct{}
|
||||||
|
|
||||||
|
func NewUserDeviceValidator() *UserDeviceValidatorImpl {
|
||||||
|
return &UserDeviceValidatorImpl{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var validDeviceTypes = map[entities.DeviceType]bool{
|
||||||
|
entities.DeviceTypeMobile: true,
|
||||||
|
entities.DeviceTypeTablet: true,
|
||||||
|
entities.DeviceTypeDesktop: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
var validPlatforms = map[entities.DevicePlatform]bool{
|
||||||
|
entities.DevicePlatformAndroid: true,
|
||||||
|
entities.DevicePlatformIOS: true,
|
||||||
|
entities.DevicePlatformWeb: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *UserDeviceValidatorImpl) ValidateRegisterDeviceRequest(req *contract.RegisterUserDeviceRequest) (error, string) {
|
||||||
|
if req == nil {
|
||||||
|
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.TrimSpace(req.DeviceID) == "" {
|
||||||
|
return errors.New("device_id is required"), constants.MissingFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(req.DeviceID) > 255 {
|
||||||
|
return errors.New("device_id must be at most 255 characters"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.DeviceName != "" && len(req.DeviceName) > 255 {
|
||||||
|
return errors.New("device_name must be at most 255 characters"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.DeviceType != "" && !validDeviceTypes[req.DeviceType] {
|
||||||
|
return errors.New("device_type must be one of: mobile, tablet, desktop"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Platform != "" && !validPlatforms[req.Platform] {
|
||||||
|
return errors.New("platform must be one of: android, ios, web"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.FCMToken != "" && len(req.FCMToken) > 512 {
|
||||||
|
return errors.New("fcm_token must be at most 512 characters"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.AppVersion != "" && len(req.AppVersion) > 50 {
|
||||||
|
return errors.New("app_version must be at most 50 characters"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.OsVersion != "" && len(req.OsVersion) > 50 {
|
||||||
|
return errors.New("os_version must be at most 50 characters"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *UserDeviceValidatorImpl) ValidateUpdateDeviceRequest(req *contract.UpdateUserDeviceRequest) (error, string) {
|
||||||
|
if req == nil {
|
||||||
|
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.DeviceName != "" && len(req.DeviceName) > 255 {
|
||||||
|
return errors.New("device_name must be at most 255 characters"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.DeviceType != "" && !validDeviceTypes[req.DeviceType] {
|
||||||
|
return errors.New("device_type must be one of: mobile, tablet, desktop"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Platform != "" && !validPlatforms[req.Platform] {
|
||||||
|
return errors.New("platform must be one of: android, ios, web"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.FCMToken != "" && len(req.FCMToken) > 512 {
|
||||||
|
return errors.New("fcm_token must be at most 512 characters"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.AppVersion != "" && len(req.AppVersion) > 50 {
|
||||||
|
return errors.New("app_version must be at most 50 characters"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.OsVersion != "" && len(req.OsVersion) > 50 {
|
||||||
|
return errors.New("os_version must be at most 50 characters"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *UserDeviceValidatorImpl) ValidateListDevicesRequest(req *contract.ListUserDevicesRequest) (error, string) {
|
||||||
|
if req == nil {
|
||||||
|
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Page < 1 {
|
||||||
|
return errors.New("page must be at least 1"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Limit < 1 || req.Limit > 100 {
|
||||||
|
return errors.New("limit must be between 1 and 100"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Platform != "" && !validPlatforms[entities.DevicePlatform(req.Platform)] {
|
||||||
|
return errors.New("platform must be one of: android, ios, web"), constants.MalformedFieldErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, ""
|
||||||
|
}
|
||||||
1
migrations/000063_create_user_devices_table.down.sql
Normal file
1
migrations/000063_create_user_devices_table.down.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS user_devices;
|
||||||
22
migrations/000063_create_user_devices_table.up.sql
Normal file
22
migrations/000063_create_user_devices_table.up.sql
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
-- User devices table
|
||||||
|
CREATE TABLE user_devices (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
device_id VARCHAR(255) NOT NULL,
|
||||||
|
device_name VARCHAR(255),
|
||||||
|
device_type VARCHAR(50) CHECK (device_type IN ('mobile', 'tablet', 'desktop')),
|
||||||
|
platform VARCHAR(50) CHECK (platform IN ('android', 'ios', 'web')),
|
||||||
|
fcm_token VARCHAR(512),
|
||||||
|
app_version VARCHAR(50),
|
||||||
|
os_version VARCHAR(50),
|
||||||
|
ip_address VARCHAR(45),
|
||||||
|
last_active_at TIMESTAMP WITH TIME ZONE,
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Indexes
|
||||||
|
CREATE INDEX idx_user_devices_user_id ON user_devices(user_id);
|
||||||
|
CREATE INDEX idx_user_devices_device_id ON user_devices(device_id);
|
||||||
|
CREATE INDEX idx_user_devices_fcm_token ON user_devices(fcm_token);
|
||||||
|
CREATE UNIQUE INDEX idx_user_devices_user_device ON user_devices(user_id, device_id);
|
||||||
Loading…
x
Reference in New Issue
Block a user