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

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +37 -8
index.js CHANGED
@@ -92,7 +92,7 @@ if (!isMainThread) {
92
  stmts.updateProfileWithUsername.run({
93
  id: data.userId,
94
  username: data.username,
95
- name: data.name || 'Unknown',
96
  bio: data.bio || '',
97
  date_of_birth: data.date_of_birth || '',
98
  is_profile_public: data.is_profile_public || 'true'
@@ -100,7 +100,7 @@ if (!isMainThread) {
100
  } else {
101
  stmts.updateProfile.run({
102
  id: data.userId,
103
- name: data.name || 'Unknown',
104
  bio: data.bio || '',
105
  date_of_birth: data.date_of_birth || '',
106
  is_profile_public: data.is_profile_public || 'true'
@@ -523,40 +523,69 @@ async function setupFastify() {
523
  });
524
 
525
  fastify.put('/updateprofiledetails', async (request, reply) => {
526
- const { id, username, ...data } = request.body || {};
 
527
  if (!id) {
528
  reply.status(400).send({ detail: 'Missing ID' });
529
  return;
530
  }
 
531
  const user = await dbRequest('getUserById', { userId: id });
532
  if (!user) {
533
  reply.status(404).send({ detail: 'Not found' });
534
  return;
535
  }
536
- if (data.name && data.name.length > MAX_NAME_LENGTH) {
 
537
  reply.status(400).send({ detail: `Name max ${MAX_NAME_LENGTH} chars` });
538
  return;
539
  }
540
- if (data.bio && data.bio.length > MAX_BIO_LENGTH) {
 
541
  reply.status(400).send({ detail: `Bio max ${MAX_BIO_LENGTH} chars` });
542
  return;
543
  }
544
- if (username !== undefined && username !== null) {
 
 
545
  const cleanUsername = username.trim().toLowerCase();
 
546
  if (cleanUsername.length < 3 || cleanUsername.length > MAX_USERNAME_LENGTH) {
547
  reply.status(400).send({ detail: `Username must be 3-${MAX_USERNAME_LENGTH} chars` });
548
  return;
549
  }
 
550
  if (cleanUsername !== user.username) {
551
  const existingUser = await dbRequest('getUserByUsername', { username: cleanUsername });
552
  if (existingUser) {
553
  reply.status(409).send({ detail: 'Username exists' });
554
  return;
555
  }
556
- data.username = cleanUsername;
 
 
 
 
 
 
 
 
 
 
 
 
557
  }
558
  }
559
- await dbRequest('updateProfile', { userId: id, ...data });
 
 
 
 
 
 
 
 
 
560
  reply.send({ success: true });
561
  });
562
 
 
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'
 
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'
 
523
  });
524
 
525
  fastify.put('/updateprofiledetails', async (request, reply) => {
526
+ const { id, username, name, bio, date_of_birth, is_profile_public } = request.body || {};
527
+
528
  if (!id) {
529
  reply.status(400).send({ detail: 'Missing ID' });
530
  return;
531
  }
532
+
533
  const user = await dbRequest('getUserById', { userId: id });
534
  if (!user) {
535
  reply.status(404).send({ detail: 'Not found' });
536
  return;
537
  }
538
+
539
+ if (name && name.length > MAX_NAME_LENGTH) {
540
  reply.status(400).send({ detail: `Name max ${MAX_NAME_LENGTH} chars` });
541
  return;
542
  }
543
+
544
+ if (bio && bio.length > MAX_BIO_LENGTH) {
545
  reply.status(400).send({ detail: `Bio max ${MAX_BIO_LENGTH} chars` });
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` });
555
  return;
556
  }
557
+
558
  if (cleanUsername !== user.username) {
559
  const existingUser = await dbRequest('getUserByUsername', { username: cleanUsername });
560
  if (existingUser) {
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 });
590
  });
591