mock sync group editing: made tables columns more consistent; initialize database rows upon editing
This commit is contained in:
+103
-16
@@ -39,6 +39,7 @@ type (
|
||||
|
||||
Listing struct {
|
||||
AccountShopListingIDs
|
||||
ShopName string
|
||||
SKU string
|
||||
Name string
|
||||
Description string
|
||||
@@ -362,17 +363,23 @@ func (db *Store) GetMockListingsForShop(ctx context.Context, acctID int64, platf
|
||||
fmt.Sprintf(
|
||||
`
|
||||
SELECT
|
||||
listing_id,
|
||||
sku,
|
||||
name,
|
||||
description,
|
||||
"count"
|
||||
sh.name as shop_name,
|
||||
li.listing_id,
|
||||
li.sku,
|
||||
li.name,
|
||||
li.description,
|
||||
li."count"
|
||||
FROM
|
||||
%s
|
||||
%s as sh
|
||||
JOIN
|
||||
%s as li
|
||||
USING
|
||||
(account_id, shop_id)
|
||||
WHERE
|
||||
account_id = @account_id
|
||||
AND shop_id = @shop_id
|
||||
`,
|
||||
in.shopTable,
|
||||
in.listingsTable,
|
||||
),
|
||||
pgx.NamedArgs{
|
||||
@@ -385,6 +392,7 @@ func (db *Store) GetMockListingsForShop(ctx context.Context, acctID int64, platf
|
||||
}
|
||||
|
||||
vs, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[struct {
|
||||
Shop_name string
|
||||
Listing_id string
|
||||
SKU string
|
||||
Name string
|
||||
@@ -401,6 +409,7 @@ func (db *Store) GetMockListingsForShop(ctx context.Context, acctID int64, platf
|
||||
AccountShopListingIDs: NewAccountIDs(acctID).
|
||||
ShopID(in.platform, shopID).
|
||||
ListingID(v.Listing_id),
|
||||
ShopName: v.Shop_name,
|
||||
SKU: v.SKU,
|
||||
Name: v.Name,
|
||||
Description: v.Description,
|
||||
@@ -422,17 +431,23 @@ func (db *Store) GetMockListing(ctx context.Context, acctID int64, platform Plat
|
||||
fmt.Sprintf(
|
||||
`
|
||||
SELECT
|
||||
name,
|
||||
count,
|
||||
sku,
|
||||
description
|
||||
sh.name as shop_name,
|
||||
li.name,
|
||||
li.count,
|
||||
li.sku,
|
||||
li.description
|
||||
FROM
|
||||
%s
|
||||
%s AS sh
|
||||
JOIN
|
||||
%s AS li
|
||||
USING
|
||||
(account_id, shop_id)
|
||||
WHERE
|
||||
account_id = @account_id
|
||||
AND shop_id = @shop_id
|
||||
AND listing_id = @listing_id
|
||||
`,
|
||||
in.shopTable,
|
||||
in.listingsTable,
|
||||
),
|
||||
pgx.NamedArgs{
|
||||
@@ -446,6 +461,7 @@ func (db *Store) GetMockListing(ctx context.Context, acctID int64, platform Plat
|
||||
}
|
||||
|
||||
v, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByNameLax[struct {
|
||||
Shop_name string
|
||||
Name string
|
||||
Count int64
|
||||
Sku string
|
||||
@@ -469,6 +485,7 @@ func (db *Store) GetMockListing(ctx context.Context, acctID int64, platform Plat
|
||||
},
|
||||
ListingID: listingID,
|
||||
},
|
||||
ShopName: v.Shop_name,
|
||||
SKU: v.Sku,
|
||||
Name: v.Name,
|
||||
Description: v.Name,
|
||||
@@ -502,7 +519,13 @@ func (db *Store) DeleteMockSyncGroup(ctx context.Context, acctID, syncGroupID in
|
||||
}
|
||||
|
||||
func (db *Store) StartEditingMockSyncGroup(ctx context.Context, acctID, syncGroupID int64) (prevSyncGroupID int64, prevSyncGroupExists bool, err error) {
|
||||
rows, err := db.db.Query(
|
||||
tx, err := db.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return 0, false, fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
rows, err := tx.Query(
|
||||
ctx,
|
||||
`
|
||||
WITH deleted_row AS (
|
||||
@@ -551,13 +574,77 @@ func (db *Store) StartEditingMockSyncGroup(ctx context.Context, acctID, syncGrou
|
||||
}
|
||||
|
||||
v, err := pgx.CollectExactlyOneRow(rows, pgx.RowTo[pgtype.Int8])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return 0, false, nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return 0, false, fmt.Errorf("failed to scan rows: %w", err)
|
||||
}
|
||||
|
||||
// insert the mock sync group listings into the draft table
|
||||
|
||||
for _, in := range getAllMockShopSchemaInfos() {
|
||||
_, err := tx.Exec(
|
||||
ctx,
|
||||
fmt.Sprintf(
|
||||
`
|
||||
WITH existing_listings AS (
|
||||
SELECT
|
||||
shop_id,
|
||||
listing_id,
|
||||
order_index
|
||||
FROM
|
||||
%s
|
||||
WHERE
|
||||
account_id = @account_id
|
||||
AND sync_group_id = @sync_group_id
|
||||
), inserted_order_indexes AS (
|
||||
INSERT INTO
|
||||
mock.sync_group_editing_listing_order_indexes (
|
||||
account_id,
|
||||
order_index
|
||||
)
|
||||
SELECT
|
||||
@account_id,
|
||||
order_index
|
||||
FROM
|
||||
existing_listings
|
||||
RETURNING
|
||||
order_index
|
||||
)
|
||||
INSERT INTO
|
||||
%s (
|
||||
account_id,
|
||||
shop_id,
|
||||
listing_id,
|
||||
order_index
|
||||
)
|
||||
SELECT
|
||||
@account_id,
|
||||
shop_id,
|
||||
listing_id,
|
||||
order_index
|
||||
FROM
|
||||
existing_listings
|
||||
JOIN
|
||||
inserted_order_indexes
|
||||
USING
|
||||
(order_index)
|
||||
`,
|
||||
in.syncGroupListingsTable,
|
||||
in.syncGroupEditingListingTable,
|
||||
),
|
||||
pgx.NamedArgs{
|
||||
"account_id": acctID,
|
||||
"sync_group_id": syncGroupID,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return 0, false, fmt.Errorf("failed to insert into editing listings table: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return 0, false, fmt.Errorf("failed to commit transaction: %w", err)
|
||||
}
|
||||
|
||||
return v.Int64, v.Valid, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -108,6 +108,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "1",
|
||||
},
|
||||
ShopName: "Etsy 1",
|
||||
SKU: "sku 1",
|
||||
Name: "name 1",
|
||||
Description: "description 1",
|
||||
@@ -124,6 +125,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "2",
|
||||
},
|
||||
ShopName: "Etsy 1",
|
||||
SKU: "sku 2",
|
||||
Name: "name 2",
|
||||
Description: "description 2",
|
||||
@@ -140,6 +142,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "3",
|
||||
},
|
||||
ShopName: "Etsy 1",
|
||||
SKU: "sku 3",
|
||||
Name: "name 3",
|
||||
Description: "description 3",
|
||||
@@ -156,6 +159,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "4",
|
||||
},
|
||||
ShopName: "Etsy 1",
|
||||
SKU: "sku 4",
|
||||
Name: "name 4",
|
||||
Description: "description 4",
|
||||
@@ -172,6 +176,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "5",
|
||||
},
|
||||
ShopName: "Etsy 1",
|
||||
SKU: "sku 5",
|
||||
Name: "name 5",
|
||||
Description: "description 5",
|
||||
@@ -188,6 +193,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "6",
|
||||
},
|
||||
ShopName: "Etsy 1",
|
||||
SKU: "sku 6",
|
||||
Name: "name 6",
|
||||
Description: "description 6",
|
||||
@@ -204,6 +210,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "7",
|
||||
},
|
||||
ShopName: "Etsy 2",
|
||||
SKU: "sku 7",
|
||||
Name: "name 7",
|
||||
Description: "description 7",
|
||||
@@ -220,6 +227,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "8",
|
||||
},
|
||||
ShopName: "Etsy 2",
|
||||
SKU: "sku 8",
|
||||
Name: "name 8",
|
||||
Description: "description 8",
|
||||
@@ -236,6 +244,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "9",
|
||||
},
|
||||
ShopName: "Etsy 2",
|
||||
SKU: "sku 9",
|
||||
Name: "name 9",
|
||||
Description: "description 9",
|
||||
@@ -252,6 +261,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "10",
|
||||
},
|
||||
ShopName: "Etsy 2",
|
||||
SKU: "sku 10",
|
||||
Name: "name 10",
|
||||
Description: "description 10",
|
||||
@@ -268,6 +278,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "11",
|
||||
},
|
||||
ShopName: "Etsy 2",
|
||||
SKU: "sku 11",
|
||||
Name: "name 11",
|
||||
Description: "description 11",
|
||||
@@ -284,6 +295,7 @@ func getDevListings(acctID int64) []Listing {
|
||||
},
|
||||
ListingID: "12",
|
||||
},
|
||||
ShopName: "Etsy 2",
|
||||
SKU: "sku 12",
|
||||
Name: "name 12",
|
||||
Description: "description 12",
|
||||
|
||||
+84
-70
@@ -523,11 +523,12 @@ func getMockShopTableName(platform Platform) (string, error) {
|
||||
}
|
||||
|
||||
type mockShopSchemaInfo struct {
|
||||
platform Platform
|
||||
shopTable string
|
||||
listingsTable string
|
||||
syncGroupListingsTable string
|
||||
syncGroupListingDraftsTable string
|
||||
platform Platform
|
||||
shopTable string
|
||||
listingsTable string
|
||||
syncGroupListingsTable string
|
||||
syncGroupListingDraftsTable string
|
||||
syncGroupEditingListingTable string
|
||||
}
|
||||
|
||||
func getMockShopSchemaInfo(platform Platform) (mockShopSchemaInfo, bool) {
|
||||
@@ -543,95 +544,108 @@ func getMockShopSchemaInfo(platform Platform) (mockShopSchemaInfo, bool) {
|
||||
func getAllMockShopSchemaInfos() []mockShopSchemaInfo {
|
||||
return []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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_amazon_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_big_cartel_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_ebay_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_ecwid_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_etsy_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_shopify_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_square_online_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_squarespace_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_tiktok_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_walmart_marketplace_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_wix_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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: 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",
|
||||
syncGroupEditingListingTable: "mock.shop_woo_commerce_sync_group_editing_listings",
|
||||
},
|
||||
{
|
||||
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",
|
||||
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",
|
||||
syncGroupEditingListingTable: "mock.shop_zoho_sync_group_editing_listings",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user