Improve ynab import by extracting methods
This commit is contained in:
parent
2423bdd3ee
commit
6929c940c4
@ -181,13 +181,14 @@ func (ynab *YNABImport) ImportTransactions(context context.Context, r io.Reader)
|
|||||||
}
|
}
|
||||||
|
|
||||||
payeeName := record[3]
|
payeeName := record[3]
|
||||||
// Transaction is a transfer to
|
// Transaction is a transfer
|
||||||
var shouldReturn bool
|
if strings.HasPrefix(payeeName, "Transfer : ") {
|
||||||
var returnValue error
|
err = ynab.ImportTransferTransaction(payeeName, context, transaction, accountName, &openTransfers, account, amount)
|
||||||
openTransfers, shouldReturn, returnValue = ynab.ImportTransaction(
|
} else {
|
||||||
payeeName, context, transaction, accountName, openTransfers, account, amount)
|
err = ynab.ImportRegularTransaction(context, payeeName, transaction)
|
||||||
if shouldReturn {
|
}
|
||||||
return returnValue
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
count++
|
count++
|
||||||
@ -207,40 +208,25 @@ func (ynab *YNABImport) ImportTransactions(context context.Context, r io.Reader)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ynab *YNABImport) ImportTransaction(payeeName string, context context.Context, transaction CreateTransactionParams, accountName string, openTransfers []Transfer, account *Account, amount Numeric) ([]Transfer, bool, error) {
|
func (ynab *YNABImport) ImportRegularTransaction(context context.Context, payeeName string, transaction CreateTransactionParams) error {
|
||||||
if strings.HasPrefix(payeeName, "Transfer : ") {
|
|
||||||
openTransfers, shouldReturn, returnValue, returnValue1, returnValue2 := ynab.ImportTransferTransaction(payeeName, context, transaction, accountName, openTransfers, account, amount)
|
|
||||||
if shouldReturn {
|
|
||||||
return returnValue, returnValue1, returnValue2
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
shouldReturn, returnValue, returnValue1, returnValue2 := ynab.ImportRegularTransaction(context, payeeName, transaction)
|
|
||||||
if shouldReturn {
|
|
||||||
return returnValue, returnValue1, returnValue2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return openTransfers, false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ynab *YNABImport) ImportRegularTransaction(context context.Context, payeeName string, transaction CreateTransactionParams) (bool, []Transfer, bool, error) {
|
|
||||||
payeeID, err := ynab.GetPayee(context, payeeName)
|
payeeID, err := ynab.GetPayee(context, payeeName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, nil, true, 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(context, transaction)
|
_, err = ynab.queries.CreateTransaction(context, transaction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, nil, true, fmt.Errorf("save transaction %v: %w", transaction, err)
|
return fmt.Errorf("save transaction %v: %w", transaction, err)
|
||||||
}
|
}
|
||||||
return false, nil, false, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ynab *YNABImport) ImportTransferTransaction(payeeName string, context context.Context, transaction CreateTransactionParams, accountName string, openTransfers []Transfer, account *Account, amount Numeric) ([]Transfer, bool, []Transfer, bool, error) {
|
func (ynab *YNABImport) ImportTransferTransaction(payeeName string, context context.Context, transaction CreateTransactionParams, accountName string, openTransfers *[]Transfer, account *Account, amount Numeric) error {
|
||||||
transferToAccountName := payeeName[11:]
|
transferToAccountName := payeeName[11:]
|
||||||
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, true, nil, true, fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
return fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
transfer := Transfer{
|
transfer := Transfer{
|
||||||
@ -251,7 +237,7 @@ func (ynab *YNABImport) ImportTransferTransaction(payeeName string, context cont
|
|||||||
}
|
}
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
for i, openTransfer := range openTransfers {
|
for i, openTransfer := range *openTransfers {
|
||||||
if openTransfer.TransferToAccount.ID != transfer.AccountID {
|
if openTransfer.TransferToAccount.ID != transfer.AccountID {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -263,8 +249,9 @@ func (ynab *YNABImport) ImportTransferTransaction(payeeName string, context cont
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Matched transfers from %s to %s over %f\n", account.Name, transferToAccount.Name, amount.GetFloat64())
|
fmt.Printf("Matched transfers from %s to %s over %f\n", account.Name, transferToAccount.Name, amount.GetFloat64())
|
||||||
openTransfers[i] = openTransfers[len(openTransfers)-1]
|
transfers := *openTransfers
|
||||||
openTransfers = openTransfers[:len(openTransfers)-1]
|
transfers[i] = transfers[len(transfers)-1]
|
||||||
|
*openTransfers = transfers[:len(transfers)-1]
|
||||||
found = true
|
found = true
|
||||||
|
|
||||||
groupID := uuid.New()
|
groupID := uuid.New()
|
||||||
@ -273,19 +260,19 @@ func (ynab *YNABImport) ImportTransferTransaction(payeeName string, context cont
|
|||||||
|
|
||||||
_, err = ynab.queries.CreateTransaction(context, transfer.CreateTransactionParams)
|
_, err = ynab.queries.CreateTransaction(context, transfer.CreateTransactionParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, true, nil, true, fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
return fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
||||||
}
|
}
|
||||||
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, true, nil, true, fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
openTransfers = append(openTransfers, transfer)
|
*openTransfers = append(*openTransfers, transfer)
|
||||||
}
|
}
|
||||||
return openTransfers, false, nil, false, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func trimLastChar(s string) string {
|
func trimLastChar(s string) string {
|
||||||
@ -366,31 +353,22 @@ func (ynab *YNABImport) GetCategory(context context.Context, group string, name
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, categoryGroup := range ynab.categoryGroups {
|
var categoryGroup *CategoryGroup
|
||||||
if categoryGroup.Name == group {
|
for _, existingGroup := range ynab.categoryGroups {
|
||||||
createCategory := CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID}
|
if existingGroup.Name == group {
|
||||||
category, err := ynab.queries.CreateCategory(context, createCategory)
|
categoryGroup = &existingGroup
|
||||||
if err != nil {
|
|
||||||
return uuid.NullUUID{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
getCategory := 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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if categoryGroup == nil {
|
||||||
newGroup := CreateCategoryGroupParams{Name: group, BudgetID: ynab.budgetID}
|
newGroup := CreateCategoryGroupParams{Name: group, BudgetID: ynab.budgetID}
|
||||||
categoryGroup, err := ynab.queries.CreateCategoryGroup(context, newGroup)
|
newCategoryGroup, err := ynab.queries.CreateCategoryGroup(context, newGroup)
|
||||||
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, newCategoryGroup)
|
||||||
|
categoryGroup = &newCategoryGroup
|
||||||
|
}
|
||||||
|
|
||||||
newCategory := CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID}
|
newCategory := CreateCategoryParams{Name: name, CategoryGroupID: categoryGroup.ID}
|
||||||
category, err := ynab.queries.CreateCategory(context, newCategory)
|
category, err := ynab.queries.CreateCategory(context, newCategory)
|
||||||
|
@ -38,7 +38,10 @@ func TestListTimezonesHandler(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("RegisterUser", func(t *testing.T) {
|
t.Run("RegisterUser", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
context.Request, err = http.NewRequest(http.MethodPost, "/api/v1/user/register", strings.NewReader(`{"password":"pass","email":"info@example.com","name":"Test"}`))
|
context.Request, err = http.NewRequest(
|
||||||
|
http.MethodPost,
|
||||||
|
"/api/v1/user/register",
|
||||||
|
strings.NewReader(`{"password":"pass","email":"info@example.com","name":"Test"}`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error creating request: %s", err)
|
t.Errorf("error creating request: %s", err)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user