mock shop sync group listing draft page

This commit is contained in:
2026-02-08 12:31:10 -07:00
parent e3b2dd3377
commit b155280081
19 changed files with 1519 additions and 166 deletions
+381 -5
View File
@@ -8,6 +8,7 @@ import (
"slices"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)
type (
@@ -28,6 +29,7 @@ type (
SyncGroupID int64
}
// TODO: the id ACTUALLY is the account_id and order_index
SyncGroupListingDraft struct {
AccountShopIDs
ListingID string
@@ -75,6 +77,44 @@ func (db *Store) CreateSyncGroupListingDraft(ctx context.Context, acctID int64)
return orderIndex, nil
}
func (db *Store) CreateMockSyncGroupListingDraft(ctx context.Context, acctID int64) (orderIndex int, err error) {
rows, err := db.db.Query(
ctx,
`
WITH max_order_index(max_index) AS (
SELECT
MAX(order_index) AS max_index
FROM
mock.sync_group_listing_draft_order_indexes
)
INSERT INTO
mock.sync_group_listing_draft_order_indexes (
account_id,
order_index
)
SELECT
@account_id,
COALESCE(max_index, -1) + 1
FROM
max_order_index
RETURNING
order_index
`,
pgx.NamedArgs{
"account_id": acctID,
},
)
if err != nil {
return 0, fmt.Errorf("failed to perform query: %w", err)
}
if orderIndex, err = pgx.CollectExactlyOneRow(rows, pgx.RowTo[int]); err != nil {
return 0, fmt.Errorf("failed to scan rows: %w", err)
}
return orderIndex, nil
}
func (db *Store) GetSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int) (SyncGroupListingDraft, error) {
rows, err := db.db.Query(
ctx,
@@ -124,8 +164,96 @@ func (db *Store) GetSyncGroupListingDraft(ctx context.Context, acctID int64, ord
}, nil
}
func (db *Store) GetMockSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int) (SyncGroupListingDraft, error) {
tx, err := db.db.BeginTx(ctx, pgx.TxOptions{
AccessMode: pgx.ReadOnly,
})
if err != nil {
return SyncGroupListingDraft{}, fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback(ctx)
rows, err := tx.Query(
ctx,
`
SELECT
true
FROM
mock.sync_group_listing_draft_order_indexes
WHERE
account_id = @account_id
AND order_index = @order_index
`,
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
},
)
if err != nil {
return SyncGroupListingDraft{}, fmt.Errorf("failed to scan rows: %w", err)
}
if _, err := pgx.CollectExactlyOneRow(rows, pgx.RowTo[bool]); err != nil {
return SyncGroupListingDraft{}, fmt.Errorf("failed to scan rows: %w", err)
}
for _, info := range getAllMockShopSchemaInfos() {
rows, err := tx.Query(
ctx,
fmt.Sprintf(
`
SELECT
shop_id,
listing_id
FROM
%s
WHERE
account_id = @account_id
AND order_index = @order_index
`,
info.syncGroupListingDraftsTable,
),
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
},
)
if err != nil {
return SyncGroupListingDraft{}, fmt.Errorf("failed to perform query: %w", err)
}
v, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByNameLax[struct {
Shop_id pgtype.Text
Listing_id pgtype.Text
}])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
continue
}
return SyncGroupListingDraft{}, fmt.Errorf("faild to scan row: %w", err)
}
return SyncGroupListingDraft{
AccountShopIDs: NewAccountIDs(acctID).
ShopID(info.platform, v.Shop_id.String),
ListingID: v.Listing_id.String,
OrderIndex: orderIndex,
}, nil
}
if err := tx.Commit(ctx); err != nil {
return SyncGroupListingDraft{}, fmt.Errorf("failed to commit transaction: %w", err)
}
return SyncGroupListingDraft{
AccountShopIDs: NewAccountIDs(acctID).
ShopID("", ""),
OrderIndex: orderIndex,
}, nil
}
func (db *Store) SetShopInSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int, platform Platform, shopID string) error {
tags, err := db.db.Exec(
tag, err := db.db.Exec(
ctx,
`
UPDATE
@@ -148,15 +276,106 @@ func (db *Store) SetShopInSyncGroupListingDraft(ctx context.Context, acctID int6
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
if tags.RowsAffected() != 1 {
if tag.RowsAffected() != 1 {
return consts.ErrNotFound
}
return nil
}
func (db *Store) SetShopInMockSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int, platform Platform, shopID string) error {
info, ok := getMockShopSchemaInfo(platform)
if !ok {
return fmt.Errorf("%w: unrecognized platform: %s", consts.ErrNotFound, platform)
}
tx, err := db.db.Begin(ctx)
if err != nil {
return fmt.Errorf("failed to start transaction: %w", err)
}
defer tx.Rollback(ctx)
_, err = tx.Exec(
ctx,
`
DELETE FROM
mock.sync_group_listing_draft_order_indexes
WHERE
account_id = @account_id
AND order_index = @order_index
RETURNING
account_id,
order_index
`,
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
},
)
if err != nil {
return fmt.Errorf("failed to perform query deleting existing index: %w", err)
}
_, err = tx.Exec(
ctx,
`
INSERT INTO
mock.sync_group_listing_draft_order_indexes (
account_id,
order_index
)
VALUES (
@account_id,
@order_index
)
`,
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
},
)
if err != nil {
// TODO: handle error where account doesn't exist?
return fmt.Errorf("failed to perform query inserting new index: %w", err)
}
_, err = tx.Exec(
ctx,
fmt.Sprintf(
`
INSERT INTO
%s (
account_id,
order_index,
shop_id
)
VALUES (
@account_id,
@order_index,
@shop_id
)
`,
info.syncGroupListingDraftsTable,
),
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
"shop_id": shopID,
},
)
if err != nil {
return fmt.Errorf("failed to perform query inserting specific shop index: %w", err)
}
if err := tx.Commit(ctx); err != nil {
return fmt.Errorf("failed to commit transaction: %w", err)
}
return nil
}
func (db *Store) SetListingInSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int, listingID string) error {
tags, err := db.db.Exec(
tag, err := db.db.Exec(
ctx,
`
UPDATE
@@ -176,13 +395,47 @@ func (db *Store) SetListingInSyncGroupListingDraft(ctx context.Context, acctID i
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
if tags.RowsAffected() != 1 {
if tag.RowsAffected() != 1 {
return consts.ErrNotFound
}
return nil
}
func (db *Store) SetListingInMockSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int, listingID string) error {
for _, info := range getAllMockShopSchemaInfos() {
tag, err := db.db.Exec(
ctx,
fmt.Sprintf(
`
UPDATE
%s
SET
listing_id = @listing_id
WHERE
account_id = @account_id
AND order_index = @order_index
`,
info.syncGroupListingDraftsTable,
),
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
"listing_id": listingID,
},
)
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
if tag.RowsAffected() > 0 {
return nil
}
}
return consts.ErrNotFound
}
func (db *Store) DeleteSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int) (numOfRows int, err error) {
rows, err := db.db.Query(
ctx,
@@ -238,6 +491,31 @@ func (db *Store) DeleteSyncGroupListingDraft(ctx context.Context, acctID int64,
return r.Num_rows, nil
}
func (db *Store) DeleteMockSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int) error {
tag, err := db.db.Exec(
ctx,
`
DELETE FROM
mock.sync_group_listing_draft_order_indexes
WHERE
account_id = @account_id
AND order_index = @order_index
`,
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
},
)
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
if tag.RowsAffected() != 1 {
return consts.ErrNotFound
}
return nil
}
func (db *Store) GetSyncGroupListingDrafts(ctx context.Context, acctID int64) ([]SyncGroupListingDraft, error) {
rows, err := db.db.Query(
ctx,
@@ -296,7 +574,105 @@ func (db *Store) GetSyncGroupListingDrafts(ctx context.Context, acctID int64) ([
return listings, nil
}
// TODO: test
func (db *Store) GetMockSyncGroupListingDrafts(ctx context.Context, acctID int64) ([]SyncGroupListingDraft, error) {
tx, err := db.db.BeginTx(ctx, pgx.TxOptions{
AccessMode: pgx.ReadOnly,
})
if err != nil {
return nil, fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback(ctx)
rows, err := tx.Query(
ctx,
`
SELECT
order_index
FROM
mock.sync_group_listing_draft_order_indexes
WHERE
account_id = @account_id
`,
pgx.NamedArgs{
"account_id": acctID,
},
)
if err != nil {
return nil, fmt.Errorf("failed to perform query: %w", err)
}
indexes, err := pgx.CollectRows(rows, pgx.RowTo[int])
if err != nil {
return nil, fmt.Errorf("failed to scan rows: %w", err)
}
listingsByOrderIndex := make(map[int]SyncGroupListingDraft, len(indexes))
for _, n := range indexes {
listingsByOrderIndex[n] = SyncGroupListingDraft{
AccountShopIDs: NewAccountIDs(acctID).
ShopID("", ""),
OrderIndex: n,
}
}
for _, info := range getAllMockShopSchemaInfos() {
rows, err := tx.Query(
ctx,
fmt.Sprintf(
`
SELECT
shop_id,
order_index,
listing_id
FROM
%s
WHERE
account_id = @account_id
`,
info.syncGroupListingDraftsTable,
),
pgx.NamedArgs{
"account_id": acctID,
},
)
if err != nil {
return nil, fmt.Errorf("failed to perform query: %w", err)
}
vs, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[struct {
Shop_id pgtype.Text
Order_index pgtype.Int8
Listing_id pgtype.Text
}])
if err != nil {
return nil, fmt.Errorf("failed to scan rows: %w", err)
}
for _, v := range vs {
n := int(v.Order_index.Int64)
l := listingsByOrderIndex[n]
l.Platform = info.platform
l.ShopID = v.Shop_id.String
l.ListingID = v.Listing_id.String
listingsByOrderIndex[n] = l
}
}
listings := make([]SyncGroupListingDraft, 0, len(listingsByOrderIndex))
for _, v := range listingsByOrderIndex {
listings = append(listings, v)
}
slices.SortFunc(listings, func(a, b SyncGroupListingDraft) int {
return a.OrderIndex - b.OrderIndex
})
if err := tx.Commit(ctx); err != nil {
return nil, fmt.Errorf("failed to commit transaction: %w", err)
}
return listings, nil
}
func (db *Store) SaveNewSyncGroup(ctx context.Context, acctID int64) (SyncGroup, error) {
txn, err := db.db.Begin(ctx)
if err != nil {