From 7f03b98f90ded8fa011e92e6c097b6f7ec9efe4c Mon Sep 17 00:00:00 2001 From: Tue Date: Mon, 31 Oct 2022 10:04:13 +0700 Subject: [PATCH] build authsms --- client/supplier_permission.go | 79 +++++++++++++++++++++++++++ client/supplier_role.go | 44 ++++++++++++++- model/supplier_permission_request.go | 24 ++++++++ model/supplier_permission_response.go | 19 +++++++ model/supplier_role_request.go | 11 ++++ model/supplier_role_response.go | 14 +++++ subject/config.go | 50 +++++++++-------- subject/supplier_permission.go | 17 ++++++ subject/supplier_role.go | 14 +++-- 9 files changed, 243 insertions(+), 29 deletions(-) create mode 100644 client/supplier_permission.go create mode 100644 model/supplier_permission_request.go create mode 100644 model/supplier_permission_response.go create mode 100644 subject/supplier_permission.go diff --git a/client/supplier_permission.go b/client/supplier_permission.go new file mode 100644 index 0000000..86ac1ba --- /dev/null +++ b/client/supplier_permission.go @@ -0,0 +1,79 @@ +package client + +import ( + "encoding/json" + "errors" + "git.selly.red/Selly-Modules/natsio" + "git.selly.red/Selly-Modules/natsio/model" + "git.selly.red/Selly-Modules/natsio/subject" +) + +// SupplierPermission ... +type SupplierPermission struct{} + +// GetSupplierPermission ... +func GetSupplierPermission() SupplierPermission { + return SupplierPermission{} +} + +func (s SupplierPermission) GetListPermission(p model.GetListPermissionRequest) (*model.GetListPermissionResponse, error) { + msg, err := natsio.GetServer().Request(subject.SupplierPermission.GetList, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.GetListPermissionResponse `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + if r.Error != "" { + return nil, errors.New(r.Error) + } + + return r.Data, nil +} + +func (s SupplierPermission) CreatePermission(p model.CreatePermissionRequest) (*model.CreatePermissionResponse, error) { + msg, err := natsio.GetServer().Request(subject.SupplierPermission.Create, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.CreatePermissionResponse `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + if r.Error != "" { + return nil, errors.New(r.Error) + } + + return r.Data, nil +} + +func (s SupplierPermission) UpdatePermission(p model.UpdatePermissionRequest) error { + msg, err := natsio.GetServer().Request(subject.SupplierPermission.Update, toBytes(p)) + if err != nil { + return err + } + + var r struct { + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return err + } + if r.Error != "" { + return errors.New(r.Error) + } + + return nil +} diff --git a/client/supplier_role.go b/client/supplier_role.go index 3bf068d..1fe6d9d 100644 --- a/client/supplier_role.go +++ b/client/supplier_role.go @@ -16,8 +16,29 @@ func GetSupplierRole() SupplierRole { return SupplierRole{} } +func (s SupplierRole) GetListRole(p model.GetListRoleRequest) (*model.GetListRoleResponse, error) { + msg, err := natsio.GetServer().Request(subject.SupplierRole.GetList, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.GetListRoleResponse `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + if r.Error != "" { + return nil, errors.New(r.Error) + } + + return r.Data, nil +} + func (s SupplierRole) CreateRole(p model.CreateRoleRequest) (*model.CreateRoleResponse, error) { - msg, err := natsio.GetServer().Request(subject.SupplierUser.CreateOwner, toBytes(p)) + msg, err := natsio.GetServer().Request(subject.SupplierRole.Create, toBytes(p)) if err != nil { return nil, err } @@ -56,3 +77,24 @@ func (s SupplierRole) UpdateRole(p model.UpdateRoleRequest) error { return nil } + +func (s SupplierRole) DetailRole(p model.DetailRoleRequest) (*model.RoleBrief, error) { + msg, err := natsio.GetServer().Request(subject.SupplierRole.Detail, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.RoleBrief `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + if r.Error != "" { + return nil, errors.New(r.Error) + } + + return r.Data, nil +} diff --git a/model/supplier_permission_request.go b/model/supplier_permission_request.go new file mode 100644 index 0000000..e3d63c7 --- /dev/null +++ b/model/supplier_permission_request.go @@ -0,0 +1,24 @@ +package model + +type GetListPermissionRequest struct { + Page int `json:"page"` + Limit int `json:"limit"` + Group string `json:"group"` +} + +type DetailPermissionRequest struct { + ID string `json:"_id"` +} + +type CreatePermissionRequest struct { + Name string `json:"name"` + Code string `json:"code"` + Group string `json:"group"` +} + +type UpdatePermissionRequest struct { + ID string `json:"_id"` + Name string `json:"name"` + Code string `json:"code"` + Group string `json:"group"` +} diff --git a/model/supplier_permission_response.go b/model/supplier_permission_response.go new file mode 100644 index 0000000..cbbb019 --- /dev/null +++ b/model/supplier_permission_response.go @@ -0,0 +1,19 @@ +package model + +type GetListPermissionResponse struct { + Permissions []PermissionBrief `json:"permissions"` + Total int64 `json:"total"` +} + +type PermissionBrief struct { + ID string `json:"_id"` + Name string `json:"name"` + Code string `json:"code"` + Group string `json:"group"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` +} + +type CreatePermissionResponse struct { + ID string `json:"_id"` +} diff --git a/model/supplier_role_request.go b/model/supplier_role_request.go index 6402622..dce6112 100644 --- a/model/supplier_role_request.go +++ b/model/supplier_role_request.go @@ -1,5 +1,16 @@ package model +type GetListRoleRequest struct { + Page int `json:"page"` + Limit int `json:"limit"` + Type string `json:"type"` + SupplierID string `json:"supplierId"` +} + +type DetailRoleRequest struct { + ID string `json:"_id"` +} + type CreateRoleRequest struct { Name string `json:"name"` Code string `json:"code"` diff --git a/model/supplier_role_response.go b/model/supplier_role_response.go index d7b68fc..24fc4be 100644 --- a/model/supplier_role_response.go +++ b/model/supplier_role_response.go @@ -1,5 +1,19 @@ package model +type GetListRoleResponse struct { + Roles []RoleBrief `json:"roles"` + Total int64 `json:"total"` +} + +type RoleBrief struct { + ID string `json:"_id"` + Name string `json:"name"` + Code string `json:"code"` + Type string `json:"type"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` +} + type CreateRoleResponse struct { ID string `json:"_id"` } diff --git a/subject/config.go b/subject/config.go index 71f275d..c838f87 100644 --- a/subject/config.go +++ b/subject/config.go @@ -1,29 +1,31 @@ package subject var prefixes = struct { - Communication string - Order string - News string - Warehouse string - Location string - Bank string - Supplier string - Seller string - AuthSMS string - Selly string - SupplierUser string - SupplierRole string + Communication string + Order string + News string + Warehouse string + Location string + Bank string + Supplier string + Seller string + AuthSMS string + Selly string + SupplierUser string + SupplierRole string + SupplierPermission string }{ - Communication: "communication", - Order: "order", - News: "news", - Warehouse: "warehouse", - Location: "location", - Supplier: "supplier", - Bank: "bank", - Seller: "seller", - AuthSMS: "auth_sms", - Selly: "selly", - SupplierUser: "supplier_user", - SupplierRole: "supplier_role", + Communication: "communication", + Order: "order", + News: "news", + Warehouse: "warehouse", + Location: "location", + Supplier: "supplier", + Bank: "bank", + Seller: "seller", + AuthSMS: "auth_sms", + Selly: "selly", + SupplierUser: "supplier_user", + SupplierRole: "supplier_role", + SupplierPermission: "supplier_permission", } diff --git a/subject/supplier_permission.go b/subject/supplier_permission.go new file mode 100644 index 0000000..f759a0c --- /dev/null +++ b/subject/supplier_permission.go @@ -0,0 +1,17 @@ +package subject + +import "fmt" + +func getPermissionValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.SupplierPermission, val) +} + +var SupplierPermission = struct { + GetList string + Create string + Update string +}{ + GetList: getPermissionValue("get_list"), + Create: getPermissionValue("create"), + Update: getPermissionValue("update"), +} diff --git a/subject/supplier_role.go b/subject/supplier_role.go index 8915759..83389da 100644 --- a/subject/supplier_role.go +++ b/subject/supplier_role.go @@ -7,9 +7,15 @@ func getRoleValue(val string) string { } var SupplierRole = struct { - Create string - Update string + GetList string + Detail string + Create string + Update string + GetListBySupplierID string }{ - Create: getRoleValue("create"), - Update: getRoleValue("update"), + GetList: getRoleValue("get_list"), + Detail: getRoleValue("detail"), + Create: getRoleValue("create"), + Update: getRoleValue("update"), + GetListBySupplierID: getRoleValue("get_list_by_supplierId"), }