Implement YNAB Import in Vue

This commit is contained in:
Jan Bader 2022-01-26 21:12:42 +00:00
parent 7776ba90f0
commit 47cbaf9660
6 changed files with 67 additions and 57 deletions

View File

@ -1,44 +1,10 @@
package http
import (
"fmt"
"net/http"
"git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type AlwaysNeededData struct {
Budget postgres.Budget
Accounts []postgres.GetAccountsWithBalanceRow
}
func (h *Handler) getImportantData(c *gin.Context) {
budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID)
if err != nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("budgetid missing from URL"))
return
}
budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
accounts, err := h.Service.GetAccountsWithBalance(c.Request.Context(), budgetUUID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
base := AlwaysNeededData{
Accounts: accounts,
Budget: budget,
}
c.Set("data", base)
c.Next()
}

View File

@ -8,6 +8,7 @@ import (
"git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type BudgetingData struct {
@ -60,8 +61,29 @@ func getDate(c *gin.Context) (time.Time, error) {
}
func (h *Handler) budgeting(c *gin.Context) {
alwaysNeededData := c.MustGet("data").(AlwaysNeededData)
budgetUUID := alwaysNeededData.Budget.ID
budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID)
if err != nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("budgetid missing from URL"))
return
}
budget, err := h.Service.GetBudget(c.Request.Context(), budgetUUID)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
accounts, err := h.Service.GetAccountsWithBalance(c.Request.Context(), budgetUUID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
data := AlwaysNeededData{
Accounts: accounts,
Budget: budget,
}
firstOfMonth, err := getDate(c)
if err != nil {
@ -72,7 +94,7 @@ func (h *Handler) budgeting(c *gin.Context) {
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0)
d := BudgetingData{
AlwaysNeededData: alwaysNeededData,
AlwaysNeededData: data,
Date: firstOfMonth,
Next: firstOfNextMonth,
Previous: firstOfPreviousMonth,
@ -87,13 +109,12 @@ func (h *Handler) budgeting(c *gin.Context) {
}
// skip everything in the future
categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, alwaysNeededData.Budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances)
categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, data.Budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances)
if err != nil {
return
}
d.Categories = categoriesWithBalance
data := c.MustGet("data").(AlwaysNeededData)
var availableBalance float64 = 0
for _, cat := range categories {
if cat.ID != data.Budget.IncomeCategoryID {

View File

@ -48,7 +48,6 @@ func (h *Handler) Serve() {
withBudget := router.Group("")
withBudget.Use(h.verifyLoginWithForbidden)
withBudget.Use(h.getImportantData)
withBudget.GET("/budget/:budgetid/:year/:month", h.budgeting)
withBudget.GET("/budget/:budgetid/all-accounts", h.allAccounts)
withBudget.GET("/budget/:budgetid/settings/clear", h.clearBudget)
@ -69,8 +68,8 @@ func (h *Handler) Serve() {
authenticated.GET("/admin/clear-database", h.clearDatabase)
withBudget2 := authenticated.Group("")
withBudget2.Use(h.getImportantData)
withBudget2.GET("/budget/:budgetid", h.budgeting)
withBudget2.POST("/budget/:budgetid/import/ynab", h.importYNAB)
budget := authenticated.Group("/budget")
budget.POST("/new", h.newBudget)
@ -78,7 +77,6 @@ func (h *Handler) Serve() {
transaction := authenticated.Group("/transaction")
transaction.POST("/new", h.newTransaction)
transaction.POST("/:transactionid", h.newTransaction)
transaction.POST("/import/ynab", h.importYNAB)
router.Run(":1323")
}

View File

@ -10,7 +10,7 @@ import (
)
func (h *Handler) importYNAB(c *gin.Context) {
budgetID, succ := c.GetPostForm("budget_id")
budgetID, succ := c.Params.Get("budgetid")
if !succ {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("no budget_id specified"))
return

View File

@ -2,15 +2,44 @@
import { TITLE } from "../store/mutation-types"
export default {
data() {
return {
transactionsFile: null,
assignmentsFile: null
}
},
computed: {
hasFiles() {
return this.$data.transactionsFile != null && this.$data.assignmentsFile != null;
}
},
mounted() {
this.$store.commit(TITLE, "Settings")
},
methods: {
gotAssignments(e){
this.$data.assignmentsFile = e.target.files[0];
},
gotTransactions(e) {
this.$data.transactionsFile = e.target.files[0];
},
clearBudget() {
// <a href="/budget/{{$store.getters.CurrentBudget.ID}}/settings/clear">Clear budget</a>
},
cleanNegative() {
// <a href="/budget/{{.Budget.ID}}/settings/clean-negative">Fix all historic negative category-balances</a>
},
ynabImport() {
// <input type="hidden" name="budget_id" value="{{$store.getters.CurrentBudget.ID}}" />
let formData = new FormData();
formData.append("transactions", this.$data.transactionsFile);
formData.append("assignments", this.$data.assignmentsFile);
fetch('/api/v1/budget/'+this.$store.getters.CurrentBudget.ID+"/import/ynab", {
method: "POST",
headers: {
'Authorization': 'Bearer ' + this.$store.state.Session.Token
},
body: formData});
}
}
}
@ -27,18 +56,15 @@ export default {
<p>This restores YNABs functionality, that would substract any overspent categories' balances from next months inflows.</p>
</v-card>
<v-card>
<form method="POST" action="/api/v1/transaction/import/ynab" enctype="multipart/form-data">
<input type="hidden" name="budget_id" value="{{$store.getters.CurrentBudget.ID}}" />
<label for="transactions_file">
Transaktionen:
<input type="file" name="transactions" accept="text/*" />
</label>
<br />
<label for="assignments_file">
Budget:
<input type="file" name="assignments" accept="text/*" />
</label>
<button type="submit">Importieren</button>
</form>
<label for="transactions_file">
Transaktionen:
<input type="file" @change="gotTransactions" accept="text/*" />
</label>
<br />
<label for="assignments_file">
Budget:
<input type="file" @change="gotAssignments" accept="text/*" />
</label>
<button v-if="hasFiles" @click="ynabImport">Importieren</button>
</v-card>
</template>

View File

@ -67,7 +67,6 @@ const dashboard = {
.then(x => commit("addBudget", x));
},
async setCurrentBudget({state, commit, dispatch, rootState}, budgetid) {
await dispatch("fetchDashboard");
for (const element of rootState.Budgets) {
if(element.ID != budgetid)
continue