Huzaifa1435 commited on
Commit
6b551c8
Β·
verified Β·
1 Parent(s): bcc653d

Update src/controllers/authController.js

Browse files
Files changed (1) hide show
  1. src/controllers/authController.js +93 -0
src/controllers/authController.js CHANGED
@@ -1,5 +1,6 @@
1
  const { db } = require('../config/firebase');
2
  const { CAPTAIN_STARTER_BALANCE } = require('../utils/walletHelper');
 
3
 
4
  const ALLOWED_USER_FIELDS = [
5
  'name',
@@ -166,8 +167,26 @@ const syncUser = async (req, res) => {
166
  if (updates.captainVerificationStatus === 'pending_verification') {
167
  updates.isVerified = false;
168
  }
 
 
169
  if (updates.captainVerificationStatus === 'verified') {
170
  updates.isVerified = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  }
172
 
173
  updates.updatedAt = new Date().toISOString();
@@ -271,8 +290,26 @@ const updateProfile = async (req, res) => {
271
  if (updates.captainVerificationStatus === 'pending_verification') {
272
  updates.isVerified = false;
273
  }
 
 
274
  if (updates.captainVerificationStatus === 'verified') {
275
  updates.isVerified = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  }
277
 
278
  updates.updatedAt = new Date().toISOString();
@@ -347,10 +384,66 @@ const updateOnlineStatus = async (req, res) => {
347
  }
348
  };
349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
  module.exports = {
351
  syncUser,
352
  getProfile,
353
  updateProfile,
354
  updateFcmToken,
355
  updateOnlineStatus,
 
356
  };
 
1
  const { db } = require('../config/firebase');
2
  const { CAPTAIN_STARTER_BALANCE } = require('../utils/walletHelper');
3
+ const { pushToUser } = require('../utils/notificationHelper');
4
 
5
  const ALLOWED_USER_FIELDS = [
6
  'name',
 
167
  if (updates.captainVerificationStatus === 'pending_verification') {
168
  updates.isVerified = false;
169
  }
170
+
171
+ // βœ… FIX: Captain verified hone par push notification
172
  if (updates.captainVerificationStatus === 'verified') {
173
  updates.isVerified = true;
174
+
175
+ // βœ… Send push notification to captain
176
+ try {
177
+ await pushToUser(uid, {
178
+ title: 'βœ… Account Verified!',
179
+ body: 'Mubarak! Aap ka captain account verify ho gaya. Ab ap rides post kar sakte hain.',
180
+ type: 'captain_verified',
181
+ data: {
182
+ screen: 'captain-home',
183
+ verified: 'true'
184
+ },
185
+ });
186
+ console.log(`βœ… Verification notification sent to captain ${uid}`);
187
+ } catch (notifErr) {
188
+ console.error('Captain verify notification failed:', notifErr.message);
189
+ }
190
  }
191
 
192
  updates.updatedAt = new Date().toISOString();
 
290
  if (updates.captainVerificationStatus === 'pending_verification') {
291
  updates.isVerified = false;
292
  }
293
+
294
+ // βœ… FIX: Captain verified hone par push notification
295
  if (updates.captainVerificationStatus === 'verified') {
296
  updates.isVerified = true;
297
+
298
+ // βœ… Send push notification to captain
299
+ try {
300
+ await pushToUser(uid, {
301
+ title: 'βœ… Account Verified!',
302
+ body: 'Mubarak! Aap ka captain account verify ho gaya. Ab ap rides post kar sakte hain.',
303
+ type: 'captain_verified',
304
+ data: {
305
+ screen: 'captain-home',
306
+ verified: 'true'
307
+ },
308
+ });
309
+ console.log(`βœ… Verification notification sent to captain ${uid}`);
310
+ } catch (notifErr) {
311
+ console.error('Captain verify notification failed:', notifErr.message);
312
+ }
313
  }
314
 
315
  updates.updatedAt = new Date().toISOString();
 
384
  }
385
  };
386
 
387
+ // βœ… New endpoint: Update user location
388
+ const updateLocation = async (req, res) => {
389
+ const { lat, lng } = req.body;
390
+ const uid = req.user.uid;
391
+
392
+ if (lat === undefined || lng === undefined) {
393
+ return res.status(400).json({
394
+ success: false,
395
+ error: 'lat and lng are required',
396
+ code: 'MISSING_COORDINATES'
397
+ });
398
+ }
399
+
400
+ if (typeof lat !== 'number' || typeof lng !== 'number') {
401
+ return res.status(400).json({
402
+ success: false,
403
+ error: 'lat and lng must be numbers',
404
+ code: 'INVALID_COORDINATES'
405
+ });
406
+ }
407
+
408
+ if (lat < -90 || lat > 90 || lng < -180 || lng > 180) {
409
+ return res.status(400).json({
410
+ success: false,
411
+ error: 'Invalid coordinates',
412
+ code: 'INVALID_COORDINATES'
413
+ });
414
+ }
415
+
416
+ try {
417
+ await db.collection('users').doc(uid).set({
418
+ lat: lat,
419
+ lng: lng,
420
+ locationUpdatedAt: new Date().toISOString(),
421
+ updatedAt: new Date().toISOString(),
422
+ }, { merge: true });
423
+
424
+ // Clear cache
425
+ userCache.delete(uid);
426
+
427
+ return res.json({
428
+ success: true,
429
+ message: 'Location updated successfully',
430
+ location: { lat, lng }
431
+ });
432
+ } catch (err) {
433
+ console.error('Location update error:', err);
434
+ return res.status(500).json({
435
+ success: false,
436
+ error: err.message,
437
+ code: 'LOCATION_UPDATE_ERROR'
438
+ });
439
+ }
440
+ };
441
+
442
  module.exports = {
443
  syncUser,
444
  getProfile,
445
  updateProfile,
446
  updateFcmToken,
447
  updateOnlineStatus,
448
+ updateLocation, // βœ… Export new endpoint
449
  };