Mhamdans17 commited on
Commit ·
938b985
1
Parent(s): dd14046
feat: add branch specific point settings
Browse files- controllers/company_controller.go +18 -8
- controllers/order_controller.go +11 -7
- models/models.go +1 -0
controllers/company_controller.go
CHANGED
|
@@ -67,9 +67,10 @@ type UpdateCompanyPointsRequest struct {
|
|
| 67 |
}
|
| 68 |
|
| 69 |
type BranchCreateRequest struct {
|
| 70 |
-
Name
|
| 71 |
-
Address
|
| 72 |
-
Phone
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
type BranchResponse struct {
|
|
@@ -704,12 +705,18 @@ func CreateBranch(c *gin.Context) {
|
|
| 704 |
return
|
| 705 |
}
|
| 706 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 707 |
newBranch := models.Branch{
|
| 708 |
-
CompanyID:
|
| 709 |
-
Name:
|
| 710 |
-
Address:
|
| 711 |
-
Phone:
|
| 712 |
-
IsActive:
|
|
|
|
| 713 |
}
|
| 714 |
|
| 715 |
if err := config.DB.Create(&newBranch).Error; err != nil {
|
|
@@ -755,6 +762,9 @@ func UpdateBranch(c *gin.Context) {
|
|
| 755 |
|
| 756 |
branch.Address = req.Address
|
| 757 |
branch.Phone = req.Phone
|
|
|
|
|
|
|
|
|
|
| 758 |
|
| 759 |
if err := config.DB.Save(&branch).Error; err != nil {
|
| 760 |
c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal meng-update cabang"})
|
|
|
|
| 67 |
}
|
| 68 |
|
| 69 |
type BranchCreateRequest struct {
|
| 70 |
+
Name string `json:"name" binding:"required"`
|
| 71 |
+
Address string `json:"address"`
|
| 72 |
+
Phone string `json:"phone"`
|
| 73 |
+
IsPointEnabled *bool `json:"is_point_enabled"`
|
| 74 |
}
|
| 75 |
|
| 76 |
type BranchResponse struct {
|
|
|
|
| 705 |
return
|
| 706 |
}
|
| 707 |
|
| 708 |
+
isPointEnabled := true
|
| 709 |
+
if req.IsPointEnabled != nil {
|
| 710 |
+
isPointEnabled = *req.IsPointEnabled
|
| 711 |
+
}
|
| 712 |
+
|
| 713 |
newBranch := models.Branch{
|
| 714 |
+
CompanyID: *owner.CompanyID,
|
| 715 |
+
Name: req.Name,
|
| 716 |
+
Address: req.Address,
|
| 717 |
+
Phone: req.Phone,
|
| 718 |
+
IsActive: true,
|
| 719 |
+
IsPointEnabled: isPointEnabled,
|
| 720 |
}
|
| 721 |
|
| 722 |
if err := config.DB.Create(&newBranch).Error; err != nil {
|
|
|
|
| 762 |
|
| 763 |
branch.Address = req.Address
|
| 764 |
branch.Phone = req.Phone
|
| 765 |
+
if req.IsPointEnabled != nil {
|
| 766 |
+
branch.IsPointEnabled = *req.IsPointEnabled
|
| 767 |
+
}
|
| 768 |
|
| 769 |
if err := config.DB.Save(&branch).Error; err != nil {
|
| 770 |
c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal meng-update cabang"})
|
controllers/order_controller.go
CHANGED
|
@@ -958,6 +958,17 @@ func awardPoints(tx *gorm.DB, order *models.Order, company *models.Company) {
|
|
| 958 |
return
|
| 959 |
}
|
| 960 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 961 |
var rules []models.PointRule
|
| 962 |
tx.Where("company_id = ?", company.ID).Order("min_amount desc").Find(&rules)
|
| 963 |
|
|
@@ -981,13 +992,6 @@ func awardPoints(tx *gorm.DB, order *models.Order, company *models.Company) {
|
|
| 981 |
tx.Save(order)
|
| 982 |
order.Member = &member
|
| 983 |
|
| 984 |
-
var branch models.Branch
|
| 985 |
-
branchName := "Pusat"
|
| 986 |
-
if order.BranchID != nil {
|
| 987 |
-
if err := tx.First(&branch, *order.BranchID).Error; err == nil {
|
| 988 |
-
branchName = branch.Name
|
| 989 |
-
}
|
| 990 |
-
}
|
| 991 |
|
| 992 |
var itemNames string
|
| 993 |
for i, item := range order.Items {
|
|
|
|
| 958 |
return
|
| 959 |
}
|
| 960 |
|
| 961 |
+
branchName := "Pusat"
|
| 962 |
+
if order.BranchID != nil {
|
| 963 |
+
var branch models.Branch
|
| 964 |
+
if err := tx.First(&branch, *order.BranchID).Error; err == nil {
|
| 965 |
+
if !branch.IsPointEnabled {
|
| 966 |
+
return // Cabang ini menonaktifkan fitur poin
|
| 967 |
+
}
|
| 968 |
+
branchName = branch.Name
|
| 969 |
+
}
|
| 970 |
+
}
|
| 971 |
+
|
| 972 |
var rules []models.PointRule
|
| 973 |
tx.Where("company_id = ?", company.ID).Order("min_amount desc").Find(&rules)
|
| 974 |
|
|
|
|
| 992 |
tx.Save(order)
|
| 993 |
order.Member = &member
|
| 994 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 995 |
|
| 996 |
var itemNames string
|
| 997 |
for i, item := range order.Items {
|
models/models.go
CHANGED
|
@@ -86,6 +86,7 @@ type Branch struct {
|
|
| 86 |
Address string `gorm:"type:varchar(255)" json:"address"`
|
| 87 |
Phone string `gorm:"type:varchar(20)" json:"phone"`
|
| 88 |
IsActive bool `json:"is_active"`
|
|
|
|
| 89 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
| 90 |
Company *Company `gorm:"foreignKey:CompanyID" json:"company,omitempty"`
|
| 91 |
Users []User `gorm:"foreignKey:BranchID;constraint:OnDelete:SET NULL" json:"-"`
|
|
|
|
| 86 |
Address string `gorm:"type:varchar(255)" json:"address"`
|
| 87 |
Phone string `gorm:"type:varchar(20)" json:"phone"`
|
| 88 |
IsActive bool `json:"is_active"`
|
| 89 |
+
IsPointEnabled bool `gorm:"default:true" json:"is_point_enabled"`
|
| 90 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
| 91 |
Company *Company `gorm:"foreignKey:CompanyID" json:"company,omitempty"`
|
| 92 |
Users []User `gorm:"foreignKey:BranchID;constraint:OnDelete:SET NULL" json:"-"`
|