everydaycats commited on
Commit
a1aa9ee
Β·
verified Β·
1 Parent(s): 67e4390

Update apps/aura_measure.js

Browse files
Files changed (1) hide show
  1. apps/aura_measure.js +40 -36
apps/aura_measure.js CHANGED
@@ -46,6 +46,41 @@ function invalidateCaches(type) {
46
  }
47
  }
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  const BASE_RULES = `You are Aura Cat πŸ•ΆοΈπŸˆ, the ultimate judge of social cachet, vibe, and "Aura".
50
  The user will upload a selfie, outfit pic, video frame, or screenshot of a text conversation.
51
 
@@ -136,7 +171,6 @@ Rules for the string:
136
  - If they're mid: be funny and constructive
137
  - The score must feel EARNED. Scarcity is what makes high aura meaningful.`;
138
 
139
-
140
  const AURA_PROMPT_STANDALONE = `You are Aura Cat πŸ•ΆοΈπŸˆ, the ultimate judge of social cachet. Evaluate the provided image absolutely. ${BASE_RULES}`;
141
  const AURA_PROMPT_CONTEXTUAL = `You are Aura Cat πŸ•ΆοΈπŸˆ, the ultimate judge of social cachet.
142
  You are evaluating a submission to a Challenge or Thread.
@@ -150,36 +184,11 @@ router.post('/initialize', async (req, res) => {
150
  const { device_id, push_token, username, avatar } = req.body;
151
  if (!device_id) return res.status(400).json({ success: false, error: 'device_id required' });
152
 
153
- console.log(`Push token: ${push_token}`);
154
-
155
- // 1. Check if profile exists first
156
- const { data: existing } = await supabase
157
- .from('aura_profiles')
158
- .select('id')
159
- .eq('device_id', device_id)
160
- .maybeSingle();
161
-
162
- // 2. Prepare payload
163
- const updateData = { last_login_at: new Date().toISOString() };
164
- if (push_token) updateData.push_token = push_token;
165
 
166
- if (!existing) {
167
- updateData.username = username || `Sorcerer_${Math.floor(Math.random() * 9000) + 1000}`;
168
- updateData.avatar = avatar || 'πŸ‘€';
169
- } else {
170
- if (username) updateData.username = username;
171
- if (avatar) updateData.avatar = avatar;
172
- }
173
-
174
- // 3. Upsert safely
175
- const { data, error } = await supabase
176
- .from('aura_profiles')
177
- .upsert({ device_id, ...updateData }, { onConflict: 'device_id' })
178
- .select('id, username, avatar, current_grade, cumulative_aura')
179
- .single();
180
 
181
- if (error) throw error;
182
- res.json({ success: true, profile: data });
183
  } catch (err) {
184
  console.error('[Initialize] Error:', err);
185
  res.status(500).json({ success: false, error: err.message });
@@ -198,13 +207,8 @@ router.post('/analyze', async (req, res) => {
198
  return res.status(400).json({ success: false, error: 'Missing media or device_id.' });
199
  }
200
 
201
- const { data: profile, error: profileError } = await supabase
202
- .from('aura_profiles')
203
- .upsert({ device_id, username, avatar, last_login_at: new Date().toISOString() }, { onConflict: 'device_id' })
204
- .select('id')
205
- .single();
206
-
207
- if (profileError) throw profileError;
208
  console.log('[Analyze] Profile ID:', profile.id);
209
 
210
  let systemPrompt = AURA_PROMPT_STANDALONE;
 
46
  }
47
  }
48
 
49
+ // ─── Shared profile bootstrap logic ───────────────────────────────────────────
50
+ // Used by both /initialize and /analyze to guarantee a valid profile always exists
51
+ async function ensureProfile({ device_id, username, avatar, push_token }) {
52
+ // 1. Check if profile already exists
53
+ const { data: existing } = await supabase
54
+ .from('aura_profiles')
55
+ .select('id, username, avatar, current_grade, cumulative_aura')
56
+ .eq('device_id', device_id)
57
+ .maybeSingle();
58
+
59
+ // 2. Build update payload
60
+ const updateData = { last_login_at: new Date().toISOString() };
61
+ if (push_token) updateData.push_token = push_token;
62
+
63
+ if (!existing) {
64
+ // Brand new user β€” must satisfy NOT NULL constraints with safe fallbacks
65
+ updateData.username = username || `Sorcerer_${Math.floor(Math.random() * 9000) + 1000}`;
66
+ updateData.avatar = avatar || 'πŸ‘€';
67
+ } else {
68
+ // Existing user β€” only overwrite if frontend explicitly sent values
69
+ if (username) updateData.username = username;
70
+ if (avatar) updateData.avatar = avatar;
71
+ }
72
+
73
+ // 3. Upsert and return full profile
74
+ const { data, error } = await supabase
75
+ .from('aura_profiles')
76
+ .upsert({ device_id, ...updateData }, { onConflict: 'device_id' })
77
+ .select('id, username, avatar, current_grade, cumulative_aura')
78
+ .single();
79
+
80
+ if (error) throw error;
81
+ return data;
82
+ }
83
+
84
  const BASE_RULES = `You are Aura Cat πŸ•ΆοΈπŸˆ, the ultimate judge of social cachet, vibe, and "Aura".
85
  The user will upload a selfie, outfit pic, video frame, or screenshot of a text conversation.
86
 
 
171
  - If they're mid: be funny and constructive
172
  - The score must feel EARNED. Scarcity is what makes high aura meaningful.`;
173
 
 
174
  const AURA_PROMPT_STANDALONE = `You are Aura Cat πŸ•ΆοΈπŸˆ, the ultimate judge of social cachet. Evaluate the provided image absolutely. ${BASE_RULES}`;
175
  const AURA_PROMPT_CONTEXTUAL = `You are Aura Cat πŸ•ΆοΈπŸˆ, the ultimate judge of social cachet.
176
  You are evaluating a submission to a Challenge or Thread.
 
184
  const { device_id, push_token, username, avatar } = req.body;
185
  if (!device_id) return res.status(400).json({ success: false, error: 'device_id required' });
186
 
187
+ console.log(`[Initialize] device_id: ${device_id} push_token: ${push_token}`);
 
 
 
 
 
 
 
 
 
 
 
188
 
189
+ const profile = await ensureProfile({ device_id, push_token, username, avatar });
190
+ res.json({ success: true, profile });
 
 
 
 
 
 
 
 
 
 
 
 
191
 
 
 
192
  } catch (err) {
193
  console.error('[Initialize] Error:', err);
194
  res.status(500).json({ success: false, error: err.message });
 
207
  return res.status(400).json({ success: false, error: 'Missing media or device_id.' });
208
  }
209
 
210
+ // Guarantee profile exists with full fallback logic β€” handles brand new users too
211
+ const profile = await ensureProfile({ device_id, username, avatar });
 
 
 
 
 
212
  console.log('[Analyze] Profile ID:', profile.id);
213
 
214
  let systemPrompt = AURA_PROMPT_STANDALONE;