From e78f8befa125d95234138b6b080b103e4d6f7acd Mon Sep 17 00:00:00 2001 From: Nam Huynh Date: Sat, 21 Aug 2021 19:28:09 +0700 Subject: [PATCH] add method get device permissioms --- action_query.go => action_get_all_devices.go | 0 action_get_device_permissions.go | 28 ++++++++++++++++++++ constant.go | 2 ++ model.go | 7 +++++ 4 files changed, 37 insertions(+) rename action_query.go => action_get_all_devices.go (100%) create mode 100644 action_get_device_permissions.go diff --git a/action_query.go b/action_get_all_devices.go similarity index 100% rename from action_query.go rename to action_get_all_devices.go diff --git a/action_get_device_permissions.go b/action_get_device_permissions.go new file mode 100644 index 0000000..05e95b5 --- /dev/null +++ b/action_get_device_permissions.go @@ -0,0 +1,28 @@ +package devicemngt + +import ( + "context" + "fmt" + + "github.com/Selly-Modules/logger" +) + +// GetDevicePermissionsByToken ... +func (s Service) GetDevicePermissionsByToken(token string) (doc StaffDevicePermissions) { + ctx := context.Background() + + stm, args, _ := s.Builder. + Select("dm.id, s.account_type, sr.permissions"). + From(fmt.Sprintf("%s AS dm", TableDeviceMngt)). + Join(fmt.Sprintf("LEFT JOIN %s s ON s.id = dm.owner_id", TableStaff)). + Join(fmt.Sprintf("LEFT JOIN %s sr ON s.role_id = sr.id", TableStaffRole)). + Where("dm.auth_token = ?", token). + ToSql() + if err := s.DB.GetContext(ctx, &doc, stm, args...); err != nil { + logger.Error("devicemngt - GetDevicePermissionsByToken", logger.LogData{ + "token": token, + "error": err.Error(), + }) + } + return +} diff --git a/constant.go b/constant.go index c2cf5a6..af34412 100644 --- a/constant.go +++ b/constant.go @@ -3,6 +3,8 @@ package devicemngt // Constant ... const ( TableDeviceMngt = "device_managements" + TableStaff = "staffs" + TableStaffRole = "staff_roles" OwnerTypeStaff = "staff" OwnerTypeSeller = "seller" diff --git a/model.go b/model.go index d492fcd..7cd84a5 100644 --- a/model.go +++ b/model.go @@ -55,3 +55,10 @@ type HeaderData struct { OSVersion string AuthToken string } + +// StaffDevicePermissions ... +type StaffDevicePermissions struct { + ID string `db:"id"` + Permissions []string `db:"permissions"` + AccountType string `db:"account_type"` +}