-
Notifications
You must be signed in to change notification settings - Fork 0
/
appuser_service.go
executable file
·245 lines (191 loc) · 6.98 KB
/
appuser_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package platform_services
import (
"fmt"
"log"
"strings"
"github.com/rs/xid"
"github.com/zapscloud/golib-dbutils/db_common"
"github.com/zapscloud/golib-dbutils/db_utils"
"github.com/zapscloud/golib-platform/platform_common"
"github.com/zapscloud/golib-platform/platform_repository"
"github.com/zapscloud/golib-utils/utils"
)
// AppUserService - Users Service structure
type AppUserService interface {
List(filter string, sort string, skip int64, limit int64) (utils.Map, error)
GetDetails(userId string) (utils.Map, error)
Find(filter string) (utils.Map, error)
Create(indata utils.Map) (utils.Map, error)
Update(userId string, indata utils.Map) (utils.Map, error)
Delete(userId string, deletePermanent bool) error
Authenticate(auth_key string, auth_user string, auth_pwd string) (utils.Map, error)
ChangePassword(userId string, newpwd string) (utils.Map, error)
BusinessList(userId string, filter string, sort string, skip int64, limit int64) (utils.Map, error)
BeginTransaction()
CommitTransaction()
RollbackTransaction()
EndService()
}
type appUserBaseService struct {
db_utils.DatabaseService
daoAppUser platform_repository.AppUserDao
daoBusiness platform_repository.BusinessDao
child AppUserService
}
func init() {
log.SetFlags(log.Lshortfile | log.LstdFlags | log.Lmicroseconds)
}
func NewAppUserService(props utils.Map) (AppUserService, error) {
p := appUserBaseService{}
err := p.OpenDatabaseService(props)
if err != nil {
log.Println("NewAppUserMongoService ", err)
return nil, err
}
log.Printf("appUserMongoService ")
p.daoAppUser = platform_repository.NewAppUserDao(p.GetClient())
p.daoBusiness = platform_repository.NewBusinessDao(p.GetClient())
p.child = &p
return &p, nil
}
func (p *appUserBaseService) EndService() {
log.Printf("EndappUserBaseService ")
p.CloseDatabaseService()
}
// List - List All records
func (p *appUserBaseService) List(filter string, sort string, skip int64, limit int64) (utils.Map, error) {
log.Println("AppUserService::FindAll - Begin")
dataresponse, err := p.daoAppUser.List(filter, sort, skip, limit)
if err != nil {
return nil, err
}
log.Println("AppUserService::FindAll - End ")
return dataresponse, nil
}
// GetDetails - Find By Code
func (p *appUserBaseService) GetDetails(userId string) (utils.Map, error) {
log.Printf("AppUserService::GetDetails:: Begin %v", userId)
data, err := p.daoAppUser.GetDetails(userId)
log.Println("AppUserService::GetDetails:: End ", data, err)
return data, err
}
func (p *appUserBaseService) Find(filter string) (utils.Map, error) {
fmt.Println("AppUserService::GetDetails:: Begin ", filter)
data, err := p.daoAppUser.Find(filter)
log.Println("AppUserService::GetDetails:: End ", data, err)
return data, err
}
// Create - Create Service
func (p *appUserBaseService) Create(indata utils.Map) (utils.Map, error) {
log.Println("UserService::Create - Begin")
var appUserId string
dataval, dataok := indata[platform_common.FLD_APP_USER_ID]
if dataok {
appUserId = strings.ToLower(dataval.(string))
} else {
guid := xid.New()
prefix := "user_"
log.Println("Unique Profile ID", prefix, guid.String())
appUserId = prefix + guid.String()
}
log.Println("Provided Profile ID:", appUserId)
// Update converted/generated id back to indata
indata[platform_common.FLD_APP_USER_ID] = appUserId
dataval, dataok = indata[platform_common.FLD_APP_USER_PASSWORD]
if dataok {
indata[platform_common.FLD_APP_USER_PASSWORD] = utils.SHA(dataval.(string))
}
dataCreated, err := p.daoAppUser.Create(indata)
if err != nil {
return dataCreated, err
}
log.Println("UserService::Create - End ")
return dataCreated, nil
}
// Update - Update Service
func (p *appUserBaseService) Update(userId string, indata utils.Map) (utils.Map, error) {
log.Println("UserService::Update - Begin")
// Delete the Key fields
delete(indata, platform_common.FLD_APP_USER_ID)
// Check whether the password is sent
if dataVal, dataOk := indata[platform_common.FLD_APP_USER_PASSWORD]; dataOk {
indata[platform_common.FLD_APP_USER_PASSWORD] = utils.SHA(dataVal.(string))
}
data, err := p.daoAppUser.Update(userId, indata)
log.Println("UserService::Update - End ")
return data, err
}
// Delete - Delete Service
func (p *appUserBaseService) Delete(userId string, deletePermanent bool) error {
log.Println("UserService::Delete - Begin", userId)
if deletePermanent {
result, err := p.daoAppUser.Delete(userId)
if err != nil {
return err
}
log.Printf("Delete %v", result)
} else {
indata := utils.Map{db_common.FLD_IS_DELETED: true}
data, err := p.Update(userId, indata)
if err != nil {
return err
}
log.Println("Update for Delete Flag", data)
}
log.Printf("UserService::Delete - End")
return nil
}
// GetDetails - Find By Code
func (p *appUserBaseService) Authenticate(auth_key string, auth_login string, auth_pwd string) (utils.Map, error) {
log.Println("Authenticate:: Begin ", auth_key, auth_login, auth_pwd)
log.Println("User Password from API", auth_pwd)
encpwd := utils.SHA(auth_pwd)
dataUser, err := p.daoAppUser.Authenticate(auth_key, auth_login, encpwd)
log.Println("Length of dataUser :", dataUser)
if err != nil {
err := &utils.AppError{ErrorCode: "S30340101", ErrorMsg: "Wrong Credentials", ErrorDetail: "Authenticate credentials is wrong !!"}
return utils.Map{}, err
}
dataval, dataok := dataUser[db_common.FLD_IS_DELETED]
if dataok && !dataval.(bool) {
err := &utils.AppError{ErrorCode: "S30340102", ErrorMsg: "User not in Active Mode. Contact Admin!", ErrorDetail: "User not in Active Mode. Contact Admin!"}
return utils.Map{}, err
}
dataval, dataok = dataUser[db_common.FLD_IS_VERIFIED]
if dataok && !dataval.(bool) {
err := &utils.AppError{ErrorCode: "S30340103", ErrorMsg: "User not yet verified!", ErrorDetail: "User not yet verified!!"}
return utils.Map{}, err
}
return dataUser, nil
}
// Update - Update Service
func (p *appUserBaseService) ChangePassword(userId string, newpwd string) (utils.Map, error) {
log.Println("AppUserService::ChangePassword - Begin")
indata := utils.Map{
platform_common.FLD_APP_USER_PASSWORD: utils.SHA(newpwd),
}
data, err := p.daoAppUser.Update(userId, indata)
log.Println("AppUserService::ChangePassword - End ")
return data, err
}
func (p *appUserBaseService) BusinessList(userId string, filter string, sort string, skip int64, limit int64) (utils.Map, error) {
_, err := p.daoAppUser.GetDetails(userId)
if err != nil {
return utils.Map{}, err
}
dataBusiness, err := p.daoAppUser.BusinessList(userId, filter, sort, skip, limit)
if err != nil {
return utils.Map{}, err
}
for _, value := range dataBusiness[db_common.LIST_RESULT].([]utils.Map) {
log.Println("AppUserService: BusinessList ", value)
dataBusiness, err := p.daoBusiness.GetDetails(value[platform_common.FLD_BUSINESS_ID].(string))
if err != nil {
continue
}
value["app_business"] = dataBusiness
}
log.Println("SysBusinessService::BusinessList ", dataBusiness)
log.Println("SysBusinessService::BusinessList:: End ", dataBusiness, err)
return dataBusiness, err
}