forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.service.ts
More file actions
71 lines (65 loc) · 2.21 KB
/
Copy pathprofile.service.ts
File metadata and controls
71 lines (65 loc) · 2.21 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
import {
Injectable,
NotFoundException,
BadRequestException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { UserProfile, DefaultSplitType } from './profile.entity';
import { UpdateProfileDto } from './dto/update-profile.dto';
import { CurrencyService } from '../modules/currency/currency.service';
@Injectable()
export class ProfileService {
constructor(
@InjectRepository(UserProfile)
private readonly profileRepository: Repository<UserProfile>,
private readonly currencyService: CurrencyService,
) {}
async getByWalletAddress(walletAddress: string): Promise<UserProfile> {
const profile = await this.profileRepository.findOne({
where: { walletAddress },
});
if (!profile) {
throw new NotFoundException(
`Profile for wallet address ${walletAddress} not found`,
);
}
return profile;
}
async update(
walletAddress: string,
dto: UpdateProfileDto,
): Promise<UserProfile> {
let preferredCurrency: string | undefined;
if (dto.preferredCurrency !== undefined) {
const supported = this.currencyService.getSupportedCurrencies();
const normalized = dto.preferredCurrency.toUpperCase().trim();
if (!supported.includes(normalized)) {
throw new BadRequestException(
`Currency "${dto.preferredCurrency}" is not supported. Supported: ${supported.join(', ')}`,
);
}
preferredCurrency = normalized;
}
let profile = await this.profileRepository.findOne({
where: { walletAddress },
});
if (!profile) {
profile = this.profileRepository.create({
walletAddress,
displayName: dto.displayName ?? null,
avatarUrl: dto.avatarUrl ?? null,
preferredCurrency: preferredCurrency ?? 'USD',
defaultSplitType: dto.defaultSplitType ?? DefaultSplitType.EQUAL,
emailNotifications: dto.emailNotifications ?? true,
pushNotifications: dto.pushNotifications ?? true,
});
} else {
Object.assign(profile, {
...dto,
...(preferredCurrency !== undefined && { preferredCurrency }),
});
}
return await this.profileRepository.save(profile);
}
}