@vezlo/assistant-server 2.12.0 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +15 -1
  2. package/dist/src/bootstrap/initializeServices.d.ts +6 -0
  3. package/dist/src/bootstrap/initializeServices.d.ts.map +1 -1
  4. package/dist/src/bootstrap/initializeServices.js +15 -2
  5. package/dist/src/bootstrap/initializeServices.js.map +1 -1
  6. package/dist/src/controllers/AISettingsController.d.ts.map +1 -1
  7. package/dist/src/controllers/AISettingsController.js +3 -7
  8. package/dist/src/controllers/AISettingsController.js.map +1 -1
  9. package/dist/src/controllers/AccountController.d.ts +20 -0
  10. package/dist/src/controllers/AccountController.d.ts.map +1 -0
  11. package/dist/src/controllers/AccountController.js +135 -0
  12. package/dist/src/controllers/AccountController.js.map +1 -0
  13. package/dist/src/controllers/ApiKeyController.d.ts.map +1 -1
  14. package/dist/src/controllers/ApiKeyController.js +2 -22
  15. package/dist/src/controllers/ApiKeyController.js.map +1 -1
  16. package/dist/src/controllers/ChatController.d.ts.map +1 -1
  17. package/dist/src/controllers/ChatController.js +7 -1
  18. package/dist/src/controllers/ChatController.js.map +1 -1
  19. package/dist/src/controllers/GenerateKeyController.d.ts +22 -0
  20. package/dist/src/controllers/GenerateKeyController.d.ts.map +1 -0
  21. package/dist/src/controllers/GenerateKeyController.js +132 -0
  22. package/dist/src/controllers/GenerateKeyController.js.map +1 -0
  23. package/dist/src/controllers/TeamController.d.ts +30 -0
  24. package/dist/src/controllers/TeamController.d.ts.map +1 -0
  25. package/dist/src/controllers/TeamController.js +208 -0
  26. package/dist/src/controllers/TeamController.js.map +1 -0
  27. package/dist/src/middleware/roleGuard.d.ts +8 -0
  28. package/dist/src/middleware/roleGuard.d.ts.map +1 -0
  29. package/dist/src/middleware/roleGuard.js +26 -0
  30. package/dist/src/middleware/roleGuard.js.map +1 -0
  31. package/dist/src/server.js +277 -66
  32. package/dist/src/server.js.map +1 -1
  33. package/dist/src/services/ApiKeyService.d.ts.map +1 -1
  34. package/dist/src/services/ApiKeyService.js.map +1 -1
  35. package/dist/src/services/SetupService.d.ts +1 -0
  36. package/dist/src/services/SetupService.d.ts.map +1 -1
  37. package/dist/src/services/SetupService.js +5 -17
  38. package/dist/src/services/SetupService.js.map +1 -1
  39. package/dist/src/services/TeamService.d.ts +53 -0
  40. package/dist/src/services/TeamService.d.ts.map +1 -0
  41. package/dist/src/services/TeamService.js +310 -0
  42. package/dist/src/services/TeamService.js.map +1 -0
  43. package/dist/src/storage/MessageRepository.d.ts.map +1 -1
  44. package/dist/src/storage/MessageRepository.js +10 -1
  45. package/dist/src/storage/MessageRepository.js.map +1 -1
  46. package/dist/src/types/index.d.ts +1 -0
  47. package/dist/src/types/index.d.ts.map +1 -1
  48. package/package.json +1 -1
  49. package/scripts/generate-key.js +9 -69
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireAdmin = void 0;
4
+ /**
5
+ * Middleware to require admin role
6
+ * Must be used after authenticateUser middleware
7
+ */
8
+ const requireAdmin = (req, res, next) => {
9
+ if (!req.profile) {
10
+ res.status(401).json({
11
+ success: false,
12
+ error: 'Not authenticated'
13
+ });
14
+ return;
15
+ }
16
+ if (req.profile.role !== 'admin') {
17
+ res.status(403).json({
18
+ success: false,
19
+ error: 'Admin access required'
20
+ });
21
+ return;
22
+ }
23
+ next();
24
+ };
25
+ exports.requireAdmin = requireAdmin;
26
+ //# sourceMappingURL=roleGuard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roleGuard.js","sourceRoot":"","sources":["../../../src/middleware/roleGuard.ts"],"names":[],"mappings":";;;AAIA;;;GAGG;AACI,MAAM,YAAY,GAAG,CAC1B,GAAyB,EACzB,GAAa,EACb,IAAkB,EACZ,EAAE;IACR,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;QACjB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACnB,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,mBAAmB;SAC3B,CAAC,CAAC;QACH,OAAO;IACT,CAAC;IAED,IAAI,GAAG,CAAC,OAAO,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QACjC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACnB,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,uBAAuB;SAC/B,CAAC,CAAC;QACH,OAAO;IACT,CAAC;IAED,IAAI,EAAE,CAAC;AACT,CAAC,CAAC;AAtBW,QAAA,YAAY,gBAsBvB"}
@@ -49,6 +49,8 @@ const global_1 = require("./config/global");
49
49
  const logger_1 = __importDefault(require("./config/logger"));
50
50
  const errorHandler_1 = require("./middleware/errorHandler");
51
51
  const auth_1 = require("./middleware/auth");
52
+ const roleGuard_1 = require("./middleware/roleGuard");
53
+ const GenerateKeyController_1 = require("./controllers/GenerateKeyController");
52
54
  const supabase_js_1 = require("@supabase/supabase-js");
53
55
  const initializeServices_1 = require("./bootstrap/initializeServices");
54
56
  const RealtimePublisher_1 = require("./services/RealtimePublisher");
@@ -117,6 +119,9 @@ let companyController;
117
119
  let slackController;
118
120
  let databaseToolConfigController;
119
121
  let aiSettingsController;
122
+ let teamController;
123
+ let accountController;
124
+ let generateKeyController;
120
125
  async function initializeServices() {
121
126
  try {
122
127
  logger_1.default.info('Initializing Vezlo services...');
@@ -134,6 +139,9 @@ async function initializeServices() {
134
139
  slackController = controllers.slackController;
135
140
  databaseToolConfigController = controllers.databaseToolConfigController;
136
141
  aiSettingsController = controllers.aiSettingsController;
142
+ teamController = controllers.teamController;
143
+ accountController = controllers.accountController;
144
+ generateKeyController = new GenerateKeyController_1.GenerateKeyController(supabase);
137
145
  logger_1.default.info('All services initialized successfully');
138
146
  }
139
147
  catch (error) {
@@ -292,7 +300,7 @@ function setupRoutes() {
292
300
  * 500:
293
301
  * description: Internal server error
294
302
  */
295
- app.post('/api/api-keys', (0, auth_1.authenticateUser)(supabase), (req, res) => apiKeyController.generateApiKey(req, res));
303
+ app.post('/api/api-keys', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => apiKeyController.generateApiKey(req, res));
296
304
  /**
297
305
  * @swagger
298
306
  * /api/api-keys/status:
@@ -321,7 +329,7 @@ function setupRoutes() {
321
329
  * 500:
322
330
  * description: Internal server error
323
331
  */
324
- app.get('/api/api-keys/status', (0, auth_1.authenticateUser)(supabase), (req, res) => apiKeyController.getApiKeyStatus(req, res));
332
+ app.get('/api/api-keys/status', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => apiKeyController.getApiKeyStatus(req, res));
325
333
  /**
326
334
  * @swagger
327
335
  * /api/company/analytics:
@@ -417,7 +425,7 @@ function setupRoutes() {
417
425
  * 401:
418
426
  * description: Not authenticated
419
427
  */
420
- app.post('/api/database-tools/config', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.createConfig(req, res));
428
+ app.post('/api/database-tools/config', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.createConfig(req, res));
421
429
  /**
422
430
  * @swagger
423
431
  * /api/database-tools/config:
@@ -433,7 +441,7 @@ function setupRoutes() {
433
441
  * 404:
434
442
  * description: No configuration found
435
443
  */
436
- app.get('/api/database-tools/config', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.getConfig(req, res));
444
+ app.get('/api/database-tools/config', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.getConfig(req, res));
437
445
  /**
438
446
  * @swagger
439
447
  * /api/database-tools/config/{configId}:
@@ -465,7 +473,7 @@ function setupRoutes() {
465
473
  * 200:
466
474
  * description: Configuration updated successfully
467
475
  */
468
- app.put('/api/database-tools/config/:configId', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.updateConfig(req, res));
476
+ app.put('/api/database-tools/config/:configId', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.updateConfig(req, res));
469
477
  /**
470
478
  * @swagger
471
479
  * /api/database-tools/config/{configId}:
@@ -485,7 +493,7 @@ function setupRoutes() {
485
493
  * 200:
486
494
  * description: Configuration deleted successfully
487
495
  */
488
- app.delete('/api/database-tools/config/:configId', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.deleteConfig(req, res));
496
+ app.delete('/api/database-tools/config/:configId', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.deleteConfig(req, res));
489
497
  /**
490
498
  * @swagger
491
499
  * /api/database-tools/validate:
@@ -513,7 +521,7 @@ function setupRoutes() {
513
521
  * 200:
514
522
  * description: Validation result
515
523
  */
516
- app.post('/api/database-tools/validate', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.validateConnection(req, res));
524
+ app.post('/api/database-tools/validate', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.validateConnection(req, res));
517
525
  /**
518
526
  * @swagger
519
527
  * /api/database-tools/tables:
@@ -541,7 +549,7 @@ function setupRoutes() {
541
549
  * 200:
542
550
  * description: List of tables
543
551
  */
544
- app.post('/api/database-tools/tables', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.getTables(req, res));
552
+ app.post('/api/database-tools/tables', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.getTables(req, res));
545
553
  /**
546
554
  * @swagger
547
555
  * /api/database-tools/tables/{tableName}/schema:
@@ -575,7 +583,7 @@ function setupRoutes() {
575
583
  * 200:
576
584
  * description: Table schema with columns
577
585
  */
578
- app.post('/api/database-tools/tables/:tableName/schema', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.getTableSchema(req, res));
586
+ app.post('/api/database-tools/tables/:tableName/schema', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.getTableSchema(req, res));
579
587
  /**
580
588
  * @swagger
581
589
  * /api/database-tools/config/{configId}/tables:
@@ -595,7 +603,7 @@ function setupRoutes() {
595
603
  * 200:
596
604
  * description: List of tables
597
605
  */
598
- app.get('/api/database-tools/config/:configId/tables', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.getTablesFromConfig(req, res));
606
+ app.get('/api/database-tools/config/:configId/tables', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.getTablesFromConfig(req, res));
599
607
  /**
600
608
  * @swagger
601
609
  * /api/database-tools/config/{configId}/tables/{tableName}/schema:
@@ -620,7 +628,7 @@ function setupRoutes() {
620
628
  * 200:
621
629
  * description: Table schema with columns
622
630
  */
623
- app.get('/api/database-tools/config/:configId/tables/:tableName/schema', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.getTableSchemaFromConfig(req, res));
631
+ app.get('/api/database-tools/config/:configId/tables/:tableName/schema', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.getTableSchemaFromConfig(req, res));
624
632
  /**
625
633
  * @swagger
626
634
  * /api/database-tools/tools:
@@ -684,7 +692,7 @@ function setupRoutes() {
684
692
  * 201:
685
693
  * description: Tool created successfully
686
694
  */
687
- app.post('/api/database-tools/tools', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.createTool(req, res));
695
+ app.post('/api/database-tools/tools', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.createTool(req, res));
688
696
  /**
689
697
  * @swagger
690
698
  * /api/database-tools/tools:
@@ -698,7 +706,7 @@ function setupRoutes() {
698
706
  * 200:
699
707
  * description: List of tools
700
708
  */
701
- app.get('/api/database-tools/tools', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.getTools(req, res));
709
+ app.get('/api/database-tools/tools', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.getTools(req, res));
702
710
  /**
703
711
  * @swagger
704
712
  * /api/database-tools/tools/{toolId}:
@@ -759,7 +767,7 @@ function setupRoutes() {
759
767
  * 200:
760
768
  * description: Tool updated successfully
761
769
  */
762
- app.put('/api/database-tools/tools/:toolId', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.updateTool(req, res));
770
+ app.put('/api/database-tools/tools/:toolId', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.updateTool(req, res));
763
771
  /**
764
772
  * @swagger
765
773
  * /api/database-tools/tools/{toolId}:
@@ -779,7 +787,7 @@ function setupRoutes() {
779
787
  * 200:
780
788
  * description: Tool deleted successfully
781
789
  */
782
- app.delete('/api/database-tools/tools/:toolId', (0, auth_1.authenticateUser)(supabase), (req, res) => databaseToolConfigController.deleteTool(req, res));
790
+ app.delete('/api/database-tools/tools/:toolId', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => databaseToolConfigController.deleteTool(req, res));
783
791
  // AI Settings API Routes
784
792
  /**
785
793
  * @swagger
@@ -837,7 +845,7 @@ function setupRoutes() {
837
845
  * 401:
838
846
  * description: Not authenticated
839
847
  */
840
- app.get('/api/companies/:companyUuid/ai-settings', (0, auth_1.authenticateUser)(supabase), (req, res) => aiSettingsController.getSettings(req, res));
848
+ app.get('/api/companies/:companyUuid/ai-settings', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => aiSettingsController.getSettings(req, res));
841
849
  /**
842
850
  * @swagger
843
851
  * /api/companies/{companyUuid}/ai-settings:
@@ -907,7 +915,222 @@ function setupRoutes() {
907
915
  * 401:
908
916
  * description: Not authenticated
909
917
  */
910
- app.put('/api/companies/:companyUuid/ai-settings', (0, auth_1.authenticateUser)(supabase), (req, res) => aiSettingsController.updateSettings(req, res));
918
+ app.put('/api/companies/:companyUuid/ai-settings', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => aiSettingsController.updateSettings(req, res));
919
+ // Team Management API Routes
920
+ /**
921
+ * @swagger
922
+ * /api/companies/{companyUuid}/team:
923
+ * post:
924
+ * summary: Create a new team member
925
+ * description: Create a new user and add them to the company (Admin only)
926
+ * tags: [Team]
927
+ * security:
928
+ * - bearerAuth: []
929
+ * parameters:
930
+ * - in: path
931
+ * name: companyUuid
932
+ * required: true
933
+ * schema:
934
+ * type: string
935
+ * description: Company UUID
936
+ * requestBody:
937
+ * required: true
938
+ * content:
939
+ * application/json:
940
+ * schema:
941
+ * type: object
942
+ * required: [email, password, name, role]
943
+ * properties:
944
+ * email:
945
+ * type: string
946
+ * format: email
947
+ * password:
948
+ * type: string
949
+ * minLength: 6
950
+ * name:
951
+ * type: string
952
+ * role:
953
+ * type: string
954
+ * enum: [admin, user]
955
+ * responses:
956
+ * 201:
957
+ * description: Team member created successfully
958
+ * 400:
959
+ * description: Invalid request
960
+ * 403:
961
+ * description: Admin access required
962
+ * 401:
963
+ * description: Not authenticated
964
+ */
965
+ app.post('/api/companies/:companyUuid/team', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => teamController.createTeamMember(req, res));
966
+ /**
967
+ * @swagger
968
+ * /api/companies/{companyUuid}/team:
969
+ * get:
970
+ * summary: Get team members
971
+ * description: Get team members for the company with pagination and search
972
+ * tags: [Team]
973
+ * security:
974
+ * - bearerAuth: []
975
+ * parameters:
976
+ * - in: path
977
+ * name: companyUuid
978
+ * required: true
979
+ * schema:
980
+ * type: string
981
+ * description: Company UUID
982
+ * - in: query
983
+ * name: search
984
+ * schema:
985
+ * type: string
986
+ * description: Search by user name
987
+ * - in: query
988
+ * name: page
989
+ * schema:
990
+ * type: integer
991
+ * default: 1
992
+ * description: Page number
993
+ * - in: query
994
+ * name: limit
995
+ * schema:
996
+ * type: integer
997
+ * default: 50
998
+ * maximum: 100
999
+ * description: Items per page
1000
+ * responses:
1001
+ * 200:
1002
+ * description: Team members retrieved successfully
1003
+ * 401:
1004
+ * description: Not authenticated
1005
+ */
1006
+ app.get('/api/companies/:companyUuid/team', (0, auth_1.authenticateUser)(supabase), (req, res) => teamController.getTeamMembers(req, res));
1007
+ /**
1008
+ * @swagger
1009
+ * /api/companies/{companyUuid}/team/{userUuid}:
1010
+ * put:
1011
+ * summary: Update team member
1012
+ * description: Update team member details (Admin only)
1013
+ * tags: [Team]
1014
+ * security:
1015
+ * - bearerAuth: []
1016
+ * parameters:
1017
+ * - in: path
1018
+ * name: companyUuid
1019
+ * required: true
1020
+ * schema:
1021
+ * type: string
1022
+ * - in: path
1023
+ * name: userUuid
1024
+ * required: true
1025
+ * schema:
1026
+ * type: string
1027
+ * description: User profile UUID
1028
+ * requestBody:
1029
+ * content:
1030
+ * application/json:
1031
+ * schema:
1032
+ * type: object
1033
+ * properties:
1034
+ * name:
1035
+ * type: string
1036
+ * role:
1037
+ * type: string
1038
+ * enum: [admin, user]
1039
+ * status:
1040
+ * type: string
1041
+ * enum: [active, inactive]
1042
+ * password:
1043
+ * type: string
1044
+ * minLength: 6
1045
+ * responses:
1046
+ * 200:
1047
+ * description: Team member updated successfully
1048
+ * 400:
1049
+ * description: Invalid request
1050
+ * 403:
1051
+ * description: Admin access required
1052
+ * 401:
1053
+ * description: Not authenticated
1054
+ */
1055
+ app.put('/api/companies/:companyUuid/team/:userUuid', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => teamController.updateTeamMember(req, res));
1056
+ /**
1057
+ * @swagger
1058
+ * /api/companies/{companyUuid}/team/{userUuid}:
1059
+ * delete:
1060
+ * summary: Remove team member
1061
+ * description: Remove a team member from the company (Admin only)
1062
+ * tags: [Team]
1063
+ * security:
1064
+ * - bearerAuth: []
1065
+ * parameters:
1066
+ * - in: path
1067
+ * name: companyUuid
1068
+ * required: true
1069
+ * schema:
1070
+ * type: string
1071
+ * - in: path
1072
+ * name: userUuid
1073
+ * required: true
1074
+ * schema:
1075
+ * type: string
1076
+ * description: User profile UUID
1077
+ * responses:
1078
+ * 200:
1079
+ * description: Team member removed successfully
1080
+ * 400:
1081
+ * description: Cannot remove last admin
1082
+ * 403:
1083
+ * description: Admin access required
1084
+ * 401:
1085
+ * description: Not authenticated
1086
+ */
1087
+ app.delete('/api/companies/:companyUuid/team/:userUuid', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => teamController.deleteTeamMember(req, res));
1088
+ // Account Management APIs (for all authenticated users)
1089
+ /**
1090
+ * @swagger
1091
+ * /api/account/profile:
1092
+ * get:
1093
+ * summary: Get current user's profile
1094
+ * description: Get the authenticated user's account information
1095
+ * tags: [Account]
1096
+ * security:
1097
+ * - bearerAuth: []
1098
+ * responses:
1099
+ * 200:
1100
+ * description: Profile retrieved successfully
1101
+ * 401:
1102
+ * description: Not authenticated
1103
+ */
1104
+ app.get('/api/account/profile', (0, auth_1.authenticateUser)(supabase), (req, res) => accountController.getProfile(req, res));
1105
+ /**
1106
+ * @swagger
1107
+ * /api/account/profile:
1108
+ * put:
1109
+ * summary: Update current user's profile
1110
+ * description: Update name and password for the authenticated user (self-update only)
1111
+ * tags: [Account]
1112
+ * security:
1113
+ * - bearerAuth: []
1114
+ * requestBody:
1115
+ * content:
1116
+ * application/json:
1117
+ * schema:
1118
+ * type: object
1119
+ * properties:
1120
+ * name:
1121
+ * type: string
1122
+ * password:
1123
+ * type: string
1124
+ * minLength: 6
1125
+ * responses:
1126
+ * 200:
1127
+ * description: Profile updated successfully
1128
+ * 400:
1129
+ * description: Invalid request
1130
+ * 401:
1131
+ * description: Not authenticated
1132
+ */
1133
+ app.put('/api/account/profile', (0, auth_1.authenticateUser)(supabase), (req, res) => accountController.updateProfile(req, res));
911
1134
  // Chat API Routes
912
1135
  /**
913
1136
  * @swagger
@@ -1110,7 +1333,7 @@ function setupRoutes() {
1110
1333
  * 500:
1111
1334
  * description: Internal server error
1112
1335
  */
1113
- app.post('/api/conversations/:uuid/join', (0, auth_1.authenticateUser)(supabase), (req, res) => chatController.joinConversation(req, res));
1336
+ app.post('/api/conversations/:uuid/join', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => chatController.joinConversation(req, res));
1114
1337
  /**
1115
1338
  * @swagger
1116
1339
  * /api/conversations/{uuid}/messages/agent:
@@ -1150,7 +1373,7 @@ function setupRoutes() {
1150
1373
  * 500:
1151
1374
  * description: Internal server error
1152
1375
  */
1153
- app.post('/api/conversations/:uuid/messages/agent', (0, auth_1.authenticateUser)(supabase), (req, res) => chatController.sendAgentMessage(req, res));
1376
+ app.post('/api/conversations/:uuid/messages/agent', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => chatController.sendAgentMessage(req, res));
1154
1377
  /**
1155
1378
  * @swagger
1156
1379
  * /api/conversations/{uuid}/close:
@@ -1179,7 +1402,7 @@ function setupRoutes() {
1179
1402
  * 500:
1180
1403
  * description: Internal server error
1181
1404
  */
1182
- app.post('/api/conversations/:uuid/close', (0, auth_1.authenticateUser)(supabase), (req, res) => chatController.closeConversation(req, res));
1405
+ app.post('/api/conversations/:uuid/close', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => chatController.closeConversation(req, res));
1183
1406
  /**
1184
1407
  * @swagger
1185
1408
  * /api/conversations/{uuid}/archive:
@@ -1208,7 +1431,7 @@ function setupRoutes() {
1208
1431
  * 500:
1209
1432
  * description: Internal server error
1210
1433
  */
1211
- app.post('/api/conversations/:uuid/archive', (0, auth_1.authenticateUser)(supabase), (req, res) => chatController.archiveConversation(req, res));
1434
+ app.post('/api/conversations/:uuid/archive', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => chatController.archiveConversation(req, res));
1212
1435
  /**
1213
1436
  * @swagger
1214
1437
  * /api/knowledge/citations/{uuid}/context:
@@ -1278,7 +1501,7 @@ function setupRoutes() {
1278
1501
  * 500:
1279
1502
  * description: Internal server error
1280
1503
  */
1281
- app.delete('/api/conversations/:uuid', (0, auth_1.authenticateUser)(supabase), (req, res) => chatController.deleteConversation(req, res));
1504
+ app.delete('/api/conversations/:uuid', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => chatController.deleteConversation(req, res));
1282
1505
  /**
1283
1506
  * @swagger
1284
1507
  * /api/conversations/{uuid}/messages:
@@ -1592,7 +1815,7 @@ function setupRoutes() {
1592
1815
  * 500:
1593
1816
  * description: Internal server error
1594
1817
  */
1595
- app.put('/api/knowledge/items/:uuid', (0, auth_1.authenticateUser)(supabase), (req, res) => knowledgeController.updateItem(req, res));
1818
+ app.put('/api/knowledge/items/:uuid', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => knowledgeController.updateItem(req, res));
1596
1819
  /**
1597
1820
  * @swagger
1598
1821
  * /api/knowledge/items/{uuid}:
@@ -1619,7 +1842,7 @@ function setupRoutes() {
1619
1842
  * 500:
1620
1843
  * description: Internal server error
1621
1844
  */
1622
- app.delete('/api/knowledge/items/:uuid', (0, auth_1.authenticateUser)(supabase), (req, res) => knowledgeController.deleteItem(req, res));
1845
+ app.delete('/api/knowledge/items/:uuid', (0, auth_1.authenticateUser)(supabase), roleGuard_1.requireAdmin, (req, res) => knowledgeController.deleteItem(req, res));
1623
1846
  // ============================================================================
1624
1847
  // MIGRATION ENDPOINTS
1625
1848
  // ============================================================================
@@ -1901,16 +2124,20 @@ function setupRoutes() {
1901
2124
  * @swagger
1902
2125
  * /api/generate-key:
1903
2126
  * post:
1904
- * summary: Generate API key for the default admin
1905
- * description: Generates an API key for the default admin user's company
2127
+ * summary: Generate API key
2128
+ * description: |
2129
+ * Generates an API key for a company. Supports two authentication methods:
2130
+ * - Bearer token: For authenticated admin users, generates key for their company
2131
+ * - Migration key: For Vercel deployments, generates key for default admin's company
1906
2132
  * tags: [System]
1907
2133
  * security:
2134
+ * - bearerAuth: []
1908
2135
  * - migrationKey: []
1909
2136
  * parameters:
1910
2137
  * - in: query
1911
2138
  * name: key
1912
- * description: Migration secret key
1913
- * required: true
2139
+ * description: Migration secret key (alternative to Bearer token)
2140
+ * required: false
1914
2141
  * schema:
1915
2142
  * type: string
1916
2143
  * responses:
@@ -1929,7 +2156,11 @@ function setupRoutes() {
1929
2156
  * example: "API key generated successfully"
1930
2157
  * api_key_details:
1931
2158
  * type: object
2159
+ * description: API key details
1932
2160
  * properties:
2161
+ * uuid:
2162
+ * type: string
2163
+ * example: "123e4567-e89b-12d3-a456-426614174000"
1933
2164
  * company_name:
1934
2165
  * type: string
1935
2166
  * example: "Vezlo"
@@ -1940,7 +2171,7 @@ function setupRoutes() {
1940
2171
  * type: string
1941
2172
  * example: "v.bzkO2h7Ga.c5MGe0zX-2CU-IeZPqreT6xSRCgq3Tw"
1942
2173
  * 401:
1943
- * description: Unauthorized
2174
+ * description: Unauthorized - Invalid or missing authentication
1944
2175
  * content:
1945
2176
  * application/json:
1946
2177
  * schema:
@@ -1955,47 +2186,27 @@ function setupRoutes() {
1955
2186
  * error:
1956
2187
  * type: string
1957
2188
  * example: "UNAUTHORIZED"
2189
+ * 403:
2190
+ * description: Forbidden - Only admin users can generate API keys
2191
+ * content:
2192
+ * application/json:
2193
+ * schema:
2194
+ * type: object
2195
+ * properties:
2196
+ * success:
2197
+ * type: boolean
2198
+ * example: false
2199
+ * message:
2200
+ * type: string
2201
+ * example: "Only admin users can generate API keys"
2202
+ * error:
2203
+ * type: string
2204
+ * example: "FORBIDDEN"
1958
2205
  * 500:
1959
2206
  * description: Failed to generate API key
1960
2207
  */
1961
2208
  app.post('/api/generate-key', (0, errorHandler_1.asyncHandler)(async (req, res) => {
1962
- // Extract API key from query or header
1963
- const apiKey = req.query.key || req.headers['x-migration-key'];
1964
- try {
1965
- // Validate API key
1966
- const { MigrationService } = await Promise.resolve().then(() => __importStar(require('./services/MigrationService')));
1967
- const keyValid = MigrationService.validateApiKey(apiKey);
1968
- if (!keyValid) {
1969
- res.status(401).json({
1970
- success: false,
1971
- message: 'Invalid or missing migration API key',
1972
- error: 'UNAUTHORIZED'
1973
- });
1974
- return;
1975
- }
1976
- // Initialize Supabase
1977
- const supabase = (0, supabase_js_1.createClient)(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_KEY);
1978
- // Execute generate-key using SetupService
1979
- const { SetupService } = await Promise.resolve().then(() => __importStar(require('./services/SetupService')));
1980
- const setupService = new SetupService(supabase);
1981
- const response = await setupService.executeGenerateKey();
1982
- res.status(200).json({
1983
- success: true,
1984
- message: 'API key generated successfully',
1985
- api_key_details: response
1986
- });
1987
- }
1988
- catch (error) {
1989
- logger_1.default.error('Generate key failed:', error);
1990
- res.status(500).json({
1991
- success: false,
1992
- message: 'Failed to generate API key',
1993
- error: error.message || 'GENERATE_KEY_FAILED',
1994
- details: {
1995
- error: error.message
1996
- }
1997
- });
1998
- }
2209
+ await generateKeyController.generateKey(req, res);
1999
2210
  }));
2000
2211
  // Slack Integration Routes
2001
2212
  /**