Huzaifa1435 commited on
Commit
c5452f2
·
1 Parent(s): c456dbb

fix: recover starter wallet balance

Browse files
src/controllers/authController.js CHANGED
@@ -46,14 +46,16 @@ function pickUpdates(body) {
46
  async function ensureCaptainWallet(uid) {
47
  const walletRef = db.collection('wallets').doc(uid);
48
  const walletSnap = await walletRef.get();
 
49
  if (!walletSnap.exists) {
50
  await walletRef.set({
51
  id: uid,
52
  userId: uid,
53
  balance: CAPTAIN_STARTER_BALANCE,
54
  starterBalanceApplied: true,
55
- createdAt: new Date().toISOString(),
56
- updatedAt: new Date().toISOString(),
 
57
  });
58
  return;
59
  }
@@ -65,7 +67,8 @@ async function ensureCaptainWallet(uid) {
65
  {
66
  balance: CAPTAIN_STARTER_BALANCE,
67
  starterBalanceApplied: true,
68
- updatedAt: new Date().toISOString(),
 
69
  },
70
  { merge: true },
71
  );
 
46
  async function ensureCaptainWallet(uid) {
47
  const walletRef = db.collection('wallets').doc(uid);
48
  const walletSnap = await walletRef.get();
49
+ const now = new Date().toISOString();
50
  if (!walletSnap.exists) {
51
  await walletRef.set({
52
  id: uid,
53
  userId: uid,
54
  balance: CAPTAIN_STARTER_BALANCE,
55
  starterBalanceApplied: true,
56
+ starterBalanceAmount: CAPTAIN_STARTER_BALANCE,
57
+ createdAt: now,
58
+ updatedAt: now,
59
  });
60
  return;
61
  }
 
67
  {
68
  balance: CAPTAIN_STARTER_BALANCE,
69
  starterBalanceApplied: true,
70
+ starterBalanceAmount: CAPTAIN_STARTER_BALANCE,
71
+ updatedAt: now,
72
  },
73
  { merge: true },
74
  );
src/utils/walletHelper.js CHANGED
@@ -6,14 +6,16 @@ const CAPTAIN_STARTER_BALANCE = 5000;
6
  async function ensureWallet(uid) {
7
  const ref = db.collection('wallets').doc(uid);
8
  const doc = await ref.get();
 
9
  if (!doc.exists) {
10
  const wallet = {
11
  id: uid,
12
  userId: uid,
13
  balance: CAPTAIN_STARTER_BALANCE,
14
  starterBalanceApplied: true,
15
- createdAt: new Date().toISOString(),
16
- updatedAt: new Date().toISOString(),
 
17
  };
18
  await ref.set(wallet);
19
  return wallet;
@@ -21,12 +23,22 @@ async function ensureWallet(uid) {
21
 
22
  const data = doc.data() || {};
23
  const currentBalance = Number(data.balance || 0);
24
- if (data.starterBalanceApplied !== true && currentBalance <= 0) {
 
 
 
 
 
 
 
 
 
25
  const updated = {
26
  ...data,
27
  balance: CAPTAIN_STARTER_BALANCE,
28
  starterBalanceApplied: true,
29
- updatedAt: new Date().toISOString(),
 
30
  };
31
  await ref.set(updated, { merge: true });
32
  return { id: uid, ...updated };
 
6
  async function ensureWallet(uid) {
7
  const ref = db.collection('wallets').doc(uid);
8
  const doc = await ref.get();
9
+ const now = new Date().toISOString();
10
  if (!doc.exists) {
11
  const wallet = {
12
  id: uid,
13
  userId: uid,
14
  balance: CAPTAIN_STARTER_BALANCE,
15
  starterBalanceApplied: true,
16
+ starterBalanceAmount: CAPTAIN_STARTER_BALANCE,
17
+ createdAt: now,
18
+ updatedAt: now,
19
  };
20
  await ref.set(wallet);
21
  return wallet;
 
23
 
24
  const data = doc.data() || {};
25
  const currentBalance = Number(data.balance || 0);
26
+ const starterMissing = data.starterBalanceApplied !== true;
27
+ const legacyZeroWallet =
28
+ currentBalance <= 0 && data.starterBalanceAmount !== CAPTAIN_STARTER_BALANCE;
29
+ if (starterMissing || legacyZeroWallet) {
30
+ const txSnap = await db
31
+ .collection('transactions')
32
+ .where('walletId', '==', uid)
33
+ .limit(1)
34
+ .get();
35
+ if (!txSnap.empty && !starterMissing) return { id: uid, ...data };
36
  const updated = {
37
  ...data,
38
  balance: CAPTAIN_STARTER_BALANCE,
39
  starterBalanceApplied: true,
40
+ starterBalanceAmount: CAPTAIN_STARTER_BALANCE,
41
+ updatedAt: now,
42
  };
43
  await ref.set(updated, { merge: true });
44
  return { id: uid, ...updated };