Import category

This commit is contained in:
2021-12-06 20:19:38 +00:00
parent 674028d394
commit 495bc2b7c3
7 changed files with 274 additions and 32 deletions

View File

@ -14,11 +14,13 @@ import (
)
type YNABImport struct {
Context context.Context
accounts []postgres.Account
payees []postgres.Payee
queries *postgres.Queries
budgetID uuid.UUID
Context context.Context
accounts []postgres.Account
payees []postgres.Payee
categories []postgres.GetCategoriesRow
categoryGroups []postgres.CategoryGroup
queries *postgres.Queries
budgetID uuid.UUID
}
func NewYNABImport(q *postgres.Queries, budgetID uuid.UUID) (*YNABImport, error) {
@ -32,12 +34,24 @@ func NewYNABImport(q *postgres.Queries, budgetID uuid.UUID) (*YNABImport, error)
return nil, err
}
categories, err := q.GetCategories(context.Background(), budgetID)
if err != nil {
return nil, err
}
categoryGroups, err := q.GetCategoryGroups(context.Background(), budgetID)
if err != nil {
return nil, err
}
return &YNABImport{
Context: context.Background(),
accounts: accounts,
payees: payees,
queries: q,
budgetID: budgetID,
Context: context.Background(),
accounts: accounts,
payees: payees,
categories: categories,
categoryGroups: categoryGroups,
queries: q,
budgetID: budgetID,
}, nil
}
@ -74,7 +88,12 @@ func (ynab *YNABImport) Import(r io.Reader) error {
return fmt.Errorf("could not get payee %s: %w", payeeName, err)
}
//category := record[4] //also in 5 + 6 split by group/category
categoryGroup, categoryName := record[5], record[6] //also in 5 + 6 split by group/category
category, err := ynab.GetCategory(categoryGroup, categoryName)
if err != nil {
return fmt.Errorf("could not get category %s: %w", payeeName, err)
}
memo := record[7]
outflow := record[8]
@ -87,11 +106,12 @@ func (ynab *YNABImport) Import(r io.Reader) error {
//cleared := record[10]
transaction := postgres.CreateTransactionParams{
Date: date,
Memo: memo,
AccountID: account.ID,
PayeeID: payeeID,
Amount: amount,
Date: date,
Memo: memo,
AccountID: account.ID,
PayeeID: payeeID,
CategoryID: category,
Amount: amount,
}
_, err = ynab.queries.CreateTransaction(ynab.Context, transaction)
if err != nil {
@ -172,3 +192,54 @@ func (ynab *YNABImport) GetPayee(name string) (uuid.NullUUID, error) {
ynab.payees = append(ynab.payees, payee)
return uuid.NullUUID{UUID: payee.ID, Valid: true}, nil
}
func (ynab *YNABImport) GetCategory(group string, name string) (uuid.NullUUID, error) {
if group == "" || name == "" {
return uuid.NullUUID{}, nil
}
for _, category := range ynab.categories {
if category.Name == name && category.Group == group {
return uuid.NullUUID{UUID: category.ID, Valid: true}, nil
}
}
for _, categoryGroup := range ynab.categoryGroups {
if categoryGroup.Name == group {
createCategory := postgres.CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID}
category, err := ynab.queries.CreateCategory(ynab.Context, createCategory)
if err != nil {
return uuid.NullUUID{}, err
}
getCategory := postgres.GetCategoriesRow{
ID: category.ID,
CategoryGroupID: category.CategoryGroupID,
Name: category.Name,
Group: categoryGroup.Name,
}
ynab.categories = append(ynab.categories, getCategory)
return uuid.NullUUID{UUID: category.ID, Valid: true}, nil
}
}
categoryGroup, err := ynab.queries.CreateCategoryGroup(ynab.Context, postgres.CreateCategoryGroupParams{Name: name, BudgetID: ynab.budgetID})
if err != nil {
return uuid.NullUUID{}, err
}
ynab.categoryGroups = append(ynab.categoryGroups, categoryGroup)
category, err := ynab.queries.CreateCategory(ynab.Context, postgres.CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID})
if err != nil {
return uuid.NullUUID{}, err
}
getCategory := postgres.GetCategoriesRow{
ID: category.ID,
CategoryGroupID: category.CategoryGroupID,
Name: category.Name,
Group: categoryGroup.Name,
}
ynab.categories = append(ynab.categories, getCategory)
return uuid.NullUUID{UUID: category.ID, Valid: true}, nil
}