blaze-aura69 commited on
Commit
e38da2c
·
verified ·
1 Parent(s): cd4aba4

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +38 -36
index.js CHANGED
@@ -54,11 +54,11 @@ if (!isMainThread) {
54
  updateLogin: db.prepare('UPDATE users SET is_login = ? WHERE id = ?'),
55
  updateProfileWithUsername: db.prepare(`
56
  UPDATE users SET
57
- username = COALESCE(NULLIF(@username, ''), username),
58
- name = COALESCE(NULLIF(@name, ''), name),
59
- bio = COALESCE(NULLIF(@bio, ''), bio),
60
- date_of_birth = COALESCE(NULLIF(@date_of_birth, ''), date_of_birth),
61
- is_profile_public = COALESCE(NULLIF(@is_profile_public, ''), is_profile_public)
62
  WHERE id = @id
63
  `),
64
  updatePhoto: db.prepare('UPDATE users SET pic_url = ? WHERE id = ?'),
@@ -92,18 +92,18 @@ if (!isMainThread) {
92
  stmts.updateProfileWithUsername.run({
93
  id: data.userId,
94
  username: data.username,
95
- name: data.name || '',
96
- bio: data.bio || '',
97
- date_of_birth: data.date_of_birth || '',
98
- is_profile_public: data.is_profile_public || 'true'
99
  });
100
  } else {
101
  stmts.updateProfile.run({
102
  id: data.userId,
103
- name: data.name || '',
104
- bio: data.bio || '',
105
- date_of_birth: data.date_of_birth || '',
106
- is_profile_public: data.is_profile_public || 'true'
107
  });
108
  }
109
  result = { success: true };
@@ -144,9 +144,22 @@ if (!isMainThread) {
144
  break;
145
  case 'updateProfile':
146
  if (op.data && op.data.username) {
147
- stmts.updateProfileWithUsername.run(op.data);
 
 
 
 
 
 
 
148
  } else {
149
- stmts.updateProfile.run(op.data);
 
 
 
 
 
 
150
  }
151
  break;
152
  case 'updatePhoto':
@@ -546,9 +559,9 @@ async function setupFastify() {
546
  return;
547
  }
548
 
549
- // Handle username update
550
- if (username !== undefined && username !== null && username !== '') {
551
- const cleanUsername = username.trim().toLowerCase();
552
 
553
  if (cleanUsername.length < 3 || cleanUsername.length > MAX_USERNAME_LENGTH) {
554
  reply.status(400).send({ detail: `Username must be 3-${MAX_USERNAME_LENGTH} chars` });
@@ -561,29 +574,18 @@ async function setupFastify() {
561
  reply.status(409).send({ detail: 'Username exists' });
562
  return;
563
  }
564
-
565
- // Pass username explicitly
566
- await dbRequest('updateProfile', {
567
- userId: id,
568
- username: cleanUsername,
569
- name: name || '',
570
- bio: bio || '',
571
- date_of_birth: date_of_birth || '',
572
- is_profile_public: is_profile_public || 'true'
573
- });
574
-
575
- reply.send({ success: true });
576
- return;
577
  }
578
  }
579
 
580
- // Update without username change
581
  await dbRequest('updateProfile', {
582
  userId: id,
583
- name: name || '',
584
- bio: bio || '',
585
- date_of_birth: date_of_birth || '',
586
- is_profile_public: is_profile_public || 'true'
 
587
  });
588
 
589
  reply.send({ success: true });
 
54
  updateLogin: db.prepare('UPDATE users SET is_login = ? WHERE id = ?'),
55
  updateProfileWithUsername: db.prepare(`
56
  UPDATE users SET
57
+ username = @username,
58
+ name = @name,
59
+ bio = @bio,
60
+ date_of_birth = @date_of_birth,
61
+ is_profile_public = @is_profile_public
62
  WHERE id = @id
63
  `),
64
  updatePhoto: db.prepare('UPDATE users SET pic_url = ? WHERE id = ?'),
 
92
  stmts.updateProfileWithUsername.run({
93
  id: data.userId,
94
  username: data.username,
95
+ name: data.name,
96
+ bio: data.bio,
97
+ date_of_birth: data.date_of_birth,
98
+ is_profile_public: data.is_profile_public
99
  });
100
  } else {
101
  stmts.updateProfile.run({
102
  id: data.userId,
103
+ name: data.name,
104
+ bio: data.bio,
105
+ date_of_birth: data.date_of_birth,
106
+ is_profile_public: data.is_profile_public
107
  });
108
  }
109
  result = { success: true };
 
144
  break;
145
  case 'updateProfile':
146
  if (op.data && op.data.username) {
147
+ stmts.updateProfileWithUsername.run({
148
+ id: op.data.id,
149
+ username: op.data.username,
150
+ name: op.data.name,
151
+ bio: op.data.bio,
152
+ date_of_birth: op.data.date_of_birth,
153
+ is_profile_public: op.data.is_profile_public
154
+ });
155
  } else {
156
+ stmts.updateProfile.run({
157
+ id: op.data.id,
158
+ name: op.data.name,
159
+ bio: op.data.bio,
160
+ date_of_birth: op.data.date_of_birth,
161
+ is_profile_public: op.data.is_profile_public
162
+ });
163
  }
164
  break;
165
  case 'updatePhoto':
 
559
  return;
560
  }
561
 
562
+ let cleanUsername = null;
563
+ if (username && username.trim() !== '') {
564
+ cleanUsername = username.trim().toLowerCase();
565
 
566
  if (cleanUsername.length < 3 || cleanUsername.length > MAX_USERNAME_LENGTH) {
567
  reply.status(400).send({ detail: `Username must be 3-${MAX_USERNAME_LENGTH} chars` });
 
574
  reply.status(409).send({ detail: 'Username exists' });
575
  return;
576
  }
577
+ } else {
578
+ cleanUsername = null;
 
 
 
 
 
 
 
 
 
 
 
579
  }
580
  }
581
 
 
582
  await dbRequest('updateProfile', {
583
  userId: id,
584
+ username: cleanUsername,
585
+ name: name || user.name,
586
+ bio: bio !== undefined ? bio : user.bio,
587
+ date_of_birth: date_of_birth || user.date_of_birth,
588
+ is_profile_public: is_profile_public || user.is_profile_public
589
  });
590
 
591
  reply.send({ success: true });