Add Voucher Undian
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"enaklo-pos-be/internal/entity"
|
||||
"enaklo-pos-be/internal/handlers/request"
|
||||
"enaklo-pos-be/internal/handlers/response"
|
||||
"enaklo-pos-be/internal/services/v2/undian"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CustomerUndianHandler struct {
|
||||
undianService undian.Service
|
||||
}
|
||||
|
||||
func NewCustomerUndianHandler(undianService undian.Service) *CustomerUndianHandler {
|
||||
return &CustomerUndianHandler{
|
||||
undianService: undianService,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *CustomerUndianHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
route := group.Group("/undian")
|
||||
|
||||
route.GET("/list", jwt, h.GetUndianList)
|
||||
route.GET("/events", jwt, h.GetActiveEvents)
|
||||
}
|
||||
|
||||
func (h *CustomerUndianHandler) GetUndianList(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
userID := ctx.RequestedBy()
|
||||
|
||||
undianResponse, err := h.undianService.GetUndianList(ctx, userID)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
responseData := h.mapToUndianListResponse(undianResponse)
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: responseData,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *CustomerUndianHandler) GetActiveEvents(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
events, err := h.undianService.GetActiveUndianEvents(ctx)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
responseData := h.mapToActiveEventsResponse(events)
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: responseData,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *CustomerUndianHandler) mapToUndianListResponse(undianResponse *entity.UndianListResponse) response.UndianListResponse {
|
||||
events := make([]response.UndianEventResponse, 0, len(undianResponse.Events))
|
||||
|
||||
for _, event := range undianResponse.Events {
|
||||
vouchers := make([]response.UndianVoucherResponse, 0, len(event.Vouchers))
|
||||
for _, voucher := range event.Vouchers {
|
||||
vouchers = append(vouchers, response.UndianVoucherResponse{
|
||||
ID: voucher.ID,
|
||||
VoucherCode: voucher.VoucherCode,
|
||||
VoucherNumber: voucher.VoucherNumber,
|
||||
IsWinner: voucher.IsWinner,
|
||||
PrizeRank: voucher.PrizeRank,
|
||||
WonAt: h.formatTimePointer(voucher.WonAt),
|
||||
CreatedAt: voucher.CreatedAt.Format("2006-01-02T15:04:05Z"),
|
||||
})
|
||||
}
|
||||
|
||||
prizes := make([]response.UndianPrizeResponse, 0, len(event.Prizes))
|
||||
for _, prize := range event.Prizes {
|
||||
prizes = append(prizes, response.UndianPrizeResponse{
|
||||
ID: prize.ID,
|
||||
Rank: prize.Rank,
|
||||
PrizeName: prize.PrizeName,
|
||||
PrizeValue: prize.PrizeValue,
|
||||
PrizeDescription: prize.PrizeDescription,
|
||||
PrizeType: prize.PrizeType,
|
||||
PrizeImageURL: prize.PrizeImageURL,
|
||||
WinningVoucherID: prize.WinningVoucherID,
|
||||
WinnerUserID: prize.WinnerUserID,
|
||||
IsWon: prize.WinningVoucherID != nil,
|
||||
Amount: prize.Amount,
|
||||
})
|
||||
}
|
||||
|
||||
events = append(events, response.UndianEventResponse{
|
||||
ID: event.ID,
|
||||
Title: event.Title,
|
||||
Description: event.Description,
|
||||
ImageURL: event.ImageURL,
|
||||
Status: event.Status,
|
||||
StartDate: event.StartDate.Format("2006-01-02T15:04:05Z"),
|
||||
EndDate: event.EndDate.Format("2006-01-02T15:04:05Z"),
|
||||
DrawDate: event.DrawDate.Format("2006-01-02T15:04:05Z"),
|
||||
MinimumPurchase: event.MinimumPurchase,
|
||||
DrawCompleted: event.DrawCompleted,
|
||||
DrawCompletedAt: h.formatTimePointer(event.DrawCompletedAt),
|
||||
TermsConditions: event.TermsConditions,
|
||||
Prefix: event.Prefix,
|
||||
CreatedAt: event.CreatedAt.Format("2006-01-02T15:04:05Z"),
|
||||
UpdatedAt: event.UpdatedAt.Format("2006-01-02T15:04:05Z"),
|
||||
VoucherCount: event.VoucherCount,
|
||||
Vouchers: vouchers,
|
||||
Prizes: prizes,
|
||||
TotalPrizes: len(prizes),
|
||||
})
|
||||
}
|
||||
|
||||
return response.UndianListResponse{
|
||||
Events: events,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *CustomerUndianHandler) mapToActiveEventsResponse(events []*entity.UndianEventDB) response.ActiveEventsResponse {
|
||||
eventResponses := make([]response.ActiveEventResponse, 0, len(events))
|
||||
|
||||
for _, event := range events {
|
||||
eventResponses = append(eventResponses, response.ActiveEventResponse{
|
||||
ID: event.ID,
|
||||
Title: event.Title,
|
||||
Description: event.Description,
|
||||
ImageURL: event.ImageURL,
|
||||
Status: event.Status,
|
||||
StartDate: event.StartDate.Format("2006-01-02T15:04:05Z"),
|
||||
EndDate: event.EndDate.Format("2006-01-02T15:04:05Z"),
|
||||
DrawDate: event.DrawDate.Format("2006-01-02T15:04:05Z"),
|
||||
MinimumPurchase: event.MinimumPurchase,
|
||||
DrawCompleted: event.DrawCompleted,
|
||||
DrawCompletedAt: h.formatTimePointer(event.DrawCompletedAt),
|
||||
TermsConditions: event.TermsAndConditions,
|
||||
Prefix: event.Prefix,
|
||||
CreatedAt: event.CreatedAt.Format("2006-01-02T15:04:05Z"),
|
||||
UpdatedAt: event.UpdatedAt.Format("2006-01-02T15:04:05Z"),
|
||||
})
|
||||
}
|
||||
|
||||
return response.ActiveEventsResponse{
|
||||
Events: eventResponses,
|
||||
}
|
||||
}
|
||||
|
||||
// formatTimePointer formats time pointer to string
|
||||
func (h *CustomerUndianHandler) formatTimePointer(t *time.Time) *string {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
formatted := t.Format("2006-01-02T15:04:05Z")
|
||||
return &formatted
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package response
|
||||
|
||||
// UndianListResponse represents the response for undian list API
|
||||
type UndianListResponse struct {
|
||||
Events []UndianEventResponse `json:"events"`
|
||||
}
|
||||
|
||||
// UndianEventResponse represents an undian event with customer's vouchers and prizes
|
||||
type UndianEventResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description *string `json:"description"`
|
||||
ImageURL *string `json:"image_url"`
|
||||
Status string `json:"status"`
|
||||
StartDate string `json:"start_date"`
|
||||
EndDate string `json:"end_date"`
|
||||
DrawDate string `json:"draw_date"`
|
||||
MinimumPurchase float64 `json:"minimum_purchase"`
|
||||
DrawCompleted bool `json:"draw_completed"`
|
||||
DrawCompletedAt *string `json:"draw_completed_at"`
|
||||
TermsConditions *string `json:"terms_and_conditions"`
|
||||
Prefix *string `json:"prefix"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
VoucherCount int `json:"voucher_count"`
|
||||
TotalPrizes int `json:"total_prizes"`
|
||||
Vouchers []UndianVoucherResponse `json:"vouchers"`
|
||||
Prizes []UndianPrizeResponse `json:"prizes"`
|
||||
}
|
||||
|
||||
// UndianVoucherResponse represents a customer's voucher
|
||||
type UndianVoucherResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
VoucherCode string `json:"voucher_code"`
|
||||
VoucherNumber *int `json:"voucher_number"`
|
||||
IsWinner bool `json:"is_winner"`
|
||||
PrizeRank *int `json:"prize_rank"`
|
||||
WonAt *string `json:"won_at"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
// UndianPrizeResponse represents a prize in the undian event
|
||||
type UndianPrizeResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
Rank int `json:"rank"`
|
||||
PrizeName string `json:"prize_name"`
|
||||
PrizeValue *float64 `json:"prize_value"`
|
||||
PrizeDescription *string `json:"prize_description"`
|
||||
PrizeType string `json:"prize_type"`
|
||||
PrizeImageURL *string `json:"prize_image_url"`
|
||||
WinningVoucherID *int64 `json:"winning_voucher_id,omitempty"`
|
||||
WinnerUserID *int64 `json:"winner_user_id,omitempty"`
|
||||
IsWon bool `json:"is_won"`
|
||||
Amount *int64 `json:"amount"`
|
||||
}
|
||||
|
||||
// ActiveEventsResponse represents the response for active events API (without customer data)
|
||||
type ActiveEventsResponse struct {
|
||||
Events []ActiveEventResponse `json:"events"`
|
||||
}
|
||||
|
||||
// ActiveEventResponse represents an active undian event (without customer-specific data)
|
||||
type ActiveEventResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description *string `json:"description"`
|
||||
ImageURL *string `json:"image_url"`
|
||||
Status string `json:"status"`
|
||||
StartDate string `json:"start_date"`
|
||||
EndDate string `json:"end_date"`
|
||||
DrawDate string `json:"draw_date"`
|
||||
MinimumPurchase float64 `json:"minimum_purchase"`
|
||||
DrawCompleted bool `json:"draw_completed"`
|
||||
DrawCompletedAt *string `json:"draw_completed_at"`
|
||||
TermsConditions *string `json:"terms_and_conditions"`
|
||||
Prefix *string `json:"prefix"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
Reference in New Issue
Block a user