Parcourir la source

modify vendor

pearlgw il y a 1 mois
Parent
commit
5afb7f0515

+ 55 - 34
src/repository/admin/HospitalRepository.js

@@ -2,7 +2,7 @@ const prisma = require('../../prisma/PrismaClient.js');
2 2
 
3 3
 const HospitalRepository = {
4 4
     findAll: async ({ skip, take, where, orderBy }) => {
5
-        return await prisma.hospital.findMany({
5
+        const hospitalsRaw = await prisma.hospital.findMany({
6 6
             where,
7 7
             skip,
8 8
             take,
@@ -27,24 +27,34 @@ const HospitalRepository = {
27 27
                 created_by: true,
28 28
                 createdAt: true,
29 29
                 updatedAt: true,
30
-                // vendor_histories: {
31
-                //     where: { deletedAt: null },
32
-                //     orderBy: { createdAt: 'desc' },
33
-                //     select: {
34
-                //         vendor: {
35
-                //             select: {
36
-                //                 id: true,
37
-                //                 name: true,
38
-                //                 name_pt: true,
39
-                //                 strengths: true,
40
-                //                 weaknesses: true,
41
-                //                 website: true,
42
-                //             }
43
-                //         }
44
-                //     }
45
-                // }
30
+                vendor_histories: {
31
+                    where: {
32
+                        status: "active",
33
+                        deletedAt: null
34
+                    },
35
+                    select: {
36
+                        vendor: {
37
+                            select: {
38
+                                id: true,
39
+                                name: true,
40
+                                name_pt: true,
41
+                                strengths: true,
42
+                                weaknesses: true,
43
+                                website: true,
44
+                            }
45
+                        }
46
+                    }
47
+                }
46 48
             }
47 49
         });
50
+
51
+        return hospitalsRaw.map(hospital => {
52
+            const { vendor_histories, ...rest } = hospital;
53
+            return {
54
+                ...rest,
55
+                vendor: vendor_histories?.[0]?.vendor || null
56
+            };
57
+        });
48 58
     },
49 59
 
50 60
     countAll: async (where) => {
@@ -52,7 +62,7 @@ const HospitalRepository = {
52 62
     },
53 63
 
54 64
     findById: async (id) => {
55
-        return prisma.hospital.findFirst({
65
+        const hospitalRaw = await prisma.hospital.findFirst({
56 66
             where: {
57 67
                 id,
58 68
                 deletedAt: null
@@ -77,24 +87,35 @@ const HospitalRepository = {
77 87
                 created_by: true,
78 88
                 createdAt: true,
79 89
                 updatedAt: true,
80
-                // vendor_histories: {
81
-                //     where: { deletedAt: null },
82
-                //     orderBy: { createdAt: 'desc' },
83
-                //     select: {
84
-                //         vendor: {
85
-                //             select: {
86
-                //                 id: true,
87
-                //                 name: true,
88
-                //                 name_pt: true,
89
-                //                 strengths: true,
90
-                //                 weaknesses: true,
91
-                //                 website: true,
92
-                //             }
93
-                //         }
94
-                //     }
95
-                // }
90
+                vendor_histories: {
91
+                    where: {
92
+                        status: "active",
93
+                        deletedAt: null
94
+                    },
95
+                    take: 1,
96
+                    select: {
97
+                        vendor: {
98
+                            select: {
99
+                                id: true,
100
+                                name: true,
101
+                                name_pt: true,
102
+                                strengths: true,
103
+                                weaknesses: true,
104
+                                website: true,
105
+                            }
106
+                        }
107
+                    }
108
+                }
96 109
             }
97 110
         });
111
+
112
+        if (!hospitalRaw) return null;
113
+
114
+        const { vendor_histories, ...rest } = hospitalRaw;
115
+        return {
116
+            ...rest,
117
+            vendor: vendor_histories?.[0]?.vendor || null
118
+        };
98 119
     },
99 120
 
100 121
     create: async (data) => {

+ 56 - 56
src/repository/admin/VendorHistoryRepository.js

@@ -9,34 +9,34 @@ const VendorHistoryRepository = {
9 9
             orderBy,
10 10
             select: {
11 11
                 id: true,
12
-                hospital: {
13
-                    select: {
14
-                        id: true,
15
-                        name: true,
16
-                        hospital_code: true,
17
-                        type: true,
18
-                        ownership: true,
19
-                        province: {
20
-                            select: {
21
-                                id: true,
22
-                                name: true
23
-                            }
24
-                        },
25
-                        city: {
26
-                            select: {
27
-                                id: true,
28
-                                name: true
29
-                            }
30
-                        },
31
-                        address: true,
32
-                        simrs_type: true,
33
-                        contact: true,
34
-                        image: true,
35
-                        progress_status: true,
36
-                        note: true,
37
-                        created_by: true
38
-                    }
39
-                },
12
+                // hospital: {
13
+                //     select: {
14
+                //         id: true,
15
+                //         name: true,
16
+                //         hospital_code: true,
17
+                //         type: true,
18
+                //         ownership: true,
19
+                //         province: {
20
+                //             select: {
21
+                //                 id: true,
22
+                //                 name: true
23
+                //             }
24
+                //         },
25
+                //         city: {
26
+                //             select: {
27
+                //                 id: true,
28
+                //                 name: true
29
+                //             }
30
+                //         },
31
+                //         address: true,
32
+                //         simrs_type: true,
33
+                //         contact: true,
34
+                //         image: true,
35
+                //         progress_status: true,
36
+                //         note: true,
37
+                //         created_by: true
38
+                //     }
39
+                // },
40 40
                 vendor: {
41 41
                     select: {
42 42
                         id: true,
@@ -70,34 +70,34 @@ const VendorHistoryRepository = {
70 70
             },
71 71
             select: {
72 72
                 id: true,
73
-                hospital: {
74
-                    select: {
75
-                        id: true,
76
-                        name: true,
77
-                        hospital_code: true,
78
-                        type: true,
79
-                        ownership: true,
80
-                        province: {
81
-                            select: {
82
-                                id: true,
83
-                                name: true
84
-                            }
85
-                        },
86
-                        city: {
87
-                            select: {
88
-                                id: true,
89
-                                name: true
90
-                            }
91
-                        },
92
-                        address: true,
93
-                        simrs_type: true,
94
-                        contact: true,
95
-                        image: true,
96
-                        progress_status: true,
97
-                        note: true,
98
-                        created_by: true
99
-                    }
100
-                },
73
+                // hospital: {
74
+                //     select: {
75
+                //         id: true,
76
+                //         name: true,
77
+                //         hospital_code: true,
78
+                //         type: true,
79
+                //         ownership: true,
80
+                //         province: {
81
+                //             select: {
82
+                //                 id: true,
83
+                //                 name: true
84
+                //             }
85
+                //         },
86
+                //         city: {
87
+                //             select: {
88
+                //                 id: true,
89
+                //                 name: true
90
+                //             }
91
+                //         },
92
+                //         address: true,
93
+                //         simrs_type: true,
94
+                //         contact: true,
95
+                //         image: true,
96
+                //         progress_status: true,
97
+                //         note: true,
98
+                //         created_by: true
99
+                //     }
100
+                // },
101 101
                 vendor: {
102 102
                     select: {
103 103
                         id: true,

+ 61 - 4
src/repository/sales/HospitalRepository.js

@@ -2,7 +2,7 @@ const prisma = require('../../prisma/PrismaClient.js');
2 2
 
3 3
 const HospitalRepository = {
4 4
     findAll: async ({ skip, take, where, orderBy }) => {
5
-        return await prisma.hospital.findMany({
5
+        const hospitalsRaw = await prisma.hospital.findMany({
6 6
             where,
7 7
             skip,
8 8
             take,
@@ -21,12 +21,40 @@ const HospitalRepository = {
21 21
                 image: true,
22 22
                 progress_status: true,
23 23
                 note: true,
24
-                // user: { select: { id: true, username: true } },
24
+                latitude: true,
25
+                longitude: true,
26
+                gmaps_url: true,
25 27
                 created_by: true,
26 28
                 createdAt: true,
27 29
                 updatedAt: true,
30
+                vendor_histories: {
31
+                    where: {
32
+                        status: "active",
33
+                        deletedAt: null
34
+                    },
35
+                    select: {
36
+                        vendor: {
37
+                            select: {
38
+                                id: true,
39
+                                name: true,
40
+                                name_pt: true,
41
+                                strengths: true,
42
+                                weaknesses: true,
43
+                                website: true,
44
+                            }
45
+                        }
46
+                    }
47
+                }
28 48
             }
29 49
         });
50
+
51
+        return hospitalsRaw.map(hospital => {
52
+            const { vendor_histories, ...rest } = hospital;
53
+            return {
54
+                ...rest,
55
+                vendor: vendor_histories?.[0]?.vendor || null
56
+            };
57
+        });
30 58
     },
31 59
 
32 60
     countAll: async (where) => {
@@ -38,7 +66,7 @@ const HospitalRepository = {
38 66
     },
39 67
 
40 68
     findById: async (id) => {
41
-        return prisma.hospital.findFirst({
69
+        const hospitalRaw = await prisma.hospital.findFirst({
42 70
             where: {
43 71
                 id,
44 72
                 deletedAt: null
@@ -57,12 +85,41 @@ const HospitalRepository = {
57 85
                 image: true,
58 86
                 progress_status: true,
59 87
                 note: true,
60
-                // user: { select: { id: true, username: true } },
88
+                latitude: true,
89
+                longitude: true,
90
+                gmaps_url: true,
61 91
                 created_by: true,
62 92
                 createdAt: true,
63 93
                 updatedAt: true,
94
+                vendor_histories: {
95
+                    where: {
96
+                        status: "active",
97
+                        deletedAt: null
98
+                    },
99
+                    take: 1,
100
+                    select: {
101
+                        vendor: {
102
+                            select: {
103
+                                id: true,
104
+                                name: true,
105
+                                name_pt: true,
106
+                                strengths: true,
107
+                                weaknesses: true,
108
+                                website: true,
109
+                            }
110
+                        }
111
+                    }
112
+                }
64 113
             }
65 114
         });
115
+
116
+        if (!hospitalRaw) return null;
117
+
118
+        const { vendor_histories, ...rest } = hospitalRaw;
119
+        return {
120
+            ...rest,
121
+            vendor: vendor_histories?.[0]?.vendor || null
122
+        };
66 123
     },
67 124
 
68 125
     // update: async (id, data) => {

+ 56 - 56
src/repository/sales/VendorHistoryRepository.js

@@ -9,34 +9,34 @@ const VendorHistoryRepository = {
9 9
             orderBy,
10 10
             select: {
11 11
                 id: true,
12
-                hospital: {
13
-                    select: {
14
-                        id: true,
15
-                        name: true,
16
-                        hospital_code: true,
17
-                        type: true,
18
-                        ownership: true,
19
-                        province: {
20
-                            select: {
21
-                                id: true,
22
-                                name: true
23
-                            }
24
-                        },
25
-                        city: {
26
-                            select: {
27
-                                id: true,
28
-                                name: true
29
-                            }
30
-                        },
31
-                        address: true,
32
-                        simrs_type: true,
33
-                        contact: true,
34
-                        image: true,
35
-                        progress_status: true,
36
-                        note: true,
37
-                        created_by: true
38
-                    }
39
-                },
12
+                // hospital: {
13
+                //     select: {
14
+                //         id: true,
15
+                //         name: true,
16
+                //         hospital_code: true,
17
+                //         type: true,
18
+                //         ownership: true,
19
+                //         province: {
20
+                //             select: {
21
+                //                 id: true,
22
+                //                 name: true
23
+                //             }
24
+                //         },
25
+                //         city: {
26
+                //             select: {
27
+                //                 id: true,
28
+                //                 name: true
29
+                //             }
30
+                //         },
31
+                //         address: true,
32
+                //         simrs_type: true,
33
+                //         contact: true,
34
+                //         image: true,
35
+                //         progress_status: true,
36
+                //         note: true,
37
+                //         created_by: true
38
+                //     }
39
+                // },
40 40
                 vendor: {
41 41
                     select: {
42 42
                         id: true,
@@ -70,34 +70,34 @@ const VendorHistoryRepository = {
70 70
             },
71 71
             select: {
72 72
                 id: true,
73
-                hospital: {
74
-                    select: {
75
-                        id: true,
76
-                        name: true,
77
-                        hospital_code: true,
78
-                        type: true,
79
-                        ownership: true,
80
-                        province: {
81
-                            select: {
82
-                                id: true,
83
-                                name: true
84
-                            }
85
-                        },
86
-                        city: {
87
-                            select: {
88
-                                id: true,
89
-                                name: true
90
-                            }
91
-                        },
92
-                        address: true,
93
-                        simrs_type: true,
94
-                        contact: true,
95
-                        image: true,
96
-                        progress_status: true,
97
-                        note: true,
98
-                        created_by: true
99
-                    }
100
-                },
73
+                // hospital: {
74
+                //     select: {
75
+                //         id: true,
76
+                //         name: true,
77
+                //         hospital_code: true,
78
+                //         type: true,
79
+                //         ownership: true,
80
+                //         province: {
81
+                //             select: {
82
+                //                 id: true,
83
+                //                 name: true
84
+                //             }
85
+                //         },
86
+                //         city: {
87
+                //             select: {
88
+                //                 id: true,
89
+                //                 name: true
90
+                //             }
91
+                //         },
92
+                //         address: true,
93
+                //         simrs_type: true,
94
+                //         contact: true,
95
+                //         image: true,
96
+                //         progress_status: true,
97
+                //         note: true,
98
+                //         created_by: true
99
+                //     }
100
+                // },
101 101
                 vendor: {
102 102
                     select: {
103 103
                         id: true,

+ 66 - 6
src/services/admin/VendorHistoryService.js

@@ -61,11 +61,21 @@ exports.storeVendorHistoryService = async (validateData, req) => {
61 61
         where: {
62 62
             id: hospitalId
63 63
         }
64
-    })
64
+    });
65
+
65 66
     if (!hospital) {
66 67
         throw new HttpException("Hospital not found", 404)
67 68
     }
68 69
 
70
+    const vendor = await prisma.vendor.findFirst({
71
+        where: {
72
+            id: validateData.vendor_id
73
+        }
74
+    });
75
+    if (!vendor) {
76
+        throw new HttpException("Vendor not found", 404)
77
+    }
78
+
69 79
     await prisma.hospital.update({
70 80
         where: { id: hospitalId },
71 81
         data: {
@@ -73,6 +83,31 @@ exports.storeVendorHistoryService = async (validateData, req) => {
73 83
         }
74 84
     });
75 85
 
86
+    if (validateData.simrs_type) {
87
+        const existingActiveVendors = await prisma.vendorHistory.findMany({
88
+            where: {
89
+                hospital_id: hospitalId,
90
+                status: "active",
91
+                deletedAt: null
92
+            }
93
+        });
94
+
95
+        if (existingActiveVendors.length > 0) {
96
+            await prisma.vendorHistory.updateMany({
97
+                where: {
98
+                    hospital_id: hospitalId,
99
+                    status: "active",
100
+                    deletedAt: null
101
+                },
102
+                data: {
103
+                    status: "inactive"
104
+                }
105
+            });
106
+        }
107
+
108
+        validateData.status = "active";
109
+    }
110
+
76 111
     const payload = {
77 112
         vendor_id: validateData.vendor_id,
78 113
         vendor_impression: validateData.vendor_impression,
@@ -99,11 +134,22 @@ exports.updateVendorHistoryService = async (validateData, req) => {
99 134
         throw new HttpException("Hospital not found", 404)
100 135
     }
101 136
 
102
-    const vendor = await VendorHistoryRepository.findById(id_vendor_history);
103
-    if (!vendor) {
137
+    const vendorHistory = await VendorHistoryRepository.findById(id_vendor_history);
138
+    if (!vendorHistory) {
104 139
         throw new HttpException("Vendor history not found", 404);
105 140
     }
106 141
 
142
+    if (validateData.vendor_id) {
143
+        const existVendor = await prisma.vendor.findFirst({
144
+            where: {
145
+                id: validateData.vendor_id
146
+            }
147
+        });
148
+        if (!existVendor) {
149
+            throw new HttpException("Vendor not found", 404)
150
+        }
151
+    }
152
+
107 153
     if (validateData.simrs_type) {
108 154
         await prisma.hospital.update({
109 155
             where: { id: id_hospital },
@@ -118,13 +164,27 @@ exports.updateVendorHistoryService = async (validateData, req) => {
118 164
             throw new HttpException("Contract expired date must be after contract date", 400)
119 165
         }
120 166
     }
121
-    
167
+
168
+    if (validateData.status === "active") {
169
+        await prisma.vendorHistory.updateMany({
170
+            where: {
171
+                hospital_id: id_hospital,
172
+                status: "active",
173
+                deletedAt: null,
174
+                NOT: { id: id_vendor_history },
175
+            },
176
+            data: {
177
+                status: "inactive",
178
+            },
179
+        });
180
+    }
181
+
122 182
     const payload = {
123 183
         vendor_id: validateData.vendor_id,
124 184
         vendor_impression: validateData.vendor_impression,
125 185
         status: validateData.status,
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,
186
+        contract_date: validateData.contract_date ? new Date(validateData.contract_date) : vendorHistory.contract_date,
187
+        contract_expired_date: validateData.contract_expired_date ? new Date(validateData.contract_expired_date) : vendorHistory.contract_expired_date,
128 188
     };
129 189
 
130 190
     const data = await VendorHistoryRepository.update(id_vendor_history, payload);

+ 64 - 5
src/services/sales/VendorHistoryService.js

@@ -61,11 +61,20 @@ exports.storeVendorHistoryService = async (validateData, req) => {
61 61
         where: {
62 62
             id: hospitalId
63 63
         }
64
-    })
64
+    });
65 65
     if (!hospital) {
66 66
         throw new HttpException("Hospital not found", 404)
67 67
     }
68 68
 
69
+    const vendor = await prisma.vendor.findFirst({
70
+        where: {
71
+            id: validateData.vendor_id
72
+        }
73
+    });
74
+    if (!vendor) {
75
+        throw new HttpException("Vendor not found", 404)
76
+    }
77
+
69 78
     await prisma.hospital.update({
70 79
         where: { id: hospitalId },
71 80
         data: {
@@ -73,6 +82,31 @@ exports.storeVendorHistoryService = async (validateData, req) => {
73 82
         }
74 83
     });
75 84
 
85
+    if (validateData.simrs_type) {
86
+        const existingActiveVendors = await prisma.vendorHistory.findMany({
87
+            where: {
88
+                hospital_id: hospitalId,
89
+                status: "active",
90
+                deletedAt: null
91
+            }
92
+        });
93
+
94
+        if (existingActiveVendors.length > 0) {
95
+            await prisma.vendorHistory.updateMany({
96
+                where: {
97
+                    hospital_id: hospitalId,
98
+                    status: "active",
99
+                    deletedAt: null
100
+                },
101
+                data: {
102
+                    status: "inactive"
103
+                }
104
+            });
105
+        }
106
+
107
+        validateData.status = "active";
108
+    }
109
+
76 110
     const payload = {
77 111
         vendor_id: validateData.vendor_id,
78 112
         vendor_impression: validateData.vendor_impression,
@@ -99,11 +133,22 @@ exports.updateVendorHistoryService = async (validateData, req) => {
99 133
         throw new HttpException("Hospital not found", 404)
100 134
     }
101 135
 
102
-    const vendor = await VendorHistoryRepository.findById(id_vendor_history);
103
-    if (!vendor) {
136
+    const vendorHistory = await VendorHistoryRepository.findById(id_vendor_history);
137
+    if (!vendorHistory) {
104 138
         throw new HttpException("Vendor history not found", 404);
105 139
     }
106 140
 
141
+    if (validateData.vendor_id) {
142
+        const existVendor = await prisma.vendor.findFirst({
143
+            where: {
144
+                id: validateData.vendor_id
145
+            }
146
+        });
147
+        if (!existVendor) {
148
+            throw new HttpException("Vendor not found", 404)
149
+        }
150
+    }
151
+
107 152
     await prisma.hospital.update({
108 153
         where: { id: id_hospital },
109 154
         data: {
@@ -117,12 +162,26 @@ exports.updateVendorHistoryService = async (validateData, req) => {
117 162
         }
118 163
     }
119 164
 
165
+    if (validateData.status === "active") {
166
+        await prisma.vendorHistory.updateMany({
167
+            where: {
168
+                hospital_id: id_hospital,
169
+                status: "active",
170
+                deletedAt: null,
171
+                NOT: { id: id_vendor_history },
172
+            },
173
+            data: {
174
+                status: "inactive",
175
+            },
176
+        });
177
+    }
178
+
120 179
     const payload = {
121 180
         vendor_id: validateData.vendor_id,
122 181
         vendor_impression: validateData.vendor_impression,
123 182
         status: validateData.status,
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,
183
+        contract_date: validateData.contract_date ? new Date(validateData.contract_date) : vendorHistory.contract_date,
184
+        contract_expired_date: validateData.contract_expired_date ? new Date(validateData.contract_expired_date) : vendorHistory.contract_expired_date,
126 185
     };
127 186
 
128 187
     const data = await VendorHistoryRepository.update(id_vendor_history, payload);

+ 9 - 0
src/validators/admin/vendor_history/VendorHistoriValidators.js

@@ -75,6 +75,15 @@ exports.validateStoreVendorHistoryRequest = (body) => {
75 75
 exports.validateUpdateVendorHistoryRequest = (body) => {
76 76
     const { simrs_type, vendor_id, vendor_impression, status, contract_date, contract_expired_date } = body;
77 77
 
78
+    if (simrs_type !== undefined && simrs_type !== null && simrs_type.trim() !== '') {
79
+        const allowedTypes = ['vendor', 'gratis', 'in house'];
80
+        const simrsTypeValue = simrs_type.trim().toLowerCase();
81
+
82
+        if (!allowedTypes.includes(simrsTypeValue)) {
83
+            throw new HttpException('Simrs type must be either "vendor", "gratis", or "in house"', 422);
84
+        }
85
+    }
86
+
78 87
     return {
79 88
         simrs_type,
80 89
         vendor_id,

+ 9 - 0
src/validators/sales/vendor_history/VendorHistoriValidators.js

@@ -75,6 +75,15 @@ exports.validateStoreVendorHistoryRequest = (body) => {
75 75
 exports.validateUpdateVendorHistoryRequest = (body) => {
76 76
     const { simrs_type, vendor_id, vendor_impression, status, contract_date, contract_expired_date } = body;
77 77
 
78
+    if (simrs_type !== undefined && simrs_type !== null && simrs_type.trim() !== '') {
79
+        const allowedTypes = ['vendor', 'gratis', 'in house'];
80
+        const simrsTypeValue = simrs_type.trim().toLowerCase();
81
+
82
+        if (!allowedTypes.includes(simrsTypeValue)) {
83
+            throw new HttpException('Simrs type must be either "vendor", "gratis", or "in house"', 422);
84
+        }
85
+    }
86
+
78 87
     return {
79 88
         simrs_type,
80 89
         vendor_id,