Skip to content

Commit 9de60dd

Browse files
authored
fix: allow transactions to be updated without an Envelope (#286)
This adds the changes made to transaction creation in #273 to the logic for transaction updates, too.
1 parent 5811440 commit 9de60dd

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

pkg/controllers/transaction.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ func CreateTransaction(c *gin.Context) {
164164
return
165165
}
166166

167-
// Check the envelope ID only if it is set. (This will always evaluate to true for incoming and outgoing transactions,
168-
// but for transfers, can evaluate to false
167+
// Check the envelope ID only if it is set.
169168
if transaction.EnvelopeID != nil {
170169
_, err = getEnvelopeResource(c, *transaction.EnvelopeID)
171170
if err != nil {
@@ -309,7 +308,7 @@ func UpdateTransaction(c *gin.Context) {
309308
if data.SourceAccountID != uuid.Nil {
310309
sourceAccountID = data.SourceAccountID
311310
}
312-
sourceAccount, err := getAccountResource(c, sourceAccountID)
311+
_, err = getAccountResource(c, sourceAccountID)
313312
if err != nil {
314313
return
315314
}
@@ -319,17 +318,17 @@ func UpdateTransaction(c *gin.Context) {
319318
if data.DestinationAccountID != uuid.Nil {
320319
destinationAccountID = data.DestinationAccountID
321320
}
322-
destinationAccount, err := getAccountResource(c, destinationAccountID)
321+
_, err = getAccountResource(c, destinationAccountID)
323322
if err != nil {
324323
return
325324
}
326325

327-
// Check if the transaction is a transfer. If yes, the envelope can be empty.
328-
//
329-
// Check that the Envelope ID is set for incoming and outgoing transactions
330-
if sourceAccount.External || destinationAccount.External && data.EnvelopeID == nil {
331-
httputil.NewError(c, http.StatusBadRequest, errors.New("For incoming and outgoing transactions, an envelope is required"))
332-
return
326+
// Check the envelope ID only if it is set.
327+
if data.EnvelopeID != nil {
328+
_, err = getEnvelopeResource(c, *data.EnvelopeID)
329+
if err != nil {
330+
return
331+
}
333332
}
334333

335334
err = database.DB.Model(&transaction).Select("", updateFields...).Updates(data).Error

pkg/controllers/transaction_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,39 @@ func (suite *TestSuiteEnv) TestUpdateNoEnvelopeTransactionOutgoing() {
432432
transaction := createTestTransaction(suite.T(), c)
433433

434434
recorder := test.Request(suite.T(), http.MethodPatch, transaction.Data.Links.Self, `{ "envelopeId": null }`)
435-
test.AssertHTTPStatus(suite.T(), http.StatusBadRequest, &recorder)
435+
test.AssertHTTPStatus(suite.T(), http.StatusOK, &recorder)
436+
}
437+
438+
func (suite *TestSuiteEnv) TestUpdateEnvelopeTransactionOutgoing() {
439+
envelope := createTestEnvelope(suite.T(), models.EnvelopeCreate{})
436440

437-
err := test.DecodeError(suite.T(), recorder.Body.Bytes())
438-
assert.Equal(suite.T(), "For incoming and outgoing transactions, an envelope is required", err, "request id %s", recorder.Header().Get("x-request-id"))
441+
c := models.TransactionCreate{
442+
BudgetID: createTestBudget(suite.T(), models.BudgetCreate{Name: "Testing budget for updating of outgoing transfer"}).Data.ID,
443+
SourceAccountID: createTestAccount(suite.T(), models.AccountCreate{Name: "Internal Source Account", External: false}).Data.ID,
444+
DestinationAccountID: createTestAccount(suite.T(), models.AccountCreate{Name: "External destination account", External: true}).Data.ID,
445+
EnvelopeID: &envelope.Data.ID,
446+
Amount: decimal.NewFromFloat(984.13),
447+
}
448+
449+
transaction := createTestTransaction(suite.T(), c)
450+
recorder := test.Request(suite.T(), http.MethodPatch, transaction.Data.Links.Self, fmt.Sprintf("{ \"envelopeId\": \"%s\" }", &envelope.Data.ID))
451+
test.AssertHTTPStatus(suite.T(), http.StatusOK, &recorder)
452+
}
453+
454+
func (suite *TestSuiteEnv) TestUpdateNonExistingEnvelopeTransactionOutgoing() {
455+
envelope := createTestEnvelope(suite.T(), models.EnvelopeCreate{})
456+
457+
c := models.TransactionCreate{
458+
BudgetID: createTestBudget(suite.T(), models.BudgetCreate{Name: "Testing budget for updating of outgoing transfer"}).Data.ID,
459+
SourceAccountID: createTestAccount(suite.T(), models.AccountCreate{Name: "Internal Source Account", External: false}).Data.ID,
460+
DestinationAccountID: createTestAccount(suite.T(), models.AccountCreate{Name: "External destination account", External: true}).Data.ID,
461+
EnvelopeID: &envelope.Data.ID,
462+
Amount: decimal.NewFromFloat(984.13),
463+
}
464+
465+
transaction := createTestTransaction(suite.T(), c)
466+
recorder := test.Request(suite.T(), http.MethodPatch, transaction.Data.Links.Self, `{ "envelopeId": "e6fa8eb5-5f2c-4292-8ef9-02f0c2af1ce4" }`)
467+
test.AssertHTTPStatus(suite.T(), http.StatusNotFound, &recorder)
439468
}
440469

441470
func (suite *TestSuiteEnv) TestUpdateNonExistingTransaction() {

0 commit comments

Comments
 (0)