From 10aed3cea7935f2f22ef713cff9839dd16afce48 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Fri, 5 Apr 2024 11:36:43 +0530 Subject: moved some validator logic and common structs to util package --- customer/customer.go | 15 ++++---------- customer/validators.go | 46 +++++++++--------------------------------- invoice/invoice.go | 8 ++++---- util/address.go | 26 ++++++++++++++++++++++++ util/validators.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 52 deletions(-) create mode 100644 util/address.go create mode 100644 util/validators.go diff --git a/customer/customer.go b/customer/customer.go index 1a5d6f1..21cae9e 100644 --- a/customer/customer.go +++ b/customer/customer.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma + * Copyright (C) 2023-2024 Vidhu Kant Sharma * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ package customer import ( "gorm.io/gorm" d "vidhukant.com/openbills/db" + u "vidhukant.com/openbills/util" "vidhukant.com/openbills/user" ) @@ -30,23 +31,15 @@ func init() { db.AutoMigrate(&Customer{}, &CustomerBillingAddress{}, &CustomerShippingAddress{}) } -type Address struct { - AddressText string - City string - State string - PostalCode string - Country string -} - type CustomerBillingAddress struct { gorm.Model - Address + u.Address CustomerID uint } type CustomerShippingAddress struct { gorm.Model - Address + u.Address CustomerID uint } diff --git a/customer/validators.go b/customer/validators.go index 086750f..b5d1d96 100644 --- a/customer/validators.go +++ b/customer/validators.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma + * Copyright (C) 2023-2024 Vidhu Kant Sharma * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,27 +18,18 @@ package customer import ( - "regexp" "strings" - "net/mail" - "net/url" "vidhukant.com/openbills/errors" + u "vidhukant.com/openbills/util" e "errors" ) -var phoneRegex, gstinRegex, urlRegex *regexp.Regexp -func init() { - phoneRegex = regexp.MustCompile(`^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$`) - gstinRegex = regexp.MustCompile(`^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$`) -} - // 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{}). - //Select(""). Where("user_id = ? and " + field + " = ?", userId, value). Count(&count). Error @@ -55,6 +46,8 @@ func checkDuplicate(field, value string, userId uint) error { return errors.ErrNonUniqueEmail case "website": return errors.ErrNonUniqueWebsite + case "gstin": + return errors.ErrNonUniqueGSTIN default: return e.New(field + " is not unique") } @@ -75,55 +68,34 @@ func (c *Customer) validate() error { // don't validate if GSTIN is empty if c.Gstin != "" { - // GSTIN regex validation - match := gstinRegex.MatchString(c.Gstin) - if (!match) { + if !u.ValidateGstin(c.Gstin) { return errors.ErrInvalidGSTIN } - - // make sure GSTIN is unique - var count int64 - err := db.Model(&Customer{}). - Select("gstin"). - Where("gstin = ? and user_id = ?", c.Gstin, c.UserID). - Count(&count). - Error - - if err != nil { - return err - } - - if count > 0 { - return errors.ErrNonUniqueGSTIN - } } // don't validate email if empty if c.Email != "" { - _, err := mail.ParseAddress(c.Email) - if err != nil { + if !u.ValidateEmail(c.Email) { return errors.ErrInvalidEmail } } // don't validate phone if empty if c.Phone != "" { - match := phoneRegex.MatchString(c.Phone) - if (!match) { + if !u.ValidatePhone(c.Phone) { return errors.ErrInvalidPhone } } // don't validate website if empty if c.Website != "" { - _, err := url.ParseRequestURI(c.Website) - if err != nil { + if !u.ValidateWebsite(c.Website) { return errors.ErrInvalidWebsite } } var err error - for _, i := range [][]string{{"phone", c.Phone}, {"email", c.Email}, {"website", c.Website}} { + 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 diff --git a/invoice/invoice.go b/invoice/invoice.go index 55552ec..6fcd3c5 100644 --- a/invoice/invoice.go +++ b/invoice/invoice.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma + * Copyright (C) 2023-2024 Vidhu Kant Sharma * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,7 +21,7 @@ import ( "gorm.io/gorm" d "vidhukant.com/openbills/db" "vidhukant.com/openbills/user" - c "vidhukant.com/openbills/customer" + u "vidhukant.com/openbills/util" i "vidhukant.com/openbills/item" "time" ) @@ -35,13 +35,13 @@ func init() { type InvoiceBillingAddress struct { gorm.Model - c.Address + u.Address InvoiceID uint } type InvoiceShippingAddress struct { gorm.Model - c.Address + u.Address InvoiceID uint } diff --git a/util/address.go b/util/address.go new file mode 100644 index 0000000..45c80c9 --- /dev/null +++ b/util/address.go @@ -0,0 +1,26 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023-2024 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package util + +type Address struct { + AddressText string + City string + State string + PostalCode string + Country string +} diff --git a/util/validators.go b/util/validators.go new file mode 100644 index 0000000..c180daf --- /dev/null +++ b/util/validators.go @@ -0,0 +1,54 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023-2024 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package util + +import ( + "regexp" + "net/mail" + "net/url" +) + +var phoneRegex, gstinRegex *regexp.Regexp +func init() { + phoneRegex = regexp.MustCompile(`^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$`) + gstinRegex = regexp.MustCompile(`^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$`) +} + +func ValidateEmail(email string) bool { + _, err := mail.ParseAddress(email) + if err != nil { + return false + } + return true +} + +func ValidateWebsite(website string) bool { + _, err := url.ParseRequestURI(website) + if err != nil { + return false + } + return true +} + +func ValidateGstin(gstin string) bool { + return gstinRegex.MatchString(gstin) +} + +func ValidatePhone(phone string) bool { + return phoneRegex.MatchString(phone) +} -- cgit v1.2.3