Update Member

This commit is contained in:
aditya.siregar
2025-03-15 15:51:18 +08:00
parent 18003313dd
commit c41826bb1b
29 changed files with 1840 additions and 65 deletions
+81
View File
@@ -0,0 +1,81 @@
package http
import (
"enaklo-pos-be/internal/services/v2/customer"
"net/http"
"strconv"
"enaklo-pos-be/internal/common/errors"
"enaklo-pos-be/internal/entity"
"enaklo-pos-be/internal/handlers/request"
"enaklo-pos-be/internal/handlers/response"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
)
type CustomerHandler struct {
service customer.Service
}
func NewCustomerHandler(service customer.Service) *CustomerHandler {
return &CustomerHandler{
service: service,
}
}
func (h *CustomerHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
route := group.Group("/customers")
route.GET("/list", jwt, h.GetCustomerList)
}
func (h *CustomerHandler) GetCustomerList(c *gin.Context) {
ctx := request.GetMyContext(c)
searchQuery := c.DefaultQuery("search", "")
limitStr := c.DefaultQuery("limit", "10")
offsetStr := c.DefaultQuery("offset", "0")
// Convert limit and offset to integers
limit, err := strconv.Atoi(limitStr)
if err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
offset, err := strconv.Atoi(offsetStr)
if err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
req := &entity.MemberSearch{
Search: searchQuery,
Limit: limit,
Offset: offset,
}
validate := validator.New()
if err := validate.Struct(req); err != nil {
response.ErrorWrapper(c, err)
return
}
customerList, totalCount, err := h.service.GetAllCustomers(ctx, req)
if err != nil {
response.ErrorWrapper(c, err)
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: response.MapToCustomerListResponse(customerList),
PagingMeta: &response.PagingMeta{
Page: offset + 1,
Total: int64(totalCount),
Limit: limit,
},
})
}
+154
View File
@@ -0,0 +1,154 @@
package http
import (
"enaklo-pos-be/internal/common/errors"
"enaklo-pos-be/internal/entity"
"enaklo-pos-be/internal/handlers/request"
"enaklo-pos-be/internal/handlers/response"
"enaklo-pos-be/internal/services/member"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"net/http"
)
type MemberHandler struct {
service member.RegistrationService
}
func NewMemberRegistrationHandler(service member.RegistrationService) *MemberHandler {
return &MemberHandler{
service: service,
}
}
func (h *MemberHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
route := group.Group("/member")
route.POST("/register", jwt, h.InitiateRegistration)
route.POST("/verify", jwt, h.VerifyOTP)
route.GET("/status", jwt, h.GetRegistrationStatus)
route.POST("/resend-otp", jwt, h.ResendOTP)
route.GET("/list", jwt, h.GetRegistrationStatus)
}
func (h *MemberHandler) InitiateRegistration(c *gin.Context) {
ctx := request.GetMyContext(c)
userID := ctx.RequestedBy()
var req request.InitiateRegistrationRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
validate := validator.New()
if err := validate.Struct(req); err != nil {
response.ErrorWrapper(c, err)
return
}
birthDate, err := req.GetBirthdate()
if err != nil {
response.ErrorWrapper(c, err)
return
}
memberReq := &entity.MemberRegistrationRequest{
Name: req.Name,
Email: req.Email,
Phone: req.Phone,
BirthDate: birthDate,
BranchID: *ctx.GetPartnerID(),
CashierID: userID,
}
result, err := h.service.InitiateRegistration(ctx, memberReq)
if err != nil {
response.ErrorWrapper(c, err)
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: response.MapToMemberRegistrationResponse(result),
})
}
func (h *MemberHandler) VerifyOTP(c *gin.Context) {
ctx := request.GetMyContext(c)
var req request.VerifyOTPRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
validate := validator.New()
if err := validate.Struct(req); err != nil {
response.ErrorWrapper(c, err)
return
}
result, err := h.service.VerifyOTP(ctx, req.Token, req.OTP)
if err != nil {
response.ErrorWrapper(c, err)
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: response.MapToMemberVerificationResponse(result),
})
}
func (h *MemberHandler) GetRegistrationStatus(c *gin.Context) {
ctx := request.GetMyContext(c)
token := c.Query("token")
if token == "" {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
result, err := h.service.GetRegistrationStatus(ctx, token)
if err != nil {
response.ErrorWrapper(c, err)
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: response.MapToMemberRegistrationStatus(result),
})
}
func (h *MemberHandler) ResendOTP(c *gin.Context) {
ctx := request.GetMyContext(c)
var req entity.ResendOTPRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
validate := validator.New()
if err := validate.Struct(req); err != nil {
response.ErrorWrapper(c, err)
return
}
result, err := h.service.ResendOTP(ctx, req.Token)
if err != nil {
response.ErrorWrapper(c, err)
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: response.MapToResendOTPResponse(result),
})
}
+34
View File
@@ -0,0 +1,34 @@
package request
import (
"time"
)
type InitiateRegistrationRequest struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
Phone string `json:"phone" validate:"required"`
BirthDate string `json:"birth_date" validate:"required"`
}
func (i *InitiateRegistrationRequest) GetBirthdate() (time.Time, error) {
parsedDate, err := time.Parse("02-01-2006", i.BirthDate)
if err != nil {
return time.Time{}, err
}
return parsedDate, nil
}
type VerifyOTPRequest struct {
Token string `json:"token" validate:"required"`
OTP string `json:"otp" validate:"required"`
}
type ResendOTPRequest struct {
Token string `json:"token" validate:"required"`
}
type CheckCustomerRequest struct {
Email string `json:"email"`
Phone string `json:"phone"`
}
+35
View File
@@ -0,0 +1,35 @@
package response
import (
"enaklo-pos-be/internal/entity"
)
func MapToCustomerResponse(customer *entity.Customer) CustomerResponse {
if customer == nil {
return CustomerResponse{}
}
return CustomerResponse{
ID: customer.ID,
Name: customer.Name,
Email: customer.Email,
Phone: customer.Phone,
Points: customer.Points,
CustomerID: customer.CustomerID,
CreatedAt: customer.CreatedAt.Format("2006-01-02"),
BirthDate: customer.BirthDate.Format("2006-01-02"),
}
}
func MapToCustomerListResponse(customers *entity.MemberList) []CustomerResponse {
if customers == nil {
return []CustomerResponse{}
}
responseList := []CustomerResponse{}
for _, customer := range *customers {
responseList = append(responseList, MapToCustomerResponse(customer))
}
return responseList
}
+111
View File
@@ -0,0 +1,111 @@
package response
import (
"enaklo-pos-be/internal/entity"
"time"
)
type MemberRegistrationResponse struct {
Token string `json:"token"`
Status string `json:"status"`
ExpiresAt time.Time `json:"expires_at"`
Message string `json:"message"`
}
type MemberVerificationResponse struct {
CustomerID int64 `json:"customer_id"`
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
Points int `json:"points"`
Status string `json:"status"`
}
type MemberRegistrationStatus struct {
Token string `json:"token"`
Status string `json:"status"`
ExpiresAt time.Time `json:"expires_at"`
IsExpired bool `json:"is_expired"`
CreatedAt time.Time `json:"created_at"`
}
type ResendOTPResponse struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
Message string `json:"message"`
}
type CustomerCheckResponse struct {
Exists bool `json:"exists"`
Customer *CustomerResponse `json:"customer,omitempty"`
Message string `json:"message,omitempty"`
}
type CustomerResponse struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
BirthDate string `json:"birth_date,omitempty"`
Points int `json:"points"`
CreatedAt string `json:"created_at"`
CustomerID string `json:"customer_id"`
}
func MapToMemberRegistrationResponse(entity *entity.MemberRegistrationResponse) MemberRegistrationResponse {
return MemberRegistrationResponse{
Token: entity.Token,
Status: entity.Status,
ExpiresAt: entity.ExpiresAt,
Message: entity.Message,
}
}
func MapToMemberVerificationResponse(entity *entity.MemberVerificationResponse) MemberVerificationResponse {
return MemberVerificationResponse{
CustomerID: entity.CustomerID,
Name: entity.Name,
Email: entity.Email,
Phone: entity.Phone,
Points: entity.Points,
Status: entity.Status,
}
}
func MapToMemberRegistrationStatus(entity *entity.MemberRegistrationStatus) MemberRegistrationStatus {
return MemberRegistrationStatus{
Token: entity.Token,
Status: entity.Status,
ExpiresAt: entity.ExpiresAt,
IsExpired: entity.IsExpired,
CreatedAt: entity.CreatedAt,
}
}
func MapToResendOTPResponse(entity *entity.ResendOTPResponse) ResendOTPResponse {
return ResendOTPResponse{
Token: entity.Token,
ExpiresAt: entity.ExpiresAt,
Message: entity.Message,
}
}
func MapToCustomerCheckResponse(entity *entity.CustomerCheckResponse) CustomerCheckResponse {
response := CustomerCheckResponse{
Exists: entity.Exists,
Message: entity.Message,
}
if entity.Customer != nil {
customer := &CustomerResponse{
ID: entity.Customer.ID,
Name: entity.Customer.Name,
Email: entity.Customer.Email,
Phone: entity.Customer.Phone,
CreatedAt: entity.Customer.CreatedAt.Format("2006-01-02"),
}
response.Customer = customer
}
return response
}