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
|
||||
}
|
||||
Reference in New Issue
Block a user