-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.c
More file actions
315 lines (252 loc) · 8.3 KB
/
manager.c
File metadata and controls
315 lines (252 loc) · 8.3 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "manager.h"
#include "common.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
static void activate_account(int sock) {
char buf[BUFFER_SIZE];
send_message(sock, "Enter customer ID: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
int cid = atoi(buf);
send_message(sock, "Activate (1) or Deactivate (0): ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
int activate = atoi(buf);
char path[100], temp_path[100];
snprintf(path, 100, "%s/customers.csv", DATA_DIR);
snprintf(temp_path, 100, "%s/customers.tmp", DATA_DIR);
int fd = open(path, O_RDWR);
if (fd == -1) {
send_message(sock, "Error opening file\n");
return;
}
if (lock_file(fd, F_WRLCK) == -1) {
close(fd);
send_message(sock, "Error acquiring lock\n");
return;
}
FILE *fp = fdopen(dup(fd), "r");
FILE *tmp = fopen(temp_path, "w");
if (!fp || !tmp) {
if (fp) fclose(fp);
if (tmp) fclose(tmp);
unlock_file(fd);
close(fd);
send_message(sock, "Error\n");
return;
}
char line[256];
int found = 0;
fgets(line, sizeof(line), fp);
fprintf(tmp, "id,username,password,balance,active\n");
while (fgets(line, sizeof(line), fp)) {
int id, active_status;
char user[50], pass[50];
double balance;
trim_string(line);
if (sscanf(line, "%d,%49[^,],%49[^,],%lf,%d", &id, user, pass, &balance, &active_status) == 5) {
trim_string(user);
trim_string(pass);
if (id == cid) {
active_status = activate;
found = 1;
}
fprintf(tmp, "%d,%s,%s,%.2f,%d\n", id, user, pass, balance, active_status);
}
}
fclose(fp);
fclose(tmp);
rename(temp_path, path);
unlock_file(fd);
close(fd);
send_message(sock, found ? "Account updated\n" : "Account not found\n");
}
static void assign_loan(int sock) {
char buf[BUFFER_SIZE];
send_message(sock, "Enter loan ID: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
int lid = atoi(buf);
send_message(sock, "Enter employee ID: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
int eid = atoi(buf);
char path[100], temp_path[100];
snprintf(path, 100, "%s/loans.csv", DATA_DIR);
snprintf(temp_path, 100, "%s/loans.tmp", DATA_DIR);
int fd = open(path, O_RDWR);
if (fd == -1) {
send_message(sock, "Error opening file\n");
return;
}
if (lock_file(fd, F_WRLCK) == -1) {
close(fd);
send_message(sock, "Error acquiring lock\n");
return;
}
FILE *fp = fdopen(dup(fd), "r");
FILE *tmp = fopen(temp_path, "w");
if (!fp || !tmp) {
if (fp) fclose(fp);
if (tmp) fclose(tmp);
unlock_file(fd);
close(fd);
send_message(sock, "Error\n");
return;
}
char line[512];
int found = 0;
fgets(line, sizeof(line), fp);
fprintf(tmp, "loan_id,customer_id,amount,loan_type,tenure,status,assigned_employee\n");
while (fgets(line, sizeof(line), fp)) {
int loan_id, customer_id, tenure;
char loan_type[100], status[50], assigned_emp[50];
double amount;
trim_string(line);
if (sscanf(line, "%d,%d,%lf,%99[^,],%d,%49[^,],%49[^\n]",
&loan_id, &customer_id, &amount, loan_type, &tenure, status, assigned_emp) >= 6) {
trim_string(loan_type);
trim_string(status);
trim_string(assigned_emp);
if (loan_id == lid) {
snprintf(assigned_emp, sizeof(assigned_emp), "%d", eid);
found = 1;
}
fprintf(tmp, "%d,%d,%.2f,%s,%d,%s,%s\n",
loan_id, customer_id, amount, loan_type, tenure, status, assigned_emp);
} else {
int loan_id2, customer_id2, assigned_employee2;
char status2[50];
double amount2;
if (sscanf(line, "%d,%d,%lf,%49[^,],%d",
&loan_id2, &customer_id2, &amount2, status2, &assigned_employee2) == 5) {
if (loan_id2 == lid) {
assigned_employee2 = eid;
found = 1;
}
fprintf(tmp, "%d,%d,%.2f,%s,%d,%s,%d\n",
loan_id2, customer_id2, amount2, "", 0, status2, assigned_employee2);
} else {
fprintf(tmp, "%s\n", line);
}
}
}
fclose(fp);
fclose(tmp);
rename(temp_path, path);
unlock_file(fd);
close(fd);
if (found)
send_message(sock, "Loan assigned\n");
else
send_message(sock, "Loan not found\n");
}
static void view_feedback(int sock) {
FILE *fp = fopen(DATA_DIR "/feedback.csv", "r");
if (!fp) {
send_message(sock, "No feedback available\n");
return;
}
char line[300], buffer[400];
send_message(sock, "\n=== Customer Feedback ===\n");
fgets(line, sizeof(line), fp);
int count = 0;
while (fgets(line, sizeof(line), fp)) {
int feedback_id, customer_id, reviewed;
char message[256];
trim_string(line);
if (sscanf(line, "%d,%d,%255[^,],%d", &feedback_id, &customer_id, message, &reviewed) == 4) {
trim_string(message);
snprintf(buffer, 400, "ID: %d | Customer: %d | Message: %s | Reviewed: %s\n",
feedback_id, customer_id, message, reviewed ? "Yes" : "No");
send_message(sock, buffer);
count++;
}
}
if (count == 0) {
send_message(sock, "No feedback available\n");
}
fclose(fp);
}
static void change_password(int sock, int mid) {
char buf[BUFFER_SIZE];
send_message(sock, "Enter current password: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
char old_pass[50];
strcpy(old_pass, buf);
send_message(sock, "Enter new password: ");
if (!read_input(sock, buf, BUFFER_SIZE)) return;
char new_pass[50];
strcpy(new_pass, buf);
char path[100];
snprintf(path, 100, "%s/managers.csv", DATA_DIR);
int fd = open(path, O_RDWR);
if (fd == -1) {
send_message(sock, "Error opening file\n");
return;
}
if (lock_file(fd, F_WRLCK) == -1) {
close(fd);
send_message(sock, "Error acquiring lock\n");
return;
}
FILE *fp = fdopen(dup(fd), "r");
FILE *tmp = fopen(DATA_DIR "/managers.tmp", "w");
if (!fp || !tmp) {
if (fp) fclose(fp);
if (tmp) fclose(tmp);
unlock_file(fd);
close(fd);
send_message(sock, "Error\n");
return;
}
char line[256];
int found = 0;
fgets(line, sizeof(line), fp);
fprintf(tmp, "id,username,password\n");
while (fgets(line, sizeof(line), fp)) {
int id;
char user[50], pass[50];
trim_string(line);
if (sscanf(line, "%d,%49[^,],%49[^\n]", &id, user, pass) == 3) {
trim_string(user);
trim_string(pass);
if (id == mid) {
if (strcmp(pass, old_pass) == 0) {
strcpy(pass, new_pass);
found = 1;
}
}
fprintf(tmp, "%d,%s,%s\n", id, user, pass);
}
}
fclose(fp);
fclose(tmp);
rename(DATA_DIR "/managers.tmp", path);
unlock_file(fd);
close(fd);
send_message(sock, found ? "Password changed successfully\n" : "Incorrect password\n");
}
void handle_manager(int sock, int mid) {
char buffer[BUFFER_SIZE];
while (1) {
send_message(sock,
"\n=== Manager Menu ===\n"
"1. Activate/Deactivate Customer Accounts\n"
"2. Assign Loan Application Processes to Employees\n"
"3. Review Customer Feedback\n"
"4. Change Password\n"
"5. Logout\n"
"Choice: ");
if (!read_input(sock, buffer, BUFFER_SIZE)) break;
int choice = atoi(buffer);
switch (choice) {
case 1: activate_account(sock); break;
case 2: assign_loan(sock); break;
case 3: view_feedback(sock); break;
case 4: change_password(sock, mid); break;
case 5: return;
default: send_message(sock, "Invalid choice\n");
}
}
}