clients.go 6.87 KB
Newer Older
1
2
3
4
5
6
7
8
package models

import (
	"encoding/json"
	"net/http"
	"strconv"
	"strings"

9
	"forge.grandlyon.com/apoyen/sdk-go/internal/auth"
10
11
12
13
14
15
16
)

// HandleClients expose the UserClients API
func (d *DataHandler) HandleClients(w http.ResponseWriter, r *http.Request) {
	id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/UserClients/"))
	switch method := r.Method; method {
	case "GET":
17
18
19

		switch auth.GetLoggedUserTechnical(w, r).Role {
		case "ADMIN":
Alexis POYEN's avatar
Alexis POYEN committed
20
			d.getClientAdmin(w, r, id)
21
		case "BANKER":
Alexis POYEN's avatar
Alexis POYEN committed
22
			d.getClientBanker(w, r, id)
23
		case "CLIENT":
Alexis POYEN's avatar
Alexis POYEN committed
24
			d.getClientClient(w, r, id)
25
		default:
Alexis POYEN's avatar
Alexis POYEN committed
26
			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
27
28
		}
	case "POST":
29
30
		switch auth.GetLoggedUserTechnical(w, r).Role {
		case "ADMIN":
Alexis POYEN's avatar
Alexis POYEN committed
31
			d.postClientAdmin(w, r, id)
32
		case "BANKER":
Alexis POYEN's avatar
Alexis POYEN committed
33
			d.postClientBanker(w, r, id)
34
		case "CLIENT":
Alexis POYEN's avatar
Alexis POYEN committed
35
			d.postClientClient(w, r, id)
36
		default:
Alexis POYEN's avatar
Alexis POYEN committed
37
38
39
40
41
42
43
44
45
46
47
48
			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
		}
	case "PUT":
		switch auth.GetLoggedUserTechnical(w, r).Role {
		case "ADMIN":
			d.putClientAdmin(w, r, id)
		case "BANKER":
			d.putClientBanker(w, r, id)
		case "CLIENT":
			d.putClientClient(w, r, id)
		default:
			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
49
50
		}
	case "DELETE":
51
52
		switch auth.GetLoggedUserTechnical(w, r).Role {
		case "ADMIN":
Alexis POYEN's avatar
Alexis POYEN committed
53
			d.deleteClientAdmin(w, r, id)
54
		case "BANKER":
Alexis POYEN's avatar
Alexis POYEN committed
55
			d.deleteClientBanker(w, r, id)
56
		case "CLIENT":
Alexis POYEN's avatar
Alexis POYEN committed
57
			d.deleteClientClient(w, r, id)
58
		default:
Alexis POYEN's avatar
Alexis POYEN committed
59
			http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
60
61
62
63
64
		}
	default:
		http.Error(w, "method not allowed", 400)
	}
}
Alexis POYEN's avatar
Alexis POYEN committed
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

func (d *DataHandler) getClientAdmin(w http.ResponseWriter, r *http.Request, id int) {
	if id != 0 {
		var o UserClient
		if err := d.db.Preload("BankAccounts").Where(reqID, id).First(&o).Error; err != nil {
			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
			return
		}
		json.NewEncoder(w).Encode(o)
	} else {
		var o []UserClient
		d.db.Preload("BankAccounts").Find(&o)
		json.NewEncoder(w).Encode(o)
	}
}

func (d *DataHandler) getClientBanker(w http.ResponseWriter, r *http.Request, id int) {
	user := d.getLoggedUser(w, r).(UserBanker)
	if id != 0 {
		var o UserClient
		if err := d.db.Preload("BankAccounts").Where(reqID, id).First(&o).Error; err != nil {
			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
			return
		}
		if o.UserBankerID != user.ID {
			http.Error(w, ErrorCannotAccessRessource, http.StatusForbidden)
			return
		}
		json.NewEncoder(w).Encode(o)
	} else {
		var o []UserClient
		d.db.Preload("BankAccounts").Where("user_banker_id = ?", user.ID).Find(&o)
		json.NewEncoder(w).Encode(o)
	}
}

func (d *DataHandler) getClientClient(w http.ResponseWriter, r *http.Request, id int) {
	user := d.getLoggedUser(w, r).(UserClient)
	if id != 0 && int(user.ID) == id {
		var o UserClient
		if err := d.db.Preload("BankAccounts").Where(reqID, id).First(&o).Error; err != nil {
			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
			return
		}
		json.NewEncoder(w).Encode(o)
	} else if id == 0 {
		var o []UserClient
		d.db.Preload("BankAccounts").Where(reqID, user.ID).Find(&o)
		json.NewEncoder(w).Encode(o)
	} else {
		http.Error(w, ErrorCannotAccessRessource, http.StatusForbidden)
		return
	}
}

func (d *DataHandler) postClientAdmin(w http.ResponseWriter, r *http.Request, id int) {
	var o UserClient
	err := json.NewDecoder(r.Body).Decode(&o)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	// check userID is not already present in DB
	var client UserClient
	if err := d.db.Where(reqUserID, o.UserID).First(&client).Error; err == nil {
Alexis POYEN's avatar
Alexis POYEN committed
129
		http.Error(w, "UserID is already bind to a Client", http.StatusInternalServerError)
Alexis POYEN's avatar
Alexis POYEN committed
130
131
132
133
		return
	}
	var banker UserBanker
	if err := d.db.Where(reqUserID, o.UserID).First(&banker).Error; err == nil {
Alexis POYEN's avatar
Alexis POYEN committed
134
		http.Error(w, "UserID is already bind to a Banker", http.StatusInternalServerError)
Alexis POYEN's avatar
Alexis POYEN committed
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
		return
	}
	d.db.Create(&o)
	d.db.Last(&o)
	json.NewEncoder(w).Encode(o)
}

func (d *DataHandler) postClientBanker(w http.ResponseWriter, r *http.Request, id int) {
	user := d.getLoggedUser(w, r).(UserBanker)
	var o UserClient
	err := json.NewDecoder(r.Body).Decode(&o)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	o.UserBankerID = user.ID
	d.db.Create(&o)
	d.db.Last(&o)
	json.NewEncoder(w).Encode(o)
}

func (d *DataHandler) postClientClient(w http.ResponseWriter, r *http.Request, id int) {
	http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}

func (d *DataHandler) putClientAdmin(w http.ResponseWriter, r *http.Request, id int) {
	var o UserClient
	if err := d.db.Preload("BankAccounts").Where(reqID, id).First(&o).Error; err != nil {
		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
		return
	}
	var updatedUser UserClient
	err := json.NewDecoder(r.Body).Decode(&updatedUser)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	o.Name = updatedUser.Name
	o.UserBankerID = updatedUser.UserBankerID
	d.db.Save(&o)
	json.NewEncoder(w).Encode(o)
}

func (d *DataHandler) putClientBanker(w http.ResponseWriter, r *http.Request, id int) {
	user := d.getLoggedUser(w, r).(UserBanker)
	var o UserClient
	if err := d.db.Preload("BankAccounts").Where(reqID, id).First(&o).Error; err != nil {
		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
		return
	}
	var updatedUser UserClient
	err := json.NewDecoder(r.Body).Decode(&updatedUser)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	o.Name = updatedUser.Name
	o.UserBankerID = user.ID
	d.db.Save(&o)
	json.NewEncoder(w).Encode(o)
}

func (d *DataHandler) putClientClient(w http.ResponseWriter, r *http.Request, id int) {
	http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}

func (d *DataHandler) deleteClientAdmin(w http.ResponseWriter, r *http.Request, id int) {
	if id != 0 {
		var o UserClient
		if err := d.db.Where(reqID, id).First(&o).Error; err != nil {
			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
			return
		}
		// Delete bank accounts of user
		d.db.Where("user_client_id = ?", o.ID).Delete(&BankAccount{})

		d.db.Delete(&o)
	} else {
		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
	}
}

func (d *DataHandler) deleteClientBanker(w http.ResponseWriter, r *http.Request, id int) {
	user := d.getLoggedUser(w, r).(UserBanker)
	if id != 0 {
		var o UserClient
		if err := d.db.Where(reqID, id).First(&o).Error; err != nil {
			http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
			return
		}
		if o.UserBankerID != user.ID {
			http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
			return
		}
		// Delete bank accounts of user
		d.db.Where("user_client_id = ?", o.ID).Delete(&BankAccount{})

		d.db.Delete(&o)
	} else {
		http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
	}
}

func (d *DataHandler) deleteClientClient(w http.ResponseWriter, r *http.Request, id int) {
	http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}