BOC v1.0: RS256 auth, Ledger integration, Prometheus metrics, CI/CD, backup
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
FROM alpine:latest
|
||||
RUN apk add --no-cache ca-certificates
|
||||
COPY event-stream /usr/local/bin/
|
||||
EXPOSE 9097
|
||||
CMD ["event-stream"]
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
module event-stream
|
||||
|
||||
go 1.25.10
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.3.1 // indirect
|
||||
github.com/klauspost/compress v1.15.9 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.15 // indirect
|
||||
github.com/segmentio/kafka-go v0.4.51 // indirect
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
github.com/go-chi/chi/v5 v5.3.1 h1:3j4HZLGZQ3JpMCrPJF/Jl3mYJfWLKBfNJ6quurUGCf8=
|
||||
github.com/go-chi/chi/v5 v5.3.1/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
|
||||
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
|
||||
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
||||
github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0=
|
||||
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/segmentio/kafka-go v0.4.51 h1:JgDPPG75tC1rWIS2Me6MwcvXJ6f49UQ4HjAOef71Hno=
|
||||
github.com/segmentio/kafka-go v0.4.51/go.mod h1:Y1gn60kzLEEaW28YshXyk2+VCUKbJ3Qr6DrnT3i4+9E=
|
||||
@@ -0,0 +1,156 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/segmentio/kafka-go"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
EventID string `json:"event_id"`
|
||||
EventType string `json:"event_type"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
UserID string `json:"user_id,omitempty"`
|
||||
UserEmail string `json:"user_email,omitempty"`
|
||||
CompanyID string `json:"company_id,omitempty"`
|
||||
JobID string `json:"job_id,omitempty"`
|
||||
Amount float64 `json:"amount,omitempty"`
|
||||
Currency string `json:"currency,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Country string `json:"country,omitempty"`
|
||||
IPAddress string `json:"ip_address,omitempty"`
|
||||
UserAgent string `json:"user_agent,omitempty"`
|
||||
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
||||
RiskScore float64 `json:"risk_score,omitempty"`
|
||||
AnomalyDetected bool `json:"anomaly_detected,omitempty"`
|
||||
}
|
||||
|
||||
type EventStore struct {
|
||||
kafkaWriter *kafka.Writer
|
||||
}
|
||||
|
||||
func NewEventStore(brokers []string) *EventStore {
|
||||
// Use explicit dialer to avoid DNS issues
|
||||
dialer := &kafka.Dialer{
|
||||
Timeout: 10 * time.Second,
|
||||
DualStack: true,
|
||||
}
|
||||
|
||||
return &EventStore{
|
||||
kafkaWriter: &kafka.Writer{
|
||||
Addr: kafka.TCP(brokers...),
|
||||
Topic: "quixzoom.events",
|
||||
Balancer: &kafka.LeastBytes{},
|
||||
Dialer: dialer,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *EventStore) PublishEvent(ctx context.Context, event Event) error {
|
||||
event.Timestamp = time.Now().UTC()
|
||||
if event.EventID == "" {
|
||||
event.EventID = fmt.Sprintf("evt_%d", time.Now().UnixNano())
|
||||
}
|
||||
|
||||
data, err := json.Marshal(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.kafkaWriter.WriteMessages(ctx, kafka.Message{
|
||||
Key: []byte(event.EventType),
|
||||
Value: data,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *EventStore) Close() error {
|
||||
return s.kafkaWriter.Close()
|
||||
}
|
||||
|
||||
func main() {
|
||||
brokers := os.Getenv("KAFKA_BROKERS")
|
||||
if brokers == "" {
|
||||
brokers = "172.24.0.5:29092"
|
||||
}
|
||||
|
||||
store := NewEventStore([]string{brokers})
|
||||
defer store.Close()
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(middleware.Recoverer)
|
||||
r.Use(middleware.RequestID)
|
||||
|
||||
// Health check
|
||||
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
||||
})
|
||||
|
||||
// Ingest event
|
||||
r.Post("/api/v1/events", func(w http.ResponseWriter, r *http.Request) {
|
||||
var event Event
|
||||
if err := json.NewDecoder(r.Body).Decode(&event); err != nil {
|
||||
http.Error(w, `{"error":"invalid json"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if event.EventType == "" {
|
||||
http.Error(w, `{"error":"event_type required"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Anomaly detection (simple rule-based)
|
||||
if event.Amount > 100000 {
|
||||
event.RiskScore = 0.8
|
||||
event.AnomalyDetected = true
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := store.PublishEvent(ctx, event); err != nil {
|
||||
log.Printf("Error publishing event: %v", err)
|
||||
http.Error(w, `{"error":"failed to publish"}`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"status": "published",
|
||||
"event_id": event.EventID,
|
||||
})
|
||||
})
|
||||
|
||||
// Search events (placeholder - would query Elasticsearch)
|
||||
r.Get("/api/v1/events/search", func(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query().Get("q")
|
||||
eventType := r.URL.Query().Get("type")
|
||||
userID := r.URL.Query().Get("user_id")
|
||||
|
||||
// TODO: Query Elasticsearch
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"query": query,
|
||||
"type": eventType,
|
||||
"user_id": userID,
|
||||
"results": []Event{},
|
||||
"total": 0,
|
||||
"note": "Elasticsearch integration pending",
|
||||
})
|
||||
})
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "9097"
|
||||
}
|
||||
|
||||
log.Printf("Event streaming server starting on :%s", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, r))
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
module sie4-import
|
||||
|
||||
go 1.25.10
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/lib/pq v1.12.3 // indirect
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ=
|
||||
github.com/lib/pq v1.12.3/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA=
|
||||
@@ -0,0 +1,236 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type SIE4Parser struct {
|
||||
accounts map[string]string // account_number -> name
|
||||
ib map[string]float64 // account_number -> opening balance
|
||||
ub map[string]float64 // account_number -> closing balance
|
||||
vouchers []Voucher
|
||||
companyID uuid.UUID
|
||||
tenantID uuid.UUID
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
type Voucher struct {
|
||||
Series string
|
||||
Number int
|
||||
Date time.Time
|
||||
Description string
|
||||
Transactions []Transaction
|
||||
}
|
||||
|
||||
type Transaction struct {
|
||||
Account string
|
||||
Amount float64
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatal("Usage: sie4-import <sie-file>")
|
||||
}
|
||||
|
||||
db, err := sql.Open("postgres", os.Getenv("DB_URL"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
parser := &SIE4Parser{
|
||||
accounts: make(map[string]string),
|
||||
ib: make(map[string]float64),
|
||||
ub: make(map[string]float64),
|
||||
db: db,
|
||||
}
|
||||
|
||||
// Get LandveX AB company ID
|
||||
var companyIDStr string
|
||||
err = db.QueryRow("SELECT id FROM boc_companies WHERE org_number = $1", "559141-7042").Scan(&companyIDStr)
|
||||
if err != nil {
|
||||
log.Fatal("LandveX AB not found:", err)
|
||||
}
|
||||
parser.companyID = uuid.MustParse(companyIDStr)
|
||||
|
||||
var tenantIDStr string
|
||||
err = db.QueryRow("SELECT tenant_id FROM boc_companies WHERE id = $1", companyIDStr).Scan(&tenantIDStr)
|
||||
if err != nil {
|
||||
log.Fatal("Tenant not found:", err)
|
||||
}
|
||||
parser.tenantID = uuid.MustParse(tenantIDStr)
|
||||
|
||||
// Parse SIE4 file
|
||||
file, err := os.Open(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
var currentVoucher *Voucher
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
if strings.HasPrefix(line, "#KONTO") {
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) >= 3 {
|
||||
accNum := parts[1]
|
||||
name := strings.Trim(strings.Join(parts[2:], " "), "\"")
|
||||
parser.accounts[accNum] = name
|
||||
}
|
||||
} else if strings.HasPrefix(line, "#IB") {
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) >= 4 {
|
||||
accNum := parts[2]
|
||||
amount, _ := strconv.ParseFloat(parts[3], 64)
|
||||
parser.ib[accNum] = amount
|
||||
}
|
||||
} else if strings.HasPrefix(line, "#UB") {
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) >= 4 {
|
||||
accNum := parts[2]
|
||||
amount, _ := strconv.ParseFloat(parts[3], 64)
|
||||
parser.ub[accNum] = amount
|
||||
}
|
||||
} else if strings.HasPrefix(line, "#VER") {
|
||||
if currentVoucher != nil && len(currentVoucher.Transactions) > 0 {
|
||||
parser.vouchers = append(parser.vouchers, *currentVoucher)
|
||||
}
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) >= 5 {
|
||||
series := parts[1]
|
||||
number, _ := strconv.Atoi(parts[2])
|
||||
dateStr := parts[3]
|
||||
date, _ := time.Parse("20060102", dateStr)
|
||||
desc := strings.Trim(strings.Join(parts[4:], " "), "\"")
|
||||
currentVoucher = &Voucher{
|
||||
Series: series,
|
||||
Number: number,
|
||||
Date: date,
|
||||
Description: desc,
|
||||
}
|
||||
}
|
||||
} else if strings.HasPrefix(line, "#TRANS") {
|
||||
if currentVoucher != nil {
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) >= 3 {
|
||||
accNum := parts[1]
|
||||
amount, _ := strconv.ParseFloat(parts[3], 64)
|
||||
currentVoucher.Transactions = append(currentVoucher.Transactions, Transaction{
|
||||
Account: accNum,
|
||||
Amount: amount,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if currentVoucher != nil && len(currentVoucher.Transactions) > 0 {
|
||||
parser.vouchers = append(parser.vouchers, *currentVoucher)
|
||||
}
|
||||
|
||||
fmt.Printf("Parsed %d accounts, %d vouchers\n", len(parser.accounts), len(parser.vouchers))
|
||||
|
||||
// Import to database
|
||||
parser.importAccounts()
|
||||
parser.importVouchers()
|
||||
parser.importBalances()
|
||||
|
||||
fmt.Println("Import complete")
|
||||
}
|
||||
|
||||
func (p *SIE4Parser) importAccounts() {
|
||||
for accNum, name := range p.accounts {
|
||||
_, err := p.db.Exec(`
|
||||
INSERT INTO boc_chart_of_accounts (company_id, account_code, name, account_type, is_active)
|
||||
VALUES ($1, $2, $3, 'asset', true)
|
||||
ON CONFLICT (company_id, account_code) DO UPDATE SET name = $3
|
||||
`, p.companyID, accNum, name)
|
||||
if err != nil {
|
||||
log.Printf("Error importing account %s: %v", accNum, err)
|
||||
}
|
||||
}
|
||||
fmt.Printf("Imported %d accounts\n", len(p.accounts))
|
||||
}
|
||||
|
||||
func (p *SIE4Parser) importVouchers() {
|
||||
for _, v := range p.vouchers {
|
||||
var entryID uuid.UUID
|
||||
err := p.db.QueryRow(`
|
||||
INSERT INTO boc_journal_entries (company_id, entry_number, entry_date, description, source, status, posted_at)
|
||||
VALUES ($1, $2, $3, $4, 'import', 'posted', NOW())
|
||||
RETURNING id
|
||||
`, p.companyID, fmt.Sprintf("%s%d", v.Series, v.Number), v.Date, v.Description).Scan(&entryID)
|
||||
if err != nil {
|
||||
log.Printf("Error importing voucher %s%d: %v", v.Series, v.Number, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, t := range v.Transactions {
|
||||
// Get account ID
|
||||
var accountID uuid.UUID
|
||||
err := p.db.QueryRow(`
|
||||
SELECT id FROM boc_chart_of_accounts
|
||||
WHERE company_id = $1 AND account_code = $2
|
||||
`, p.companyID, t.Account).Scan(&accountID)
|
||||
if err != nil {
|
||||
log.Printf("Account not found: %s", t.Account)
|
||||
continue
|
||||
}
|
||||
|
||||
var debit, credit float64
|
||||
if t.Amount > 0 {
|
||||
debit = t.Amount
|
||||
} else {
|
||||
credit = -t.Amount
|
||||
}
|
||||
|
||||
_, err = p.db.Exec(`
|
||||
INSERT INTO boc_journal_lines (company_id, entry_id, account_id, debit, credit, description)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
`, p.companyID, entryID, accountID, debit, credit, v.Description)
|
||||
if err != nil {
|
||||
log.Printf("Error importing line: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Printf("Imported %d vouchers\n", len(p.vouchers))
|
||||
}
|
||||
|
||||
func (p *SIE4Parser) importBalances() {
|
||||
fiscalYear := 2026
|
||||
for accNum, amount := range p.ub {
|
||||
var accountID uuid.UUID
|
||||
err := p.db.QueryRow(`
|
||||
SELECT id FROM boc_chart_of_accounts
|
||||
WHERE company_id = $1 AND account_code = $2
|
||||
`, p.companyID, accNum).Scan(&accountID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err = p.db.Exec(`
|
||||
INSERT INTO boc_period_balances (company_id, account_id, fiscal_year, period, closing_balance)
|
||||
VALUES ($1, $2, $3, 0, $4)
|
||||
ON CONFLICT (company_id, account_id, fiscal_year, period)
|
||||
DO UPDATE SET closing_balance = $4
|
||||
`, p.companyID, accountID, fiscalYear, amount)
|
||||
if err != nil {
|
||||
log.Printf("Error importing balance for %s: %v", accNum, err)
|
||||
}
|
||||
}
|
||||
fmt.Println("Imported balances")
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user