Skip to content

Commit f3ea14c

Browse files
committed
working qty clamping
1 parent 8eeec9f commit f3ea14c

File tree

6 files changed

+53
-32
lines changed

6 files changed

+53
-32
lines changed

main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ func checkoutWebhookHandler(event stripe.Event) bool {
8989
}
9090
slog.Info("Link " + link.LinkID + " now used " + fmt.Sprintf("%d", link.Used) + " times")
9191
db_res = DB.Conn.Save(&link)
92+
if db_res.Error != nil {
93+
slog.Error("Error while saving link paid event to database: " + db_res.Error.Error())
94+
return false
95+
}
9296

9397
if len(link.TrackingInventoryIDs) > 0 {
9498
// Expand line_items field
@@ -106,11 +110,6 @@ func checkoutWebhookHandler(event stripe.Event) bool {
106110
slog.Info(fmt.Sprintf("Inventory item %s was bought %d times in link %s", trackingItem, boughtItems[trackingItem], link.LinkID))
107111
}
108112

109-
if db_res.Error != nil {
110-
slog.Error("Error while saving link paid event to database: " + db_res.Error.Error())
111-
return false
112-
}
113-
114113
}
115114

116115
}

menu/createNewLink.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ func CreateNewLink() {
6363
}
6464

6565
li, trackedItems := addItems(*settings)
66+
67+
for _, trackedItem := range trackedItems {
68+
tracking := &models.Inventory{}
69+
DB.Conn.Where(&models.Inventory{Product: trackedItem}).First(&tracking)
70+
tracking.SessionLinkIDs = append(tracking.SessionLinkIDs, linkID)
71+
DB.Conn.Save(&tracking)
72+
}
73+
6674
params.LineItems = li
6775
params.Mode = stripe.String("payment")
6876
params.SuccessURL = stripe.String("http://" + settings.Domain + ":4242/success")

menu/inventory.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ func UpdateInventory(product string, decrementQuantity int64) {
180180
slog.Error("Error while searching database for product " + product + err.Error())
181181
return
182182
}
183-
inventory.Quantity -= decrementQuantity
183+
inventory.Quantity = inventory.Quantity - decrementQuantity
184184
if inventory.Quantity > 0 {
185-
go ClampAdjustableQty(inventory.SessionLinkIDs, product, inventory.Quantity)
185+
ClampAdjustableQty(inventory.SessionLinkIDs, product, inventory.Quantity)
186186
} else {
187-
go RemoveItemFromLink(inventory.SessionLinkIDs, product)
187+
RemoveItemFromLink(inventory.SessionLinkIDs, product)
188188
}
189189

190190
DB.Conn.Save(&inventory)
@@ -199,12 +199,30 @@ func ClampAdjustableQty(links []string, product string, qty int64) {
199199
return
200200
}
201201
for _, li := range seslink.Params.LineItems {
202-
if *li.PriceData.Product == product && *li.AdjustableQuantity.Maximum > qty {
203-
li.AdjustableQuantity.Maximum = stripe.Int64(qty)
202+
price := &models.Price{}
203+
err := DB.Conn.Where(&models.Price{PriceID: *li.Price}).First(price).Error
204+
if err != nil {
205+
slog.Error("Error while getting price from DB: " + err.Error())
206+
return
207+
}
208+
if price.Product == product {
209+
if li.AdjustableQuantity != nil {
210+
if *li.AdjustableQuantity.Maximum > qty {
211+
li.AdjustableQuantity.Maximum = stripe.Int64(qty)
212+
}
213+
if *li.AdjustableQuantity.Minimum == *li.AdjustableQuantity.Maximum {
214+
li.AdjustableQuantity = nil
215+
}
216+
}
217+
218+
if *li.Quantity > qty {
219+
li.Quantity = stripe.Int64(qty)
220+
}
204221
break
205222
}
206223

207224
}
225+
208226
DB.Conn.Save(&seslink)
209227
}
210228
}
@@ -219,13 +237,19 @@ func RemoveItemFromLink(links []string, product string) {
219237
}
220238
if len(seslink.Params.LineItems) == 1 {
221239
seslink.Active = false
240+
DB.Conn.Save(&seslink)
222241
continue
223242
}
224243
newLineItems := []*stripe.CheckoutSessionLineItemParams{}
225244
for _, li := range seslink.Params.LineItems {
226-
if *li.PriceData.Product != product {
245+
price := &models.Price{}
246+
err := DB.Conn.Where(&models.Price{PriceID: *li.Price}).First(price).Error
247+
if err != nil {
248+
slog.Error("Error while getting price from DB: " + err.Error())
249+
return
250+
}
251+
if price.Product != product {
227252
newLineItems = append(newLineItems, li)
228-
break
229253
}
230254

231255
}

menu/modifyExistingLink.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ func ModifyExistingLink() {
7474
case "Change List Item Price":
7575
prompt := promptui.Select{
7676
Label: "Change Item Price",
77-
Items: stripeapi.GetProductsInLinkReadable(allLinks[selection].LinkID),
77+
Items: stripeapi.GetProductsInLink(allLinks[selection].LinkID),
7878
}
7979
itemSelection, _, _ := prompt.Run()
80-
println("Selected: " + *allLinks[selection].Params.LineItems[itemSelection].PriceData.ProductData.Name)
81-
println("Current Price: " + strconv.Itoa(int(*allLinks[selection].Params.LineItems[itemSelection].PriceData.UnitAmount)))
8280
pricePrompt := promptui.Prompt{
8381
Label: "Price in cents. (e.g. $1.99 = 199)",
8482
}
@@ -104,12 +102,15 @@ func ModifyExistingLink() {
104102
println("Error: %v\n", err)
105103
return
106104
}
107-
newPrice, err := stripeapi.NewPriceIfNotExist(settings.DefaultCurrency, priceInt, *allLinks[selection].Params.LineItems[itemSelection].PriceData.Product)
105+
price := &models.Price{}
106+
DB.Conn.Where(&models.Price{PriceID: *allLinks[selection].Params.LineItems[itemSelection].Price}).First(price)
107+
newPrice, err := stripeapi.NewPriceIfNotExist(settings.DefaultCurrency, priceInt, price.Product)
108108
if err != nil {
109109
println("Error: %v\n", err)
110110
return
111111
}
112112
*allLinks[selection].Params.LineItems[itemSelection].Price = newPrice
113+
DB.Conn.Save(&allLinks[selection])
113114
case "Change Max Uses":
114115
prompt := promptui.Prompt{
115116
Label: "Change Max Uses",

models/models.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type DropdownOption struct {
9393

9494
type InvoicePDF struct {
9595
gorm.Model `json:"-"`
96-
Nickname string
96+
Nickname string `gorm:"unique"`
9797
TaxID string
9898
CustomFieldName string
9999
CustomFieldValue string
@@ -104,7 +104,7 @@ type InvoicePDF struct {
104104
type Inventory struct {
105105
gorm.Model
106106
DisplayName string
107-
Product string
107+
Product string `gorm:"unique"`
108108
Quantity int64
109109
SessionLinkIDs IDs `gorm:"embedded"`
110110
}

stripe-api/stripe-api.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,6 @@ func GetAllProduct(active bool) ([]*stripe.Product, error) {
5050
return products, nil
5151
}
5252

53-
func GetProductsInLinkReadable(LinkID string) []string {
54-
link := models.SessionLink{}
55-
err := DB.Conn.Where(&models.SessionLink{LinkID: LinkID}).First(&link)
56-
if err.Error != nil {
57-
return nil
58-
}
59-
var products []string
60-
for _, li := range link.Params.LineItems {
61-
products = append(products, *li.PriceData.ProductData.Name)
62-
}
63-
return products
64-
}
65-
6653
func GetProductsInLink(LinkID string) []string {
6754
link := models.SessionLink{}
6855
err := DB.Conn.Where(&models.SessionLink{LinkID: LinkID}).First(&link)
@@ -71,7 +58,9 @@ func GetProductsInLink(LinkID string) []string {
7158
}
7259
var products []string
7360
for _, li := range link.Params.LineItems {
74-
products = append(products, *li.PriceData.Product)
61+
price := &models.Price{}
62+
DB.Conn.Where(&models.Price{PriceID: *li.Price}).First(price)
63+
products = append(products, price.Product)
7564
}
7665
return products
7766
}

0 commit comments

Comments
 (0)