chore: get announcements for user that buy service
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { createHmac } from "node:crypto";
|
||||
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { FastifyReply } from "fastify";
|
||||
import slugify from "slugify";
|
||||
@@ -9,6 +9,7 @@ import { In, Not, QueryRunner } from "typeorm";
|
||||
import {
|
||||
AdminMessage,
|
||||
AdsMessage,
|
||||
AuthMessage,
|
||||
CommonMessage,
|
||||
LegalUserMessage,
|
||||
RealUserMessage,
|
||||
@@ -16,10 +17,14 @@ import {
|
||||
} from "../../../common/enums/message.enum";
|
||||
import { AddressService } from "../../address/providers/address.service";
|
||||
import { CompleteRegistrationDto } from "../../auth/DTO/complete-register.dto";
|
||||
import { RequestOtpDto } from "../../auth/DTO/request-otp.dto";
|
||||
import { VerifyOtpDto } from "../../auth/DTO/verify-otp.dto";
|
||||
import { UserSettingsService } from "../../settings/providers/user-settings.service";
|
||||
import { EmailService } from "../../utils/providers/email.service";
|
||||
import { OTPService } from "../../utils/providers/otp.service";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { PasswordService } from "../../utils/providers/password.service";
|
||||
import { SmsService } from "../../utils/providers/sms.service";
|
||||
import { WalletsService } from "../../wallets/providers/wallets.service";
|
||||
import { CheckValidityDTO } from "../DTO/check-validity.dto";
|
||||
import { CreateAdminDto } from "../DTO/create-admin.dto";
|
||||
@@ -47,6 +52,7 @@ import { UserRepository } from "../repositories/users.repository";
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
private readonly logger = new Logger(UsersService.name);
|
||||
constructor(
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly userGroupRepository: UserGroupRepository,
|
||||
@@ -60,6 +66,8 @@ export class UsersService {
|
||||
private readonly addressService: AddressService,
|
||||
private readonly emailService: EmailService,
|
||||
private readonly configService: ConfigService,
|
||||
private readonly otpService: OTPService,
|
||||
private readonly smsService: SmsService,
|
||||
) {}
|
||||
|
||||
/************************************************************ */
|
||||
@@ -178,6 +186,50 @@ export class UsersService {
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
async updatePhone(userId: string, requestOtpDto: RequestOtpDto) {
|
||||
const { phone } = requestOtpDto;
|
||||
const user = await this.userRepository.findOneBy({ id: userId });
|
||||
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||
|
||||
const existPhone = await this.userRepository.findOneBy({ phone });
|
||||
if (existPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
|
||||
|
||||
const existCode = await this.otpService.checkExistOtp(phone, "LOGIN");
|
||||
if (existCode) {
|
||||
return {
|
||||
message: AuthMessage.OTP_ALREADY_SENT,
|
||||
ttlSecond: existCode,
|
||||
};
|
||||
}
|
||||
|
||||
const otpCode = await this.otpService.generateAndSetInCache(phone, "LOGIN");
|
||||
//
|
||||
await this.smsService.sendSmsVerifyCode(phone, otpCode);
|
||||
this.logger.debug(`OTP sent to ${phone}: ${otpCode}`);
|
||||
|
||||
return {
|
||||
message: AuthMessage.OTP_SENT,
|
||||
otpCode,
|
||||
};
|
||||
}
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
async verifyPhone(verifyOtpDto: VerifyOtpDto, userId: string) {
|
||||
const { code, phone } = verifyOtpDto;
|
||||
|
||||
const user = await this.checkUserLoginCredentialWithPhone(phone, code, userId);
|
||||
|
||||
user.phone = phone;
|
||||
await this.userRepository.save(user);
|
||||
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
};
|
||||
}
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
async findOneWithEmail(email: string): Promise<User | null> {
|
||||
const user = await this.userRepository.findOneWithEmail(email);
|
||||
return user;
|
||||
@@ -594,4 +646,18 @@ export class UsersService {
|
||||
}
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
private async checkUserLoginCredentialWithPhone(phone: string, otpCode: string, userId: string) {
|
||||
const isValid = await this.otpService.verifyOtp(phone, otpCode, "LOGIN");
|
||||
if (!isValid) throw new BadRequestException(AuthMessage.INVALID_OTP);
|
||||
|
||||
await this.otpService.delOtpFormCache(phone, "LOGIN");
|
||||
|
||||
const user = await this.userRepository.findOneBy({ id: userId });
|
||||
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
/************************************************************ */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user