chore: get announcements for user that buy service

This commit is contained in:
Matin
2025-02-20 11:29:17 +03:30
parent 99fa47356a
commit 5b8f419d81
5 changed files with 181 additions and 18 deletions
+67 -1
View File
@@ -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;
}
/************************************************************ */
}
+25
View File
@@ -21,6 +21,8 @@ import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
import { PermissionsDec } from "../../common/decorators/permission.decorator";
import { UserDec } from "../../common/decorators/user.decorator";
import { ParamDto } from "../../common/DTO/param.dto";
import { RequestOtpDto } from "../auth/DTO/request-otp.dto";
import { VerifyOtpDto } from "../auth/DTO/verify-otp.dto";
@Controller("users")
@ApiTags("users")
@@ -54,6 +56,8 @@ export class UsersController {
return this.usersService.updateProfile(userId, updateProfileDto);
}
/************************************************************ */
@AuthGuards()
@ApiOperation({ summary: "Update user email" })
@Patch("update-email")
@@ -61,6 +65,8 @@ export class UsersController {
return this.usersService.updateEmail(userId, updateEmailDto);
}
/************************************************************ */
@ApiOperation({ summary: "Verify email" })
@HttpCode(HttpStatus.PERMANENT_REDIRECT)
@Get("verify-email")
@@ -70,6 +76,25 @@ export class UsersController {
/************************************************************ */
@AuthGuards()
@ApiOperation({ summary: "Update user phone" })
@Patch("update-phone")
updatePhone(@Body() updateDto: RequestOtpDto, @UserDec("id") userId: string) {
return this.usersService.updatePhone(userId, updateDto);
}
/************************************************************ */
@AuthGuards()
@ApiOperation({ summary: "Verify phone" })
@HttpCode(HttpStatus.OK)
@Patch("verify-phone")
verifyPhone(@Body() verifyOtpDto: VerifyOtpDto, @UserDec("id") userId: string) {
return this.usersService.verifyPhone(verifyOtpDto, userId);
}
/************************************************************ */
@AuthGuards()
@ApiOperation({ summary: "Check validity of user field" })
@HttpCode(HttpStatus.OK)