Skip to content

Commit

Permalink
Merge pull request #298 from Receipt-Wrangler/feature/user-shortcuts
Browse files Browse the repository at this point in the history
Feature/user shortcuts
  • Loading branch information
Noah231515 authored Sep 20, 2024
2 parents 3ae5437 + 7dc0d43 commit 1a19cd2
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/commands/paged_request_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (command *ReceiptPagedRequestCommand) LoadDataFromRequest(w http.ResponseWr
}

if command.Filter.PaidBy.Value == nil || command.Filter.PaidBy.Value == "" {
command.Filter.PaidBy.Value = 0
command.Filter.PaidBy.Value = make([]interface{}, 0)
}

if command.Filter.Categories.Value == nil || command.Filter.Categories.Value == "" {
Expand Down
105 changes: 105 additions & 0 deletions internal/constants/icons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package constants

import "receipt-wrangler/api/internal/structs"

var Icons = []structs.Icon{
{Value: "receipt_long", DisplayValue: "Long Receipt"},
{Value: "account_balance_wallet", DisplayValue: "Wallet"},
{Value: "shopping_cart", DisplayValue: "Shopping Cart"},
{Value: "credit_card", DisplayValue: "Credit Card"},
{Value: "attach_money", DisplayValue: "Money"},
{Value: "local_atm", DisplayValue: "ATM"},
{Value: "currency_exchange", DisplayValue: "Currency Exchange"},
{Value: "pie_chart", DisplayValue: "Pie Chart"},
{Value: "bar_chart", DisplayValue: "Bar Chart"},
{Value: "trending_up", DisplayValue: "Trending Up"},
{Value: "trending_down", DisplayValue: "Trending Down"},
{Value: "compare_arrows", DisplayValue: "Compare"},
{Value: "store", DisplayValue: "Store"},
{Value: "local_offer", DisplayValue: "Offer"},
{Value: "local_mall", DisplayValue: "Mall"},
{Value: "loyalty", DisplayValue: "Loyalty"},
{Value: "redeem", DisplayValue: "Redeem"},
{Value: "group", DisplayValue: "Group"},
{Value: "person", DisplayValue: "Person"},
{Value: "family_restroom", DisplayValue: "Family"},
{Value: "notifications", DisplayValue: "Notifications"},
{Value: "calendar_today", DisplayValue: "Calendar"},
{Value: "event", DisplayValue: "Event"},
{Value: "schedule", DisplayValue: "Schedule"},
{Value: "today", DisplayValue: "Today"},
{Value: "history", DisplayValue: "History"},
{Value: "update", DisplayValue: "Update"},
{Value: "sync", DisplayValue: "Sync"},
{Value: "cloud_upload", DisplayValue: "Upload"},
{Value: "photo_camera", DisplayValue: "Camera"},
{Value: "document_scanner", DisplayValue: "Scanner"},
{Value: "folder", DisplayValue: "Folder"},
{Value: "description", DisplayValue: "Description"},
{Value: "list", DisplayValue: "List"},
{Value: "checklist", DisplayValue: "Checklist"},
{Value: "rule", DisplayValue: "Rule"},
{Value: "calculate", DisplayValue: "Calculate"},
{Value: "savings", DisplayValue: "Savings"},
{Value: "payments", DisplayValue: "Payments"},
{Value: "category", DisplayValue: "Category"},
{Value: "local_grocery_store", DisplayValue: "Grocery Store"},
{Value: "restaurant", DisplayValue: "Restaurant"},
{Value: "local_gas_station", DisplayValue: "Gas Station"},
{Value: "flight", DisplayValue: "Flight"},
{Value: "hotel", DisplayValue: "Hotel"},
{Value: "directions_car", DisplayValue: "Car"},
{Value: "home", DisplayValue: "Home"},
{Value: "work", DisplayValue: "Work"},
{Value: "settings", DisplayValue: "Settings"},
{Value: "account_balance", DisplayValue: "Account Balance"},
{Value: "assignment", DisplayValue: "Assignment"},
{Value: "article", DisplayValue: "Article"},
{Value: "backup", DisplayValue: "Backup"},
{Value: "bookmark", DisplayValue: "Bookmark"},
{Value: "bug_report", DisplayValue: "Bug Report"},
{Value: "build", DisplayValue: "Build"},
{Value: "cached", DisplayValue: "Cached"},
{Value: "chat", DisplayValue: "Chat"},
{Value: "cloud", DisplayValue: "Cloud"},
{Value: "code", DisplayValue: "Code"},
{Value: "content_copy", DisplayValue: "Copy"},
{Value: "dashboard", DisplayValue: "Dashboard"},
{Value: "delete", DisplayValue: "Delete"},
{Value: "eco", DisplayValue: "Eco"},
{Value: "edit", DisplayValue: "Edit"},
{Value: "email", DisplayValue: "Email"},
{Value: "euro_symbol", DisplayValue: "Euro"},
{Value: "explore", DisplayValue: "Explore"},
{Value: "favorite", DisplayValue: "Favorite"},
{Value: "flag", DisplayValue: "Flag"},
{Value: "flash_on", DisplayValue: "Flash"},
{Value: "format_list_bulleted", DisplayValue: "Bullet List"},
{Value: "functions", DisplayValue: "Functions"},
{Value: "group_add", DisplayValue: "Add Group"},
{Value: "help", DisplayValue: "Help"},
{Value: "info", DisplayValue: "Info"},
{Value: "insert_chart", DisplayValue: "Insert Chart"},
{Value: "label", DisplayValue: "Label"},
{Value: "language", DisplayValue: "Language"},
{Value: "launch", DisplayValue: "Launch"},
{Value: "lightbulb", DisplayValue: "Idea"},
{Value: "link", DisplayValue: "Link"},
{Value: "location_on", DisplayValue: "Location"},
{Value: "lock", DisplayValue: "Lock"},
{Value: "map", DisplayValue: "Map"},
{Value: "menu_book", DisplayValue: "Menu"},
{Value: "more_horiz", DisplayValue: "More"},
{Value: "note_add", DisplayValue: "Add Note"},
{Value: "pending_actions", DisplayValue: "Pending"},
{Value: "query_stats", DisplayValue: "Statistics"},
{Value: "report", DisplayValue: "Report"},
{Value: "search", DisplayValue: "Search"},
{Value: "share", DisplayValue: "Share"},
{Value: "star", DisplayValue: "Star"},
{Value: "summarize", DisplayValue: "Summary"},
{Value: "swap_horiz", DisplayValue: "Swap"},
{Value: "visibility", DisplayValue: "Visibility"},
{Value: "warning", DisplayValue: "Warning"},
{Value: "inventory", DisplayValue: "Inventory"},
}
17 changes: 9 additions & 8 deletions internal/models/user_preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

type UserPrefernces struct {
BaseModel
UserId uint `gorm:"not null; uniqueIndex" json:"userId"`
User *User `json:"-"`
ShowLargeImagePreviews bool `json:"showLargeImagePreviews"`
QuickScanDefaultGroupId *uint `json:"quickScanDefaultGroupId"`
QuickScanDefaultGroup *Group `json:"-"`
QuickScanDefaultPaidById *uint `json:"quickScanDefaultPaidById"`
QuickScanDefaultPaidBy *User `json:"-"`
QuickScanDefaultStatus ReceiptStatus `json:"quickScanDefaultStatus"`
UserId uint `gorm:"not null; uniqueIndex" json:"userId"`
User *User `json:"-"`
ShowLargeImagePreviews bool `json:"showLargeImagePreviews"`
QuickScanDefaultGroupId *uint `json:"quickScanDefaultGroupId"`
QuickScanDefaultGroup *Group `json:"-"`
QuickScanDefaultPaidById *uint `json:"quickScanDefaultPaidById"`
QuickScanDefaultPaidBy *User `json:"-"`
QuickScanDefaultStatus ReceiptStatus `json:"quickScanDefaultStatus"`
UserShortcuts []UserShortcut `json:"userShortcuts"`
}

func (userPreferences *UserPrefernces) LoadDataFromRequest(w http.ResponseWriter, r *http.Request) error {
Expand Down
10 changes: 10 additions & 0 deletions internal/models/user_shortcut.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type UserShortcut struct {
BaseModel
UserPrefernces UserPrefernces `json:"-"`
UserPreferncesId uint `json:"userPreferncesId"`
Name string `json:"name"`
Url string `json:"url"`
Icon string `json:"icon"`
}
1 change: 1 addition & 0 deletions internal/repositories/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func MakeMigrations() error {
&models.GroupMember{},
&models.Comment{},
&models.Notification{},
&models.UserShortcut{},
&models.UserPrefernces{},
&models.SubjectLineRegex{},
&models.GroupSettingsWhiteListEmail{},
Expand Down
65 changes: 53 additions & 12 deletions internal/repositories/user_preferences.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repositories

import (
"gorm.io/gorm/clause"
"receipt-wrangler/api/internal/models"

"gorm.io/gorm"
Expand All @@ -22,16 +23,17 @@ func (repository UserPreferncesRepository) GetUserPreferencesOrCreate(userId uin
db := repository.GetDB()
var userPreferences models.UserPrefernces

err := db.Model(models.UserPrefernces{}).Where("user_id = ?", userId).Find(&userPreferences).Error
err := db.
Model(models.UserPrefernces{}).
Where("user_id = ?", userId).
Preload(clause.Associations).
Find(&userPreferences).
Error
if err != nil {
return models.UserPrefernces{}, err
}

if userPreferences.ID == 0 {
if err != nil {
return models.UserPrefernces{}, err
}

userPreferencesToCreate := models.UserPrefernces{
UserId: userId,
}
Expand All @@ -53,7 +55,7 @@ func (repository UserPreferncesRepository) CreateUserPreferences(userPreferences
return models.UserPrefernces{}, err
}

return userPreferences, nil
return repository.GetUserPreferencesOrCreate(userPreferences.UserId)
}

func (repository UserPreferncesRepository) UpdateUserPreferences(userId uint, userPreferences models.UserPrefernces) (models.UserPrefernces, error) {
Expand All @@ -69,12 +71,27 @@ func (repository UserPreferncesRepository) UpdateUserPreferences(userId uint, us
userPreferencesToUpdate.QuickScanDefaultGroupId = userPreferences.QuickScanDefaultGroupId
userPreferencesToUpdate.QuickScanDefaultPaidById = userPreferences.QuickScanDefaultPaidById
userPreferencesToUpdate.QuickScanDefaultStatus = userPreferences.QuickScanDefaultStatus
userPreferencesToUpdate.UserShortcuts = userPreferences.UserShortcuts

err = db.
Model(models.UserPrefernces{}).
Select("*").
Where("id = ?", userPreferencesToUpdate.ID).
Updates(&userPreferencesToUpdate).Error
err = db.Transaction(func(tx *gorm.DB) error {
err = db.
Model(&userPreferencesToUpdate).
Select("*").
Updates(&userPreferencesToUpdate).Error
if err != nil {
return err
}

err = db.
Model(&userPreferencesToUpdate).
Association("UserShortcuts").
Replace(&userPreferencesToUpdate.UserShortcuts)
if err != nil {
return err
}

return nil
})

if err != nil {
return models.UserPrefernces{}, err
Expand All @@ -86,7 +103,31 @@ func (repository UserPreferncesRepository) UpdateUserPreferences(userId uint, us
func (repository UserPreferncesRepository) DeleteUserPreferences(userId uint) error {
db := repository.GetDB()

err := db.Model(models.UserPrefernces{}).Delete("user_id = ?", userId).Error
var userPreferencesId uint

err := db.
Model(models.UserPrefernces{}).
Where("user_id = ?", userId).
Select("id").
First(&userPreferencesId).Error

err = db.Transaction(func(tx *gorm.DB) error {
txErr := tx.
Model(models.UserShortcut{}).
Where("user_preferences_id = ?", userPreferencesId).
Delete(&models.UserShortcut{}).
Error
if txErr != nil {
return txErr
}

err = db.Model(models.UserPrefernces{}).Delete("user_id = ?", userId).Error
if err != nil {
return txErr
}

return nil
})
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func GetAppData(userId uint, r *http.Request) (structs.AppData, error) {
appData.Categories = categories
appData.Tags = tags
appData.CurrencyDisplay = systemSettings.CurrencyDisplay
appData.Icons = constants.Icons

if r != nil {
claims := structs.GetJWT(r)
Expand Down
1 change: 1 addition & 0 deletions internal/structs/app_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type AppData struct {
UserPreferences models.UserPrefernces `json:"userPreferences"`
Users []UserView `json:"users"`
FeatureConfig FeatureConfig `json:"featureConfig"`
Icons []Icon `json:"icons"`
}
6 changes: 6 additions & 0 deletions internal/structs/icon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package structs

type Icon struct {
Value string `json:"value"`
DisplayValue string `json:"displayValue"`
}
45 changes: 44 additions & 1 deletion swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3176,7 +3176,32 @@ components:
showLargeImagePreviews:
type: boolean
description: Whether to show large image previews
default: false
default:
userShortcuts:
type: array
items:
$ref: "#/components/schemas/UserShortcut"
UserShortcut:
allOf:
- $ref: "#/components/schemas/BaseModel"
- type: object
required:
- id
- userId
- name
properties:
userPreferncesId:
type: integer
description: User preferences id
name:
type: string
description: Name of the shortcut
url:
type: string
description: Destination of the shortcut
icon:
type: string
description: Icon of shortcut
GroupSettings:
type: object
required:
Expand Down Expand Up @@ -3397,6 +3422,7 @@ components:
- categories
- tags
- currencyDisplay
- icons
properties:
claims:
$ref: "#/components/schemas/Claims"
Expand Down Expand Up @@ -3433,6 +3459,11 @@ components:
currencyDisplay:
type: string
description: Currency display
icons:
type: array
description: Icons in the system
items:
$ref: "#/components/schemas/Icon"
TokenPair:
type: object
required:
Expand Down Expand Up @@ -3883,3 +3914,15 @@ components:
type: string
format: binary
description: Files to quick scan
Icon:
type: object
required:
- value
- displayValue
properties:
value:
type: string
description: Icon value
displayValue:
type: string
description: Icon display value

0 comments on commit 1a19cd2

Please sign in to comment.