|
|
|
@ -1,21 +1,30 @@
@@ -1,21 +1,30 @@
|
|
|
|
|
import { Inject, Injectable } from '@nestjs/common' |
|
|
|
|
import { $config } from 'src/config' |
|
|
|
|
import { Mailer, Sessions, Sms, Users } from 'src/core' |
|
|
|
|
import { MAILER_SERVICE, SESSIONS_SERVICE, SMS_SERVICE, USERS_SERVICE } from 'src/core/consts' |
|
|
|
|
import { FilesStorage, Mailer, Sessions, Sms, Users } from 'src/core' |
|
|
|
|
import { |
|
|
|
|
FILES_STORAGE_SERVICE, |
|
|
|
|
MAILER_SERVICE, |
|
|
|
|
SESSIONS_SERVICE, |
|
|
|
|
SMS_SERVICE, |
|
|
|
|
USERS_PHONE_NUMBERS_SERVICE, |
|
|
|
|
USERS_SERVICE, |
|
|
|
|
} from 'src/core/consts' |
|
|
|
|
import { |
|
|
|
|
getChangePasswordNotificationHTML, |
|
|
|
|
getChangePasswordNotificationText, |
|
|
|
|
} from 'src/shared/templates' |
|
|
|
|
import { UpdateAccountDto, UpdatePasswordDto } from '../dto' |
|
|
|
|
|
|
|
|
|
import * as _ from 'lodash' |
|
|
|
|
@Injectable() |
|
|
|
|
export class AdminAccountsService { |
|
|
|
|
constructor( |
|
|
|
|
@Inject(USERS_SERVICE) private readonly usersService: Users.IUsersService, |
|
|
|
|
@Inject(SESSIONS_SERVICE) private readonly sessionsService: Sessions.ISessionsService, |
|
|
|
|
@Inject(MAILER_SERVICE) private readonly mailerService: Mailer.IMailerService, |
|
|
|
|
@Inject(SMS_SERVICE) private readonly smsService: Sms.ISmsService, |
|
|
|
|
) {} |
|
|
|
|
@Inject(USERS_SERVICE) private readonly usersService: Users.IUsersService |
|
|
|
|
@Inject(SESSIONS_SERVICE) private readonly sessionsService: Sessions.ISessionsService |
|
|
|
|
@Inject(MAILER_SERVICE) private readonly mailerService: Mailer.IMailerService |
|
|
|
|
@Inject(SMS_SERVICE) private readonly smsService: Sms.ISmsService |
|
|
|
|
@Inject(USERS_PHONE_NUMBERS_SERVICE) |
|
|
|
|
private readonly phoneNumbersService: Users.IUsersPhoneNumbersService |
|
|
|
|
@Inject(FILES_STORAGE_SERVICE) |
|
|
|
|
private readonly filesStorageService: FilesStorage.IFilesStorageService |
|
|
|
|
|
|
|
|
|
public async getOne(id: number, token: string) { |
|
|
|
|
try { |
|
|
|
@ -26,11 +35,66 @@ export class AdminAccountsService {
@@ -26,11 +35,66 @@ export class AdminAccountsService {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async update(id: number, dto: UpdateAccountDto) { |
|
|
|
|
const user = await this.usersService.getOne(id) |
|
|
|
|
|
|
|
|
|
await this.changePhoneNumber(user.phoneNumber, dto) |
|
|
|
|
await this.changePass(id, dto, user) |
|
|
|
|
|
|
|
|
|
await this.usersService.update({ id, ...dto }) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async changePhoneNumber(oldPhoneNumber: string, dto: UpdateAccountDto) { |
|
|
|
|
if (!dto.phoneNumber) return |
|
|
|
|
|
|
|
|
|
if (dto.password) await this.usersService.changeUserPassword(id, dto.password) |
|
|
|
|
await this.phoneNumbersService.change({ |
|
|
|
|
phoneNumber: oldPhoneNumber, |
|
|
|
|
newPhoneNumber: dto.phoneNumber, |
|
|
|
|
}) |
|
|
|
|
delete dto.phoneNumber |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async changePass(id: number, dto: UpdateAccountDto, user: Users.UserModel) { |
|
|
|
|
if (!dto.password) return |
|
|
|
|
await this.usersService.changeUserPassword(id, dto.password) |
|
|
|
|
|
|
|
|
|
return await this.usersService.getOne(id, ['info']) |
|
|
|
|
await this.sendPasswordToUser(user, dto.password, dto.sendPasswordBy) |
|
|
|
|
await this.sessionsService.closeAllUserSessions(user.id) |
|
|
|
|
|
|
|
|
|
delete dto.password |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async sendPasswordToUser( |
|
|
|
|
_user: Users.UserModel | number, |
|
|
|
|
password: string, |
|
|
|
|
sendBy: 'email' | 'sms', |
|
|
|
|
) { |
|
|
|
|
if (!sendBy) return |
|
|
|
|
|
|
|
|
|
let user |
|
|
|
|
if (_.isNumber(_user)) user = await this.usersService.getOne(_user) |
|
|
|
|
else user = _user |
|
|
|
|
|
|
|
|
|
const params = { |
|
|
|
|
linkToWeb: $config.getLinkToWeb(), |
|
|
|
|
phoneNumber: user.phoneNumber, |
|
|
|
|
login: user.login, |
|
|
|
|
password, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (sendBy === 'email') { |
|
|
|
|
await this.mailerService.send({ |
|
|
|
|
subject: 'Дані для входу в програму Task Me ;)', |
|
|
|
|
to: user.email, |
|
|
|
|
html: getChangePasswordNotificationHTML(params), |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (sendBy === 'sms') { |
|
|
|
|
await this.smsService.send({ |
|
|
|
|
to: user.phoneNumber, |
|
|
|
|
text: getChangePasswordNotificationText(params), |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async changePassword(id: number, dto: UpdatePasswordDto) { |
|
|
|
@ -59,4 +123,36 @@ export class AdminAccountsService {
@@ -59,4 +123,36 @@ export class AdminAccountsService {
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async updateAvatar(id: number, file: FilesStorage.IInputFile) { |
|
|
|
|
try { |
|
|
|
|
const user = await this.usersService.getOne(id, ['info']) |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
if (user.info.avatarUrl) { |
|
|
|
|
await this.filesStorageService.removeObject(user.info.avatarUrl) |
|
|
|
|
} |
|
|
|
|
} catch (e) {} |
|
|
|
|
|
|
|
|
|
const avatarUrl = await this.filesStorageService.putObject(file) |
|
|
|
|
|
|
|
|
|
await this.usersService.update({ id, avatarUrl }) |
|
|
|
|
|
|
|
|
|
return avatarUrl |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log('Update avatar error ', e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async removeAvatar(id: number) { |
|
|
|
|
const user = await this.usersService.getOne(id, ['info']) |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
if (user.info.avatarUrl) { |
|
|
|
|
await this.filesStorageService.removeObject(user.info.avatarUrl) |
|
|
|
|
} |
|
|
|
|
} catch (e) {} |
|
|
|
|
|
|
|
|
|
await this.usersService.update({ id, avatarUrl: null }) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|