From e324b88fe6a68527739ab3533dc08a6e8fe4f2f6 Mon Sep 17 00:00:00 2001 From: Tue Date: Fri, 28 Oct 2022 09:31:39 +0700 Subject: [PATCH] build authsms --- client/supplier_role.go | 58 +++++++++++++++++++++++++++++ client/supplier_user.go | 65 +++++++++++++++++++++++++++------ model/supplier_role_request.go | 14 +++++++ model/supplier_role_response.go | 5 +++ model/supplier_user_request.go | 36 +++++++++++------- model/supplier_user_response.go | 8 +++- subject/config.go | 2 + subject/supplier_role.go | 15 ++++++++ subject/supplier_user.go | 24 +++++++----- 9 files changed, 191 insertions(+), 36 deletions(-) create mode 100644 client/supplier_role.go create mode 100644 model/supplier_role_request.go create mode 100644 model/supplier_role_response.go create mode 100644 subject/supplier_role.go diff --git a/client/supplier_role.go b/client/supplier_role.go new file mode 100644 index 0000000..3bf068d --- /dev/null +++ b/client/supplier_role.go @@ -0,0 +1,58 @@ +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" +) + +// SupplierRole ... +type SupplierRole struct{} + +// GetSupplierRole ... +func GetSupplierRole() SupplierRole { + return SupplierRole{} +} + +func (s SupplierRole) CreateRole(p model.CreateRoleRequest) (*model.CreateRoleResponse, error) { + msg, err := natsio.GetServer().Request(subject.SupplierUser.CreateOwner, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.CreateRoleResponse `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) UpdateRole(p model.UpdateRoleRequest) error { + msg, err := natsio.GetServer().Request(subject.SupplierRole.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_user.go b/client/supplier_user.go index 8bb10c5..fb6a2db 100644 --- a/client/supplier_user.go +++ b/client/supplier_user.go @@ -16,15 +16,15 @@ func GetSupplierUser() SupplierUser { return SupplierUser{} } -func (s SupplierUser) CreateSupplierOwnerUsers(p model.CreateSupplierOwnerUserRequest) (*model.CreateSupplierUserOwnerResponse, error) { - msg, err := natsio.GetServer().Request(subject.SupplierUser.CreateSupplierOwnerUser, toBytes(p)) +func (s SupplierUser) CreateSupplierOwnerUsers(p model.CreateOwnerRequest) (*model.CreateOwnerResponse, error) { + msg, err := natsio.GetServer().Request(subject.SupplierUser.CreateOwner, toBytes(p)) if err != nil { return nil, err } var r struct { - Data *model.CreateSupplierUserOwnerResponse `json:"data"` - Error string `json:"error"` + Data *model.CreateOwnerResponse `json:"data"` + Error string `json:"error"` } if err = json.Unmarshal(msg.Data, &r); err != nil { @@ -37,8 +37,8 @@ func (s SupplierUser) CreateSupplierOwnerUsers(p model.CreateSupplierOwnerUserRe return r.Data, nil } -func (s SupplierUser) UpdateSupplierOwnerUsers(p model.UpdateSupplierOwnerUserRequest) error { - msg, err := natsio.GetServer().Request(subject.SupplierUser.UpdateSupplierOwnerUser, toBytes(p)) +func (s SupplierUser) UpdateSupplierOwnerUsers(p model.UpdateOwnerRequest) error { + msg, err := natsio.GetServer().Request(subject.SupplierUser.UpdateOwner, toBytes(p)) if err != nil { return err } @@ -57,15 +57,15 @@ func (s SupplierUser) UpdateSupplierOwnerUsers(p model.UpdateSupplierOwnerUserRe return nil } -func (s SupplierUser) CreateSupplierStaffUsers(p model.CreateSupplierStaffUserRequest) (*model.CreateSupplierUserStaffResponse, error) { - msg, err := natsio.GetServer().Request(subject.SupplierUser.CreateSupplierStaffUser, toBytes(p)) +func (s SupplierUser) CreateSupplierStaffUsers(p model.CreateStaffRequest) (*model.CreateStaffResponse, error) { + msg, err := natsio.GetServer().Request(subject.SupplierUser.CreateStaff, toBytes(p)) if err != nil { return nil, err } var r struct { - Data *model.CreateSupplierUserStaffResponse `json:"data"` - Error string `json:"error"` + Data *model.CreateStaffResponse `json:"data"` + Error string `json:"error"` } if err = json.Unmarshal(msg.Data, &r); err != nil { @@ -78,8 +78,8 @@ func (s SupplierUser) CreateSupplierStaffUsers(p model.CreateSupplierStaffUserRe return r.Data, nil } -func (s SupplierUser) UpdateSupplierStaffUsers(p model.CreateSupplierStaffUserRequest) error { - msg, err := natsio.GetServer().Request(subject.SupplierUser.UpdateSupplierStaffUser, toBytes(p)) +func (s SupplierUser) UpdateSupplierStaffUsers(p model.UpdateStaffRequest) error { + msg, err := natsio.GetServer().Request(subject.SupplierUser.UpdateStaff, toBytes(p)) if err != nil { return err } @@ -97,3 +97,44 @@ func (s SupplierUser) UpdateSupplierStaffUsers(p model.CreateSupplierStaffUserRe return nil } + +func (s SupplierUser) UpdateStatus(p model.UpdateStatusRequest) error { + msg, err := natsio.GetServer().Request(subject.SupplierUser.UpdateStaff, 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 +} + +func (s SupplierUser) ResetPassword(p model.ResetPasswordRequest) (*model.ResetPasswordResponse, error) { + msg, err := natsio.GetServer().Request(subject.SupplierUser.ResetPassword, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.ResetPasswordResponse `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_role_request.go b/model/supplier_role_request.go new file mode 100644 index 0000000..6402622 --- /dev/null +++ b/model/supplier_role_request.go @@ -0,0 +1,14 @@ +package model + +type CreateRoleRequest struct { + Name string `json:"name"` + Code string `json:"code"` + Type string `json:"type"` +} + +type UpdateRoleRequest struct { + ID string `json:"_id"` + Name string `json:"name"` + Code string `json:"code"` + Type string `json:"type"` +} diff --git a/model/supplier_role_response.go b/model/supplier_role_response.go new file mode 100644 index 0000000..d7b68fc --- /dev/null +++ b/model/supplier_role_response.go @@ -0,0 +1,5 @@ +package model + +type CreateRoleResponse struct { + ID string `json:"_id"` +} diff --git a/model/supplier_user_request.go b/model/supplier_user_request.go index f4556b2..66ccb17 100644 --- a/model/supplier_user_request.go +++ b/model/supplier_user_request.go @@ -1,6 +1,6 @@ package model -type CreateSupplierOwnerUserRequest struct { +type CreateOwnerRequest struct { Name string `json:"name"` Phone string `json:"phone"` Email string `json:"email"` @@ -9,7 +9,7 @@ type CreateSupplierOwnerUserRequest struct { Password string `json:"password"` } -type UpdateSupplierOwnerUserRequest struct { +type UpdateOwnerRequest struct { ID string `json:"_id"` SupplierID string `json:"supplierId"` Name string `json:"name"` @@ -18,7 +18,7 @@ type UpdateSupplierOwnerUserRequest struct { RoleID string `json:"roleId"` } -type CreateSupplierStaffUserRequest struct { +type CreateStaffRequest struct { Name string `json:"name"` Phone string `json:"phone"` Email string `json:"email"` @@ -28,14 +28,24 @@ type CreateSupplierStaffUserRequest struct { Warehouses []string `json:"warehouses"` } -type UpdateSupplierStaffUserRequest struct { - ID string `json:"_id"` - Name string `json:"name"` - Phone string `json:"phone"` - Email string `json:"email"` - SupplierID string `json:"supplierId"` - Role string `json:"role"` - Password string `json:"password"` - Warehouses []string `json:"warehouses"` - RoleId string `json:"roleId"` +type UpdateStaffRequest struct { + ID string `json:"_id"` + Name string `json:"name"` + Phone string `json:"phone"` + Email string `json:"email"` + SupplierID string `json:"supplierId"` + RoleID string `json:"roleId"` + Password string `json:"password"` + SupplierUserWarehouseID string `json:"supplierUserWarehouseId"` + Warehouses []string `json:"warehouses"` +} + +type UpdateStatusRequest struct { + ID string `json:"_id"` + Status string `json:"status"` +} + +type ResetPasswordRequest struct { + ID string `json:"_id"` + Password string `json:"password"` } diff --git a/model/supplier_user_response.go b/model/supplier_user_response.go index 7241bad..38a1d0c 100644 --- a/model/supplier_user_response.go +++ b/model/supplier_user_response.go @@ -1,9 +1,13 @@ package model -type CreateSupplierUserOwnerResponse struct { +type CreateOwnerResponse struct { ID string `json:"_id"` } -type CreateSupplierUserStaffResponse struct { +type CreateStaffResponse struct { ID string `json:"_id"` } + +type ResetPasswordResponse struct { + Password string `json:"password"` +} diff --git a/subject/config.go b/subject/config.go index d50d9d4..90a014f 100644 --- a/subject/config.go +++ b/subject/config.go @@ -10,6 +10,7 @@ var prefixes = struct { Supplier string Seller string SupplierUser string + SupplierRole string }{ Communication: "communication", Order: "order", @@ -20,4 +21,5 @@ var prefixes = struct { Bank: "bank", Seller: "seller", SupplierUser: "supplier_user", + SupplierRole: "supplier_role", } diff --git a/subject/supplier_role.go b/subject/supplier_role.go new file mode 100644 index 0000000..8915759 --- /dev/null +++ b/subject/supplier_role.go @@ -0,0 +1,15 @@ +package subject + +import "fmt" + +func getRoleValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.SupplierRole, val) +} + +var SupplierRole = struct { + Create string + Update string +}{ + Create: getRoleValue("create"), + Update: getRoleValue("update"), +} diff --git a/subject/supplier_user.go b/subject/supplier_user.go index 7abb4c7..adc2f1f 100644 --- a/subject/supplier_user.go +++ b/subject/supplier_user.go @@ -2,18 +2,24 @@ package subject import "fmt" -func getAuthSMSValue(val string) string { +func getSupplierUserValue(val string) string { return fmt.Sprintf("%s.%s", prefixes.SupplierUser, val) } var SupplierUser = struct { - CreateSupplierOwnerUser string - UpdateSupplierOwnerUser string - CreateSupplierStaffUser string - UpdateSupplierStaffUser string + // Users + CreateOwner string + UpdateOwner string + CreateStaff string + UpdateStaff string + UpdateStatus string + ResetPassword string }{ - CreateSupplierOwnerUser: getAuthSMSValue("create_supplier_owner_user"), - UpdateSupplierOwnerUser: getAuthSMSValue("update_supplier_owner_user"), - CreateSupplierStaffUser: getAuthSMSValue("create_supplier_staff_user"), - UpdateSupplierStaffUser: getAuthSMSValue("update_supplier_staff_user"), + // Users + CreateOwner: getSupplierUserValue("create_owner"), + UpdateOwner: getSupplierUserValue("update_owner"), + CreateStaff: getSupplierUserValue("create_staff"), + UpdateStaff: getSupplierUserValue("update_staff"), + UpdateStatus: getSupplierUserValue("update_status"), + ResetPassword: getSupplierUserValue("reset_password"), } -- 2.34.1