Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add total pricing to Homebox locations #42

Merged
merged 12 commits into from
Jun 29, 2024
9 changes: 9 additions & 0 deletions backend/app/api/handlers/v1/v1_ctrl_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/csv"
"errors"
"math/big"
"net/http"
"strings"

Expand Down Expand Up @@ -80,6 +81,14 @@ func (ctrl *V1Controller) HandleItemsGetAll() errchain.HandlerFunc {
ctx := services.NewContext(r.Context())

items, err := ctrl.repo.Items.QueryByGroup(ctx, ctx.GID, extractQuery(r))
totalPrice := new(big.Int)
for _, item := range items.Items {
totalPrice.Add(totalPrice, big.NewInt(int64(item.PurchasePrice*100)))
}

totalPriceFloat := new(big.Float).SetInt(totalPrice)
totalPriceFloat.Quo(totalPriceFloat, big.NewFloat(100))

if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return server.JSON(w, http.StatusOK, repo.PaginationResult[repo.ItemSummary]{
Expand Down
32 changes: 31 additions & 1 deletion backend/app/api/handlers/v1/v1_ctrl_locations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1

import (
"context"
"math/big"
"net/http"

"github.com/google/uuid"
Expand Down Expand Up @@ -83,6 +85,32 @@ func (ctrl *V1Controller) HandleLocationDelete() errchain.HandlerFunc {
return adapters.CommandID("id", fn, http.StatusNoContent)
}

func (ctrl *V1Controller) GetLocationWithPrice(auth context.Context, GID uuid.UUID, ID uuid.UUID) (repo.LocationOut, error) {
var location, err = ctrl.repo.Locations.GetOneByGroup(auth, GID, ID)

// Add direct child items price
totalPrice := new(big.Int)
items, err := ctrl.repo.Items.QueryByGroup(auth, GID, repo.ItemQuery{LocationIDs: []uuid.UUID{ID}})
for _, item := range items.Items {
totalPrice.Add(totalPrice, big.NewInt(int64(item.PurchasePrice*100)))
}

totalPriceFloat := new(big.Float).SetInt(totalPrice)
totalPriceFloat.Quo(totalPriceFloat, big.NewFloat(100))
location.TotalPrice, _ = totalPriceFloat.Float64()

// Add price from child locations
for _, childLocation := range location.Children {
var childLocation, err = ctrl.GetLocationWithPrice(auth, GID, childLocation.ID)
if err != nil {
return repo.LocationOut{}, err
}
location.TotalPrice += childLocation.TotalPrice
}

return location, err
}

// HandleLocationGet godoc
//
// @Summary Get Location
Expand All @@ -95,7 +123,9 @@ func (ctrl *V1Controller) HandleLocationDelete() errchain.HandlerFunc {
func (ctrl *V1Controller) HandleLocationGet() errchain.HandlerFunc {
fn := func(r *http.Request, ID uuid.UUID) (repo.LocationOut, error) {
auth := services.NewContext(r.Context())
return ctrl.repo.Locations.GetOneByGroup(auth, auth.GID, ID)
var location, err = ctrl.GetLocationWithPrice(auth, auth.GID, ID)

return location, err
}

return adapters.CommandID("id", fn, http.StatusOK)
Expand Down
8 changes: 7 additions & 1 deletion backend/app/api/static/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2469,6 +2469,9 @@
"parent": {
"$ref": "#/definitions/repo.LocationSummary"
},
"totalPrice": {
"type": "number"
},
"updatedAt": {
"type": "string"
}
Expand Down Expand Up @@ -2707,6 +2710,9 @@
},
"total": {
"type": "integer"
},
"totalPrice": {
"type": "number"
}
}
},
Expand Down Expand Up @@ -2989,4 +2995,4 @@
"in": "header"
}
}
}
}
1 change: 1 addition & 0 deletions backend/internal/data/repo/repo_locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type (
Parent *LocationSummary `json:"parent,omitempty"`
LocationSummary
Children []LocationSummary `json:"children"`
TotalPrice float64 `json:"totalPrice"`
}
)

Expand Down
10 changes: 7 additions & 3 deletions docs/docs/api/openapi-2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -1683,14 +1683,12 @@
"parameters": [
{
"type": "string",
"example": "[email protected]",
"description": "string",
"name": "username",
"in": "formData"
},
{
"type": "string",
"example": "admin",
"description": "string",
"name": "password",
"in": "formData"
Expand Down Expand Up @@ -2469,6 +2467,9 @@
"parent": {
"$ref": "#/definitions/repo.LocationSummary"
},
"totalPrice": {
"type": "number"
},
"updatedAt": {
"type": "string"
}
Expand Down Expand Up @@ -2707,6 +2708,9 @@
},
"total": {
"type": "integer"
},
"totalPrice": {
"type": "number"
}
}
},
Expand Down Expand Up @@ -2989,4 +2993,4 @@
"in": "header"
}
}
}
}
2 changes: 2 additions & 0 deletions frontend/lib/api/types/data-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export interface LocationOut {
id: string;
name: string;
parent: LocationSummary;
totalPrice: number;
updatedAt: Date | string;
}

Expand Down Expand Up @@ -329,6 +330,7 @@ export interface PaginationResultItemSummary {
page: number;
pageSize: number;
total: number;
totalPrice: number;
}

export interface TotalsByOrganizer {
Expand Down
15 changes: 12 additions & 3 deletions frontend/pages/label/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
return [];
}

return resp.data.items;
return resp.data;
});
</script>

Expand All @@ -115,8 +115,17 @@
</div>
</div>
<div>
<h1 class="text-2xl pb-1">
<h1 class="text-2xl pb-1 flex items-center gap-3">
{{ label ? label.name : "" }}

<div
v-if="items && items.totalPrice"
class="text-xs bg-secondary text-secondary-content rounded-full px-2 py-1"
>
<div>
<Currency :amount="items.totalPrice" />
</div>
</div>
</h1>
<div class="flex gap-1 flex-wrap text-xs">
<div>
Expand Down Expand Up @@ -144,7 +153,7 @@
<Markdown v-if="label && label.description" class="text-base" :source="label.description"> </Markdown>
</div>
<section v-if="label && items">
<ItemViewSelectable :items="items" />
<ItemViewSelectable :items="items.items" />
</section>
</BaseContainer>
</BaseContainer>
Expand Down
11 changes: 10 additions & 1 deletion frontend/pages/location/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,17 @@
<li>{{ location.name }}</li>
</ul>
</div>
<h1 class="text-2xl pb-1">
<h1 class="text-2xl pb-1 flex items-center gap-3">
{{ location ? location.name : "" }}

<div
v-if="location && location.totalPrice"
class="text-xs bg-secondary text-secondary-content rounded-full px-2 py-1"
>
<div>
<Currency :amount="location.totalPrice" />
</div>
</div>
</h1>
<div class="flex gap-1 flex-wrap text-xs">
<div>
Expand Down
Loading