more fix and update

This commit is contained in:
lendry
2026-06-24 20:15:19 +03:00
parent dcab6557d3
commit 9727cf3f35
53 changed files with 3479 additions and 494 deletions

View File

@@ -5,6 +5,7 @@ import { PrismaService } from '../infra/prisma.service';
import { SettingsService } from './settings.service';
import { MessagingService } from '../infra/messaging.service';
import { RedisService } from '../infra/redis.service';
import { NotificationsService } from './notifications.service';
@Injectable()
export class OtpService {
@@ -12,10 +13,11 @@ export class OtpService {
private readonly prisma: PrismaService,
private readonly settings: SettingsService,
private readonly messaging: MessagingService,
private readonly redis: RedisService
private readonly redis: RedisService,
private readonly notifications: NotificationsService
) {}
async send(command: { target: string; channel: string; purpose: string; userId?: string }) {
async send(command: { target: string; channel: string; purpose: string; userId?: string; ipAddress?: string }) {
const expiryMinutes = await this.settings.getNumber('OTP_EXPIRY_MINUTES', 10);
const code = String(randomInt(100000, 999999));
const expiresAt = new Date(Date.now() + expiryMinutes * 60_000);
@@ -36,6 +38,16 @@ export class OtpService {
code,
expiryMinutes
});
if (command.userId) {
const ipLabel = command.ipAddress?.trim() || 'неизвестного IP';
await this.notifications.create(
command.userId,
'otp_code',
this.purposeLabel(command.purpose),
`Попытка входа с ${ipLabel} — код для верификации: ${code}`,
{ code, ipAddress: command.ipAddress ?? null, purpose: command.purpose }
);
}
await this.redis.client.del(this.attemptKey(command.target, command.purpose));
return { sent: true, expiresAt: expiresAt.toISOString() };
}
@@ -67,10 +79,26 @@ export class OtpService {
}
await this.redis.client.del(attemptKey);
await this.prisma.authCode.update({ where: { id: authCode.id }, data: { usedAt: new Date() } });
if (authCode.userId) {
await this.notifications.dismissOtpCodeNotifications(authCode.userId);
}
return { verified: true };
}
private attemptKey(target: string, purpose: string) {
return `otp:attempts:${purpose}:${target.toLowerCase()}`;
}
private purposeLabel(purpose: string) {
switch (purpose) {
case 'register':
return 'Регистрация';
case 'login':
return 'Вход в аккаунт';
case 'password-reset':
return 'Сброс пароля';
default:
return 'Подтверждение действия';
}
}
}