ability to delete mock sync groups

This commit is contained in:
2026-02-10 18:05:30 -07:00
parent 0944703d2a
commit 95ef64ce18
15 changed files with 963 additions and 13 deletions
+90
View File
@@ -410,6 +410,96 @@ func (db *Store) GetMockListingsForShop(ctx context.Context, acctID int64, platf
return listings, nil
}
func (db *Store) GetMockListing(ctx context.Context, acctID int64, platform Platform, shopID, listingID string) (*Listing, error) {
in, ok := getMockShopSchemaInfo(platform)
if !ok {
return nil, fmt.Errorf("%w: unrecognized platform: %s", consts.ErrNotFound, platform)
}
rows, err := db.db.Query(
ctx,
fmt.Sprintf(
`
SELECT
name,
count,
sku,
description
FROM
%s
WHERE
account_id = @account_id
AND shop_id = @shop_id
AND listing_id = @listing_id
`,
in.listingsTable,
),
pgx.NamedArgs{
"account_id": acctID,
"shop_id": shopID,
"listing_id": listingID,
},
)
if err != nil {
return nil, fmt.Errorf("failed to perform query: %w", err)
}
v, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByNameLax[struct {
Name string
Count int64
Sku string
Description string
}])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, consts.ErrNotFound
}
return nil, fmt.Errorf("failed to scan row: %w", err)
}
return &Listing{
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
Platform: platform,
ShopID: shopID,
},
ListingID: listingID,
},
SKU: v.Sku,
Name: v.Name,
Description: v.Name,
Count: v.Count,
}, nil
}
func (db *Store) DeleteMockSyncGroup(ctx context.Context, acctID, syncGroupID int64) error {
tag, err := db.db.Exec(
ctx,
`
DELETE FROM
mock.sync_groups
WHERE
account_id = @account_id
AND sync_group_id = @sync_group_id
`,
pgx.NamedArgs{
"account_id": acctID,
"sync_group_id": syncGroupID,
},
)
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
if tag.RowsAffected() == 0 {
return consts.ErrNotFound
}
return nil
}
// additional context
func (db *Store) GetAccountPointerByUserID(ctx context.Context, userID string) (*Account, error) {
+14
View File
@@ -526,6 +526,7 @@ type mockShopSchemaInfo struct {
platform Platform
shopTable string
listingsTable string
syncGroupListingsTable string
syncGroupListingDraftsTable string
}
@@ -545,78 +546,91 @@ func getAllMockShopSchemaInfos() []mockShopSchemaInfo {
platform: Amazon,
shopTable: "mock.shop_amazon",
listingsTable: "mock.shop_amazon_listings",
syncGroupListingsTable: "mock.shop_amazon_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_amazon_sync_group_listing_drafts",
},
{
platform: BigCartel,
shopTable: "mock.shop_big_cartel",
listingsTable: "mock.shop_big_cartel_listings",
syncGroupListingsTable: "mock.shop_big_cartel_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_big_cartel_sync_group_listing_drafts",
},
{
platform: Ebay,
shopTable: "mock.shop_ebay",
listingsTable: "mock.shop_ebay_listings",
syncGroupListingsTable: "mock.shop_ebay_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_ebay_sync_group_listing_drafts",
},
{
platform: Ecwid,
shopTable: "mock.shop_ecwid",
listingsTable: "mock.shop_ecwid_listings",
syncGroupListingsTable: "mock.shop_ecwid_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_ecwid_sync_group_listing_drafts",
},
{
platform: Etsy,
shopTable: "mock.shop_etsy",
listingsTable: "mock.shop_etsy_listings",
syncGroupListingsTable: "mock.shop_etsy_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_etsy_sync_group_listing_drafts",
},
{
platform: Shopify,
shopTable: "mock.shop_shopify",
listingsTable: "mock.shop_shopify_listings",
syncGroupListingsTable: "mock.shop_shopify_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_shopify_sync_group_listing_drafts",
},
{
platform: SquareOnline,
shopTable: "mock.shop_square_online",
listingsTable: "mock.shop_square_online_listings",
syncGroupListingsTable: "mock.shop_square_online_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_square_online_sync_group_listing_drafts",
},
{
platform: Squarespace,
shopTable: "mock.shop_squarespace",
listingsTable: "mock.shop_squarespace_listings",
syncGroupListingsTable: "mock.shop_squarespace_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_squarespace_sync_group_listing_drafts",
},
{
platform: Tiktok,
shopTable: "mock.shop_tiktok",
listingsTable: "mock.shop_tiktok_listings",
syncGroupListingsTable: "mock.shop_tiktok_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_tiktok_sync_group_listing_drafts",
},
{
platform: WalmartMarketplace,
shopTable: "mock.shop_walmart_marketplace",
listingsTable: "mock.shop_walmart_marketplace_listings",
syncGroupListingsTable: "mock.shop_walmart_marketplace_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_walmart_marketplace_sync_group_listing_drafts",
},
{
platform: Wix,
shopTable: "mock.shop_wix",
listingsTable: "mock.shop_wix_listings",
syncGroupListingsTable: "mock.shop_wix_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_wix_sync_group_listing_drafts",
},
{
platform: WooCommerce,
shopTable: "mock.shop_woo_commerce",
listingsTable: "mock.shop_woo_commerce_listings",
syncGroupListingsTable: "mock.shop_woo_commerce_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_woo_commerce_sync_group_listing_drafts",
},
{
platform: Zoho,
shopTable: "mock.shop_zoho",
listingsTable: "mock.shop_zoho_listings",
syncGroupListingsTable: "mock.shop_zoho_sync_group_listings",
syncGroupListingDraftsTable: "mock.shop_zoho_sync_group_listing_drafts",
},
}
+17
View File
@@ -6,6 +6,7 @@ package accounts
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
type StoreWithContext struct {
@@ -56,6 +57,10 @@ func (v_ctx *StoreWithContext) GetMockListingsForShop(acctID int64, platform Pla
return v_ctx.Store.GetMockListingsForShop(v_ctx.ctx, acctID, platform, shopID)
}
func (v_ctx *StoreWithContext) GetMockListing(acctID int64, platform Platform, shopID string, listingID string) (*Listing, error) {
return v_ctx.Store.GetMockListing(v_ctx.ctx, acctID, platform, shopID, listingID)
}
func (v_ctx *StoreWithContext) GetAccountPointerByUserID(userID string) (*Account, error) {
return v_ctx.Store.GetAccountPointerByUserID(v_ctx.ctx, userID)
}
@@ -151,3 +156,15 @@ func (v_ctx *StoreWithContext) GetMockSyncGroupListingDrafts(acctID int64) ([]Sy
func (v_ctx *StoreWithContext) SaveNewSyncGroup(acctID int64) (SyncGroup, error) {
return v_ctx.Store.SaveNewSyncGroup(v_ctx.ctx, acctID)
}
func (v_ctx *StoreWithContext) SaveNewMockSyncGroup(acctID int64, name string) (SyncGroup, error) {
return v_ctx.Store.SaveNewMockSyncGroup(v_ctx.ctx, acctID, name)
}
func (v_ctx *StoreWithContext) ListMockSyncGroups(acctID int64) ([]SyncGroup, error) {
return v_ctx.Store.ListMockSyncGroups(v_ctx.ctx, acctID)
}
func (v_ctx *StoreWithContext) listMockSyncGroupListings(tx pgx.Tx, acctID int64, syncGroupID int64) ([]SyncGroupListing, error) {
return v_ctx.Store.listMockSyncGroupListings(v_ctx.ctx, tx, acctID, syncGroupID)
}
+367
View File
@@ -15,6 +15,7 @@ import (
type (
SyncGroup struct {
SyncGroupIDs
Name string
Listings []SyncGroupListing
}
@@ -811,6 +812,372 @@ func (db *Store) SaveNewSyncGroup(ctx context.Context, acctID int64) (SyncGroup,
}, nil
}
func (db *Store) SaveNewMockSyncGroup(ctx context.Context, acctID int64, name string) (SyncGroup, error) {
tx, err := db.db.Begin(ctx)
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to start transaction: %w", err)
}
defer tx.Rollback(ctx)
// verify there are enough listings
rows, err := tx.Query(
ctx,
`
SELECT
COUNT(*)
FROM
mock.sync_group_listing_draft_order_indexes
WHERE
account_id = @account_id
`,
pgx.NamedArgs{
"account_id": acctID,
},
)
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to perform query: %w", err)
}
numDrafts, err := pgx.CollectExactlyOneRow(rows, pgx.RowTo[int])
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to scan rows: %w", err)
}
if numDrafts < 2 {
return SyncGroup{}, fmt.Errorf("%w: insufficient listings: must be at least 2: %d", consts.ErrConflict, numDrafts)
}
// create the new sync group row
rows, err = tx.Query(
ctx,
`
INSERT INTO
mock.sync_groups (
account_id,
name
)
VALUES (
@account_id,
@name
)
RETURNING
sync_group_id
`,
pgx.NamedArgs{
"account_id": acctID,
"name": name,
},
)
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to perform query to insert new mock sync group: %w", err)
}
syncGroupID, err := pgx.CollectExactlyOneRow(rows, pgx.RowTo[int64])
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to scan row from inserting new mock sync group: %w", err)
}
// read and delete the draft listings
type DraftListingRow struct {
Order_index int
Shop_id pgtype.Text
Listing_id pgtype.Text
}
infos := getAllMockShopSchemaInfos()
newListingsPerPlatform := make(map[Platform][]SyncGroupListing, len(infos))
var numListings int
for _, info := range infos {
rows, err := tx.Query(
ctx,
fmt.Sprintf(
`
DELETE FROM
%s
WHERE
account_id = @account_id
RETURNING
order_index,
shop_id,
listing_id
`,
info.syncGroupListingDraftsTable,
),
pgx.NamedArgs{
"account_id": acctID,
},
)
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to perform query deleting mock sync group listing draft order indexes for platform %s: %w", info.platform, err)
}
vs, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[DraftListingRow])
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to scan rows: %w", err)
}
ls := make([]SyncGroupListing, len(vs))
for i, v := range vs {
if !v.Shop_id.Valid {
return SyncGroup{}, fmt.Errorf("%w: shop_id not specified on listing of order index %d", consts.ErrConflict, v.Order_index)
}
if !v.Listing_id.Valid {
return SyncGroup{}, fmt.Errorf("%w: listing_id not specified on listing of order index %d", consts.ErrConflict, v.Order_index)
}
ls[i] = SyncGroupListing{
SyncGroupIDs: SyncGroupIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
SyncGroupID: syncGroupID,
},
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
Platform: info.platform,
ShopID: v.Shop_id.String,
},
ListingID: v.Listing_id.String,
OrderIndex: v.Order_index,
}
}
newListingsPerPlatform[info.platform] = ls
numListings += len(ls)
}
_, err = tx.Exec(
ctx,
`
DELETE FROM
mock.sync_group_listing_draft_order_indexes
`,
)
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to perform query deleting mock sync group listing draft order indexes: %w", err)
}
// create new sync listing rows
listings := make([]SyncGroupListing, 0, numListings)
for _, info := range infos {
for _, l := range newListingsPerPlatform[info.platform] {
_, err := tx.Exec(
ctx,
`
INSERT INTO
mock.sync_group_listing_order_indexes (
sync_group_id,
order_index
)
VALUES (
@sync_group_id,
@order_index
)
`,
pgx.NamedArgs{
"sync_group_id": syncGroupID,
"order_index": l.OrderIndex,
},
)
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to perform query to insert row into mock.sync_group_listing_order_indexes: %w", err)
}
_, err = tx.Exec(
ctx,
fmt.Sprintf(
`
INSERT INTO
%s (
account_id,
sync_group_id,
shop_id,
listing_id,
order_index
)
VALUES (
@account_id,
@sync_group_id,
@shop_id,
@listing_id,
@order_index
)
`,
info.syncGroupListingsTable,
),
pgx.NamedArgs{
"account_id": acctID,
"sync_group_id": syncGroupID,
"order_index": l.OrderIndex,
"shop_id": l.ShopID,
"listing_id": l.ListingID,
},
)
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to perform query to insert row into %s: %w", info.syncGroupListingsTable, err)
}
listings = append(listings, l)
}
}
if err := tx.Commit(ctx); err != nil {
return SyncGroup{}, fmt.Errorf("failed to commit transaction: %w", err)
}
// prepare return value
slices.SortFunc(listings, func(a, b SyncGroupListing) int {
return a.OrderIndex - b.OrderIndex
})
return SyncGroup{
SyncGroupIDs: SyncGroupIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
SyncGroupID: syncGroupID,
},
Name: name,
Listings: listings,
}, nil
}
func (db *Store) ListMockSyncGroups(ctx context.Context, acctID int64) ([]SyncGroup, 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
sync_group_id,
name
FROM
mock.sync_groups
WHERE
account_id = @account_id
`,
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 {
Sync_group_id int64
Name string
}])
if err != nil {
return nil, fmt.Errorf("failed to scan rows: %w", err)
}
grps := make([]SyncGroup, len(vs))
for i, v := range vs {
listings, err := db.listMockSyncGroupListings(ctx, tx, acctID, v.Sync_group_id)
if err != nil {
return nil, fmt.Errorf("failed to load listings: %w", err)
}
grps[i] = SyncGroup{
SyncGroupIDs: SyncGroupIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
SyncGroupID: v.Sync_group_id,
},
Name: v.Name,
Listings: listings,
}
}
if err := tx.Commit(ctx); err != nil {
return nil, fmt.Errorf("failed to commit transaction: %w", err)
}
return grps, nil
}
// listMockSyncGroupListings will sort the returned listings by order_index.
func (db *Store) listMockSyncGroupListings(ctx context.Context, tx pgx.Tx, acctID, syncGroupID int64) ([]SyncGroupListing, error) {
var listings []SyncGroupListing
for _, info := range getAllMockShopSchemaInfos() {
rows, err := tx.Query(
ctx,
fmt.Sprintf(
`
SELECT
account_id,
shop_id,
listing_id,
order_index
FROM
%s
WHERE
account_id = @account_id
AND sync_group_id = @sync_group_id
`,
info.syncGroupListingsTable,
),
pgx.NamedArgs{
"account_id": acctID,
"sync_group_id": syncGroupID,
},
)
if err != nil {
return nil, fmt.Errorf("failed to perform query on %s: %w", info.syncGroupListingsTable, err)
}
vs, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[struct {
Account_id int64
Shop_id string
Listing_id string
Order_index int
}])
if err != nil {
return nil, fmt.Errorf("failed to scan rows from %s: %w", info.syncGroupListingsTable, err)
}
for _, v := range vs {
listings = append(listings, SyncGroupListing{
SyncGroupIDs: SyncGroupIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
SyncGroupID: syncGroupID,
},
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
Platform: info.platform,
ShopID: v.Shop_id,
},
ListingID: v.Listing_id,
OrderIndex: v.Order_index,
})
}
}
slices.SortFunc(listings, func(a, b SyncGroupListing) int {
return a.OrderIndex - b.OrderIndex
})
return listings, nil
}
func deref[T any](ptr *T) T {
if ptr == nil {
var zero T