Browse Source

update validate executive history & vendor history

pearlgw 2 months ago
parent
commit
cd6740f9b9

+ 2 - 2
src/controllers/admin/ExecutivesHistoryController.js

@@ -3,7 +3,7 @@ const { ExecutivesHistoriResource } = require('../../resources/admin/executives_
3 3
 const executivesHistoryService = require('../../services/admin/ExecutivesHistoryService.js');
4 4
 const { PaginationParam } = require('../../utils/PaginationParams.js');
5 5
 const { errorResponse, messageSuccessResponse } = require('../../utils/Response.js');
6
-const { validateStoreExecutivesHistoryRequest } = require('../../validators/sales/executives_history/ExecutivesHistoriValidators.js');
6
+const { validateStoreExecutivesHistoryRequest, validateUpdateExecutivesHistoryRequest } = require('../../validators/admin/executives_history/ExecutivesHistoriValidators.js');
7 7
 
8 8
 exports.getAllExecutivesHistory = async (req, res) => {
9 9
     try {
@@ -40,7 +40,7 @@ exports.storeExecutivesHistory = async (req, res) => {
40 40
 
41 41
 exports.updateExecutivesHistory = async (req, res) => {
42 42
     try {
43
-        const validatedData = validateStoreExecutivesHistoryRequest(req.body);
43
+        const validatedData = validateUpdateExecutivesHistoryRequest(req.body);
44 44
         await executivesHistoryService.updateExecutivesHistoryService(validatedData, req);
45 45
         return messageSuccessResponse(res, 'Success update executives history');
46 46
     } catch (err) {

+ 2 - 2
src/controllers/admin/VendorHistoryController.js

@@ -3,7 +3,7 @@ const { VendorHistoriResource } = require('../../resources/admin/vendor_history/
3 3
 const vendorHistoryService = require('../../services/admin/VendorHistoryService.js');
4 4
 const { PaginationParam } = require('../../utils/PaginationParams.js');
5 5
 const { errorResponse, messageSuccessResponse } = require('../../utils/Response.js');
6
-const { validateStoreVendorHistoryRequest } = require('../../validators/sales/vendor_history/VendorHistoriValidators.js');
6
+const { validateStoreVendorHistoryRequest, validateUpdateVendorHistoryRequest } = require('../../validators/admin/vendor_history/VendorHistoriValidators.js');
7 7
 
8 8
 exports.getAllVendorHistory = async (req, res) => {
9 9
     try {
@@ -41,7 +41,7 @@ exports.storeVendorHistory = async (req, res) => {
41 41
 
42 42
 exports.updateVendorHistory = async (req, res) => {
43 43
     try {
44
-        const validatedData = validateStoreVendorHistoryRequest(req.body);
44
+        const validatedData = validateUpdateVendorHistoryRequest(req.body);
45 45
         await vendorHistoryService.updateVendorHistoryService(validatedData, req);
46 46
         return messageSuccessResponse(res, 'Success update vendor history');
47 47
     } catch (err) {

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

@@ -3,7 +3,7 @@ const { ExecutivesHistoriCollection } = require('../../resources/sales/executive
3 3
 const executivesHistoryService = require('../../services/sales/ExecutivesHistoryService.js');
4 4
 const { PaginationParam } = require('../../utils/PaginationParams.js');
5 5
 const { errorResponse, messageSuccessResponse } = require('../../utils/Response.js');
6
-const { validateStoreExecutivesHistoryRequest } = require('../../validators/admin/executives_history/ExecutivesHistoriValidators.js');
6
+const { validateStoreExecutivesHistoryRequest, validateUpdateExecutivesHistoryRequest } = require('../../validators/sales/executives_history/ExecutivesHistoriValidators.js');
7 7
 
8 8
 exports.getAllExecutivesHistory = async (req, res) => {
9 9
     try {
@@ -40,7 +40,7 @@ exports.storeExecutivesHistory = async (req, res) => {
40 40
 
41 41
 exports.updateExecutivesHistory = async (req, res) => {
42 42
     try {
43
-        const validatedData = validateStoreExecutivesHistoryRequest(req.body);
43
+        const validatedData = validateUpdateExecutivesHistoryRequest(req.body);
44 44
         await executivesHistoryService.updateExecutivesHistoryService(validatedData, req);
45 45
         return messageSuccessResponse(res, 'Success update executives history');
46 46
     } catch (err) {

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

@@ -3,7 +3,7 @@ const { VendorHistoriResource } = require('../../resources/sales/vendor_history/
3 3
 const vendorHistoryService = require('../../services/sales/VendorHistoryService.js');
4 4
 const { PaginationParam } = require('../../utils/PaginationParams.js');
5 5
 const { errorResponse, messageSuccessResponse } = require('../../utils/Response.js');
6
-const { validateStoreVendorHistoryRequest } = require('../../validators/admin/vendor_history/VendorHistoriValidators.js');
6
+const { validateStoreVendorHistoryRequest, validateUpdateVendorHistoryRequest } = require('../../validators/sales/vendor_history/VendorHistoriValidators.js');
7 7
 
8 8
 exports.getAllVendorHistory = async (req, res) => {
9 9
     try {
@@ -41,7 +41,7 @@ exports.storeVendorHistory = async (req, res) => {
41 41
 
42 42
 exports.updateVendorHistory = async (req, res) => {
43 43
     try {
44
-        const validatedData = validateStoreVendorHistoryRequest(req.body);
44
+        const validatedData = validateUpdateVendorHistoryRequest(req.body);
45 45
         await vendorHistoryService.updateVendorHistoryService(validatedData, req);
46 46
         return messageSuccessResponse(res, 'Success update vendor history');
47 47
     } catch (err) {

+ 9 - 1
src/services/admin/ExecutivesHistoryService.js

@@ -93,8 +93,16 @@ exports.updateExecutivesHistoryService = async (validateData, req) => {
93 93
         throw new HttpException("Executives history not found", 404);
94 94
     }
95 95
 
96
+    if (validateData.start_term && validateData.end_term) {
97
+        if (validateData.start_term >= validateData.end_term) {
98
+            throw new HttpException("End term must be after start_term", 400)
99
+        }
100
+    }
101
+
96 102
     const payload = {
97
-        ...validateData
103
+        ...validateData,
104
+        start_term: validateData.start_term ? new Date(validateData.start_term) : executivesHistory.start_term,
105
+        end_term: validateData.end_term ? new Date(validateData.end_term) : executivesHistory.end_term,
98 106
     };
99 107
 
100 108
     const data = await ExecutivesHistoryRepository.update(id_executives_history, payload);

+ 16 - 8
src/services/admin/VendorHistoryService.js

@@ -104,19 +104,27 @@ exports.updateVendorHistoryService = async (validateData, req) => {
104 104
         throw new HttpException("Vendor history not found", 404);
105 105
     }
106 106
 
107
-    await prisma.hospital.update({
108
-        where: { id: id_hospital },
109
-        data: {
110
-            simrs_type: validateData.simrs_type
111
-        }
112
-    });
107
+    if (validateData.simrs_type) {
108
+        await prisma.hospital.update({
109
+            where: { id: id_hospital },
110
+            data: {
111
+                simrs_type: validateData.simrs_type
112
+            },
113
+        });
114
+    }
113 115
 
116
+    if (validateData.contract_date && validateData.contract_expired_date) {
117
+        if (validateData.contract_date >= validateData.contract_expired_date) {
118
+            throw new HttpException("Contract expired date must be after contract date", 400)
119
+        }
120
+    }
121
+    
114 122
     const payload = {
115 123
         vendor_id: validateData.vendor_id,
116 124
         vendor_impression: validateData.vendor_impression,
117 125
         status: validateData.status,
118
-        contract_date: validateData.contract_date,
119
-        contract_expired_date: validateData.contract_expired_date
126
+        contract_date: validateData.contract_date ? new Date(validateData.contract_date) : vendor.contract_date,
127
+        contract_expired_date: validateData.contract_expired_date ? new Date(validateData.contract_expired_date) : vendor.contract_expired_date,
120 128
     };
121 129
 
122 130
     const data = await VendorHistoryRepository.update(id_vendor_history, payload);

+ 9 - 1
src/services/sales/ExecutivesHistoryService.js

@@ -92,8 +92,16 @@ exports.updateExecutivesHistoryService = async (validateData, req) => {
92 92
         throw new HttpException("Executives history not found", 404);
93 93
     }
94 94
 
95
+    if (validateData.start_term && validateData.end_term) {
96
+        if (validateData.start_term >= validateData.end_term) {
97
+            throw new HttpException("End term must be after start_term", 400)
98
+        }
99
+    }
100
+
95 101
     const payload = {
96
-        ...validateData
102
+        ...validateData,
103
+        start_term: validateData.start_term ? new Date(validateData.start_term) : executivesHistory.start_term,
104
+        end_term: validateData.end_term ? new Date(validateData.end_term) : executivesHistory.end_term,
97 105
     };
98 106
 
99 107
     const data = await ExecutivesHistoryRepository.update(id_executives_history, payload);

+ 8 - 2
src/services/sales/VendorHistoryService.js

@@ -111,12 +111,18 @@ exports.updateVendorHistoryService = async (validateData, req) => {
111 111
         }
112 112
     });
113 113
 
114
+    if (validateData.contract_date && validateData.contract_expired_date) {
115
+        if (validateData.contract_date >= validateData.contract_expired_date) {
116
+            throw new HttpException("Contract expired date must be after contract date", 400)
117
+        }
118
+    }
119
+
114 120
     const payload = {
115 121
         vendor_id: validateData.vendor_id,
116 122
         vendor_impression: validateData.vendor_impression,
117 123
         status: validateData.status,
118
-        contract_date: validateData.contract_date,
119
-        contract_expired_date: validateData.contract_expired_date
124
+        contract_date: validateData.contract_date ? new Date(validateData.contract_date) : vendor.contract_date,
125
+        contract_expired_date: validateData.contract_expired_date ? new Date(validateData.contract_expired_date) : vendor.contract_expired_date,
120 126
     };
121 127
 
122 128
     const data = await VendorHistoryRepository.update(id_vendor_history, payload);

+ 12 - 1
src/validators/admin/executives_history/ExecutivesHistoriValidators.js

@@ -74,4 +74,15 @@ exports.validateStoreExecutivesHistoryRequest = (body) => {
74 74
         start_term: start_term ? new Date(start_term) : null,
75 75
         end_term: end_term ? new Date(end_term) : null,
76 76
     };
77
-};
77
+};
78
+
79
+exports.validateUpdateExecutivesHistoryRequest = (body) => {
80
+    const { executive_name, contact, status, start_term, end_term } = body;
81
+    return {
82
+        executive_name,
83
+        contact,
84
+        status,
85
+        start_term,
86
+        end_term,
87
+    };
88
+}

+ 14 - 1
src/validators/admin/vendor_history/VendorHistoriValidators.js

@@ -70,4 +70,17 @@ exports.validateStoreVendorHistoryRequest = (body) => {
70 70
         contract_date: contract_date ? new Date(contract_date) : null,
71 71
         contract_expired_date: contract_expired_date ? new Date(contract_expired_date) : null,
72 72
     };
73
-};
73
+};
74
+
75
+exports.validateUpdateVendorHistoryRequest = (body) => {
76
+    const { simrs_type, vendor_id, vendor_impression, status, contract_date, contract_expired_date } = body;
77
+
78
+    return {
79
+        simrs_type,
80
+        vendor_id,
81
+        vendor_impression,
82
+        status,
83
+        contract_date,
84
+        contract_expired_date
85
+    };
86
+}

+ 13 - 2
src/validators/sales/executives_history/ExecutivesHistoriValidators.js

@@ -60,7 +60,7 @@ exports.validateStoreExecutivesHistoryRequest = (body) => {
60 60
         !errors.end_term &&
61 61
         new Date(end_term) <= new Date(start_term)
62 62
     ) {
63
-        errors.end_term = ['End term expired date must be after start term date'];
63
+        errors.end_term = ['End term date must be after start term date'];
64 64
     }
65 65
 
66 66
     if (Object.keys(errors).length > 0) {
@@ -74,4 +74,15 @@ exports.validateStoreExecutivesHistoryRequest = (body) => {
74 74
         start_term: start_term ? new Date(start_term) : null,
75 75
         end_term: end_term ? new Date(end_term) : null,
76 76
     };
77
-};
77
+};
78
+
79
+exports.validateUpdateExecutivesHistoryRequest = (body) => {
80
+    const { executive_name, contact, status, start_term, end_term } = body;
81
+    return {
82
+        executive_name,
83
+        contact,
84
+        status,
85
+        start_term,
86
+        end_term,
87
+    };
88
+}

+ 0 - 35
src/validators/sales/vendor/VendorValidators.js

@@ -1,35 +0,0 @@
1
-const HttpException = require('../../../utils/HttpException.js');
2
-
3
-exports.validateStoreVendorRequest = (body) => {
4
-    const { name, name_pt, strengths, weaknesses, website } = body;
5
-
6
-    const errors = {};
7
-
8
-    if (!name || name.trim() === '') {
9
-        errors.name = ['Vendor name is required'];
10
-    }
11
-    // if (!name_pt || name_pt.trim() === '') {
12
-    //     errors.name_pt = ['Vendor name pt is required'];
13
-    // }
14
-    // if (!strengths || strengths.trim() === '') {
15
-    //     errors.strengths = ['Vendor strengths is required'];
16
-    // }
17
-    // if (!weaknesses || weaknesses.trim() === '') {
18
-    //     errors.weaknesses = ['Vendor weaknesses is required'];
19
-    // }
20
-    // if (!website || website.trim() === '') {
21
-    //     errors.website = ['Vendor website is required'];
22
-    // }
23
-
24
-    if (Object.keys(errors).length > 0) {
25
-        throw new HttpException(errors, 422);
26
-    }
27
-
28
-    return {
29
-        name: name.trim(),
30
-        name_pt: name_pt.trim(),
31
-        strengths: strengths.trim(),
32
-        weaknesses: weaknesses.trim(),
33
-        website: website.trim(),
34
-    };
35
-};

+ 14 - 1
src/validators/sales/vendor_history/VendorHistoriValidators.js

@@ -70,4 +70,17 @@ exports.validateStoreVendorHistoryRequest = (body) => {
70 70
         contract_date: contract_date ? new Date(contract_date) : null,
71 71
         contract_expired_date: contract_expired_date ? new Date(contract_expired_date) : null,
72 72
     };
73
-};
73
+};
74
+
75
+exports.validateUpdateVendorHistoryRequest = (body) => {
76
+    const { simrs_type, vendor_id, vendor_impression, status, contract_date, contract_expired_date } = body;
77
+
78
+    return {
79
+        simrs_type,
80
+        vendor_id,
81
+        vendor_impression,
82
+        status,
83
+        contract_date,
84
+        contract_expired_date
85
+    };
86
+}