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 package http
import ( import (
"fmt"
"net/http"
"git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
) )
type AlwaysNeededData struct { type AlwaysNeededData struct {
Budget postgres.Budget Budget postgres.Budget
Accounts []postgres.GetAccountsWithBalanceRow 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" "git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
) )
type BudgetingData struct { type BudgetingData struct {
@ -60,8 +61,29 @@ func getDate(c *gin.Context) (time.Time, error) {
} }
func (h *Handler) budgeting(c *gin.Context) { func (h *Handler) budgeting(c *gin.Context) {
alwaysNeededData := c.MustGet("data").(AlwaysNeededData) budgetID := c.Param("budgetid")
budgetUUID := alwaysNeededData.Budget.ID 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) firstOfMonth, err := getDate(c)
if err != nil { if err != nil {
@ -72,7 +94,7 @@ func (h *Handler) budgeting(c *gin.Context) {
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0) firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0)
d := BudgetingData{ d := BudgetingData{
AlwaysNeededData: alwaysNeededData, AlwaysNeededData: data,
Date: firstOfMonth, Date: firstOfMonth,
Next: firstOfNextMonth, Next: firstOfNextMonth,
Previous: firstOfPreviousMonth, Previous: firstOfPreviousMonth,
@ -87,13 +109,12 @@ func (h *Handler) budgeting(c *gin.Context) {
} }
// skip everything in the future // 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 { if err != nil {
return return
} }
d.Categories = categoriesWithBalance d.Categories = categoriesWithBalance
data := c.MustGet("data").(AlwaysNeededData)
var availableBalance float64 = 0 var availableBalance float64 = 0
for _, cat := range categories { for _, cat := range categories {
if cat.ID != data.Budget.IncomeCategoryID { if cat.ID != data.Budget.IncomeCategoryID {

View File

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

View File

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

View File

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

View File

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