File size: 5,792 Bytes
486340a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const { db } = require('../config/firebase');
const { DEAL_STATUS, RIDE_STATUS } = require('../constants/statuses');
const { addBalance, deductBalance } = require('./walletHelper');

const ACTIVE_RIDE_STATUSES = [
  RIDE_STATUS.ACTIVE,
  RIDE_STATUS.FILLED,
  RIDE_STATUS.IN_PROGRESS,
];

const ACTIVE_CUSTOMER_REQUEST_STATUSES = ['open', 'countered', 'accepted'];
const ACCEPTED_REQUEST_RETENTION_MS = 10 * 60 * 1000;

function isPastIso(value, now = new Date()) {
  const dt = new Date(value || '');
  return !Number.isNaN(dt.getTime()) && dt < now;
}

function isOlderThanIso(value, ms, now = new Date()) {
  const dt = new Date(value || '');
  return !Number.isNaN(dt.getTime()) && now.getTime() - dt.getTime() >= ms;
}

async function cleanupExpiredRides() {
  const now = new Date();
  const snap = await db
    .collection('rides')
    .where('status', 'in', ACTIVE_RIDE_STATUSES)
    .limit(100)
    .get();

  const batch = db.batch();
  let writes = 0;
  const walletSettlements = [];

  for (const doc of snap.docs) {
    const ride = doc.data() || {};
    if (!isPastIso(ride.departureTime, now)) continue;

    const completedAt = now.toISOString();
    const dealsSnap = await db.collection('deals').where('rideId', '==', doc.id).get();
    const deals = dealsSnap.docs.map((dealDoc) => ({
      ref: dealDoc.ref,
      data: dealDoc.data() || {},
    }));
    const hasConfirmedDeal = deals.some((deal) =>
      [DEAL_STATUS.CONFIRMED, DEAL_STATUS.STARTED, DEAL_STATUS.COMPLETED].includes(deal.data.status),
    );

    if (!hasConfirmedDeal) {
      for (const deal of deals) {
        batch.delete(deal.ref);
        writes += 1;
      }
      batch.delete(doc.ref);
      writes += 1;
      continue;
    }

    for (const deal of deals) {
      if ([DEAL_STATUS.CONFIRMED, DEAL_STATUS.STARTED].includes(deal.data.status)) {
        batch.update(deal.ref, {
          status: DEAL_STATUS.COMPLETED,
          completedAt,
          updatedAt: completedAt,
        });
        writes += 1;
        walletSettlements.push({
          dealId: deal.ref.id,
          captainId: deal.data.captainId,
          customerId: deal.data.customerId,
          agreedFare: deal.data.agreedFare,
        });
      } else if (deal.data.status === DEAL_STATUS.PENDING) {
        batch.update(deal.ref, {
          status: DEAL_STATUS.CANCELLED,
          updatedAt: completedAt,
        });
        writes += 1;
      }
    }

    batch.update(doc.ref, {
      status: RIDE_STATUS.COMPLETED,
      full: true,
      completedAt,
      updatedAt: completedAt,
    });
    writes += 1;
  }

  if (writes > 0) await batch.commit();

  for (const settlement of walletSettlements) {
    const { dealId, captainId, customerId, agreedFare } = settlement;
    const fare = parseFloat(agreedFare || 0);
    if (!fare || fare <= 0 || !captainId || !customerId) continue;

    try {
      await addBalance(captainId, fare, {
        type: 'ride_earning',
        description: 'Auto-completed ride earning',
        reference: dealId,
      });
    } catch (err) {
      console.error(`Auto-complete captain payout failed for deal ${dealId}:`, err.message);
    }

    try {
      await deductBalance(customerId, fare, {
        type: 'ride_payment',
        description: 'Auto-completed ride payment',
        reference: dealId,
      });
    } catch (err) {
      console.error(`Auto-complete customer payment failed for deal ${dealId}:`, err.message);
    }
  }
}

async function cleanupExpiredCustomerRequests() {
  const now = new Date();
  const snap = await db
    .collection('customerRideRequests')
    .where('status', 'in', ACTIVE_CUSTOMER_REQUEST_STATUSES)
    .limit(100)
    .get();

  const batch = db.batch();
  let writes = 0;

  for (const doc of snap.docs) {
    const request = doc.data() || {};
    const status = (request.status || '').toString().toLowerCase();
    const offersSnap = await db
      .collection('customerRideOffers')
      .where('requestId', '==', doc.id)
      .get();

    if (
      ['accepted', 'completed'].includes(status) &&
      isOlderThanIso(
        request.acceptedAt || request.completedAt || request.updatedAt,
        ACCEPTED_REQUEST_RETENTION_MS,
        now,
      )
    ) {
      for (const offerDoc of offersSnap.docs) {
        batch.delete(offerDoc.ref);
        writes += 1;
      }
      batch.delete(doc.ref);
      writes += 1;
      continue;
    }

    if (!isPastIso(request.requestedAt, now)) continue;

    const completedAt = now.toISOString();

    const acceptedOffer = offersSnap.docs.find((offerDoc) => {
      const offer = offerDoc.data() || {};
      return offer.status === 'accepted' || offerDoc.id === request.acceptedOfferId;
    });

    if (status === 'accepted' && acceptedOffer) {
      batch.update(doc.ref, {
        status: 'completed',
        completedAt,
        updatedAt: completedAt,
      });
      writes += 1;

      for (const offerDoc of offersSnap.docs) {
        const offer = offerDoc.data() || {};
        if (offerDoc.id === acceptedOffer.id || offer.status === 'accepted') {
          batch.update(offerDoc.ref, {
            status: 'completed',
            phoneRevealed: true,
            completedAt,
            updatedAt: completedAt,
          });
        } else if (['offered', 'countered'].includes(offer.status)) {
          batch.update(offerDoc.ref, {
            status: 'cancelled',
            updatedAt: completedAt,
          });
        }
        writes += 1;
      }
    } else {
      for (const offerDoc of offersSnap.docs) {
        batch.delete(offerDoc.ref);
        writes += 1;
      }
      batch.delete(doc.ref);
      writes += 1;
    }
  }

  if (writes > 0) await batch.commit();
}

module.exports = {
  cleanupExpiredRides,
  cleanupExpiredCustomerRequests,
};