Parcourir la source

update use token keycloak all routes

pearlgw il y a 2 mois
Parent
commit
32ee3094b2

+ 1 - 1
src/controllers/admin/HospitalController.js

@@ -13,7 +13,7 @@ exports.getAllHospital = async (req, res) => {
13 13
             page, limit, search, sortBy, orderBy, province, city, type, ownership, progress_status
14 14
         });
15 15
 
16
-        return HospitalCollection(req, res, hospitals, total, page, limit, 'Hospital data successfully retrieved');
16
+        return HospitalCollection({ req, res, data: hospitals, total, page, limit, message: 'Hospital data successfully retrieved' });
17 17
     } catch (err) {
18 18
         return errorResponse(res, err);
19 19
     }

+ 1 - 1
src/controllers/admin/StatusHistoryController.js

@@ -12,7 +12,7 @@ exports.getAllStatusHistory = async (req, res) => {
12 12
             page, limit, search, sortBy, orderBy
13 13
         }, req);
14 14
 
15
-        return StatusHistoryCollection(req, res, status_histories, total, page, limit, 'Status history successfully retrieved');
15
+        return StatusHistoryCollection({ req, res, data: status_histories, total, page, limit, message: 'Status history successfully retrieved' });
16 16
     } catch (err) {
17 17
         return errorResponse(res, err);
18 18
     }

+ 2 - 2
src/controllers/sales/StatusHistoryController.js

@@ -1,7 +1,7 @@
1 1
 const { PaginationParam } = require("../../utils/PaginationParams");
2 2
 const statusHistoryService = require('../../services/sales/StatusHistoryService.js');
3 3
 const { validateCreateStatusHisotryRequest } = require("../../validators/admin/status_history/StatusHistoryValidators.js");
4
-const { StatusHistoryCollection } = require("../../resources/admin/status_history/StatusHistoryCollection.js");
4
+const { StatusHistoryCollection } = require("../../resources/sales/status_history/StatusHistoryCollection.js");
5 5
 const { errorResponse, messageSuccessResponse } = require("../../utils/Response.js");
6 6
 
7 7
 exports.getAllStatusHistory = async (req, res) => {
@@ -12,7 +12,7 @@ exports.getAllStatusHistory = async (req, res) => {
12 12
             page, limit, search, sortBy, orderBy
13 13
         }, req);
14 14
 
15
-        return StatusHistoryCollection(req, res, status_histories, total, page, limit, 'Status history successfully retrieved');
15
+        return StatusHistoryCollection({ req, res, data: status_histories, total, page, limit, message: 'Status history successfully retrieved' });
16 16
     } catch (err) {
17 17
         return errorResponse(res, err);
18 18
     }

+ 2 - 12
src/repository/admin/ExecutivesHistoryRepository.js

@@ -34,12 +34,7 @@ const ExecutivesHistoryRepository = {
34 34
                         image: true,
35 35
                         progress_status: true,
36 36
                         note: true,
37
-                        user: {
38
-                            select: {
39
-                                id: true,
40
-                                username: true
41
-                            }
42
-                        }
37
+                        created_by: true
43 38
                     }
44 39
                 },
45 40
                 executive_name: true,
@@ -90,12 +85,7 @@ const ExecutivesHistoryRepository = {
90 85
                         image: true,
91 86
                         progress_status: true,
92 87
                         note: true,
93
-                        user: {
94
-                            select: {
95
-                                id: true,
96
-                                username: true
97
-                            }
98
-                        }
88
+                        created_by: true
99 89
                     }
100 90
                 },
101 91
                 executive_name: true,

+ 2 - 2
src/repository/admin/HospitalRepository.js

@@ -24,7 +24,7 @@ const HospitalRepository = {
24 24
                 latitude: true,
25 25
                 longitude: true,
26 26
                 gmaps_url: true,
27
-                user: { select: { id: true, username: true } },
27
+                created_by: true,
28 28
                 createdAt: true,
29 29
                 updatedAt: true,
30 30
                 // vendor_histories: {
@@ -74,7 +74,7 @@ const HospitalRepository = {
74 74
                 latitude: true,
75 75
                 longitude: true,
76 76
                 gmaps_url: true,
77
-                user: { select: { id: true, username: true } },
77
+                created_by: true,
78 78
                 createdAt: true,
79 79
                 updatedAt: true,
80 80
                 // vendor_histories: {

+ 1 - 6
src/repository/admin/StatusHistoryRepository.js

@@ -42,12 +42,7 @@ const StatusHistoryRepository = {
42 42
                         // }
43 43
                     }
44 44
                 },
45
-                user: {
46
-                    select: {
47
-                        id: true,
48
-                        username: true,
49
-                    }
50
-                },
45
+                user_id: true,
51 46
                 old_status: true,
52 47
                 new_status: true,
53 48
                 note: true,

+ 4 - 24
src/repository/admin/VendorHistoryRepository.js

@@ -34,12 +34,7 @@ const VendorHistoryRepository = {
34 34
                         image: true,
35 35
                         progress_status: true,
36 36
                         note: true,
37
-                        user: {
38
-                            select: {
39
-                                id: true,
40
-                                username: true
41
-                            }
42
-                        }
37
+                        created_by: true
43 38
                     }
44 39
                 },
45 40
                 vendor: {
@@ -50,12 +45,7 @@ const VendorHistoryRepository = {
50 45
                         strengths: true,
51 46
                         weaknesses: true,
52 47
                         website: true,
53
-                        user: {
54
-                            select: {
55
-                                id: true,
56
-                                username: true
57
-                            }
58
-                        }
48
+                        created_by: true,
59 49
                     }
60 50
                 },
61 51
                 vendor_impression: true,
@@ -105,12 +95,7 @@ const VendorHistoryRepository = {
105 95
                         image: true,
106 96
                         progress_status: true,
107 97
                         note: true,
108
-                        user: {
109
-                            select: {
110
-                                id: true,
111
-                                username: true
112
-                            }
113
-                        }
98
+                        created_by: true
114 99
                     }
115 100
                 },
116 101
                 vendor: {
@@ -121,12 +106,7 @@ const VendorHistoryRepository = {
121 106
                         strengths: true,
122 107
                         weaknesses: true,
123 108
                         website: true,
124
-                        user: {
125
-                            select: {
126
-                                id: true,
127
-                                username: true
128
-                            }
129
-                        }
109
+                        created_by: true,
130 110
                     }
131 111
                 },
132 112
                 vendor_impression: true,

+ 8 - 12
src/repository/sales/ExecutivesHistoryRepository.js

@@ -34,12 +34,13 @@ const ExecutivesHistoryRepository = {
34 34
                         image: true,
35 35
                         progress_status: true,
36 36
                         note: true,
37
-                        user: {
38
-                            select: {
39
-                                id: true,
40
-                                username: true
41
-                            }
42
-                        }
37
+                        // user: {
38
+                        //     select: {
39
+                        //         id: true,
40
+                        //         username: true
41
+                        //     }
42
+                        // }
43
+                        created_by: true
43 44
                     }
44 45
                 },
45 46
                 executive_name: true,
@@ -90,12 +91,7 @@ const ExecutivesHistoryRepository = {
90 91
                         image: true,
91 92
                         progress_status: true,
92 93
                         note: true,
93
-                        user: {
94
-                            select: {
95
-                                id: true,
96
-                                username: true
97
-                            }
98
-                        }
94
+                        created_by: true
99 95
                     }
100 96
                 },
101 97
                 executive_name: true,

+ 7 - 6
src/repository/sales/StatusHistoryRepository.js

@@ -42,12 +42,13 @@ const StatusHistoryRepository = {
42 42
                         // }
43 43
                     }
44 44
                 },
45
-                user: {
46
-                    select: {
47
-                        id: true,
48
-                        username: true,
49
-                    }
50
-                },
45
+                // user: {
46
+                //     select: {
47
+                //         id: true,
48
+                //         username: true,
49
+                //     }
50
+                // },
51
+                user_id: true,
51 52
                 old_status: true,
52 53
                 new_status: true,
53 54
                 note: true,

+ 4 - 24
src/repository/sales/VendorHistoryRepository.js

@@ -34,12 +34,7 @@ const VendorHistoryRepository = {
34 34
                         image: true,
35 35
                         progress_status: true,
36 36
                         note: true,
37
-                        user: {
38
-                            select: {
39
-                                id: true,
40
-                                username: true
41
-                            }
42
-                        }
37
+                        created_by: true
43 38
                     }
44 39
                 },
45 40
                 vendor: {
@@ -50,12 +45,7 @@ const VendorHistoryRepository = {
50 45
                         strengths: true,
51 46
                         weaknesses: true,
52 47
                         website: true,
53
-                        user: {
54
-                            select: {
55
-                                id: true,
56
-                                username: true
57
-                            }
58
-                        }
48
+                        created_by: true,
59 49
                     }
60 50
                 },
61 51
                 vendor_impression: true,
@@ -105,12 +95,7 @@ const VendorHistoryRepository = {
105 95
                         image: true,
106 96
                         progress_status: true,
107 97
                         note: true,
108
-                        user: {
109
-                            select: {
110
-                                id: true,
111
-                                username: true
112
-                            }
113
-                        }
98
+                        created_by: true
114 99
                     }
115 100
                 },
116 101
                 vendor: {
@@ -121,12 +106,7 @@ const VendorHistoryRepository = {
121 106
                         strengths: true,
122 107
                         weaknesses: true,
123 108
                         website: true,
124
-                        user: {
125
-                            select: {
126
-                                id: true,
127
-                                username: true
128
-                            }
129
-                        }
109
+                        created_by: true,
130 110
                     }
131 111
                 },
132 112
                 vendor_impression: true,

+ 20 - 15
src/resources/admin/hospital/HospitalCollection.js

@@ -1,22 +1,27 @@
1 1
 const { ListResponse } = require("../../../utils/ListResponse");
2 2
 const { formatISOWithoutTimezone } = require("../../../utils/FormatDate.js");
3
+const { getUserNameById } = require("../../../utils/CheckUserKeycloak.js");
3 4
 
4
-const formatItem = (item) => ({
5
-    ...item,
6
-    createdAt: formatISOWithoutTimezone(item.createdAt),
7
-    updatedAt: formatISOWithoutTimezone(item.updatedAt)
8
-});
5
+// Fungsi transform per item
6
+const transformHospitalList = async (data = []) => {
7
+    return Promise.all(data.map(async ({ created_by, ...rest }) => {
8
+        const name = await getUserNameById(created_by);
9 9
 
10
-exports.HospitalCollection = (req, res, data = [], total = null, page = 1, limit = 10, message = 'Success') => {
11
-    const formattedData = data.map(formatItem);
10
+        return {
11
+            ...rest,
12
+            user: {
13
+                id: created_by,
14
+                name: name
15
+            },
16
+            createdAt: formatISOWithoutTimezone(rest.createdAt),
17
+            updatedAt: formatISOWithoutTimezone(rest.updatedAt)
18
+        };
19
+    }));
20
+};
12 21
 
13
-    if (typeof total !== 'number') {
14
-        return res.status(200).json({
15
-            success: true,
16
-            message,
17
-            data: Array.isArray(formattedData)
18
-        });
19
-    }
22
+// Collection yang async
23
+exports.HospitalCollection = async ({ req, res, data = [], total = 0, page = 1, limit = 10, message = 'Success' }) => {
24
+    const formatted = await transformHospitalList(data);
20 25
 
21
-    return ListResponse({ req, res, data: formattedData, total, page, limit, message });
26
+    return ListResponse({ req, res, data: formatted, total, page, limit, message });
22 27
 };

+ 20 - 16
src/resources/admin/status_history/StatusHistoryCollection.js

@@ -1,23 +1,27 @@
1 1
 const { ListResponse } = require("../../../utils/ListResponse");
2 2
 const { formatISOWithoutTimezone } = require("../../../utils/FormatDate.js");
3
+const { getUserNameById } = require("../../../utils/CheckUserKeycloak.js");
3 4
 
4
-const formatItem = (item) => ({
5
-    ...item,
6
-    note: item.note?.trim() === '' ? null : item.note,
7
-    createdAt: formatISOWithoutTimezone(item.createdAt),
8
-    updatedAt: formatISOWithoutTimezone(item.updatedAt)
9
-});
5
+const transformStatusHistoryList = async (data = []) => {
6
+    return Promise.all(data.map(async ({ user_id, ...rest }) => {
7
+        const name = await getUserNameById(user_id);
10 8
 
11
-exports.StatusHistoryCollection = (req, res, data = [], total = null, page = 1, limit = 10, message = 'Success') => {
12
-    const formattedData = data.map(formatItem);
9
+        return {
10
+            ...rest,
11
+            user: {
12
+                id: user_id,
13
+                name: name
14
+            },
15
+            note: rest.note?.trim() === '' ? null : rest.note,
16
+            createdAt: formatISOWithoutTimezone(rest.createdAt),
17
+            updatedAt: formatISOWithoutTimezone(rest.updatedAt)
18
+        };
19
+    }));
20
+};
13 21
 
14
-    if (typeof total !== 'number') {
15
-        return res.status(200).json({
16
-            success: true,
17
-            message,
18
-            data: Array.isArray(formattedData)
19
-        });
20
-    }
22
+// Collection yang async
23
+exports.StatusHistoryCollection = async ({ req, res, data = [], total = 0, page = 1, limit = 10, message = 'Success' }) => {
24
+    const formatted = await transformStatusHistoryList(data);
21 25
 
22
-    return ListResponse({ req, res, data: formattedData, total, page, limit, message });
26
+    return ListResponse({ req, res, data: formatted, total, page, limit, message });
23 27
 };

+ 27 - 0
src/resources/sales/status_history/StatusHistoryCollection.js

@@ -0,0 +1,27 @@
1
+const { ListResponse } = require("../../../utils/ListResponse");
2
+const { formatISOWithoutTimezone } = require("../../../utils/FormatDate.js");
3
+const { getUserNameById } = require("../../../utils/CheckUserKeycloak.js");
4
+
5
+const transformStatusHistoryList = async (data = []) => {
6
+    return Promise.all(data.map(async ({ user_id, ...rest }) => {
7
+        const name = await getUserNameById(user_id);
8
+
9
+        return {
10
+            ...rest,
11
+            user: {
12
+                id: user_id,
13
+                name: name
14
+            },
15
+            note: rest.note?.trim() === '' ? null : rest.note,
16
+            createdAt: formatISOWithoutTimezone(rest.createdAt),
17
+            updatedAt: formatISOWithoutTimezone(rest.updatedAt)
18
+        };
19
+    }));
20
+};
21
+
22
+// Collection yang async
23
+exports.StatusHistoryCollection = async ({ req, res, data = [], total = 0, page = 1, limit = 10, message = 'Success' }) => {
24
+    const formatted = await transformStatusHistoryList(data);
25
+
26
+    return ListResponse({ req, res, data: formatted, total, page, limit, message });
27
+};

+ 3 - 2
src/routes/admin/HospitalRoute.js

@@ -3,6 +3,7 @@ const router = express.Router()
3 3
 const hospitalController = require('../../controllers/admin/HospitalController.js')
4 4
 const vendorHistoryController = require('../../controllers/admin/VendorHistoryController.js')
5 5
 const executivesHistoryController = require('../../controllers/admin/ExecutivesHistoryController.js')
6
+const statusHistoriesController = require('../../controllers/admin/StatusHistoryController.js')
6 7
 // const verifyJWT = require('../../middleware/VerifyJWT.js');
7 8
 // const checkRole = require('../../middleware/CheckRole.js');
8 9
 const upload = require('../../middleware/UploadImage.js');
@@ -52,7 +53,7 @@ router.patch('/:id/executives-history/:id_executives_history', [keycloak.protect
52 53
 router.delete('/:id/executives-history/:id_executives_history', [keycloak.protect(), extractToken, checkRoles(["admin"])], executivesHistoryController.deleteExecutivesHistory);
53 54
 
54 55
 // Status History
55
-router.get('/:id/status-histories', verifyJWT, checkRole(['admin']), statusHistoriesController.getAllStatusHistory);
56
-router.post('/:id/status-histories', verifyJWT, checkRole(['admin']), statusHistoriesController.storeStatusHistory);
56
+router.get('/:id/status-histories', [keycloak.protect(), extractToken, checkRoles(["admin"])], statusHistoriesController.getAllStatusHistory);
57
+router.post('/:id/status-histories', [keycloak.protect(), extractToken, checkRoles(["admin"])], statusHistoriesController.storeStatusHistory);
57 58
 
58 59
 module.exports = router;

+ 3 - 2
src/routes/sales/HospitalRoute.js

@@ -3,6 +3,7 @@ const router = express.Router()
3 3
 const hospitalController = require('../../controllers/sales/HospitalController.js')
4 4
 const vendorHistoryController = require('../../controllers/sales/VendorHistoryController.js')
5 5
 const executivesHistoryController = require('../../controllers/sales/ExecutivesHistoryController.js')
6
+const statusHistoriesController = require('../../controllers/sales/StatusHistoryController.js')
6 7
 // const verifyJWT = require('../../middleware/VerifyJWT.js');
7 8
 // const checkRole = require('../../middleware/CheckRole.js');
8 9
 const upload = require('../../middleware/UploadImage.js');
@@ -49,7 +50,7 @@ router.get('/:id/executives-history/:id_executives_history', [keycloak.protect()
49 50
 router.patch('/:id/executives-history/:id_executives_history', [keycloak.protect(), extractToken, checkRoles(['sales'])], executivesHistoryController.updateExecutivesHistory);
50 51
 router.delete('/:id/executives-history/:id_executives_history', [keycloak.protect(), extractToken, checkRoles(['sales'])], executivesHistoryController.deleteExecutivesHistory);
51 52
 
52
-router.get('/:id/status-history', verifyJWT, checkRole(['sales']), statusHistoryController.getAllStatusHistory);
53
-router.post('/:id/status-history', verifyJWT, checkRole(['sales']), statusHistoryController.storeStatusHistory);
53
+router.get('/:id/status-history', [keycloak.protect(), extractToken, checkRoles(["sales"])], statusHistoriesController.getAllStatusHistory);
54
+router.post('/:id/status-history', [keycloak.protect(), extractToken, checkRoles(["sales"])], statusHistoriesController.storeStatusHistory);
54 55
 
55 56
 module.exports = router;

+ 2 - 5
src/services/admin/StatusHistoryService.js

@@ -1,9 +1,6 @@
1 1
 const HttpException = require('../../utils/HttpException.js');
2 2
 const prisma = require('../../prisma/PrismaClient.js');
3
-const { SearchFilter } = require('../../utils/SearchFilter.js');
4
-const timeLocal = require('../../utils/TimeLocal.js');
5
-const { createLog, updateLog, deleteLog } = require('../../utils/LogActivity.js');
6
-const { formatDateOnly, formatISOWithoutTimezone } = require('../../utils/FormatDate.js');
3
+const { createLog } = require('../../utils/LogActivity.js');
7 4
 const StatusHistoryRepository = require('../../repository/admin/StatusHistoryRepository.js');
8 5
 
9 6
 exports.getAllStatusHistoryService = async ({ page, limit, search, sortBy, orderBy }, req) => {
@@ -36,7 +33,7 @@ const validProgressStatuses = ['cari_data', 'dihubungi', 'negosiasi', 'follow_up
36 33
 
37 34
 exports.storeStatusHistoryService = async (validateData, req) => {
38 35
     const hospitalId = req.params.id;
39
-    const userId = req.user.id;
36
+    const userId = req.tokenData.sub;
40 37
 
41 38
     const hospital = await prisma.hospital.findFirst({
42 39
         where: {

+ 2 - 2
src/services/sales/StatusHistoryService.js

@@ -6,7 +6,7 @@ const StatusHistoryRepository = require('../../repository/sales/StatusHistoryRep
6 6
 exports.getAllStatusHistoryService = async ({ page, limit, search, sortBy, orderBy }, req) => {
7 7
     const skip = (page - 1) * limit;
8 8
 
9
-    const userId = req.user.id;
9
+    const userId = req.tokenData.sub;
10 10
     const hospitalId = req.params.id;
11 11
     const hospital = await prisma.hospital.findFirst({
12 12
         where: {
@@ -45,7 +45,7 @@ const validProgressStatuses = ['cari_data', 'dihubungi', 'negosiasi', 'follow_up
45 45
 
46 46
 exports.storeStatusHistoryService = async (validateData, req) => {
47 47
     const hospitalId = req.params.id;
48
-    const userId = req.user.id;
48
+    const userId = req.tokenData.sub;
49 49
 
50 50
     const hospital = await prisma.hospital.findFirst({
51 51
         where: {