Split transactions and assignments export into two endpoints
This commit is contained in:
parent
e0dc7800af
commit
bc65249c03
@ -67,31 +67,33 @@ func (ynab *YNABExport) ExportTransactions(context context.Context, w io.Writer)
|
||||
csv := csv.NewWriter(w)
|
||||
csv.Comma = '\t'
|
||||
|
||||
transactions, err := ynab.queries.GetTransactionsForBudget(context, ynab.budgetID)
|
||||
transactions, err := ynab.queries.GetAllTransactionsForBudget(context, ynab.budgetID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load transactions: %w", err)
|
||||
}
|
||||
|
||||
header := []string{
|
||||
"Account",
|
||||
"Flag",
|
||||
"Date",
|
||||
"Payee",
|
||||
"Category Group/Category",
|
||||
"Category Group",
|
||||
"Category",
|
||||
"Memo",
|
||||
"Outflow",
|
||||
"Inflow",
|
||||
"Cleared",
|
||||
}
|
||||
|
||||
err = csv.Write(header)
|
||||
if err != nil {
|
||||
return fmt.Errorf("write transaction: %w", err)
|
||||
}
|
||||
|
||||
count := 0
|
||||
for _, transaction := range transactions {
|
||||
row := []string{
|
||||
transaction.Account,
|
||||
"", // Flag
|
||||
transaction.Date.Format("02.01.2006"),
|
||||
transaction.Payee,
|
||||
transaction.CategoryGroup + " : " + transaction.Category,
|
||||
transaction.CategoryGroup,
|
||||
transaction.Category,
|
||||
transaction.Memo,
|
||||
}
|
||||
|
||||
if transaction.Amount.IsPositive() {
|
||||
row = append(row, transaction.Amount.String()+"€", "0,00€")
|
||||
} else {
|
||||
row = append(row, "0,00€", transaction.Amount.String()[1:]+"€")
|
||||
}
|
||||
|
||||
row = append(row, string(transaction.Status))
|
||||
row := GetTransactionRow(transaction)
|
||||
|
||||
err := csv.Write(row)
|
||||
if err != nil {
|
||||
@ -106,3 +108,31 @@ func (ynab *YNABExport) ExportTransactions(context context.Context, w io.Writer)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetTransactionRow(transaction GetAllTransactionsForBudgetRow) []string {
|
||||
row := []string{
|
||||
transaction.Account,
|
||||
"", // Flag
|
||||
transaction.Date.Format("02.01.2006"),
|
||||
transaction.Payee,
|
||||
}
|
||||
|
||||
if transaction.CategoryGroup != "" && transaction.Category != "" {
|
||||
row = append(row,
|
||||
transaction.CategoryGroup+" : "+transaction.Category,
|
||||
transaction.CategoryGroup,
|
||||
transaction.Category)
|
||||
} else {
|
||||
row = append(row, "", "", "")
|
||||
}
|
||||
|
||||
row = append(row, transaction.Memo)
|
||||
|
||||
if transaction.Amount.IsPositive() {
|
||||
row = append(row, transaction.Amount.String()+"€", "0,00€")
|
||||
} else {
|
||||
row = append(row, "0,00€", transaction.Amount.String()[1:]+"€")
|
||||
}
|
||||
|
||||
return append(row, string(transaction.Status))
|
||||
}
|
||||
|
@ -67,7 +67,8 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
authenticated.GET("/budget/:budgetid/autocomplete/categories", h.autocompleteCategories)
|
||||
authenticated.DELETE("/budget/:budgetid", h.deleteBudget)
|
||||
authenticated.POST("/budget/:budgetid/import/ynab", h.importYNAB)
|
||||
authenticated.POST("/budget/:budgetid/export/ynab", h.exportYNAB)
|
||||
authenticated.POST("/budget/:budgetid/export/ynab/transactions", h.exportYNABTransactions)
|
||||
authenticated.POST("/budget/:budgetid/export/ynab/assignments", h.exportYNABAssignments)
|
||||
authenticated.POST("/budget/:budgetid/settings/clear", h.clearBudget)
|
||||
|
||||
budget := authenticated.Group("/budget")
|
||||
|
@ -64,7 +64,7 @@ func (h *Handler) importYNAB(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) exportYNAB(c *gin.Context) {
|
||||
func (h *Handler) exportYNABTransactions(c *gin.Context) {
|
||||
budgetID, succ := c.Params.Get("budgetid")
|
||||
if !succ {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"no budget_id specified"})
|
||||
@ -88,6 +88,26 @@ func (h *Handler) exportYNAB(c *gin.Context) {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) exportYNABAssignments(c *gin.Context) {
|
||||
budgetID, succ := c.Params.Get("budgetid")
|
||||
if !succ {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"no budget_id specified"})
|
||||
return
|
||||
}
|
||||
|
||||
budgetUUID, err := uuid.Parse(budgetID)
|
||||
if !succ {
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
ynab, err := postgres.NewYNABExport(c.Request.Context(), h.Service.Queries, budgetUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = ynab.ExportAssignments(c.Request.Context(), c.Writer)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user