save "create sync group" table in database

This commit is contained in:
2026-01-11 05:19:20 -07:00
parent a7c8fe4d64
commit 889aead6b6
28 changed files with 1396 additions and 408 deletions
+12 -15
View File
@@ -30,15 +30,11 @@ type (
AccountShop struct {
AccountShopIDs
Platform Platform
Name string
Name string
}
Listing struct {
AccountIDs
Platform Platform
ShopID string
ListingID int64
AccountShopListingIDs
SKU string
Name string
Description string
@@ -53,16 +49,14 @@ type (
AccountShopIDs struct {
AccountIDs
ShopID string
Platform Platform
ShopID string
}
Platform string
)
const (
Etsy Platform = "Etsy"
Tiktok Platform = "Tiktok"
Wix Platform = "Wix"
AccountShopListingIDs struct {
AccountShopIDs
ListingID string
}
)
func NewStore(db *pgxpool.Pool) *Store {
@@ -300,12 +294,15 @@ func (db *Store) GetShops(ctx context.Context, acctID int64) ([]AccountShop, err
return shops, nil
}
func (db *Store) GetListingsForShop(ctx context.Context, acctID int64, shopID string) ([]Listing, error) {
func (db *Store) GetListingsForShop(ctx context.Context, acctID int64, platform Platform, shopID string) ([]Listing, error) {
var vs []Listing
for _, v := range devListings {
if v.AccountID != acctID {
continue
}
if v.Platform != platform {
continue
}
if v.ShopID != shopID {
continue
}
+135 -87
View File
@@ -7,233 +7,281 @@ var (
AccountIDs: AccountIDs{
AccountID: 6,
},
ShopID: "2",
Platform: Etsy,
ShopID: "2",
},
Platform: Etsy,
Name: "Etsy 1",
Name: "Etsy 1",
},
AccountShop{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
ShopID: "5",
Platform: Etsy,
ShopID: "5",
},
Platform: Etsy,
Name: "Etsy 2",
Name: "Etsy 2",
},
AccountShop{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
ShopID: "8",
Platform: Etsy,
ShopID: "8",
},
Platform: Etsy,
Name: "Etsy 3",
Name: "Etsy 3",
},
AccountShop{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
ShopID: "11",
Platform: Tiktok,
ShopID: "11",
},
Platform: Tiktok,
Name: "Tiktok 1",
Name: "Tiktok 1",
},
AccountShop{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
ShopID: "14",
Platform: Tiktok,
ShopID: "14",
},
Platform: Tiktok,
Name: "Tiktok 2",
Name: "Tiktok 2",
},
AccountShop{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
ShopID: "17",
Platform: Tiktok,
ShopID: "17",
},
Platform: Tiktok,
Name: "Tiktok 3",
Name: "Tiktok 3",
},
AccountShop{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
ShopID: "20",
Platform: Wix,
ShopID: "20",
},
Platform: Wix,
Name: "Wix 1",
Name: "Wix 1",
},
AccountShop{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
ShopID: "23",
Platform: Wix,
ShopID: "23",
},
Platform: Wix,
Name: "Wix 2",
Name: "Wix 2",
},
AccountShop{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
ShopID: "26",
Platform: Wix,
ShopID: "26",
},
Platform: Wix,
Name: "Wix 3",
Name: "Wix 3",
},
}
devListings = []Listing{
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Etsy,
ShopID: "1",
},
ListingID: "1",
},
Platform: Etsy,
ShopID: "1",
ListingID: 1,
SKU: "sku 1",
Name: "name 1",
Description: "description 1",
Count: 51,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Etsy,
ShopID: "1",
},
ListingID: "2",
},
Platform: Etsy,
ShopID: "1",
ListingID: 2,
SKU: "sku 2",
Name: "name 2",
Description: "description 2",
Count: 52,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Etsy,
ShopID: "1",
},
ListingID: "3",
},
Platform: Etsy,
ShopID: "1",
ListingID: 3,
SKU: "sku 3",
Name: "name 3",
Description: "description 3",
Count: 53,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Tiktok,
ShopID: "4",
},
ListingID: "4",
},
Platform: Tiktok,
ShopID: "4",
ListingID: 4,
SKU: "sku 4",
Name: "name 4",
Description: "description 4",
Count: 54,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Tiktok,
ShopID: "4",
},
ListingID: "5",
},
Platform: Tiktok,
ShopID: "4",
ListingID: 5,
SKU: "sku 5",
Name: "name 5",
Description: "description 5",
Count: 55,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Tiktok,
ShopID: "4",
},
ListingID: "6",
},
Platform: Tiktok,
ShopID: "4",
ListingID: 6,
SKU: "sku 6",
Name: "name 6",
Description: "description 6",
Count: 56,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Wix,
ShopID: "7",
},
ListingID: "7",
},
Platform: Wix,
ShopID: "7",
ListingID: 7,
SKU: "sku 7",
Name: "name 7",
Description: "description 7",
Count: 57,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Wix,
ShopID: "7",
},
ListingID: "8",
},
Platform: Wix,
ShopID: "7",
ListingID: 8,
SKU: "sku 8",
Name: "name 8",
Description: "description 8",
Count: 58,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Wix,
ShopID: "7",
},
ListingID: "9",
},
Platform: Wix,
ShopID: "7",
ListingID: 9,
SKU: "sku 9",
Name: "name 9",
Description: "description 9",
Count: 59,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Etsy,
ShopID: "2",
},
ListingID: "10",
},
Platform: Etsy,
ShopID: "2",
ListingID: 10,
SKU: "sku 10",
Name: "name 10",
Description: "description 10",
Count: 60,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Etsy,
ShopID: "2",
},
ListingID: "11",
},
Platform: Etsy,
ShopID: "2",
ListingID: 11,
SKU: "sku 11",
Name: "name 11",
Description: "description 11",
Count: 61,
},
{
AccountIDs: AccountIDs{
AccountID: 6,
AccountShopListingIDs: AccountShopListingIDs{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: 6,
},
Platform: Etsy,
ShopID: "2",
},
ListingID: "12",
},
Platform: Etsy,
ShopID: "2",
ListingID: 12,
SKU: "sku 12",
Name: "name 12",
Description: "description 12",
+54
View File
@@ -0,0 +1,54 @@
package accounts
import (
"fmt"
"strings"
)
type (
Platform string
)
const (
Etsy Platform = "Etsy"
Tiktok Platform = "Tiktok"
Wix Platform = "Wix"
)
func NewPlatform(s string) (Platform, error) {
switch strings.ToLower(s) {
case strings.ToLower(string(Etsy)):
return Etsy, nil
case strings.ToLower(string(Tiktok)):
return Tiktok, nil
case strings.ToLower(string(Wix)):
return Wix, nil
default:
return "", fmt.Errorf("unrecognized constant: %q", s)
}
}
// sql.Scanner implementation
func (p *Platform) Scan(src any) error {
var s string
switch v := src.(type) {
case string:
s = v
case byte:
s = string(v)
default:
return fmt.Errorf("unsupported type: %T: %v", src, src)
}
dst, err := NewPlatform(s)
if err == nil {
*p = dst
}
return err
}
// sql/driver.Valuer implementation
func (p Platform) Value() (any, error) {
return string(p), nil
}
@@ -43,10 +43,38 @@ func (v_ctx *StoreWithContext) GetShops(acctID int64) ([]AccountShop, error) {
return v_ctx.Store.GetShops(v_ctx.ctx, acctID)
}
func (v_ctx *StoreWithContext) GetListingsForShop(acctID int64, shopID string) ([]Listing, error) {
return v_ctx.Store.GetListingsForShop(v_ctx.ctx, acctID, shopID)
func (v_ctx *StoreWithContext) GetListingsForShop(acctID int64, platform Platform, shopID string) ([]Listing, error) {
return v_ctx.Store.GetListingsForShop(v_ctx.ctx, acctID, platform, shopID)
}
func (v_ctx *StoreWithContext) GetAccountPointerByUserID(userID string) (*Account, error) {
return v_ctx.Store.GetAccountPointerByUserID(v_ctx.ctx, userID)
}
func (v_ctx *StoreWithContext) CreateSyncGroupListingDraft(acctID int64) (int, error) {
return v_ctx.Store.CreateSyncGroupListingDraft(v_ctx.ctx, acctID)
}
func (v_ctx *StoreWithContext) GetSyncGroupListingDraft(acctID int64, orderIndex int) (SyncGroupListingDraft, error) {
return v_ctx.Store.GetSyncGroupListingDraft(v_ctx.ctx, acctID, orderIndex)
}
func (v_ctx *StoreWithContext) SetShopInSyncGroupListingDraft(acctID int64, orderIndex int, platform Platform, shopID string) error {
return v_ctx.Store.SetShopInSyncGroupListingDraft(v_ctx.ctx, acctID, orderIndex, platform, shopID)
}
func (v_ctx *StoreWithContext) SetListingInSyncGroupListingDraft(acctID int64, orderIndex int, listingID string) error {
return v_ctx.Store.SetListingInSyncGroupListingDraft(v_ctx.ctx, acctID, orderIndex, listingID)
}
func (v_ctx *StoreWithContext) DeleteSyncGroupListingDraft(acctID int64, orderIndex int) (int, error) {
return v_ctx.Store.DeleteSyncGroupListingDraft(v_ctx.ctx, acctID, orderIndex)
}
func (v_ctx *StoreWithContext) GetSyncGroupListingDrafts(acctID int64) ([]SyncGroupListingDraft, error) {
return v_ctx.Store.GetSyncGroupListingDrafts(v_ctx.ctx, acctID)
}
func (v_ctx *StoreWithContext) SaveNewSyncGroup(acctID int64) (SyncGroup, error) {
return v_ctx.Store.SaveNewSyncGroup(v_ctx.ctx, acctID)
}
+436
View File
@@ -0,0 +1,436 @@
package accounts
import (
"context"
"errors"
"fmt"
"ruben/inventory2/internal/consts"
"github.com/jackc/pgx/v5"
)
type (
SyncGroup struct {
SyncGroupIDs
Listings []SyncGroupListing
}
SyncGroupListing struct {
SyncGroupIDs
AccountShopIDs
ListingID string
}
SyncGroupIDs struct {
AccountIDs
SyncGroupID int64
}
SyncGroupListingDraft struct {
AccountShopIDs
ListingID string
}
)
func (db *Store) CreateSyncGroupListingDraft(ctx context.Context, acctID int64) (orderIndex int, err error) {
rows, err := db.db.Query(
ctx,
`
WITH new_order_index AS (
SELECT
COALESCE(MAX(order_index), -1) + 1 AS order_index
FROM
sync_group_listing_drafts
WHERE
account_id = @account_id
)
INSERT INTO
sync_group_listing_drafts (
account_id,
order_index
)
SELECT
@account_id,
order_index
FROM
new_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,
`
SELECT
platform,
shop_id,
listing_id
FROM
sync_group_listing_drafts
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 perform query: %w", err)
}
r, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByNameLax[struct {
Platform *Platform
Shop_id *string
Listing_id *string
}])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return SyncGroupListingDraft{}, consts.ErrNotFound
}
return SyncGroupListingDraft{}, fmt.Errorf("failed to scan rows: %w", err)
}
return SyncGroupListingDraft{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
Platform: deref(r.Platform),
ShopID: deref(r.Shop_id),
},
ListingID: deref(r.Listing_id),
}, nil
}
func (db *Store) SetShopInSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int, platform Platform, shopID string) error {
tags, err := db.db.Exec(
ctx,
`
UPDATE
sync_group_listing_drafts
SET
platform = @platform,
shop_id = @shop_id,
listing_id = NULL
WHERE
account_id = @account_id
AND order_index = @order_index
`,
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
"platform": platform,
"shop_id": shopID,
},
)
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
if tags.RowsAffected() != 1 {
return consts.ErrNotFound
}
return nil
}
func (db *Store) SetListingInSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int, listingID string) error {
tags, err := db.db.Exec(
ctx,
`
UPDATE
sync_group_listing_drafts
SET
listing_id = @listing_id
WHERE
account_id = @account_id
AND order_index = @order_index
`,
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
"listing_id": listingID,
},
)
if err != nil {
return fmt.Errorf("failed to perform query: %w", err)
}
if tags.RowsAffected() != 1 {
return consts.ErrNotFound
}
return nil
}
func (db *Store) DeleteSyncGroupListingDraft(ctx context.Context, acctID int64, orderIndex int) (numOfRows int, err error) {
rows, err := db.db.Query(
ctx,
`
WITH updated_drafts AS (
UPDATE
sync_group_listing_drafts
SET
order_index = (order_index - 1)
WHERE
account_id = @account_id
AND order_index > @order_index
RETURNING
order_index
), deleted_draft AS (
DELETE FROM
sync_group_listing_drafts
WHERE
account_id = @account_id
AND order_index = @order_index
RETURNING
true AS found
)
SELECT
COALESCE(dd.found, false) AS found,
(COALESCE(MAX(ud.order_index), -1) + 1) AS num_rows
FROM
deleted_draft dd
LEFT JOIN
updated_drafts ud
ON true
GROUP BY
ud.order_index, dd.found
LIMIT
1
`,
pgx.NamedArgs{
"account_id": acctID,
"order_index": orderIndex,
},
)
if err != nil {
return 0, fmt.Errorf("failed to perform query: %w", err)
}
type Row struct {
Found bool
Num_rows int
}
r, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByNameLax[Row])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return 0, consts.ErrNotFound
}
return 0, fmt.Errorf("failed to scan rows: %w", err)
}
if !r.Found {
return 0, consts.ErrNotFound
}
return r.Num_rows, nil
}
func (db *Store) GetSyncGroupListingDrafts(ctx context.Context, acctID int64) ([]SyncGroupListingDraft, error) {
rows, err := db.db.Query(
ctx,
`
SELECT
order_index,
platform,
shop_id,
listing_id
FROM
sync_group_listing_drafts
WHERE
account_id = @account_id
`,
pgx.NamedArgs{
"account_id": acctID,
},
)
if err != nil {
return nil, fmt.Errorf("failed to perform query: %w", err)
}
type Row struct {
Order_index int
Platform *Platform
Shop_id *string
Listing_id *string
}
rs, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[Row])
if err != nil {
return nil, fmt.Errorf("failed to scan rows: %w", err)
}
listings := make([]SyncGroupListingDraft, len(rs))
for _, r := range rs {
listings[r.Order_index] = SyncGroupListingDraft{
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
Platform: deref(r.Platform),
ShopID: deref(r.Shop_id),
},
ListingID: deref(r.Listing_id),
}
}
return listings, nil
}
// TODO: test
func (db *Store) SaveNewSyncGroup(ctx context.Context, acctID int64) (SyncGroup, error) {
txn, err := db.db.Begin(ctx)
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to start transaction: %w", err)
}
defer txn.Rollback(ctx)
rows, err := txn.Query(
ctx,
"SELECT COUNT(*) FROM sync_group_listing_drafts 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)
}
rows, err = txn.Query(
ctx,
`
WITH deleted_sync_group_listing_drafts AS (
DELETE FROM
sync_group_listing_drafts
WHERE
account_id = @account_id
RETURNING
order_index,
platform,
shop_id,
listing_id
), new_sync_group AS (
INSERT INTO
sync_groups (
account_id
)
VALUES (
@account_id
)
RETURNING
sync_group_id
)
INSERT INTO
sync_groups_listings (
sync_group_id,
order_index,
platform,
shop_id,
listing_id
)
SELECT
sync_group_id,
order_index,
platform,
shop_id,
listing_id
FROM
new_sync_group
JOIN
deleted_sync_group_listing_drafts
ON
TRUE
RETURNING
sync_group_id,
order_index,
platform,
shop_id,
listing_id
`,
pgx.NamedArgs{
"account_id": acctID,
},
)
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to perform query: %w", err)
}
rs, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[struct {
Sync_group_id int64
Order_index int
Platform Platform
Shop_id string
Listing_id string
}])
if err != nil {
return SyncGroup{}, fmt.Errorf("failed to scan rows: %w", err)
}
if err := txn.Commit(ctx); err != nil {
return SyncGroup{}, fmt.Errorf("failed to commit transaction: %w", err)
}
listings := make([]SyncGroupListing, len(rs))
for _, r := range rs {
listings[r.Order_index] = SyncGroupListing{
SyncGroupIDs: SyncGroupIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
SyncGroupID: r.Sync_group_id,
},
AccountShopIDs: AccountShopIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
Platform: r.Platform,
ShopID: r.Shop_id,
},
ListingID: r.Listing_id,
}
}
return SyncGroup{
SyncGroupIDs: SyncGroupIDs{
AccountIDs: AccountIDs{
AccountID: acctID,
},
SyncGroupID: listings[0].SyncGroupID,
},
Listings: listings,
}, nil
}
func deref[T any](ptr *T) T {
if ptr == nil {
var zero T
return zero
}
return *ptr
}