From 0dd3dce224ab1357c8165c18cf66376da721f49e Mon Sep 17 00:00:00 2001 From: tuannt20 <105765641+tuannt20@users.noreply.github.com> Date: Tue, 22 Nov 2022 10:04:40 +0700 Subject: [PATCH] add nats staff --- client/staff.go | 41 +++++++++++++++++++++++++++++++++++++++++ model/staff_request.go | 6 ++++++ model/staff_response.go | 12 ++++++++++++ subject/config.go | 2 ++ subject/staff.go | 14 ++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 client/staff.go create mode 100644 model/staff_request.go create mode 100644 model/staff_response.go create mode 100644 subject/staff.go diff --git a/client/staff.go b/client/staff.go new file mode 100644 index 0000000..b175f53 --- /dev/null +++ b/client/staff.go @@ -0,0 +1,41 @@ +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" +) + +// Staff ... +type Staff struct{} + +func GetStaff() Staff { + return Staff{} +} + +// GetListStaffInfoByIds ... +func (s Staff) GetListStaffInfoByIds(p model.GetListStaffRequest) (*model.ResponseListStaffInfo, error) { + msg, err := natsio.GetServer().Request(subject.Staff.GetListStaffInfo, toBytes(p)) + + if err != nil { + return nil, err + } + + var r struct { + Data *model.ResponseListStaffInfo `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/staff_request.go b/model/staff_request.go new file mode 100644 index 0000000..176b34e --- /dev/null +++ b/model/staff_request.go @@ -0,0 +1,6 @@ +package model + +// GetListStaffRequest ... +type GetListStaffRequest struct { + StaffIds []string `json:"staffIds"` +} diff --git a/model/staff_response.go b/model/staff_response.go new file mode 100644 index 0000000..5076d0c --- /dev/null +++ b/model/staff_response.go @@ -0,0 +1,12 @@ +package model + +// ResponseListStaffInfo ... +type ResponseListStaffInfo struct { + Staffs []ResponseStaffInfo `json:"staffs"` +} + +// ResponseStaffInfo ... +type ResponseStaffInfo struct { + ID string `json:"_id"` + Name string `json:"name"` +} diff --git a/subject/config.go b/subject/config.go index e8cf263..2a2f743 100644 --- a/subject/config.go +++ b/subject/config.go @@ -12,6 +12,7 @@ var prefixes = struct { SupplierUser string SupplierRole string SocialPost string + Staff string }{ Communication: "communication", Order: "order", @@ -24,4 +25,5 @@ var prefixes = struct { SupplierUser: "supplier_user", SupplierRole: "supplier_role", SocialPost: "social_post", + Staff: "staff", } diff --git a/subject/staff.go b/subject/staff.go new file mode 100644 index 0000000..4777f06 --- /dev/null +++ b/subject/staff.go @@ -0,0 +1,14 @@ +package subject + +import "fmt" + +func getStaffValue(val string) string { + return fmt.Sprintf("%s.%s", prefixes.Staff, val) +} + +// Staff ... +var Staff = struct { + GetListStaffInfo string +}{ + GetListStaffInfo: getStaffValue("get_list_staff_info"), +}