-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathprofile.service.ts
More file actions
73 lines (61 loc) · 2.42 KB
/
profile.service.ts
File metadata and controls
73 lines (61 loc) · 2.42 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
import { inject, Injectable, Signal } from '@angular/core';
import { SupabaseStorage } from '@jet/enums/supabase-storage.enum';
import { SupabaseTable } from '@jet/enums/supabase-table.enum';
import { SUPABASE_CLIENT } from '@jet/injection-tokens/supabase-client.injection-token';
import { Profile } from '@jet/interfaces/profile.interface';
import { FileObject, StorageError } from '@supabase/storage-js';
import { User } from '@supabase/supabase-js';
import { LoggerService } from '../logger/logger.service';
import { UserService } from '../user/user.service';
@Injectable()
export class ProfileService {
readonly #supabaseClient = inject(SUPABASE_CLIENT);
readonly #loggerService = inject(LoggerService);
readonly #userService = inject(UserService);
readonly #user: Signal<null | User>;
public constructor() {
this.#user = this.#userService.user;
this.#loggerService.logServiceInitialization('ProfileService');
}
public deleteAvatar(
publicUrl: string,
): Promise<{ data: FileObject[]; error: null } | { data: null; error: StorageError }> {
const fileName: string | undefined = publicUrl.split('/').pop();
const path: string = `${this.#userService.user()?.id}/${fileName}`;
return this.#supabaseClient.storage.from(SupabaseStorage.ProfileAvatars).remove([path]);
}
public getAvatarPublicUrl(path: string): string {
const { data } = this.#supabaseClient.storage
.from(SupabaseStorage.ProfileAvatars)
.getPublicUrl(path);
return data.publicUrl;
}
public selectProfile() {
return this.#supabaseClient
.from(SupabaseTable.Profiles)
.select()
.eq('user_id', this.#user()?.id)
.single()
.throwOnError();
}
public updateAndSelectProfile(partialProfile: Partial<Profile>) {
return this.#supabaseClient
.from(SupabaseTable.Profiles)
.update(partialProfile)
.eq('user_id', this.#user()?.id)
.select()
.single()
.throwOnError();
}
public uploadAvatar(
file: File,
): Promise<
| { data: { fullPath: string; id: string; path: string }; error: null }
| { data: null; error: StorageError }
> {
const fileExtension: string | undefined = file.name.split('.').pop();
const timestamp: number = Date.now();
const path: string = `${this.#userService.user()?.id}/avatar-${timestamp}.${fileExtension}`;
return this.#supabaseClient.storage.from(SupabaseStorage.ProfileAvatars).upload(path, file);
}
}