|
@@ -94,7 +94,7 @@ exports.getAllHospitalByAreaService = async ({ page, limit, search, sortBy, orde
|
94
|
94
|
// };
|
95
|
95
|
|
96
|
96
|
exports.storeHospitalService = async (validateData, req) => {
|
97
|
|
- const creatorId = req.user.id;
|
|
97
|
+ const creatorId = req.tokenData.sub;
|
98
|
98
|
|
99
|
99
|
const province = await ProvinceRepository.findById(validateData.province_id);
|
100
|
100
|
if (!province) {
|
|
@@ -103,7 +103,7 @@ exports.storeHospitalService = async (validateData, req) => {
|
103
|
103
|
|
104
|
104
|
const userArea = await prisma.userArea.findFirst({
|
105
|
105
|
where: {
|
106
|
|
- user_id: req.user.id,
|
|
106
|
+ user_id: req.tokenData.sub,
|
107
|
107
|
province_id: validateData.province_id
|
108
|
108
|
}
|
109
|
109
|
});
|
|
@@ -135,12 +135,45 @@ exports.storeHospitalService = async (validateData, req) => {
|
135
|
135
|
|
136
|
136
|
const imagePath = req.file ? `/storage/img/${req.file.filename}` : null;
|
137
|
137
|
|
|
138
|
+ let latitude = validateData.latitude ?? null;
|
|
139
|
+ let longitude = validateData.longitude ?? null;
|
|
140
|
+ let gmapsUrl = validateData.gmaps_url ?? null;
|
|
141
|
+
|
|
142
|
+ if (gmapsUrl) {
|
|
143
|
+ if (gmapsUrl.includes("www.google.com/maps")) {
|
|
144
|
+ const regex = /@(-?\d+\.\d+),(-?\d+\.\d+)/;
|
|
145
|
+ const match = gmapsUrl.match(regex);
|
|
146
|
+
|
|
147
|
+ if (match) {
|
|
148
|
+ latitude = parseFloat(match[1]);
|
|
149
|
+ longitude = parseFloat(match[2]);
|
|
150
|
+ } else {
|
|
151
|
+ throw new HttpException("Unable to extract coordinates from gmaps_url", 400);
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ } else if (gmapsUrl.includes("maps.app.goo.gl")) {
|
|
155
|
+ latitude = null;
|
|
156
|
+ longitude = null;
|
|
157
|
+
|
|
158
|
+ } else {
|
|
159
|
+ // URL disediakan tapi bukan dari domain yang valid
|
|
160
|
+ throw new HttpException("gmaps_url must be a valid Google Maps URL", 400);
|
|
161
|
+ }
|
|
162
|
+ } else if (latitude !== null && longitude !== null) {
|
|
163
|
+ gmapsUrl = null;
|
|
164
|
+ } else {
|
|
165
|
+ throw new HttpException("Either gmaps_url or coordinates must be provided", 400);
|
|
166
|
+ }
|
|
167
|
+
|
138
|
168
|
const payload = {
|
139
|
169
|
...validateData,
|
140
|
170
|
image: imagePath,
|
141
|
171
|
progress_status: "cari_data",
|
142
|
172
|
simrs_type: "-",
|
143
|
|
- created_by: creatorId
|
|
173
|
+ created_by: creatorId,
|
|
174
|
+ latitude,
|
|
175
|
+ longitude,
|
|
176
|
+ gmaps_url: gmapsUrl,
|
144
|
177
|
};
|
145
|
178
|
|
146
|
179
|
const data = await salesHospitalRepository.create(payload);
|
|
@@ -157,7 +190,7 @@ exports.updateHospitalService = async (validateData, id, req) => {
|
157
|
190
|
|
158
|
191
|
const userArea = await prisma.userArea.findFirst({
|
159
|
192
|
where: {
|
160
|
|
- user_id: req.user.id,
|
|
193
|
+ user_id: req.tokenData.sub,
|
161
|
194
|
province_id: validateData.province_id
|
162
|
195
|
}
|
163
|
196
|
});
|
|
@@ -166,14 +199,18 @@ exports.updateHospitalService = async (validateData, id, req) => {
|
166
|
199
|
throw new HttpException("You are not authorized to update hospital in this province", 403);
|
167
|
200
|
}
|
168
|
201
|
|
169
|
|
- const province = await ProvinceRepository.findById(validateData.province_id);
|
170
|
|
- if (!province) {
|
171
|
|
- throw new HttpException('Province not found', 404);
|
|
202
|
+ if (validateData.province_id) {
|
|
203
|
+ const province = await ProvinceRepository.findById(validateData.province_id);
|
|
204
|
+ if (!province) {
|
|
205
|
+ throw new HttpException('Province not found', 404);
|
|
206
|
+ }
|
172
|
207
|
}
|
173
|
208
|
|
174
|
|
- const city = await CityRepository.findById(validateData.city_id);
|
175
|
|
- if (!city) {
|
176
|
|
- throw new HttpException('City not found', 404);
|
|
209
|
+ if (validateData.city_id) {
|
|
210
|
+ const city = await CityRepository.findById(validateData.city_id);
|
|
211
|
+ if (!city) {
|
|
212
|
+ throw new HttpException('City not found', 404);
|
|
213
|
+ }
|
177
|
214
|
}
|
178
|
215
|
|
179
|
216
|
if (validateData.progress_status && !validProgressStatuses.includes(validateData.progress_status)) {
|
|
@@ -183,16 +220,19 @@ exports.updateHospitalService = async (validateData, id, req) => {
|
183
|
220
|
);
|
184
|
221
|
}
|
185
|
222
|
|
186
|
|
- const existingHospital = await prisma.hospital.findFirst({
|
187
|
|
- where: {
|
188
|
|
- name: validateData.name,
|
189
|
|
- city_id: validateData.city_id,
|
190
|
|
- deletedAt: null
|
|
223
|
+ if (validateData.name && validateData.city_id) {
|
|
224
|
+ const existingHospital = await prisma.hospital.findFirst({
|
|
225
|
+ where: {
|
|
226
|
+ name: validateData.name,
|
|
227
|
+ city_id: validateData.city_id,
|
|
228
|
+ deletedAt: null,
|
|
229
|
+ NOT: { id }
|
|
230
|
+ }
|
|
231
|
+ });
|
|
232
|
+
|
|
233
|
+ if (existingHospital) {
|
|
234
|
+ throw new HttpException('Hospital with same name in this city already exists', 400);
|
191
|
235
|
}
|
192
|
|
- });
|
193
|
|
-
|
194
|
|
- if (existingHospital) {
|
195
|
|
- throw new HttpException('Hospital with same name in this city already exists', 400);
|
196
|
236
|
}
|
197
|
237
|
|
198
|
238
|
// Jika ada file baru, replace image
|
|
@@ -201,10 +241,53 @@ exports.updateHospitalService = async (validateData, id, req) => {
|
201
|
241
|
imagePath = `/storage/img/${req.file.filename}`; // path relatif
|
202
|
242
|
}
|
203
|
243
|
|
|
244
|
+ // Handle koordinat dan gmaps_url
|
|
245
|
+ let latitude = hospital.latitude;
|
|
246
|
+ let longitude = hospital.longitude;
|
|
247
|
+ let gmapsUrl = hospital.gmaps_url;
|
|
248
|
+
|
|
249
|
+ if (
|
|
250
|
+ validateData.latitude !== undefined &&
|
|
251
|
+ validateData.longitude !== undefined &&
|
|
252
|
+ validateData.latitude !== null &&
|
|
253
|
+ validateData.longitude !== null
|
|
254
|
+ ) {
|
|
255
|
+ // Jika diberikan lat long langsung
|
|
256
|
+ latitude = validateData.latitude;
|
|
257
|
+ longitude = validateData.longitude;
|
|
258
|
+ gmapsUrl = validateData.gmaps_url || gmapsUrl;
|
|
259
|
+ } else if (
|
|
260
|
+ validateData.gmaps_url &&
|
|
261
|
+ typeof validateData.gmaps_url === "string" &&
|
|
262
|
+ validateData.gmaps_url.trim() !== ""
|
|
263
|
+ ) {
|
|
264
|
+ gmapsUrl = validateData.gmaps_url;
|
|
265
|
+
|
|
266
|
+ if (gmapsUrl.includes("www.google.com/maps")) {
|
|
267
|
+ const regex = /@(-?\d+\.\d+),(-?\d+\.\d+)/;
|
|
268
|
+ const match = gmapsUrl.match(regex);
|
|
269
|
+ if (match) {
|
|
270
|
+ latitude = parseFloat(match[1]);
|
|
271
|
+ longitude = parseFloat(match[2]);
|
|
272
|
+ } else {
|
|
273
|
+ throw new HttpException("Unable to extract coordinates from gmaps_url", 400);
|
|
274
|
+ }
|
|
275
|
+ } else if (gmapsUrl.includes("maps.app.goo.gl")) {
|
|
276
|
+ // Tidak bisa ambil koordinat langsung
|
|
277
|
+ latitude = null;
|
|
278
|
+ longitude = null;
|
|
279
|
+ } else {
|
|
280
|
+ throw new HttpException("gmaps_url must be a valid Google Maps URL", 400);
|
|
281
|
+ }
|
|
282
|
+ }
|
|
283
|
+
|
204
|
284
|
const payload = {
|
205
|
285
|
...validateData,
|
206
|
286
|
image: imagePath,
|
207
|
287
|
// created_by: req.user.id,
|
|
288
|
+ latitude,
|
|
289
|
+ longitude,
|
|
290
|
+ gmaps_url: gmapsUrl,
|
208
|
291
|
};
|
209
|
292
|
|
210
|
293
|
const data = await salesHospitalRepository.update(id, payload);
|