Remove context from YNABImport struct
This commit is contained in:
parent
0f2501dcbd
commit
649f937254
@ -13,7 +13,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type YNABImport struct {
|
type YNABImport struct {
|
||||||
Context context.Context
|
|
||||||
accounts []Account
|
accounts []Account
|
||||||
payees []Payee
|
payees []Payee
|
||||||
categories []GetCategoriesRow
|
categories []GetCategoriesRow
|
||||||
@ -44,7 +43,6 @@ func NewYNABImport(context context.Context, q *Queries, budgetID uuid.UUID) (*YN
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &YNABImport{
|
return &YNABImport{
|
||||||
Context: context,
|
|
||||||
accounts: accounts,
|
accounts: accounts,
|
||||||
payees: payees,
|
payees: payees,
|
||||||
categories: categories,
|
categories: categories,
|
||||||
@ -60,7 +58,7 @@ func NewYNABImport(context context.Context, q *Queries, budgetID uuid.UUID) (*YN
|
|||||||
//"Apr 2019" "Income: Next Month" "Income" "Next Month" 0,00€ 0,00€ 0,00€
|
//"Apr 2019" "Income: Next Month" "Income" "Next Month" 0,00€ 0,00€ 0,00€
|
||||||
//
|
//
|
||||||
// Activity and Available are not imported, since they are determined by the transactions and historic assignments
|
// Activity and Available are not imported, since they are determined by the transactions and historic assignments
|
||||||
func (ynab *YNABImport) ImportAssignments(r io.Reader) error {
|
func (ynab *YNABImport) ImportAssignments(context context.Context, r io.Reader) error {
|
||||||
csv := csv.NewReader(r)
|
csv := csv.NewReader(r)
|
||||||
csv.Comma = '\t'
|
csv.Comma = '\t'
|
||||||
csv.LazyQuotes = true
|
csv.LazyQuotes = true
|
||||||
@ -80,7 +78,7 @@ func (ynab *YNABImport) ImportAssignments(r io.Reader) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
categoryGroup, categoryName := record[2], record[3] //also in 1 joined by :
|
categoryGroup, categoryName := record[2], record[3] //also in 1 joined by :
|
||||||
category, err := ynab.GetCategory(categoryGroup, categoryName)
|
category, err := ynab.GetCategory(context, categoryGroup, categoryName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("get category %s/%s: %w", categoryGroup, categoryName, err)
|
return fmt.Errorf("get category %s/%s: %w", categoryGroup, categoryName, err)
|
||||||
}
|
}
|
||||||
@ -100,7 +98,7 @@ func (ynab *YNABImport) ImportAssignments(r io.Reader) error {
|
|||||||
CategoryID: category.UUID,
|
CategoryID: category.UUID,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
}
|
}
|
||||||
_, err = ynab.queries.CreateAssignment(ynab.Context, assignment)
|
_, err = ynab.queries.CreateAssignment(context, assignment)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("save assignment %v: %w", assignment, err)
|
return fmt.Errorf("save assignment %v: %w", assignment, err)
|
||||||
}
|
}
|
||||||
@ -122,7 +120,7 @@ type Transfer struct {
|
|||||||
|
|
||||||
// ImportTransactions expects a TSV-file as exported by YNAB in the following format:
|
// ImportTransactions expects a TSV-file as exported by YNAB in the following format:
|
||||||
|
|
||||||
func (ynab *YNABImport) ImportTransactions(r io.Reader) error {
|
func (ynab *YNABImport) ImportTransactions(context context.Context, r io.Reader) error {
|
||||||
csv := csv.NewReader(r)
|
csv := csv.NewReader(r)
|
||||||
csv.Comma = '\t'
|
csv.Comma = '\t'
|
||||||
csv.LazyQuotes = true
|
csv.LazyQuotes = true
|
||||||
@ -137,7 +135,7 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error {
|
|||||||
count := 0
|
count := 0
|
||||||
for _, record := range csvData[1:] {
|
for _, record := range csvData[1:] {
|
||||||
accountName := record[0]
|
accountName := record[0]
|
||||||
account, err := ynab.GetAccount(accountName)
|
account, err := ynab.GetAccount(context, accountName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("get account %s: %w", accountName, err)
|
return fmt.Errorf("get account %s: %w", accountName, err)
|
||||||
}
|
}
|
||||||
@ -151,7 +149,7 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
categoryGroup, categoryName := record[5], record[6] //also in 4 joined by :
|
categoryGroup, categoryName := record[5], record[6] //also in 4 joined by :
|
||||||
category, err := ynab.GetCategory(categoryGroup, categoryName)
|
category, err := ynab.GetCategory(context, categoryGroup, categoryName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("get category %s/%s: %w", categoryGroup, categoryName, err)
|
return fmt.Errorf("get category %s/%s: %w", categoryGroup, categoryName, err)
|
||||||
}
|
}
|
||||||
@ -188,7 +186,7 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error {
|
|||||||
if strings.HasPrefix(payeeName, "Transfer : ") {
|
if strings.HasPrefix(payeeName, "Transfer : ") {
|
||||||
// Transaction is a transfer to
|
// Transaction is a transfer to
|
||||||
transferToAccountName := payeeName[11:]
|
transferToAccountName := payeeName[11:]
|
||||||
transferToAccount, err := ynab.GetAccount(transferToAccountName)
|
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
return fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
||||||
}
|
}
|
||||||
@ -221,11 +219,11 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error {
|
|||||||
transfer.GroupID = uuid.NullUUID{UUID: groupID, Valid: true}
|
transfer.GroupID = uuid.NullUUID{UUID: groupID, Valid: true}
|
||||||
openTransfer.GroupID = uuid.NullUUID{UUID: groupID, Valid: true}
|
openTransfer.GroupID = uuid.NullUUID{UUID: groupID, Valid: true}
|
||||||
|
|
||||||
_, err = ynab.queries.CreateTransaction(ynab.Context, transfer.CreateTransactionParams)
|
_, err = ynab.queries.CreateTransaction(context, transfer.CreateTransactionParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
return fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
||||||
}
|
}
|
||||||
_, err = ynab.queries.CreateTransaction(ynab.Context, openTransfer.CreateTransactionParams)
|
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
||||||
}
|
}
|
||||||
@ -236,13 +234,13 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error {
|
|||||||
openTransfers = append(openTransfers, transfer)
|
openTransfers = append(openTransfers, transfer)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
payeeID, err := ynab.GetPayee(payeeName)
|
payeeID, err := ynab.GetPayee(context, payeeName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("get payee %s: %w", payeeName, err)
|
return fmt.Errorf("get payee %s: %w", payeeName, err)
|
||||||
}
|
}
|
||||||
transaction.PayeeID = payeeID
|
transaction.PayeeID = payeeID
|
||||||
|
|
||||||
_, err = ynab.queries.CreateTransaction(ynab.Context, transaction)
|
_, err = ynab.queries.CreateTransaction(context, transaction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("save transaction %v: %w", transaction, err)
|
return fmt.Errorf("save transaction %v: %w", transaction, err)
|
||||||
}
|
}
|
||||||
@ -253,7 +251,7 @@ func (ynab *YNABImport) ImportTransactions(r io.Reader) error {
|
|||||||
|
|
||||||
for _, openTransfer := range openTransfers {
|
for _, openTransfer := range openTransfers {
|
||||||
fmt.Printf("Saving unmatched transfer from %s to %s on %s over %f as regular transaction\n", openTransfer.FromAccount, openTransfer.ToAccount, openTransfer.Date, openTransfer.Amount.GetFloat64())
|
fmt.Printf("Saving unmatched transfer from %s to %s on %s over %f as regular transaction\n", openTransfer.FromAccount, openTransfer.ToAccount, openTransfer.Date, openTransfer.Amount.GetFloat64())
|
||||||
_, err = ynab.queries.CreateTransaction(ynab.Context, openTransfer.CreateTransactionParams)
|
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
||||||
}
|
}
|
||||||
@ -295,14 +293,14 @@ func GetAmount(inflow string, outflow string) (Numeric, error) {
|
|||||||
return num, nil
|
return num, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ynab *YNABImport) GetAccount(name string) (*Account, error) {
|
func (ynab *YNABImport) GetAccount(context context.Context, name string) (*Account, error) {
|
||||||
for _, acc := range ynab.accounts {
|
for _, acc := range ynab.accounts {
|
||||||
if acc.Name == name {
|
if acc.Name == name {
|
||||||
return &acc, nil
|
return &acc, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
account, err := ynab.queries.CreateAccount(ynab.Context, CreateAccountParams{Name: name, BudgetID: ynab.budgetID})
|
account, err := ynab.queries.CreateAccount(context, CreateAccountParams{Name: name, BudgetID: ynab.budgetID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -311,7 +309,7 @@ func (ynab *YNABImport) GetAccount(name string) (*Account, error) {
|
|||||||
return &account, nil
|
return &account, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ynab *YNABImport) GetPayee(name string) (uuid.NullUUID, error) {
|
func (ynab *YNABImport) GetPayee(context context.Context, name string) (uuid.NullUUID, error) {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
return uuid.NullUUID{}, nil
|
return uuid.NullUUID{}, nil
|
||||||
}
|
}
|
||||||
@ -322,7 +320,7 @@ func (ynab *YNABImport) GetPayee(name string) (uuid.NullUUID, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
payee, err := ynab.queries.CreatePayee(ynab.Context, CreatePayeeParams{Name: name, BudgetID: ynab.budgetID})
|
payee, err := ynab.queries.CreatePayee(context, CreatePayeeParams{Name: name, BudgetID: ynab.budgetID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uuid.NullUUID{}, err
|
return uuid.NullUUID{}, err
|
||||||
}
|
}
|
||||||
@ -331,7 +329,7 @@ func (ynab *YNABImport) GetPayee(name string) (uuid.NullUUID, error) {
|
|||||||
return uuid.NullUUID{UUID: payee.ID, Valid: true}, nil
|
return uuid.NullUUID{UUID: payee.ID, Valid: true}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ynab *YNABImport) GetCategory(group string, name string) (uuid.NullUUID, error) {
|
func (ynab *YNABImport) GetCategory(context context.Context, group string, name string) (uuid.NullUUID, error) {
|
||||||
if group == "" || name == "" {
|
if group == "" || name == "" {
|
||||||
return uuid.NullUUID{}, nil
|
return uuid.NullUUID{}, nil
|
||||||
}
|
}
|
||||||
@ -345,7 +343,7 @@ func (ynab *YNABImport) GetCategory(group string, name string) (uuid.NullUUID, e
|
|||||||
for _, categoryGroup := range ynab.categoryGroups {
|
for _, categoryGroup := range ynab.categoryGroups {
|
||||||
if categoryGroup.Name == group {
|
if categoryGroup.Name == group {
|
||||||
createCategory := CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID}
|
createCategory := CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID}
|
||||||
category, err := ynab.queries.CreateCategory(ynab.Context, createCategory)
|
category, err := ynab.queries.CreateCategory(context, createCategory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uuid.NullUUID{}, err
|
return uuid.NullUUID{}, err
|
||||||
}
|
}
|
||||||
@ -361,13 +359,13 @@ func (ynab *YNABImport) GetCategory(group string, name string) (uuid.NullUUID, e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
categoryGroup, err := ynab.queries.CreateCategoryGroup(ynab.Context, CreateCategoryGroupParams{Name: group, BudgetID: ynab.budgetID})
|
categoryGroup, err := ynab.queries.CreateCategoryGroup(context, CreateCategoryGroupParams{Name: group, BudgetID: ynab.budgetID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uuid.NullUUID{}, err
|
return uuid.NullUUID{}, err
|
||||||
}
|
}
|
||||||
ynab.categoryGroups = append(ynab.categoryGroups, categoryGroup)
|
ynab.categoryGroups = append(ynab.categoryGroups, categoryGroup)
|
||||||
|
|
||||||
category, err := ynab.queries.CreateCategory(ynab.Context, CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID})
|
category, err := ynab.queries.CreateCategory(context, CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uuid.NullUUID{}, err
|
return uuid.NullUUID{}, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user