generated from PenaSide/GolangTemplate
add test ltv
This commit is contained in:
parent
962aa779a0
commit
0a8f184207
@ -279,25 +279,24 @@ func (receiver *HistoryRepository) CalculateCustomerLTV(ctx context.Context, fro
|
||||
if from != 0 || to != 0 {
|
||||
timeRange := bson.M{}
|
||||
if from != 0 {
|
||||
timeRange["$gte"] = time.Unix(from, 0)
|
||||
timeRange["$gte"] = time.Unix(from, 0).UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
if to != 0 {
|
||||
timeRange["$lte"] = time.Unix(to, 0)
|
||||
timeRange["$lte"] = time.Unix(to, 0).UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
timeFilter["createdAt"] = timeRange
|
||||
}
|
||||
|
||||
pipeline := mongo.Pipeline{
|
||||
{{"$match", bson.M{"key": models.CustomerHistoryKeyPayCart, "isDeleted": false}}},
|
||||
{{"$match", timeFilter}},
|
||||
{{"$group", bson.M{
|
||||
"_id": "$userId",
|
||||
"firstPayment": bson.M{"$first": "$createdAt"},
|
||||
"lastPayment": bson.M{"$last": "$createdAt"},
|
||||
"firstPayment": bson.M{"$min": "$createdAt"},
|
||||
"lastPayment": bson.M{"$max": "$createdAt"},
|
||||
}}},
|
||||
{{"$project", bson.M{
|
||||
"lifeTimeInDays": bson.M{"$divide": []interface{}{
|
||||
bson.M{"$subtract": []interface{}{"$lastPayment", "$firstPayment"}},
|
||||
bson.M{"$subtract": []interface{}{bson.M{"$toDate": "$lastPayment"}, bson.M{"$toDate": "$firstPayment"}}},
|
||||
86400000,
|
||||
}},
|
||||
}}},
|
||||
@ -318,7 +317,6 @@ func (receiver *HistoryRepository) CalculateCustomerLTV(ctx context.Context, fro
|
||||
)
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
|
||||
var results []struct{ AverageLTV float64 }
|
||||
if err := cursor.All(ctx, &results); err != nil {
|
||||
receiver.logger.Error("failed to getting result LTV <CalculateCustomerLTV> of <HistoryRepository>",
|
||||
@ -329,11 +327,10 @@ func (receiver *HistoryRepository) CalculateCustomerLTV(ctx context.Context, fro
|
||||
errors.ErrInternalError,
|
||||
)
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
averageLTV := results[0].AverageLTV
|
||||
return int64(averageLTV), nil
|
||||
averageLTV := int64(results[0].AverageLTV)
|
||||
return averageLTV, nil
|
||||
}
|
||||
|
54
tests/integration/calculate_LTV_test.go
Normal file
54
tests/integration/calculate_LTV_test.go
Normal file
@ -0,0 +1,54 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/client"
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/tests/helpers"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCalculateLTV(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
jwtUtil := helpers.InitializeJWT()
|
||||
token, err := jwtUtil.Create("807f1f77bcf81cd799439077")
|
||||
|
||||
if ok := assert.NoError(t, err); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
layout := "2006-01-02T15:04:05.000Z"
|
||||
fromString := "2023-11-08T22:29:48.719Z"
|
||||
toString := "2023-12-27T15:00:00.000Z"
|
||||
fromTime, err := time.Parse(layout, fromString)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
toTime, err := time.Parse(layout, toString)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
from := fromTime.Unix()
|
||||
to := toTime.Unix()
|
||||
fmt.Println(from, to)
|
||||
|
||||
response, err := client.Post[struct{}, models.ResponseErrorHTTP](ctx, &client.RequestSettings{
|
||||
URL: "http://" + "localhost:8000" + "/history/ltv",
|
||||
Body: swagger.CalculateLTVJSONBody{From: from, To: to},
|
||||
Headers: map[string]string{"Authorization": fmt.Sprintf("Bearer %s", token)},
|
||||
})
|
||||
if ok := assert.NoError(t, err); !ok {
|
||||
return
|
||||
}
|
||||
if ok := assert.Nil(t, response.Error); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, 200, response.StatusCode)
|
||||
|
||||
fmt.Println(response.Body)
|
||||
}
|
Loading…
Reference in New Issue
Block a user