fix(backend): address gift card review findings

Fix #1: remove expiry_date from gift card INSERTs (rolling 24-month via last_used_at only)

Fix #2: add FOR UPDATE to RedeemGiftCard SELECT (race condition)

Fix #6: add ?type=customer|inventory filter to GetGiftCards

Fix #7: add admin_audit_log INSERT to GetUserGiftCardBalanceAdmin

Fix #10: refactor normalizeCode to validators.NormalizeGiftCardCode

Schema: add admin_audit_log table (GDPR Article 30), update testdb.go

Tests: expiry-date-null, inventory-filter, audit-log, db table references

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-06-12 10:50:49 +01:00
co-authored by Sisyphus
parent e7fd9c89eb
commit 3c447a5c33
6 changed files with 399 additions and 154 deletions
+20 -3
View File
@@ -1754,9 +1754,26 @@ CREATE INDEX idx_gift_card_expired_balances_unclaimed ON gift_card_expired_balan
WHERE claimed_at IS NULL;
CREATE INDEX idx_gift_card_transactions_created_at ON gift_card_transactions(created_at);
-- Foreign Key Constraints added after all dependent tables are created
-- to avoid creation order dependencies during schema initialization.
ALTER TABLE payments ADD CONSTRAINT fk_payments_gift_card FOREIGN KEY (gift_card_id) REFERENCES gift_cards(id);
-- =======================================
-- ADMIN AUDIT LOG TABLE
-- =======================================
-- Records admin actions that access or modify user financial data.
-- Legal basis: GDPR Article 30 (records of processing) and financial audit requirements.
-- =======================================
CREATE TABLE admin_audit_log (
id CHAR(12) PRIMARY KEY DEFAULT generate_short_id('admin_audit_log'),
admin_id CHAR(12) NOT NULL REFERENCES users(id),
action_type VARCHAR(30) NOT NULL,
target_user_id CHAR(12) REFERENCES users(id),
target_gift_card_id CHAR(12) REFERENCES gift_cards(id),
details JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_admin_audit_log_admin ON admin_audit_log(admin_id);
CREATE INDEX idx_admin_audit_log_target_user ON admin_audit_log(target_user_id);
CREATE INDEX idx_admin_audit_log_created_at ON admin_audit_log(created_at);
-- =======================================
-- SQUARE DEPOSITS TABLE (Bank Reconciliation)