From fb660c6715baee0b3929b19a2e7f8f04f51ae092 Mon Sep 17 00:00:00 2001 From: Tue Date: Thu, 6 Oct 2022 14:09:22 +0700 Subject: [PATCH] fix code --- internal/helper.go | 9 +++++++++ model/user_response.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/internal/helper.go b/internal/helper.go index 31e8f60..a64dc4d 100644 --- a/internal/helper.go +++ b/internal/helper.go @@ -2,6 +2,8 @@ package internal import ( "fmt" + "github.com/thoas/go-funk" + "go.mongodb.org/mongo-driver/bson/primitive" "strings" "github.com/Selly-Modules/mongodb" @@ -46,3 +48,10 @@ func GenerateCode(s string) string { ) return strings.ReplaceAll(mongodb.NonAccentVietnamese(s), emptySpace, underscore) } + +// ConvertObjectIDsToStrings ... +func ConvertObjectIDsToStrings(ids []primitive.ObjectID) []string { + return funk.Map(ids, func(item primitive.ObjectID) string { + return item.Hex() + }).([]string) +} diff --git a/model/user_response.go b/model/user_response.go index 7480bf3..05b9e60 100644 --- a/model/user_response.go +++ b/model/user_response.go @@ -1,6 +1,9 @@ package model import ( + "github.com/Selly-Modules/usermngmt/internal" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" "time" ) @@ -19,6 +22,32 @@ type User struct { UpdatedAt time.Time `json:"updatedAt"` } +// UserOtherBson ... +type UserOtherBson struct { + Supplier primitive.ObjectID `bson:"supplier"` + Inventories []primitive.ObjectID `bson:"inventories"` + IsPresident bool `bson:"isPresident"` +} + +type UserOther struct { + Supplier string `json:"supplier"` + Inventories []string `json:"inventories"` + IsPresident bool `json:"isPresident"` +} + +func (m User) GetUserOther() UserOther { + var ( + userOtherBson UserOtherBson + ) + bsonBytes, _ := bson.Marshal(m.Other) + bson.Unmarshal(bsonBytes, &userOtherBson) + return UserOther{ + Supplier: userOtherBson.Supplier.Hex(), + Inventories: internal.ConvertObjectIDsToStrings(userOtherBson.Inventories), + IsPresident: userOtherBson.IsPresident, + } +} + type ( // UserAll ... UserAll struct {