protoype: list raw events on mock mode reports page
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package reports
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"ruben/inventory2/domains/accounts"
|
||||
"ruben/inventory2/logging"
|
||||
)
|
||||
|
||||
//go:generate concurry -s Store
|
||||
|
||||
type (
|
||||
Store struct {
|
||||
log *logging.Logger
|
||||
db *pgxpool.Pool
|
||||
accts *accounts.Store
|
||||
}
|
||||
)
|
||||
|
||||
func NewStore(logger *logging.Logger, db *pgxpool.Pool, accts *accounts.Store) *Store {
|
||||
return &Store{
|
||||
log: logger,
|
||||
db: db,
|
||||
accts: accts,
|
||||
}
|
||||
}
|
||||
|
||||
func (db *Store) WithContext(ctx context.Context) *StoreWithContext {
|
||||
return NewStoreWithContext(ctx, db)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Code generated by concurry DO NOT EDIT.
|
||||
// https://github.com/angelbeltran/concurry
|
||||
// concurry
|
||||
package reports
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ruben/inventory2/domains/accounts"
|
||||
)
|
||||
|
||||
type StoreWithContext struct {
|
||||
ctx context.Context
|
||||
*Store
|
||||
}
|
||||
|
||||
func NewStoreWithContext(ctx context.Context, v *Store) *StoreWithContext {
|
||||
return &StoreWithContext{
|
||||
ctx: ctx,
|
||||
Store: v,
|
||||
}
|
||||
}
|
||||
|
||||
func (v_ctx *StoreWithContext) GetRawShopEvents(acctID int64, platform accounts.Platform, shopID string) ([]RawShopEvent, error) {
|
||||
return v_ctx.Store.GetRawShopEvents(v_ctx.ctx, acctID, platform, shopID)
|
||||
}
|
||||
Reference in New Issue
Block a user