Files
apskel-pos-backend/internal/service/customer_device_service.go
T
2026-09-30 15:31:11 +07:00

59 lines
2.0 KiB
Go

package service
import (
"context"
"errors"
"github.com/google/uuid"
"apskel-pos-be/internal/constants"
"apskel-pos-be/internal/contract"
"apskel-pos-be/internal/processor"
"apskel-pos-be/internal/repository"
)
// CustomerDeviceService registers the customer app's devices for push notifications.
type CustomerDeviceService interface {
Register(ctx context.Context, customerID uuid.UUID, req *contract.RegisterCustomerDeviceRequest) *contract.Response
Unregister(ctx context.Context, customerID uuid.UUID, deviceID string) *contract.Response
}
type CustomerDeviceServiceImpl struct {
devices *processor.CustomerDeviceProcessor
}
func NewCustomerDeviceService(devices *processor.CustomerDeviceProcessor) *CustomerDeviceServiceImpl {
return &CustomerDeviceServiceImpl{devices: devices}
}
func (s *CustomerDeviceServiceImpl) Register(ctx context.Context, customerID uuid.UUID, req *contract.RegisterCustomerDeviceRequest) *contract.Response {
err := s.devices.Register(ctx, repository.CustomerDevice{
CustomerID: customerID,
DeviceID: req.DeviceID,
FCMToken: req.FCMToken,
Platform: req.Platform,
AppVersion: req.AppVersion,
})
if err != nil {
return customerDeviceErrorResponse(err)
}
return contract.BuildSuccessResponse(map[string]interface{}{"device_id": req.DeviceID})
}
func (s *CustomerDeviceServiceImpl) Unregister(ctx context.Context, customerID uuid.UUID, deviceID string) *contract.Response {
if err := s.devices.Unregister(ctx, customerID, deviceID); err != nil {
return customerDeviceErrorResponse(err)
}
return contract.BuildSuccessResponse(map[string]interface{}{"device_id": deviceID})
}
func customerDeviceErrorResponse(err error) *contract.Response {
code := constants.InternalServerErrorCode
if errors.Is(err, processor.ErrInvalidCustomerDevice) {
code = constants.ValidationErrorCode
}
return contract.BuildErrorResponse([]*contract.ResponseError{
contract.NewResponseError(code, constants.RequestEntity, err.Error()),
})
}