From f55d59a0fda0088be4af51a04b618dfbc60b5d9b Mon Sep 17 00:00:00 2001 From: Wiktor Date: Tue, 14 Apr 2026 13:12:21 +0200 Subject: [PATCH] feat: add no vat property to customers --- app/delivery/web/api/restricted/customer.go | 27 +++++++++++++++++++ app/model/customer.go | 1 + app/repos/customerRepo/customerRepo.go | 5 ++++ .../customerService/customerService.go | 4 +++ bruno/api_v1/customer/Set is_no_vat.yml | 15 +++++++++++ .../20260302163122_create_tables.sql | 3 ++- 6 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 bruno/api_v1/customer/Set is_no_vat.yml diff --git a/app/delivery/web/api/restricted/customer.go b/app/delivery/web/api/restricted/customer.go index 6e1a41c..b458bdc 100644 --- a/app/delivery/web/api/restricted/customer.go +++ b/app/delivery/web/api/restricted/customer.go @@ -3,6 +3,7 @@ package restricted import ( "strconv" + "git.ma-al.com/goc_daniel/b2b/app/delivery/middleware" "git.ma-al.com/goc_daniel/b2b/app/delivery/middleware/perms" "git.ma-al.com/goc_daniel/b2b/app/model" "git.ma-al.com/goc_daniel/b2b/app/service/customerService" @@ -31,6 +32,7 @@ func CustomerHandlerRoutes(r fiber.Router) fiber.Router { r.Get("", handler.customerData) r.Get("/list", handler.listCustomers) + r.Patch("/no-vat", middleware.Require(perms.UserWriteAny), handler.setCustomerNoVatStatus) return r } @@ -109,3 +111,28 @@ var columnMappingListUsers map[string]string = map[string]string{ "first_name": "users.first_name", "last_name": "users.last_name", } + +func (h *customerHandler) setCustomerNoVatStatus(fc fiber.Ctx) error { + user, ok := localeExtractor.GetCustomer(fc) + if !ok || user == nil { + return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). + JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrBadAttribute))) + } + + var req struct { + CustomerID uint `json:"customer_id"` + IsNoVat bool `json:"is_no_vat"` + } + + if err := fc.Bind().Body(&req); err != nil { + return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrJSONBody)). + JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrJSONBody))) + } + + if err := h.service.SetCustomerNoVatStatus(req.CustomerID, req.IsNoVat); err != nil { + return fc.Status(responseErrors.GetErrorStatus(err)). + JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, err))) + } + + return fc.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(fc, response.Message_OK))) +} diff --git a/app/model/customer.go b/app/model/customer.go index cc4b9f1..b15b145 100644 --- a/app/model/customer.go +++ b/app/model/customer.go @@ -35,6 +35,7 @@ type Customer struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` + IsNoVat bool `gorm:"default:false" json:"is_no_vat"` } func (u *Customer) HasPermission(permission perms.Permission) bool { diff --git a/app/repos/customerRepo/customerRepo.go b/app/repos/customerRepo/customerRepo.go index 18dea15..c45e35c 100644 --- a/app/repos/customerRepo/customerRepo.go +++ b/app/repos/customerRepo/customerRepo.go @@ -16,6 +16,7 @@ type UICustomerRepo interface { Find(langId uint, p find.Paging, filt *filters.FiltersList, search string) (*find.Found[model.UserInList], error) Save(customer *model.Customer) error Create(customer *model.Customer) error + SetCustomerNoVatStatus(customerID uint, isNoVat bool) error } type CustomerRepo struct{} @@ -111,6 +112,10 @@ func (repo *CustomerRepo) Create(customer *model.Customer) error { return db.DB.Create(customer).Error } +func (repo *CustomerRepo) SetCustomerNoVatStatus(customerID uint, isNoVat bool) error { + return db.DB.Model(&model.Customer{}).Where("id = ?", customerID).Update("is_no_vat", isNoVat).Error +} + // func (repo *CustomerRepo) Search( // customerId uint, // partnerCode string, diff --git a/app/service/customerService/customerService.go b/app/service/customerService/customerService.go index bce463d..be8cc9c 100644 --- a/app/service/customerService/customerService.go +++ b/app/service/customerService/customerService.go @@ -24,3 +24,7 @@ func (s *CustomerService) GetById(id uint) (*model.Customer, error) { func (s *CustomerService) Find(langId uint, p find.Paging, filt *filters.FiltersList, search string) (*find.Found[model.UserInList], error) { return s.repo.Find(langId, p, filt, search) } + +func (s *CustomerService) SetCustomerNoVatStatus(customerID uint, isNoVat bool) error { + return s.repo.SetCustomerNoVatStatus(customerID, isNoVat) +} diff --git a/bruno/api_v1/customer/Set is_no_vat.yml b/bruno/api_v1/customer/Set is_no_vat.yml new file mode 100644 index 0000000..c159ad3 --- /dev/null +++ b/bruno/api_v1/customer/Set is_no_vat.yml @@ -0,0 +1,15 @@ +info: + name: Set is_no_vat + type: http + seq: 4 + +http: + method: PATCH + url: "{{bas_url" + auth: inherit + +settings: + encodeUrl: true + timeout: 0 + followRedirects: true + maxRedirects: 5 diff --git a/i18n/migrations/20260302163122_create_tables.sql b/i18n/migrations/20260302163122_create_tables.sql index c03014a..825ad46 100644 --- a/i18n/migrations/20260302163122_create_tables.sql +++ b/i18n/migrations/20260302163122_create_tables.sql @@ -112,7 +112,8 @@ CREATE TABLE IF NOT EXISTS b2b_customers ( country_id INT NULL DEFAULT 2, created_at DATETIME(6) NULL, updated_at DATETIME(6) NULL, - deleted_at DATETIME(6) NULL + deleted_at DATETIME(6) NULL, + is_no_vat TINYINT(1) NULL DEFAULT 0 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE UNIQUE INDEX IF NOT EXISTS idx_customers_email