Files
inventory-plus-plus/domains/reports/events.go
T

82 lines
1.6 KiB
Go

package reports
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/jackc/pgx/v5"
"ruben/inventory2/consts"
"ruben/inventory2/domains/accounts"
)
type (
RawShopEvent struct {
Platform accounts.Platform
ShopID string
EventTimestamp time.Time
EventID string
RawPayload json.RawMessage
}
)
func (db *Store) GetRawShopEvents(ctx context.Context, acctID int64, platform accounts.Platform, shopID string) ([]RawShopEvent, error) {
if _, err := db.accts.GetMockShop(ctx, acctID, platform, shopID); err != nil {
if errors.Is(err, consts.ErrNotFound) {
return nil, fmt.Errorf("shop not found: %w", err)
}
return nil, fmt.Errorf("failed to look up shop: %w", err)
}
rows, err := db.db.Query(
ctx,
`
SELECT
event_timestamp,
event_id,
raw_payload
FROM
mock.raw_shop_events
WHERE
platform = @platform
AND shop_id = @shop_id
`,
pgx.NamedArgs{
"platform": platform,
"shop_id": shopID,
},
)
if err != nil {
return nil, fmt.Errorf("failed to perform query: %w", err)
}
vs, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[struct {
Event_timestamp time.Time
Event_id string
Raw_payload json.RawMessage
}])
if err != nil {
return nil, fmt.Errorf("failed to scan rows: %w", err)
}
fmt.Println("vs:", vs)
evts := make([]RawShopEvent, len(vs))
for i, v := range vs {
evts[i] = RawShopEvent{
Platform: platform,
ShopID: shopID,
EventTimestamp: v.Event_timestamp,
EventID: v.Event_id,
RawPayload: v.Raw_payload,
}
}
fmt.Println("evts:", evts)
return evts, nil
}