aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2025-10-10 19:27:42 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2025-10-10 19:27:42 +0530
commitd55d43bb4ac0b3114c54a4dd5a1e53c76a3df3a2 (patch)
treef80cb7ab32f46df4101833daec66381937b64b1b
parenta3884ebb05564951164f4c880d573950299ba85e (diff)
properly editing customer detailsv0.13.0
-rw-r--r--customer/controller.go12
-rw-r--r--customer/validators.go44
-rw-r--r--main.go2
3 files changed, 53 insertions, 5 deletions
diff --git a/customer/controller.go b/customer/controller.go
index 2bacd02..f2704bd 100644
--- a/customer/controller.go
+++ b/customer/controller.go
@@ -98,7 +98,17 @@ func handleSaveCustomer (ctx *gin.Context) {
}
userId := uId.(uint)
- customer.UserID = userId
+ customer.UserID = userId // necessary even when editing, just in case if UserID was ommitted in the request
+
+ if customer.ID != 0 {
+ // if customer is being edited, check ownership
+ err := checkCustomerOwnership(customer.ID, userId)
+ if err != nil {
+ ctx.Error(err)
+ ctx.Abort()
+ return
+ }
+ }
err := customer.upsert()
if err != nil {
diff --git a/customer/validators.go b/customer/validators.go
index 17ca2ae..ea2a9d5 100644
--- a/customer/validators.go
+++ b/customer/validators.go
@@ -57,6 +57,37 @@ func checkDuplicate(field, value string, userId uint) error {
return nil
}
+func checkDuplicateExisting(field, value string, userId, customerId uint) error {
+ if value != "" {
+ var count int64
+ err := db.Model(&Customer{}).
+ Where("user_id = ? and id != ? and" + field + " = ?", userId, customerId, value).
+ Count(&count).
+ Error
+
+ if err != nil {
+ return err
+ }
+
+ if count > 0 {
+ switch(field) {
+ case "phone":
+ return errors.ErrNonUniquePhone
+ case "email":
+ return errors.ErrNonUniqueEmail
+ case "website":
+ return errors.ErrNonUniqueWebsite
+ case "gstin":
+ return errors.ErrNonUniqueGSTIN
+ default:
+ return e.New(field + " is not unique")
+ }
+ }
+ }
+
+ return nil
+}
+
func (c *Customer) validate() error {
// trim whitespaces
c.FirmName = strings.TrimSpace(c.FirmName)
@@ -96,9 +127,16 @@ func (c *Customer) validate() error {
var err error
for _, i := range [][]string{{"phone", c.Phone}, {"email", c.Email}, {"website", c.Website}, {"gstin", c.Gstin}} {
- err = checkDuplicate(i[0], i[1], c.UserID)
- if err != nil {
- return err
+ if c.ID != 0 {
+ err = checkDuplicateExisting(i[0], i[1], c.UserID, c.ID)
+ if err != nil {
+ return err
+ }
+ } else {
+ err = checkDuplicate(i[0], i[1], c.UserID)
+ if err != nil {
+ return err
+ }
}
}
diff --git a/main.go b/main.go
index 90d5d3a..24e3ab9 100644
--- a/main.go
+++ b/main.go
@@ -38,7 +38,7 @@ import (
"log"
)
-const OPENBILLS_VERSION = "v0.12.0"
+const OPENBILLS_VERSION = "v0.13.0"
func init() {
if !viper.GetBool("debug_mode") {