Skip to content

Commit

Permalink
feat: change entries amount column to float32
Browse files Browse the repository at this point in the history
  • Loading branch information
noobj committed Sep 26, 2022
1 parent e6e2d79 commit 245bc1e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/ahorro/fetchentries/fetchentries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var _ = Describe("Fetchentries", func() {

Context("when handler return expected json response", func() {
It("should pass", func() {
expectedRes := "{\"categories\":[{\"_id\":\"62badc82d420270009a51019\",\"sum\":110,\"percentage\":\"0.55\",\"name\":\"Food\",\"entries\":[{\"_id\":\"62badc82d420270009a51019\",\"amount\":110,\"date\":\"2022-01-05\",\"descr\":\"fuck\",\"category\":\"62badc82d420270009a51019\",\"user\":\"62badc82d420270009a51019\"}],\"color\":\"#a4e56c\"},{\"_id\":\"62badc82d420270009a51019\",\"sum\":90,\"percentage\":\"0.45\",\"name\":\"Abc\",\"entries\":[{\"_id\":\"62badc82d420270009a51019\",\"amount\":90,\"date\":\"2022-01-05\",\"descr\":\"fuck\",\"category\":\"62badc82d420270009a51019\",\"user\":\"62badc82d420270009a51019\"}],\"color\":\"#a4e51c\"}],\"total\":200}"
expectedRes := "{\"categories\":[{\"_id\":\"62badc82d420270009a51019\",\"sum\":110,\"percentage\":\"0.55\",\"name\":\"Food\",\"entries\":[{\"_id\":\"62badc82d420270009a51019\",\"amount\":110,\"date\":\"2022-01-05\",\"descr\":\"fuck\"}],\"color\":\"#a4e56c\"},{\"_id\":\"62badc82d420270009a51019\",\"sum\":90,\"percentage\":\"0.45\",\"name\":\"Abc\",\"entries\":[{\"_id\":\"62badc82d420270009a51019\",\"amount\":90,\"date\":\"2022-01-05\",\"descr\":\"fuck\"}],\"color\":\"#a4e51c\"}],\"total\":200}"
res, err := main.Handler(ctx, fakeRequest)
fmt.Printf("%+v", res.Body)
Expect(res.Body).To(Equal(expectedRes))
Expand Down
23 changes: 14 additions & 9 deletions cmd/ahorro/fetchentries/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ import (

type Response events.APIGatewayProxyResponse

type Entry struct {
Id primitive.ObjectID `json:"_id" bson:"_id"`
Amount float32 `json:"amount" bson:"amount"`
Date string `json:"date"`
Descr string `json:"descr"`
}

type AggregateResult struct {
Entries []AhorroRepository.Entry
Entries []Entry
Sum float32
Category []AhorroRepository.Category
}

type CategoryEntriesBundle struct {
Id primitive.ObjectID `json:"_id" bson:"_id"`
Sum float32 `json:"sum"`
Percentage string `json:"percentage"`
Name string `json:"name"`
Entries []AhorroRepository.Entry `json:"entries"`
Color string `json:"color"`
Id primitive.ObjectID `json:"_id" bson:"_id"`
Sum float32 `json:"sum"`
Percentage string `json:"percentage"`
Name string `json:"name"`
Entries []Entry `json:"entries"`
Color string `json:"color"`
}

func checkTimeFormat(format string, timeString string) bool {
Expand Down Expand Up @@ -127,10 +134,8 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event
for _, repoResult := range repoResults {
fmt.Printf("%+v", repoResult)
doc, _ := bson.Marshal(repoResult)

var result AggregateResult
err := bson.Unmarshal(doc, &result)

if err != nil {
fmt.Println("Unmarshall error: ", err)
return helper.GenerateErrorResponse[events.APIGatewayProxyResponse](500)
Expand Down
6 changes: 6 additions & 0 deletions cmd/ahorro/sync/callback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,11 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event
}

func main() {
userRepo := UserRepository.New()
defer userRepo.Disconnect()()
container.NamedSingleton("UserRepo", func() repositories.IRepository {
return userRepo
})

lambda.Start(jwtMiddleWare.Auth(Handler))
}
9 changes: 8 additions & 1 deletion cmd/ahorro/sync/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log"
"math/rand"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -205,8 +206,14 @@ func collateCategoryItems(categoryItems []CategoryItem, userId primitive.ObjectI
func collateEntryItems(entryItems []EntryItem, cateIdMap map[string]primitive.ObjectID, userId primitive.ObjectID) []interface{} {
var result []interface{}
for _, entryItem := range entryItems {
amount, err := strconv.ParseFloat(entryItem.Amount, 32)
if err != nil {
log.Println("Collating entries failed:", err)
panic("Collating entries failed")
}

newItemBson := bson.M{
"amount": entryItem.Amount,
"amount": amount,
"date": entryItem.Date,
"descr": entryItem.Descr,
"category": cateIdMap[entryItem.CategoryId],
Expand Down
6 changes: 3 additions & 3 deletions internal/repositories/ahorro/entry_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

type Entry struct {
Id primitive.ObjectID `json:"_id" bson:"_id"`
Amount string `json:"amount" bson:"amount"`
Amount float32 `json:"amount" bson:"amount"`
Date string `json:"date"`
Descr string `json:"descr"`
Category primitive.ObjectID `json:"category" bson:"category,omitempty"`
User primitive.ObjectID `json:"user" bson:"user,omitempty"`
Category primitive.ObjectID `json:"category,omitempty" bson:"category,omitempty"`
User primitive.ObjectID `json:"user,omitempty" bson:"user,omitempty"`
}

type Category struct {
Expand Down

0 comments on commit 245bc1e

Please sign in to comment.