aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2025-10-11 19:37:40 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2025-10-11 19:37:40 +0530
commit8a47978ca17d2f251d67d12b0e34fa26bb1e4ace (patch)
tree230669efabbbeb405ce316b0d70b80c1fe55a1ed
parenta735e0fbe865984c8e76777e34731610ed5e4509 (diff)
allowing duplicate fields in customers, doesn't matter that muchv0.16.0
-rw-r--r--customer/hooks.go2
-rw-r--r--customer/validators.go80
-rw-r--r--main.go2
3 files changed, 2 insertions, 82 deletions
diff --git a/customer/hooks.go b/customer/hooks.go
index bef3308..148004f 100644
--- a/customer/hooks.go
+++ b/customer/hooks.go
@@ -47,7 +47,7 @@ func (c *Customer) BeforeDelete(tx *gorm.DB) error {
return err
}
- // delete shipping addresses
+ // delete shipping address
err = db.Where("customer_id = ?", c.ID).Delete(&CustomerShippingAddress{}).Error
if err != nil {
return err
diff --git a/customer/validators.go b/customer/validators.go
index a495772..b8c2a14 100644
--- a/customer/validators.go
+++ b/customer/validators.go
@@ -21,73 +21,8 @@ import (
"strings"
"vidhukant.com/openbills/errors"
u "vidhukant.com/openbills/util"
- e "errors"
)
-// NOTE: very inefficient and really really really dumb but it works
-// TODO: find a better (or even a remotely good) way
-func checkDuplicate(field, value string, userId uint) error {
- if value != "" {
- var count int64
- err := db.Model(&Customer{}).
- Where("user_id = ? and " + field + " = ?", userId, 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 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)
@@ -125,21 +60,6 @@ func (c *Customer) validate() error {
}
}
- var err error
- for _, i := range [][]string{{"phone", c.Phone}, {"email", c.Email}, {"website", c.Website}, {"gstin", c.Gstin}} {
- 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
- }
- }
- }
-
return nil
}
diff --git a/main.go b/main.go
index 92c1b0a..bfa17d4 100644
--- a/main.go
+++ b/main.go
@@ -38,7 +38,7 @@ import (
"log"
)
-const OPENBILLS_VERSION = "v0.15.0"
+const OPENBILLS_VERSION = "v0.16.0"
func init() {
if !viper.GetBool("debug_mode") {