feat(sms): add WALLET_CHARGED sms trigger for online topup, manual admin credit, refund, and cashback
This commit is contained in:
1 parent
7093978e27
commit
e15c25dae1
5 files changed
+168
-3
No files matched your search
@@ -2,9 +2,11 @@ import {
|
||||
Injectable,
|
||||
BadRequestException,
|
||||
NotFoundException,
|
||||
Optional,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { SmsService } from '../common/services/sms.service';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { normalizeMobile } from '../common/utils/phone.utils';
|
||||
import { UpdateProfileDto } from './dto/update-profile.dto';
|
||||
@@ -22,7 +24,10 @@ export interface UserAddressInput {
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
@Optional() private smsService?: SmsService,
|
||||
) {}
|
||||
|
||||
async findById(id: string) {
|
||||
if (id === '12345678-1234-1234-1234-123456789012') {
|
||||
@@ -277,7 +282,7 @@ export class UsersService {
|
||||
throw new Error('مبلغ شارژ باید بزرگتر از صفر باشد');
|
||||
}
|
||||
|
||||
const [transaction] = await this.prisma.$transaction([
|
||||
const [transaction, updatedUser] = await this.prisma.$transaction([
|
||||
this.prisma.walletTransaction.create({
|
||||
data: {
|
||||
userId,
|
||||
@@ -293,6 +298,28 @@ export class UsersService {
|
||||
}),
|
||||
]);
|
||||
|
||||
if (this.smsService && updatedUser.mobile) {
|
||||
try {
|
||||
const customerFullName = (
|
||||
updatedUser.firstName
|
||||
? `${updatedUser.firstName} ${updatedUser.lastName || ''}`
|
||||
: 'کاربر گرامی'
|
||||
).trim();
|
||||
const topupData = {
|
||||
customerName: customerFullName,
|
||||
customerPhone: updatedUser.mobile,
|
||||
amount: Number(amount || 0).toLocaleString('fa-IR'),
|
||||
balance: Number(updatedUser.walletBalance || 0).toLocaleString('fa-IR'),
|
||||
reason: 'افزایش موجودی کیف پول',
|
||||
date: new Date().toLocaleDateString('fa-IR'),
|
||||
};
|
||||
|
||||
this.smsService.triggerEvent('WALLET_CHARGED', topupData).catch(() => {});
|
||||
} catch (err) {
|
||||
// Silently catch SMS error
|
||||
}
|
||||
}
|
||||
|
||||
return transaction;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user