Files
bank/collections/beneficiaries.go
zeekay 77c69fb333 fix: bump Base v0.39.0 → v1.3.1 + pin explicit collection IDs
Base v0.39.0 had a startup panic in MigrationsRunner.initMigrationsTable
(nil pointer at base.go:808 → migrations_runner.go:256 because app.DB()
returned nil before serve.go could finish init). Fixed in Base v1.x.

In Base v1.3.1, RelationField.CollectionId validation now requires the
ACTUAL collection ID, not the name (field_relation.go:277-280):

  relCollection, _ := app.FindCachedCollectionByNameOrId(v)
  if relCollection == nil || relCollection.Id != v {
      return validation.NewError("...The relation collection doesn't exist.")
  }

Previous code passed "users" / "accounts" by name; Base looked them up
fine but rejected the field because the stored ID was a random
`hbc_xxxxxxxx` not the name. Two fixes:

- All RelationField{CollectionId: "users"} → "_users_auth_" (Base's
  canonical id for the system users auth collection).
- All NewBaseCollection(Name) → NewBaseCollection(Name, Name) so each
  bank collection gets a stable id matching its name. Sibling collections
  can then keep referencing CollectionId: AccountCollectionName etc.

Verified locally:
  $ bankd serve --http :8070 --dir /tmp/bank-data-test
  Server started at http://0.0.0.0:8070
  $ sqlite3 /tmp/bank-data-test/data.db "SELECT name FROM _collections WHERE NOT system"
  users / accounts / beneficiaries / transactions / fees / sessions /
  balances / audit_log / documents / wallets
2026-05-27 17:49:24 -07:00

103 lines
2.2 KiB
Go

package collections
import "github.com/hanzoai/base/core"
const BeneficiaryCollectionName = "beneficiaries"
// EnsureBeneficiaryCollection creates the beneficiaries collection if it does
// not exist. Each beneficiary belongs to an account and stores bank routing
// details for outbound payments (wires, SEPA, ACH).
func EnsureBeneficiaryCollection(app core.App) error {
_, err := app.FindCollectionByNameOrId(BeneficiaryCollectionName)
if err == nil {
return nil
}
c := core.NewBaseCollection(BeneficiaryCollectionName, BeneficiaryCollectionName)
// API rules: list/view scoped to account owner; mutations superuser only (nil).
beneRule := `account.owner = @request.auth.id`
c.ListRule = &beneRule
c.ViewRule = &beneRule
c.Fields.Add(
// Parent account.
&core.RelationField{
Name: "account",
CollectionId: AccountCollectionName,
Required: true,
MaxSelect: 1,
},
// CurrencyCloud beneficiary id.
&core.TextField{
Name: "ccBeneficiaryId",
},
// Display name for this beneficiary.
&core.TextField{
Name: "name",
Required: true,
},
// Bank account holder name (may differ from display name).
&core.TextField{
Name: "bankAccountHolder",
Required: true,
},
// ISO 4217 currency code.
&core.TextField{
Name: "currency",
Required: true,
Min: 3,
Max: 3,
},
// ISO-3166 country code.
&core.TextField{
Name: "country",
Required: true,
Min: 2,
Max: 2,
},
// Payment type: regular | priority.
&core.SelectField{
Name: "paymentType",
Values: []string{"regular", "priority"},
MaxSelect: 1,
},
// Bank routing details stored as structured JSON:
// { "iban": "...", "bic": "...", "sortCode": "...", "accountNumber": "..." }
&core.JSONField{
Name: "bankDetails",
Required: true,
MaxSize: 1 << 20,
},
// Beneficiary address (JSON: street, city, postCode, country).
&core.JSONField{
Name: "address",
MaxSize: 1 << 20,
},
&core.BoolField{
Name: "verified",
},
&core.AutodateField{
Name: "created",
OnCreate: true,
},
&core.AutodateField{
Name: "updated",
OnCreate: true,
OnUpdate: true,
},
)
return app.Save(c)
}