visamaster: record order actions for the "Ammallar" table
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

Every create, update and delete of a visa/master or sber order is now
recorded in visa_master_order_actions: who did it, finished or failed
(with the exception), and for an update only the columns that changed -
original before, changes after. Marking an order paid when a payment is
finalized is recorded the same way, with no user when the GBÜS callback
did it.

Actions are written in the transaction that changes the order, so one is
on record exactly when its change is; failed ones are written after the
rollback with the values they attempted. The payment finalize records
after its commit instead, so recording never holds up a payment GBÜS has
already taken. Updates that change nothing and creates that fail (no
order to attach to) are not recorded.

GET /visa-master-payment-orders/{id}/actions lists them, newest first;
GET /visa-master-payment-orders/{id}/actions/{actionId} adds original,
changes and exception. Both are staff-only and order-scoped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
guwandykandClaude Opus 5.5 committed 2026-10-25 22:39:42 +05:00
1 parent 742e078090
commit 11e1fa4972
18 files changed
+1527 -10

No files matched your search

+142 -4
View File
@@ -7,6 +7,8 @@ import (
"strings"
"online-back/internal/cards/visamaster"
"online-back/internal/cards/visamasteractions"
actionsdb "online-back/internal/cards/visamasteractions/db"
"online-back/internal/identity/rbac"
"online-back/internal/shared"
"online-back/pkg/client/postgresql"
@@ -214,7 +216,34 @@ func (r *repository) Create(ctx context.Context, dto visamaster.CreateVisaMaster
orderColumnsSQL, orderSelectColumns,
)
return scanOrder(r.client.QueryRow(ctx, query, args...))
// A failed create has no order to attach an action to, so only the
// successful one is recorded - in the same transaction as the insert.
tx, err := r.client.Begin(ctx)
if err != nil {
return visamaster.VisaMasterPaymentOrderDTO{}, err
}
defer tx.Rollback(ctx)
item, err := scanOrder(tx.QueryRow(ctx, query, args...))
if err != nil {
return visamaster.VisaMasterPaymentOrderDTO{}, err
}
created, err := actionsdb.Snapshot(ctx, tx, item.Id)
if err != nil {
return visamaster.VisaMasterPaymentOrderDTO{}, err
}
if err := actionsdb.Insert(ctx, tx, visamasteractions.CreateOrderActionDTO{
OrderId: item.Id, Name: visamasteractions.NameCreate, Status: visamasteractions.StatusFinished,
Changes: created, CreatedBy: dto.CreatedBy,
}); err != nil {
return visamaster.VisaMasterPaymentOrderDTO{}, err
}
if err := tx.Commit(ctx); err != nil {
return visamaster.VisaMasterPaymentOrderDTO{}, err
}
return item, nil
}
func (r *repository) Update(ctx context.Context, id int64, dto visamaster.UpdateVisaMasterPaymentOrderDTO) (visamaster.VisaMasterPaymentOrderDTO, error) {
@@ -240,15 +269,124 @@ func (r *repository) Update(ctx context.Context, id int64, dto visamaster.Update
strings.Join(assignments, ", "), len(args), orderColumnsSQL, orderSelectColumns,
)
item, err := scanOrder(r.client.QueryRow(ctx, query, args...))
tx, err := r.client.Begin(ctx)
if err != nil {
return visamaster.VisaMasterPaymentOrderDTO{}, err
}
defer tx.Rollback(ctx)
before, err := actionsdb.Snapshot(ctx, tx, id)
if err != nil {
return visamaster.VisaMasterPaymentOrderDTO{}, err
}
// fail rolls the update back and records it as failed, with the
// values it attempted.
fail := func(err error) (visamaster.VisaMasterPaymentOrderDTO, error) {
tx.Rollback(ctx)
original, changes := r.attemptedChanges(before, args)
r.recordFailure(ctx, id, visamasteractions.NameUpdate, dto.UpdatedBy, original, changes, err)
return visamaster.VisaMasterPaymentOrderDTO{}, err
}
item, err := scanOrder(tx.QueryRow(ctx, query, args...))
if err != nil {
return fail(err)
}
after, err := actionsdb.Snapshot(ctx, tx, id)
if err != nil {
return fail(err)
}
// An update that changed nothing (the form saved as-is) is not an action.
if original, changes := visamasteractions.Diff(before, after); changes != nil {
if err := actionsdb.Insert(ctx, tx, visamasteractions.CreateOrderActionDTO{
OrderId: id, Name: visamasteractions.NameUpdate, Status: visamasteractions.StatusFinished,
Original: original, Changes: changes, CreatedBy: dto.UpdatedBy,
}); err != nil {
return fail(err)
}
}
if err := tx.Commit(ctx); err != nil {
return fail(err)
}
return item, nil
}
func (r *repository) Delete(ctx context.Context, id int64) error {
return r.generic.Delete(ctx, id)
func (r *repository) Delete(ctx context.Context, id int64, deletedBy *int64) error {
tx, err := r.client.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
before, err := actionsdb.Snapshot(ctx, tx, id)
if err != nil {
return err
}
fail := func(err error) error {
tx.Rollback(ctx)
r.recordFailure(ctx, id, visamasteractions.NameDelete, deletedBy, before, nil, err)
return err
}
// Same soft delete as genericrepo.Delete, inside the transaction.
if _, err := tx.Exec(ctx, `UPDATE visa_master_payment_orders SET deleted_at = now() WHERE id = $1 AND deleted_at IS NULL`, id); err != nil {
return fail(err)
}
if err := actionsdb.Insert(ctx, tx, visamasteractions.CreateOrderActionDTO{
OrderId: id, Name: visamasteractions.NameDelete, Status: visamasteractions.StatusFinished,
Original: before, CreatedBy: deletedBy,
}); err != nil {
return fail(err)
}
if err := tx.Commit(ctx); err != nil {
return fail(err)
}
return nil
}
// attemptedChanges is what a failed update tried to change: the writable
// columns whose attempted value (args, in orderWritableColumns order)
// differs from before.
func (r *repository) attemptedChanges(before map[string]interface{}, args []interface{}) (original, changes map[string]interface{}) {
values := make(map[string]interface{}, len(orderWritableColumns))
current := make(map[string]interface{}, len(orderWritableColumns))
for i, column := range orderWritableColumns {
value := args[i]
// jsonArg's bytes are JSON, not a byte string to base64-encode.
if raw, ok := value.([]byte); ok {
value = json.RawMessage(raw)
}
values[column] = value
current[column] = before[column]
}
attempted, err := visamasteractions.Normalize(values)
if err != nil {
// Unencodable (e.g. invalid JSON in senderDatas): record the failure
// without the attempted values rather than not at all.
r.logger.Errorf("visa master order action: encode attempted values failed: %v", err)
return nil, nil
}
return visamasteractions.Diff(current, attempted)
}
// recordFailure records a failed action through the plain client, after
// its transaction rolled back. Best-effort: a write failure here is
// logged, not propagated over the action's own error.
func (r *repository) recordFailure(ctx context.Context, orderID int64, name string, createdBy *int64, original, changes map[string]interface{}, cause error) {
exception := cause.Error()
// Detached from cancellation: a client dropping the request must not
// cost us the record of what it tried.
if err := actionsdb.Insert(context.WithoutCancel(ctx), r.client, visamasteractions.CreateOrderActionDTO{
OrderId: orderID, Name: name, Status: visamasteractions.StatusFailed,
Original: original, Changes: changes, Exception: &exception, CreatedBy: createdBy,
}); err != nil {
r.logger.Errorf("record failed visa master order action (order %d, %s) failed: %v", orderID, name, err)
}
}
func scanOrder(row genericrepo.Scanner) (visamaster.VisaMasterPaymentOrderDTO, error) {