aboutsummaryrefslogtreecommitdiff
path: root/invoice/service.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2024-07-09 07:58:34 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2024-07-09 07:58:34 +0530
commitd0a44ff5cfad5d063929426e2420f6f0d55b1dbe (patch)
tree476ad4ff14e39bed40049b4d1cba296c6d395c1b /invoice/service.go
parent908fb4b2b0fd1c31aa8c1cc87f56d490b42aca7a (diff)
added custom fields supportv0.9.0
Diffstat (limited to 'invoice/service.go')
-rw-r--r--invoice/service.go66
1 files changed, 64 insertions, 2 deletions
diff --git a/invoice/service.go b/invoice/service.go
index 91579c6..163b21e 100644
--- a/invoice/service.go
+++ b/invoice/service.go
@@ -52,7 +52,7 @@ func getNewInvoiceNumber(userId uint) (uint, error) {
}
func getInvoice(invoice *Invoice, id uint) error {
- res := db.Preload("BillingAddress").Preload("ShippingAddress").Preload("Items").Find(&invoice, id)
+ res := db.Preload("BillingAddress").Preload("ShippingAddress").Preload("Items").Preload("CustomFields").Find(&invoice, id)
// TODO: handle potential errors
if res.Error != nil {
@@ -96,7 +96,20 @@ func getInvoiceItems(items *[]InvoiceItem, invoiceId uint) error {
return nil
}
-// TODO: route to only get the invouce's items
+func getInvoiceCustomFields(customFields *[]CustomField, invoiceId uint) error {
+ res := db.Where("invoice_id = ?", invoiceId).Find(&customFields)
+
+ // TODO: handle potential errors
+ if res.Error != nil {
+ return res.Error
+ }
+
+ if res.RowsAffected == 0 {
+ return e.ErrEmptyResponse
+ }
+
+ return nil
+}
func (i *Invoice) upsert() error {
res := db.Save(i)
@@ -147,6 +160,33 @@ func getItemInvoice(itemId, userId uint) (uint, error) {
return invoiceId, nil
}
+// also checks for ownership
+func getCustomFieldInvoice(fieldId, userId uint) (uint, error) {
+ var invoiceId uint
+ res := db.
+ Model(&CustomField{}).
+ Select("invoice_id").
+ Where("id = ?", fieldId).
+ Find(&invoiceId)
+
+ // TODO: handle potential errors
+ if res.Error != nil {
+ return invoiceId, res.Error
+ }
+
+ if res.RowsAffected == 0 {
+ return invoiceId, e.ErrNotFound
+ }
+
+ err := checkInvoiceOwnership(invoiceId, userId)
+
+ if err != nil {
+ return invoiceId, err
+ }
+
+ return invoiceId, nil
+}
+
func (i *InvoiceItem) del() error {
res := db.Delete(i)
@@ -163,8 +203,30 @@ func (i *InvoiceItem) del() error {
return nil
}
+func (c *CustomField) del() error {
+ res := db.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
+ }
+
+ return nil
+}
+
func (i *InvoiceItem) upsert() error {
res := db.Save(i)
// TODO: handle potential errors
return res.Error
}
+
+func (c *CustomField) upsert() error {
+ res := db.Save(c)
+ // TODO: handle potential errors
+ return res.Error
+}