aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/customer
diff options
context:
space:
mode:
Diffstat (limited to 'customer')
-rw-r--r--customer/controller.go71
-rw-r--r--customer/customer.go5
-rw-r--r--customer/hooks.go2
-rw-r--r--customer/service.go13
-rw-r--r--customer/validators.go106
5 files changed, 11 insertions, 186 deletions
diff --git a/customer/controller.go b/customer/controller.go
index f2704bd..83423da 100644
--- a/customer/controller.go
+++ b/customer/controller.go
@@ -31,17 +31,7 @@ func handleGetSingleCustomer (ctx *gin.Context) {
return
}
- uId, ok := ctx.Get("UserID")
- if !ok {
- ctx.Error(e.ErrUnauthorized)
- ctx.Abort()
- return
- }
-
- userId := uId.(uint)
-
var customer Customer
-
err = getCustomer(&customer, uint(id))
if err != nil {
ctx.Error(err)
@@ -49,14 +39,7 @@ func handleGetSingleCustomer (ctx *gin.Context) {
return
}
- if customer.UserID != userId {
- ctx.Error(e.ErrForbidden)
- ctx.Abort()
- return
- }
-
ctx.JSON(http.StatusOK, gin.H{
- "message": "success",
"data": customer,
})
}
@@ -64,16 +47,7 @@ func handleGetSingleCustomer (ctx *gin.Context) {
func handleGetCustomers (ctx *gin.Context) {
var customers []Customer
- uId, ok := ctx.Get("UserID")
- if !ok {
- ctx.Error(e.ErrUnauthorized)
- ctx.Abort()
- return
- }
-
- userId := uId.(uint)
-
- err := getCustomers(&customers, userId)
+ err := getCustomers(&customers)
if err != nil {
ctx.Error(err)
ctx.Abort()
@@ -81,7 +55,6 @@ func handleGetCustomers (ctx *gin.Context) {
}
ctx.JSON(http.StatusOK, gin.H{
- "message": "success",
"data": customers,
})
}
@@ -90,26 +63,6 @@ func handleSaveCustomer (ctx *gin.Context) {
var customer Customer
ctx.Bind(&customer)
- uId, ok := ctx.Get("UserID")
- if !ok {
- ctx.Error(e.ErrUnauthorized)
- ctx.Abort()
- return
- }
-
- userId := uId.(uint)
- 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 {
ctx.Error(err)
@@ -118,7 +71,6 @@ func handleSaveCustomer (ctx *gin.Context) {
}
ctx.JSON(http.StatusOK, gin.H{
- "message": "success",
"data": customer,
})
}
@@ -133,23 +85,6 @@ func handleDelCustomer (ctx *gin.Context) {
var customer Customer
customer.ID = uint(id)
- uId, ok := ctx.Get("UserID")
- if !ok {
- ctx.Error(e.ErrUnauthorized)
- ctx.Abort()
- return
- }
-
- userId := uId.(uint)
- customer.UserID = userId
-
- err = checkCustomerOwnership(customer.ID, customer.UserID)
- if err != nil {
- ctx.Error(err)
- ctx.Abort()
- return
- }
-
err = customer.del()
if err != nil {
ctx.Error(err)
@@ -157,7 +92,5 @@ func handleDelCustomer (ctx *gin.Context) {
return
}
- ctx.JSON(http.StatusOK, gin.H{
- "message": "success",
- })
+ ctx.JSON(http.StatusOK, nil)
}
diff --git a/customer/customer.go b/customer/customer.go
index 6e7d2e5..521c531 100644
--- a/customer/customer.go
+++ b/customer/customer.go
@@ -21,7 +21,6 @@ import (
"gorm.io/gorm"
d "vidhukant.com/openbills/db"
u "vidhukant.com/openbills/util"
- "vidhukant.com/openbills/user"
)
var db *gorm.DB
@@ -45,8 +44,6 @@ type CustomerShippingAddress struct {
type Customer struct {
gorm.Model
- UserID uint `json:"-"`
- User user.User `json:"-"`
FirmName string
Gstin string
ContactName string
@@ -54,5 +51,5 @@ type Customer struct {
Email string
Website string
BillingAddress CustomerBillingAddress
- ShippingAddresses []CustomerShippingAddress
+ ShippingAddress CustomerShippingAddress
}
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/service.go b/customer/service.go
index a79e466..ca401f1 100644
--- a/customer/service.go
+++ b/customer/service.go
@@ -19,10 +19,11 @@ package customer
import (
e "vidhukant.com/openbills/errors"
+ "gorm.io/gorm"
)
func getCustomer(customer *Customer, id uint) error {
- res := db.Preload("BillingAddress").Preload("ShippingAddresses").Find(&customer, id)
+ res := db.Preload("BillingAddress").Preload("ShippingAddress").Find(&customer, id)
// TODO: handle potential errors
if res.Error != nil {
@@ -36,8 +37,8 @@ func getCustomer(customer *Customer, id uint) error {
return nil
}
-func getCustomers(customers *[]Customer, userId uint) error {
- res := db.Where("user_id = ?", userId).Find(&customers)
+func getCustomers(customers *[]Customer) error {
+ res := db.Find(&customers)
// TODO: handle potential errors
if res.Error != nil {
@@ -52,20 +53,20 @@ func getCustomers(customers *[]Customer, userId uint) error {
}
func (c *Customer) upsert() error {
- res := db.Save(c)
+ db.Model(&c).Association("ShippingAddress").Replace(c.ShippingAddress)
+ res := db.Session(&gorm.Session{FullSaveAssociations: true}).Save(&c)
// TODO: handle potential errors
return res.Error
}
func (c *Customer) del() error {
- res := db.Where("id = ? and user_id = ?", c.ID, c.UserID).Delete(c)
+ res := db.Where("id = ?", c.ID).Delete(c)
// TODO: handle potential errors
if res.Error != nil {
return res.Error
}
- // returns 404 if either row doesn't exist or if the user doesn't own it
if res.RowsAffected == 0 {
return e.ErrNotFound
}
diff --git a/customer/validators.go b/customer/validators.go
index a495772..5693116 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,46 +60,5 @@ 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
-}
-
-func checkCustomerOwnership(customerId, userId uint) error {
- var customer Customer
- err := db.
- Select("id", "user_id").
- Where("id = ?", customerId).
- Find(&customer).
- Error
-
- // TODO: handle potential errors
- if err != nil {
- return err
- }
-
- // customer doesn't exist
- if customer.ID == 0 {
- return errors.ErrNotFound
- }
-
- // user doesn't own this customer
- if customer.UserID != userId {
- return errors.ErrForbidden
- }
-
return nil
}