add promo stats activation with time duration
This commit is contained in:
parent
e175593b3b
commit
c7a8e3b6e9
16
docs/proto/promo.proto
Normal file
16
docs/proto/promo.proto
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package codeword;
|
||||||
|
|
||||||
|
message Time {
|
||||||
|
int64 from = 1;
|
||||||
|
int64 to = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
service PromoCodeService {
|
||||||
|
rpc GetAllPromoActivations(Time) returns (PromoActivationResp);
|
||||||
|
}
|
||||||
|
|
||||||
|
message PromoActivationResp {
|
||||||
|
map<string, repeated string> response = 1;
|
||||||
|
}
|
@ -102,13 +102,13 @@ func (p *PromoCodeController) GetList(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
func (p *PromoCodeController) Activate(c *fiber.Ctx) error {
|
func (p *PromoCodeController) Activate(c *fiber.Ctx) error {
|
||||||
err := p.authMiddleware(c)
|
err := p.authMiddleware(c)
|
||||||
fmt.Println("SKER0",err)
|
fmt.Println("SKER0", err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err})
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err})
|
||||||
}
|
}
|
||||||
|
|
||||||
userID := c.Locals(models.AuthJWTDecodedUserIDKey).(string)
|
userID := c.Locals(models.AuthJWTDecodedUserIDKey).(string)
|
||||||
fmt.Println("SKER1",userID)
|
fmt.Println("SKER1", userID)
|
||||||
if userID == "" {
|
if userID == "" {
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "failed to get jwt payload"})
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "failed to get jwt payload"})
|
||||||
}
|
}
|
||||||
@ -121,10 +121,10 @@ func (p *PromoCodeController) Activate(c *fiber.Ctx) error {
|
|||||||
if req.Codeword == "" && req.FastLink == "" {
|
if req.Codeword == "" && req.FastLink == "" {
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "codeword or fastlink is required"})
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "codeword or fastlink is required"})
|
||||||
}
|
}
|
||||||
fmt.Println("SKER2",req)
|
fmt.Println("SKER2", req)
|
||||||
|
|
||||||
greetings, err := p.promoCodeService.ActivatePromo(c.Context(), &req, userID)
|
greetings, err := p.promoCodeService.ActivatePromo(c.Context(), &req, userID)
|
||||||
fmt.Println("SKER3",err)
|
fmt.Println("SKER3", err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger.Error("Failed to activate promocode", zap.Error(err))
|
p.logger.Error("Failed to activate promocode", zap.Error(err))
|
||||||
|
|
||||||
@ -206,7 +206,23 @@ func (p *PromoCodeController) GetStats(c *fiber.Ctx) error {
|
|||||||
promoStats, err := p.promoCodeService.GetStats(c.Context(), req)
|
promoStats, err := p.promoCodeService.GetStats(c.Context(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger.Error("Failed getting promo stats", zap.Error(err))
|
p.logger.Error("Failed getting promo stats", zap.Error(err))
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error: "+err.Error()})
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error: " + err.Error()})
|
||||||
}
|
}
|
||||||
return c.Status(fiber.StatusOK).JSON(promoStats)
|
return c.Status(fiber.StatusOK).JSON(promoStats)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PromoCodeController) GetAllPromoActivations(c *fiber.Ctx) error {
|
||||||
|
var req models.Time
|
||||||
|
|
||||||
|
if err := c.BodyParser(&req); err != nil {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request payload"})
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := p.promoCodeService.GetAllPromoActivations(c.Context(), &req)
|
||||||
|
if err != nil {
|
||||||
|
p.logger.Error("Failed getting all promo activations", zap.Error(err))
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error: " + err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(result)
|
||||||
|
}
|
||||||
|
@ -10,7 +10,6 @@ func (p *PromoCodeController) Register(router fiber.Router) {
|
|||||||
router.Delete("/:promocodeID", p.Delete)
|
router.Delete("/:promocodeID", p.Delete)
|
||||||
router.Post("/fastlink", p.CreateFastLink)
|
router.Post("/fastlink", p.CreateFastLink)
|
||||||
router.Post("/stats", p.GetStats)
|
router.Post("/stats", p.GetStats)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PromoCodeController) Name() string {
|
func (p *PromoCodeController) Name() string {
|
||||||
|
@ -69,11 +69,68 @@ func (r *StatsRepository) GetStatistics(ctx context.Context, promoCodeID string)
|
|||||||
var promoCodeStats models.PromoCodeStats
|
var promoCodeStats models.PromoCodeStats
|
||||||
err = r.mdb.FindOne(ctx, filter).Decode(&promoCodeStats)
|
err = r.mdb.FindOne(ctx, filter).Decode(&promoCodeStats)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == mongo.ErrNoDocuments {
|
if err == mongo.ErrNoDocuments {
|
||||||
return models.PromoCodeStats{}, nil
|
return models.PromoCodeStats{}, nil
|
||||||
}
|
}
|
||||||
return models.PromoCodeStats{}, err
|
return models.PromoCodeStats{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return promoCodeStats, nil
|
return promoCodeStats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *models.Time) (map[string][]string, error) {
|
||||||
|
pipeline := []bson.M{
|
||||||
|
{
|
||||||
|
"$project": bson.M{
|
||||||
|
"_id": 1,
|
||||||
|
"usageArray": bson.M{"$objectToArray": "$usageMap"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$unwind": "$usageArray",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$unwind": "$usageArray.v",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$match": bson.M{
|
||||||
|
"usageArray.v.time": bson.M{
|
||||||
|
"$gte": req.From,
|
||||||
|
"$lte": req.To,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$group": bson.M{
|
||||||
|
"_id": "$_id",
|
||||||
|
"users": bson.M{"$push": bson.M{
|
||||||
|
"UserID": "$usageArray.v.userID",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor, err := r.mdb.Aggregate(ctx, pipeline)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make(map[string][]string)
|
||||||
|
for cursor.Next(ctx) {
|
||||||
|
var data struct {
|
||||||
|
ID string `bson:"_id"`
|
||||||
|
Users []struct {
|
||||||
|
UserID string `bson:"UserID"`
|
||||||
|
} `bson:"users"`
|
||||||
|
}
|
||||||
|
err := cursor.Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, user := range data.Users {
|
||||||
|
result[data.ID] = append(result[data.ID], user.UserID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
@ -28,6 +28,7 @@ type PromoCodeRepository interface {
|
|||||||
type PromoStatsRepository interface {
|
type PromoStatsRepository interface {
|
||||||
UpdateStatistics(ctx context.Context, req *models.ActivateReq, promoCode *models.PromoCode, userID string) error
|
UpdateStatistics(ctx context.Context, req *models.ActivateReq, promoCode *models.PromoCode, userID string) error
|
||||||
GetStatistics(ctx context.Context, promoCodeID string) (models.PromoCodeStats, error)
|
GetStatistics(ctx context.Context, promoCodeID string) (models.PromoCodeStats, error)
|
||||||
|
GetAllPromoActivations(ctx context.Context, req *models.Time) (map[string][]string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type PromoDeps struct {
|
type PromoDeps struct {
|
||||||
@ -268,3 +269,13 @@ func (s *PromoCodeService) GetStats(ctx context.Context, req models.PromoStatReq
|
|||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, req *models.Time) (map[string][]string, error) {
|
||||||
|
result, err := s.statsRepo.GetAllPromoActivations(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("error getting all promo activations data", zap.Error(err))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user