mock events saved to db

This commit is contained in:
2026-08-20 00:35:05 -06:00
parent 4386ede68e
commit df1b93261f
13 changed files with 970 additions and 366 deletions
+294
View File
@@ -5,11 +5,13 @@ package accounts
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
@@ -1461,6 +1463,290 @@ func (db *Store) DeleteListingInListingInMockSyncGroupBeingEdited(ctx context.Co
return nil
}
// if err := s.accts.SetShopInMockSaleForm(c, acctID, platform, shopID); err != nil {
func (db *Store) SetShopInMockSaleForm(ctx context.Context, acctID int64, platform Platform, shopID string) error {
_, err := db.db.Exec(
ctx,
`
INSERT INTO
mock.sale_form_shop_selection (
account_id,
platform,
shop_id
)
VALUES (
@account_id,
@platform,
@shop_id
)
ON CONFLICT (account_id) DO UPDATE
SET platform = EXCLUDED.platform,
shop_id = EXCLUDED.shop_id
`,
pgx.NamedArgs{
"account_id": acctID,
"platform": platform,
"shop_id": shopID,
},
)
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
return nil
}
func (db *Store) SetShopInMockRefundForm(ctx context.Context, acctID int64, platform Platform, shopID string) error {
_, err := db.db.Exec(
ctx,
`
INSERT INTO
mock.refund_form_shop_selection (
account_id,
platform,
shop_id
)
VALUES (
@account_id,
@platform,
@shop_id
)
ON CONFLICT (account_id) DO UPDATE
SET platform = EXCLUDED.platform,
shop_id = EXCLUDED.shop_id
`,
pgx.NamedArgs{
"account_id": acctID,
"platform": platform,
"shop_id": shopID,
},
)
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
return nil
}
func (db *Store) SetShopInMockCountForm(ctx context.Context, acctID int64, platform Platform, shopID string) error {
_, err := db.db.Exec(
ctx,
`
INSERT INTO
mock.count_form_shop_selection (
account_id,
platform,
shop_id
)
VALUES (
@account_id,
@platform,
@shop_id
)
ON CONFLICT (account_id) DO UPDATE
SET platform = EXCLUDED.platform,
shop_id = EXCLUDED.shop_id
`,
pgx.NamedArgs{
"account_id": acctID,
"platform": platform,
"shop_id": shopID,
},
)
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
return nil
}
type MockEventFormValues struct {
SaleShopID string
SalePlatform Platform
RefundShopID string
RefundPlatform Platform
CountShopID string
CountPlatform Platform
}
func (db *Store) GetMockEventFormValues(ctx context.Context, acctID int64) (*MockEventFormValues, error) {
rows, err := db.db.Query(
ctx,
`
SELECT
sf.platform AS sale_platform,
sf.shop_id AS sale_shop_id,
rf.platform AS refund_platform,
rf.shop_id AS refund_shop_id,
cf.platform AS count_platform,
cf.shop_id AS count_shop_id
FROM
mock.sale_form_shop_selection AS sf
FULL OUTER JOIN
mock.refund_form_shop_selection AS rf
USING
(account_id)
FULL OUTER JOIN
mock.count_form_shop_selection AS cf
USING
(account_id)
WHERE
sf.account_id = @account_id
`,
pgx.NamedArgs{
"account_id": acctID,
},
)
if err != nil {
return nil, fmt.Errorf("failed to perform query: %w", err)
}
v, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByNameLax[struct {
Sale_shop_id pgtype.Text
Sale_platform *Platform
Refund_shop_id pgtype.Text
Refund_platform *Platform
Count_shop_id pgtype.Text
Count_platform *Platform
}])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return &MockEventFormValues{}, nil
}
return nil, fmt.Errorf("failed to scan rows: %w", err)
}
return &MockEventFormValues{
SaleShopID: v.Sale_shop_id.String,
SalePlatform: deref(v.Sale_platform),
RefundShopID: v.Refund_shop_id.String,
RefundPlatform: deref(v.Refund_platform),
CountShopID: v.Count_shop_id.String,
CountPlatform: deref(v.Count_platform),
}, nil
}
func (db *Store) SaveNewMockSale(ctx context.Context, acctID int64, platform Platform, shopID, listingID string, count int) (uuid.UUID, error) {
eventID := uuid.New()
_, err := db.db.Exec(
ctx,
`
INSERT INTO
mock.raw_shop_events (
platform,
shop_id,
event_timestamp,
event_id,
raw_payload
)
VALUES (
@platform,
@shop_id,
NOW(),
@event_id,
@raw_payload
)
`,
pgx.NamedArgs{
"platform": platform,
"shop_id": shopID,
"event_id": eventID,
"raw_payload": json.RawMessage(fmt.Sprintf(`{
"type": "sale",
"listingID": %q,
"count": %d
}`, listingID, count)),
},
)
if err != nil {
return uuid.Nil, fmt.Errorf("failed to perform query: %w", err)
}
return eventID, nil
}
func (db *Store) SaveNewMockRefund(ctx context.Context, acctID int64, platform Platform, shopID, listingID string, count int) (uuid.UUID, error) {
eventID := uuid.New()
_, err := db.db.Exec(
ctx,
`
INSERT INTO
mock.raw_shop_events (
platform,
shop_id,
event_timestamp,
event_id,
raw_payload
)
VALUES (
@platform,
@shop_id,
NOW(),
@event_id,
@raw_payload
)
`,
pgx.NamedArgs{
"platform": platform,
"shop_id": shopID,
"event_id": eventID,
"raw_payload": json.RawMessage(fmt.Sprintf(`{
"type": "refund",
"listingID": %q,
"count": %d
}`, listingID, count)),
},
)
if err != nil {
return uuid.Nil, fmt.Errorf("failed to perform query: %w", err)
}
return eventID, nil
}
func (db *Store) SaveNewMockInventoryReset(ctx context.Context, acctID int64, platform Platform, shopID, listingID string, count int) (uuid.UUID, error) {
eventID := uuid.New()
_, err := db.db.Exec(
ctx,
`
INSERT INTO
mock.raw_shop_events (
platform,
shop_id,
event_timestamp,
event_id,
raw_payload
)
VALUES (
@platform,
@shop_id,
NOW(),
@event_id,
@raw_payload
)
`,
pgx.NamedArgs{
"platform": platform,
"shop_id": shopID,
"event_id": eventID,
"raw_payload": json.RawMessage(fmt.Sprintf(`{
"type": "inventory-reset",
"listingID": %q,
"count": %d
}`, listingID, count)),
},
)
if err != nil {
return uuid.Nil, fmt.Errorf("failed to perform query: %w", err)
}
return eventID, nil
}
// additional context
func (db *Store) GetAccountPointerByUserID(ctx context.Context, userID string) (*Account, error) {
@@ -1481,3 +1767,11 @@ func (db *Store) beginReadonlyTxn(ctx context.Context, cb func(pgx.Tx) error) er
func (db *Store) beginTxn(ctx context.Context, cb func(pgx.Tx) error) error {
return pgx.BeginFunc(ctx, db.db, cb)
}
func deref[T any](ptr *T) T {
if ptr != nil {
return *ptr
}
var zero T
return zero
}
+16
View File
@@ -105,6 +105,22 @@ func (v_ctx *StoreWithContext) DeleteListingInListingInMockSyncGroupBeingEdited(
return v_ctx.Store.DeleteListingInListingInMockSyncGroupBeingEdited(v_ctx.ctx, acctID, orderIndex)
}
func (v_ctx *StoreWithContext) SetShopInMockSaleForm(acctID int64, platform Platform, shopID string) error {
return v_ctx.Store.SetShopInMockSaleForm(v_ctx.ctx, acctID, platform, shopID)
}
func (v_ctx *StoreWithContext) SetShopInMockRefundForm(acctID int64, platform Platform, shopID string) error {
return v_ctx.Store.SetShopInMockRefundForm(v_ctx.ctx, acctID, platform, shopID)
}
func (v_ctx *StoreWithContext) SetShopInMockCountForm(acctID int64, platform Platform, shopID string) error {
return v_ctx.Store.SetShopInMockCountForm(v_ctx.ctx, acctID, platform, shopID)
}
func (v_ctx *StoreWithContext) GetMockEventFormValues(acctID int64) (*MockEventFormValues, error) {
return v_ctx.Store.GetMockEventFormValues(v_ctx.ctx, acctID)
}
func (v_ctx *StoreWithContext) GetAccountPointerByUserID(userID string) (*Account, error) {
return v_ctx.Store.GetAccountPointerByUserID(v_ctx.ctx, userID)
}
-8
View File
@@ -1235,11 +1235,3 @@ func (db *Store) listMockSyncGroupListings(ctx context.Context, tx pgx.Tx, acctI
return listings, nil
}
func deref[T any](ptr *T) T {
if ptr == nil {
var zero T
return zero
}
return *ptr
}