forked from StepFi-app/StepFi-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.repository.ts
More file actions
354 lines (313 loc) · 12.9 KB
/
Copy pathusers.repository.ts
File metadata and controls
354 lines (313 loc) · 12.9 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import { Injectable, InternalServerErrorException, ConflictException } from '@nestjs/common';
import { SupabaseService } from '../supabase.client';
import { UpdateUserDto } from '../../modules/users/dto/update-user.dto';
/** Multipart upload file shape produced by the profile-image interceptor. */
export interface UploadedAvatarFile {
originalname: string;
buffer: Buffer;
mimetype: string;
size?: number;
}
export interface UserPreferencesRecord {
notifications_enabled: boolean;
language: string;
theme: string;
}
export type UserRole = 'sponsor' | 'vendor' | 'mentor';
export interface UserRecord {
id: string;
wallet_address: string;
username?: string | null;
display_name: string | null;
avatar_url: string | null;
status: 'active' | 'blocked';
/** Permanent role chosen once after registration — null until chosen */
role: UserRole | null;
created_at: string;
/** Nested from user_preferences table — null if row does not exist yet */
user_preferences: UserPreferencesRecord | null;
}
/**
* Encapsulates all Supabase queries for the `users` table.
*
* The service-role client is used for write operations so that
* Row Level Security does not block the auto-creation on first login.
*/
@Injectable()
export class UsersRepository {
constructor(private readonly supabaseService: SupabaseService) { }
/**
* Returns the user row (with nested preferences) matching the wallet,
* or null if no matching row exists yet.
*
* Note: Supabase nested selects always return the relation as an array,
* even for 1-to-1 relations. We normalize it to a single record here.
*/
async findByWallet(wallet: string): Promise<UserRecord | null> {
const { data, error } = await this.supabaseService
.getServiceRoleClient()
.from('users')
.select(
'id, wallet_address, username, display_name, avatar_url, status, role, created_at, user_preferences(notifications_enabled, language, theme)',
)
.eq('wallet_address', wallet)
.maybeSingle();
if (error) {
throw new InternalServerErrorException({
code: 'DATABASE_QUERY_ERROR',
message: error.message,
});
}
if (!data) return null;
// Normalize: Supabase returns the nested relation as an array
const raw = data as unknown as Omit<UserRecord, 'user_preferences'> & {
user_preferences: UserPreferencesRecord[];
};
return {
...raw,
user_preferences: raw.user_preferences?.[0] ?? null,
};
}
/**
* Inserts a new user row with default values for the given wallet address.
* Called automatically on the user's first authenticated request.
*/
async create(wallet: string): Promise<UserRecord> {
const { data, error } = await this.supabaseService
.getServiceRoleClient()
.from('users')
.insert({ wallet_address: wallet })
.select('id, wallet_address, username, display_name, avatar_url, status, role, created_at')
.single();
if (error) {
throw new InternalServerErrorException({
code: 'DATABASE_QUERY_ERROR',
message: error.message,
});
}
return { ...(data as Omit<UserRecord, 'user_preferences'>), user_preferences: null };
}
/**
* Inserts a default user_preferences row for the given user ID.
* Called when a user exists but has no preferences row yet (first-access or legacy users).
*/
async createDefaultPreferences(userId: string): Promise<UserPreferencesRecord> {
const { data, error } = await this.supabaseService
.getServiceRoleClient()
.from('user_preferences')
.insert({ user_id: userId })
.select('notifications_enabled, language, theme')
.single();
if (error) {
throw new InternalServerErrorException({
code: 'DATABASE_QUERY_ERROR',
message: error.message,
});
}
return data as UserPreferencesRecord;
}
/**
* Updates the user's profile fields and/or preferences.
* Uses upsert so the row is created if it doesn't exist yet.
* `updated_at` is maintained automatically by the DB trigger.
*
* @param wallet - Stellar wallet address (from JWT via JwtAuthGuard)
* @param data - Validated and sanitized update payload (API-05)
*/
async update(
wallet: string,
data: UpdateUserDto,
): Promise<{ wallet_address: string; display_name: string | null; avatar_url: string | null; updated_at: string; id: string }> {
const client = this.supabaseService.getServiceRoleClient();
// Build the users table payload — only include provided fields
const userPayload: Record<string, unknown> = { wallet_address: wallet };
if (data.name !== undefined) userPayload.display_name = data.name;
if (data.avatar !== undefined) userPayload.avatar_url = data.avatar;
const { data: user, error: userError } = await client
.from('users')
.upsert(userPayload, { onConflict: 'wallet_address' })
.select('id, wallet_address, display_name, avatar_url, updated_at')
.single();
if (userError || !user) {
throw new InternalServerErrorException({
code: 'DATABASE_USER_UPDATE_FAILED',
message: 'Failed to update user profile.',
});
}
// Update preferences if provided
if (data.preferences !== undefined) {
const prefPayload: Record<string, unknown> = { user_id: user.id };
if (data.preferences.notifications !== undefined) prefPayload.notifications_enabled = data.preferences.notifications;
if (data.preferences.theme !== undefined) prefPayload.theme = data.preferences.theme;
if (data.preferences.language !== undefined) prefPayload.language = data.preferences.language;
const { error: prefError } = await client
.from('user_preferences')
.upsert(prefPayload, { onConflict: 'user_id' });
if (prefError) {
throw new InternalServerErrorException({
code: 'DATABASE_PREFERENCES_UPDATE_FAILED',
message: 'Failed to update user preferences.',
});
}
}
return user as { wallet_address: string; display_name: string | null; avatar_url: string | null; updated_at: string; id: string };
}
/**
* Returns only the user's current role, or null if the user does not exist
* or has not chosen a role yet.
*/
async findRoleByWallet(wallet: string): Promise<UserRole | null> {
const { data, error } = await this.supabaseService
.getServiceRoleClient()
.from('users')
.select('role')
.eq('wallet_address', wallet)
.maybeSingle();
if (error) {
throw new InternalServerErrorException({
code: 'DATABASE_QUERY_ERROR',
message: error.message,
});
}
return (data?.role as UserRole | undefined) ?? null;
}
/**
* Sets the user's role, but only if no role is set yet.
*
* The `.is('role', null)` filter makes the write atomic: two concurrent
* requests cannot both succeed, because the second one matches zero rows.
* Returns the updated row, or null when no row was updated (role already
* set, or user does not exist).
*/
async setRoleIfUnset(
wallet: string,
role: UserRole,
): Promise<{ wallet_address: string; role: UserRole } | null> {
const { data, error } = await this.supabaseService
.getServiceRoleClient()
.from('users')
.update({ role })
.eq('wallet_address', wallet)
.is('role', null)
.select('wallet_address, role')
.maybeSingle();
if (error) {
throw new InternalServerErrorException({
code: 'DATABASE_ROLE_UPDATE_FAILED',
message: 'Failed to set user role.',
});
}
return (data as { wallet_address: string; role: UserRole } | null) ?? null;
}
/**
* Admin override: sets or resets the user's role regardless of whether a role was set previously.
* Returns the updated user row, or null if the user does not exist.
*/
async forceSetRole(
wallet: string,
role: UserRole | null,
): Promise<{ wallet_address: string; role: UserRole | null } | null> {
const { data, error } = await this.supabaseService
.getServiceRoleClient()
.from('users')
.update({ role })
.eq('wallet_address', wallet)
.select('wallet_address, role')
.maybeSingle();
if (error) {
throw new InternalServerErrorException({
code: 'DATABASE_ROLE_UPDATE_FAILED',
message: 'Failed to update user role.',
});
}
return (data as { wallet_address: string; role: UserRole | null } | null) ?? null;
}
// --- REGISTRATION METHODS ---
async checkUsernameExists(username: string): Promise<boolean> {
const { data, error } = await this.supabaseService
.getServiceRoleClient()
.from('users')
.select('id')
.eq('username', username)
.maybeSingle();
if (error) {
throw new InternalServerErrorException({
code: 'DATABASE_QUERY_ERROR',
message: error.message,
});
}
return !!data;
}
async createProfile(data: { wallet: string; username: string; displayName: string; avatarUrl: string | null }): Promise<UserRecord> {
const { data: user, error } = await this.supabaseService
.getServiceRoleClient()
.from('users')
.insert({
wallet_address: data.wallet,
username: data.username,
display_name: data.displayName,
avatar_url: data.avatarUrl,
status: 'active',
})
.select('id, wallet_address, username, display_name, avatar_url, status, role, created_at')
.single();
if (error) {
const combinedErr = `${error.code || ''} ${error.message || ''} ${error.details || ''} ${error.hint || ''}`;
if (error.code === '23505' || combinedErr.includes('duplicate key') || combinedErr.includes('unique constraint')) {
if (combinedErr.includes('username')) {
throw new ConflictException({
code: 'AUTH_USERNAME_TAKEN',
message: 'Username is already taken.',
});
}
throw new ConflictException({
code: 'AUTH_WALLET_EXISTS',
message: 'Wallet address is already registered.',
});
}
throw new InternalServerErrorException({
code: 'DATABASE_INSERT_ERROR',
message: `Failed to create user profile: ${error.message}`,
});
}
return { ...(user as Omit<UserRecord, 'user_preferences'>), user_preferences: null };
}
async uploadAvatar(walletAddress: string, file: UploadedAvatarFile): Promise<string> {
const fileExt = file.originalname.split('.').pop();
const fileName = `${walletAddress}-${Date.now()}.${fileExt}`;
const client = this.supabaseService.getServiceRoleClient();
const { error } = await client
.storage
.from('avatars')
.upload(fileName, file.buffer, {
contentType: file.mimetype,
upsert: true,
});
if (error) {
throw new InternalServerErrorException({
code: 'STORAGE_UPLOAD_FAILED',
message: `Failed to upload avatar: ${error.message}`,
});
}
const { data } = client.storage.from('avatars').getPublicUrl(fileName);
return data.publicUrl;
}
async deleteAvatar(avatarUrl: string): Promise<void> {
try {
const fileName = avatarUrl.substring(avatarUrl.lastIndexOf('/') + 1);
if (!fileName) return;
const client = this.supabaseService.getServiceRoleClient();
await client.storage.from('avatars').remove([fileName]);
} catch {
// Ignore cleanup failures
}
}
async deleteUserById(id: string): Promise<void> {
try {
const client = this.supabaseService.getServiceRoleClient();
await client.from('users').delete().eq('id', id);
} catch {
// Ignore cleanup failures
}
}
}