Skip to content

Commit

Permalink
fix decimal cost and revenue not being stored correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
EricFrancis12 committed Sep 30, 2024
1 parent f02809c commit 463f433
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 11 deletions.
39 changes: 39 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"net/http"
"net/url"
"reflect"
"testing"

Expand Down Expand Up @@ -54,3 +55,41 @@ func isValidVercelHandler(handler http.HandlerFunc) bool {

return true
}

func TestGetRevenue(t *testing.T) {
t.Run("Test integer payout", func(t *testing.T) {
assert.Equal(t, float64(12), getRevenue(
url.URL{
RawQuery: "payout=12",
},
))
assert.Equal(t, float64(12), getRevenue(
url.URL{
RawQuery: "payout=12.0",
},
))
assert.Equal(t, float64(12), getRevenue(
url.URL{
RawQuery: "payout=12.00000",
},
))
})

t.Run("Test decimal payout", func(t *testing.T) {
assert.Equal(t, float64(12.56789), getRevenue(
url.URL{
RawQuery: "payout=12.56789",
},
))
assert.Equal(t, float64(12.56789), getRevenue(
url.URL{
RawQuery: "payout=12.567890",
},
))
assert.Equal(t, float64(12.56789), getRevenue(
url.URL{
RawQuery: "payout=12.5678900000",
},
))
})
}
6 changes: 3 additions & 3 deletions api/postback.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Postback(w http.ResponseWriter, r *http.Request) {
}
}

func convertClick(click pkg.Click, convTime time.Time, revenue int) pkg.Click {
func convertClick(click pkg.Click, convTime time.Time, revenue float64) pkg.Click {
click.ConvTime = convTime
click.Revenue += revenue
return click
Expand All @@ -68,9 +68,9 @@ func getPid(url url.URL) string {
return url.Query().Get(string(pkg.QueryParamPid))
}

func getRevenue(url url.URL) int {
func getRevenue(url url.URL) float64 {
payoutStr := url.Query().Get(string(pkg.QueryParamPayout))
revenue, err := strconv.Atoi(payoutStr)
revenue, err := strconv.ParseFloat(payoutStr, 64)
if err != nil || revenue < 0 {
return 0
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/click.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ func (cl *Click) TokenMatcherMap() URLTokenMatcherMap {
URLTokenMatcherID: strconv.Itoa(cl.ID),
URLTokenMatcherPublicID: cl.PublicID,
URLTokenMatcherExternalID: cl.ExternalID,
URLTokenMatcherCost: strconv.Itoa(cl.Cost),
URLTokenMatcherRevenue: strconv.Itoa(cl.Revenue),
URLTokenMatcherCost: fmt.Sprintf("%f", cl.Cost),
URLTokenMatcherRevenue: fmt.Sprintf("%f", cl.Revenue),
URLTokenMatcherViewTime: timeString(cl.ViewTime),
URLTokenMatcherClickTime: timeString(cl.ClickTime),
URLTokenMatcherConvTime: timeString(cl.ConvTime),
Expand Down
4 changes: 2 additions & 2 deletions pkg/trafficSource.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func (ts *TrafficSource) GetExternalId(url url.URL) string {
return url.Query().Get(ts.ExternalIDToken.QueryParam)
}

func (ts *TrafficSource) GetCost(url url.URL) int {
func (ts *TrafficSource) GetCost(url url.URL) float64 {
costStr := url.Query().Get(ts.CostToken.QueryParam)
cost, err := strconv.Atoi(costStr)
cost, err := strconv.ParseFloat(costStr, 64)
if err != nil || cost < 0 {
return 0
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/trafficSource_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -20,3 +21,47 @@ func TestParseNamedTokens(t *testing.T) {
assert.NotNil(t, parseNamedTokens("{"))
assert.Len(t, parseNamedTokens("{"), 0)
}

func TestGetCost(t *testing.T) {
ts := TrafficSource{
CostToken: Token{
QueryParam: "cost",
},
}

t.Run("Test integer cost", func(t *testing.T) {
assert.Equal(t, float64(12), ts.GetCost(
url.URL{
RawQuery: "cost=12",
},
))
assert.Equal(t, float64(12), ts.GetCost(
url.URL{
RawQuery: "cost=12.0",
},
))
assert.Equal(t, float64(12), ts.GetCost(
url.URL{
RawQuery: "cost=12.00000",
},
))
})

t.Run("Test decimal cost", func(t *testing.T) {
assert.Equal(t, float64(12.56789), ts.GetCost(
url.URL{
RawQuery: "cost=12.56789",
},
))
assert.Equal(t, float64(12.56789), ts.GetCost(
url.URL{
RawQuery: "cost=12.567890",
},
))
assert.Equal(t, float64(12.56789), ts.GetCost(
url.URL{
RawQuery: "cost=12.5678900000",
},
))
})
}
4 changes: 2 additions & 2 deletions pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type Click struct {
type ClickCreationReq struct {
PublicId string
ExternalId string
Cost int
Revenue int
Cost float64
Revenue float64
ViewTime time.Time
ClickTime time.Time
ConvTime time.Time
Expand Down
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ model Click {
id Int @id @default(autoincrement())
publicId String @unique
externalId String
cost Int
revenue Int
cost Float
revenue Float
viewTime DateTime
clickTime DateTime?
convTime DateTime?
Expand Down

0 comments on commit 463f433

Please sign in to comment.