feat(auth): referral agent code and auto-assign agent codes
Validate optional referrer code at OTP registration and persist assignedAgentId. Auto-generate unique W-XXXX codes when creating or promoting agents and backfill on agent login when missing. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "OtpChallenge" ADD COLUMN "assignedAgentId" TEXT;
|
||||||
+10
-9
@@ -290,15 +290,16 @@ model AdminUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model OtpChallenge {
|
model OtpChallenge {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
phone String
|
phone String
|
||||||
code String
|
code String
|
||||||
firstName String?
|
firstName String?
|
||||||
lastName String?
|
lastName String?
|
||||||
isRegister Boolean @default(false)
|
isRegister Boolean @default(false)
|
||||||
attempts Int @default(0)
|
assignedAgentId String?
|
||||||
expiresAt DateTime
|
attempts Int @default(0)
|
||||||
createdAt DateTime @default(now())
|
expiresAt DateTime
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
|
||||||
@@index([phone])
|
@@index([phone])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "OtpChallenge" ADD COLUMN "assignedAgentId" TEXT;
|
||||||
+10
-9
@@ -290,15 +290,16 @@ model AdminUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model OtpChallenge {
|
model OtpChallenge {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
phone String
|
phone String
|
||||||
code String
|
code String
|
||||||
firstName String?
|
firstName String?
|
||||||
lastName String?
|
lastName String?
|
||||||
isRegister Boolean @default(false)
|
isRegister Boolean @default(false)
|
||||||
attempts Int @default(0)
|
assignedAgentId String?
|
||||||
expiresAt DateTime
|
attempts Int @default(0)
|
||||||
createdAt DateTime @default(now())
|
expiresAt DateTime
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
|
||||||
@@index([phone])
|
@@index([phone])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import {
|
|||||||
normalizePhone,
|
normalizePhone,
|
||||||
toEnglishDigits,
|
toEnglishDigits,
|
||||||
} from '../common/utils/phone';
|
} from '../common/utils/phone';
|
||||||
|
import { generateUniqueAgentCode } from '../common/utils/agent-code';
|
||||||
|
|
||||||
const MAX_OTP_ATTEMPTS = 5;
|
const MAX_OTP_ATTEMPTS = 5;
|
||||||
|
|
||||||
@@ -62,6 +63,13 @@ export class AuthService {
|
|||||||
throw new UnauthorizedException('حساب کاربری شما غیرفعال است');
|
throw new UnauthorizedException('حساب کاربری شما غیرفعال است');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let assignedAgentId: string | null = null;
|
||||||
|
if (isRegister) {
|
||||||
|
assignedAgentId = await this.resolveAssignedAgentIdForReferral(
|
||||||
|
dto.referrerAgentCode,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const smsEnabled = this.sms.isEnabled();
|
const smsEnabled = this.sms.isEnabled();
|
||||||
const code = smsEnabled
|
const code = smsEnabled
|
||||||
? String(randomInt(1000, 10000))
|
? String(randomInt(1000, 10000))
|
||||||
@@ -78,6 +86,7 @@ export class AuthService {
|
|||||||
firstName: dto.firstName?.trim(),
|
firstName: dto.firstName?.trim(),
|
||||||
lastName: dto.lastName?.trim(),
|
lastName: dto.lastName?.trim(),
|
||||||
isRegister,
|
isRegister,
|
||||||
|
assignedAgentId,
|
||||||
expiresAt,
|
expiresAt,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -148,6 +157,7 @@ export class AuthService {
|
|||||||
phone,
|
phone,
|
||||||
firstName: challenge.firstName ?? 'کاربر',
|
firstName: challenge.firstName ?? 'کاربر',
|
||||||
lastName: challenge.lastName ?? 'جدید',
|
lastName: challenge.lastName ?? 'جدید',
|
||||||
|
assignedAgentId: challenge.assignedAgentId ?? null,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else if (!user) {
|
} else if (!user) {
|
||||||
@@ -159,6 +169,7 @@ export class AuthService {
|
|||||||
await this.prisma.otpChallenge.deleteMany({ where: { phone } });
|
await this.prisma.otpChallenge.deleteMany({ where: { phone } });
|
||||||
|
|
||||||
if (user.appRole === AppUserRole.agent) {
|
if (user.appRole === AppUserRole.agent) {
|
||||||
|
user = await this.ensureAgentHasReferralCode(user);
|
||||||
await this.agentLevelService.syncAgentLevel(user.id);
|
await this.agentLevelService.syncAgentLevel(user.id);
|
||||||
user =
|
user =
|
||||||
(await this.prisma.customer.findUnique({ where: { id: user.id } })) ??
|
(await this.prisma.customer.findUnique({ where: { id: user.id } })) ??
|
||||||
@@ -233,6 +244,7 @@ export class AuthService {
|
|||||||
if (!user) throw new UnauthorizedException();
|
if (!user) throw new UnauthorizedException();
|
||||||
|
|
||||||
if (user.appRole === AppUserRole.agent) {
|
if (user.appRole === AppUserRole.agent) {
|
||||||
|
user = await this.ensureAgentHasReferralCode(user);
|
||||||
await this.agentLevelService.syncAgentLevel(user.id);
|
await this.agentLevelService.syncAgentLevel(user.id);
|
||||||
user =
|
user =
|
||||||
(await this.prisma.customer.findUnique({ where: { id: userId } })) ??
|
(await this.prisma.customer.findUnique({ where: { id: userId } })) ??
|
||||||
@@ -282,6 +294,30 @@ export class AuthService {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* کد واسطه را به شناسه واسطه فعال تبدیل میکند.
|
||||||
|
* خالی = مشتری مستقیم (null). نامعتبر = خطای 400 با پیام ثابت برای UI.
|
||||||
|
*/
|
||||||
|
private async resolveAssignedAgentIdForReferral(
|
||||||
|
rawCode?: string | null,
|
||||||
|
): Promise<string | null> {
|
||||||
|
const code = rawCode?.trim();
|
||||||
|
if (!code) return null;
|
||||||
|
|
||||||
|
const agent = await this.prisma.customer.findFirst({
|
||||||
|
where: {
|
||||||
|
appRole: AppUserRole.agent,
|
||||||
|
isActive: true,
|
||||||
|
agentCode: { equals: code, mode: 'insensitive' },
|
||||||
|
},
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
if (!agent) {
|
||||||
|
throw new BadRequestException('کد واسطه معتبر نیست');
|
||||||
|
}
|
||||||
|
return agent.id;
|
||||||
|
}
|
||||||
|
|
||||||
private async signCustomerToken(
|
private async signCustomerToken(
|
||||||
id: string,
|
id: string,
|
||||||
phone: string,
|
phone: string,
|
||||||
@@ -295,6 +331,21 @@ export class AuthService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async ensureAgentHasReferralCode(user: {
|
||||||
|
id: string;
|
||||||
|
appRole: AppUserRole;
|
||||||
|
agentCode: string | null;
|
||||||
|
}) {
|
||||||
|
if (user.appRole !== AppUserRole.agent || user.agentCode?.trim()) {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
const agentCode = await generateUniqueAgentCode(this.prisma);
|
||||||
|
return this.prisma.customer.update({
|
||||||
|
where: { id: user.id },
|
||||||
|
data: { agentCode },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private mapCustomer(user: {
|
private mapCustomer(user: {
|
||||||
id: string;
|
id: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
|
|||||||
@@ -22,6 +22,12 @@ export class RequestOtpDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsBoolean()
|
@IsBoolean()
|
||||||
isRegister?: boolean;
|
isRegister?: boolean;
|
||||||
|
|
||||||
|
/** کد معرفی واسطه؛ اختیاری — در ثبتنام برای assignedAgentId استفاده میشود */
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
@MaxLength(32)
|
||||||
|
referrerAgentCode?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class VerifyOtpDto {
|
export class VerifyOtpDto {
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { AppUserRole } from '@prisma/client';
|
||||||
|
import { resolveAgentCodeForRole } from './agent-code';
|
||||||
|
|
||||||
|
describe('resolveAgentCodeForRole', () => {
|
||||||
|
it('returns null for customer role', async () => {
|
||||||
|
const db = { customer: { findFirst: jest.fn(), count: jest.fn() } };
|
||||||
|
await expect(
|
||||||
|
resolveAgentCodeForRole(db as never, AppUserRole.customer, 'W-1'),
|
||||||
|
).resolves.toBeNull();
|
||||||
|
expect(db.customer.findFirst).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps a unique provided agent code', async () => {
|
||||||
|
const db = {
|
||||||
|
customer: {
|
||||||
|
findFirst: jest.fn().mockResolvedValue(null),
|
||||||
|
count: jest.fn(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
await expect(
|
||||||
|
resolveAgentCodeForRole(
|
||||||
|
db as never,
|
||||||
|
AppUserRole.agent,
|
||||||
|
'W-1247',
|
||||||
|
'cust-1',
|
||||||
|
),
|
||||||
|
).resolves.toBe('W-1247');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import { ConflictException } from '@nestjs/common';
|
||||||
|
import { AppUserRole, Prisma } from '@prisma/client';
|
||||||
|
import { randomInt } from 'node:crypto';
|
||||||
|
import type { PrismaService } from '../../prisma/prisma.service';
|
||||||
|
|
||||||
|
type AgentCodeDb =
|
||||||
|
| Pick<PrismaService, 'customer'>
|
||||||
|
| Prisma.TransactionClient;
|
||||||
|
|
||||||
|
export async function resolveAgentCodeForRole(
|
||||||
|
db: AgentCodeDb,
|
||||||
|
appRole: AppUserRole,
|
||||||
|
agentCode: string | null | undefined,
|
||||||
|
excludeCustomerId?: string,
|
||||||
|
): Promise<string | null> {
|
||||||
|
if (appRole !== AppUserRole.agent) return null;
|
||||||
|
|
||||||
|
const trimmed = agentCode?.trim();
|
||||||
|
if (trimmed) {
|
||||||
|
await assertAgentCodeAvailable(db, trimmed, excludeCustomerId);
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
|
||||||
|
return generateUniqueAgentCode(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generateUniqueAgentCode(db: AgentCodeDb): Promise<string> {
|
||||||
|
for (let attempt = 0; attempt < 40; attempt++) {
|
||||||
|
const code = formatAgentCode(randomInt(1000, 10000));
|
||||||
|
const taken = await findAgentByCode(db, code);
|
||||||
|
if (!taken) return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
const count = await db.customer.count({
|
||||||
|
where: { appRole: AppUserRole.agent },
|
||||||
|
});
|
||||||
|
const fallback = formatAgentCode(1000 + count);
|
||||||
|
await assertAgentCodeAvailable(db, fallback);
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatAgentCode(numericPart: number): string {
|
||||||
|
return `W-${numericPart}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findAgentByCode(db: AgentCodeDb, code: string) {
|
||||||
|
return db.customer.findFirst({
|
||||||
|
where: { agentCode: { equals: code, mode: 'insensitive' } },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function assertAgentCodeAvailable(
|
||||||
|
db: AgentCodeDb,
|
||||||
|
code: string,
|
||||||
|
excludeCustomerId?: string,
|
||||||
|
) {
|
||||||
|
const existing = await db.customer.findFirst({
|
||||||
|
where: {
|
||||||
|
agentCode: { equals: code, mode: 'insensitive' },
|
||||||
|
...(excludeCustomerId ? { id: { not: excludeCustomerId } } : {}),
|
||||||
|
},
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
if (existing) {
|
||||||
|
throw new ConflictException('کد واسطه قبلاً استفاده شده است');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
toEnglishDigits,
|
toEnglishDigits,
|
||||||
} from '../common/utils/phone';
|
} from '../common/utils/phone';
|
||||||
import { joinFullName, splitFullName } from '../common/utils/name';
|
import { joinFullName, splitFullName } from '../common/utils/name';
|
||||||
|
import { resolveAgentCodeForRole } from '../common/utils/agent-code';
|
||||||
|
|
||||||
const customerInclude = {
|
const customerInclude = {
|
||||||
company: { select: { name: true } },
|
company: { select: { name: true } },
|
||||||
@@ -73,8 +74,15 @@ export class CustomersService {
|
|||||||
await this.assertLevel(dto.levelDefId);
|
await this.assertLevel(dto.levelDefId);
|
||||||
await this.assertAgent(dto.assignedAgentId);
|
await this.assertAgent(dto.assignedAgentId);
|
||||||
|
|
||||||
|
const appRole = dto.appRole ?? AppUserRole.customer;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const customer = await this.prisma.$transaction(async (tx) => {
|
const customer = await this.prisma.$transaction(async (tx) => {
|
||||||
|
const agentCode = await resolveAgentCodeForRole(
|
||||||
|
tx,
|
||||||
|
appRole,
|
||||||
|
dto.agentCode?.trim() || null,
|
||||||
|
);
|
||||||
const created = await tx.customer.create({
|
const created = await tx.customer.create({
|
||||||
data: {
|
data: {
|
||||||
firstName,
|
firstName,
|
||||||
@@ -86,8 +94,8 @@ export class CustomersService {
|
|||||||
isActive: dto.isActive ?? true,
|
isActive: dto.isActive ?? true,
|
||||||
levelDefId: dto.levelDefId || null,
|
levelDefId: dto.levelDefId || null,
|
||||||
assignedAgentId: dto.assignedAgentId || null,
|
assignedAgentId: dto.assignedAgentId || null,
|
||||||
appRole: dto.appRole ?? AppUserRole.customer,
|
appRole,
|
||||||
agentCode: dto.agentCode?.trim() || null,
|
agentCode,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return tx.customer.findUniqueOrThrow({
|
return tx.customer.findUniqueOrThrow({
|
||||||
@@ -108,7 +116,7 @@ export class CustomersService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, dto: UpdateCustomerDto) {
|
async update(id: string, dto: UpdateCustomerDto) {
|
||||||
await this.ensure(id);
|
const existing = await this.ensure(id);
|
||||||
const phone =
|
const phone =
|
||||||
dto.phone !== undefined
|
dto.phone !== undefined
|
||||||
? normalizePhone(toEnglishDigits(dto.phone))
|
? normalizePhone(toEnglishDigits(dto.phone))
|
||||||
@@ -135,8 +143,26 @@ export class CustomersService {
|
|||||||
await this.assertAgent(dto.assignedAgentId, id);
|
await this.assertAgent(dto.assignedAgentId, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const nextAppRole = dto.appRole ?? existing.appRole;
|
||||||
|
const proposedAgentCode =
|
||||||
|
dto.agentCode !== undefined
|
||||||
|
? dto.agentCode?.trim() || null
|
||||||
|
: existing.agentCode;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const customer = await this.prisma.$transaction(async (tx) => {
|
const customer = await this.prisma.$transaction(async (tx) => {
|
||||||
|
const agentCode =
|
||||||
|
nextAppRole === AppUserRole.agent
|
||||||
|
? await resolveAgentCodeForRole(
|
||||||
|
tx,
|
||||||
|
nextAppRole,
|
||||||
|
proposedAgentCode,
|
||||||
|
id,
|
||||||
|
)
|
||||||
|
: dto.appRole !== undefined
|
||||||
|
? null
|
||||||
|
: undefined;
|
||||||
|
|
||||||
await tx.customer.update({
|
await tx.customer.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
@@ -158,10 +184,7 @@ export class CustomersService {
|
|||||||
? undefined
|
? undefined
|
||||||
: dto.assignedAgentId || null,
|
: dto.assignedAgentId || null,
|
||||||
appRole: dto.appRole,
|
appRole: dto.appRole,
|
||||||
agentCode:
|
...(agentCode !== undefined ? { agentCode } : {}),
|
||||||
dto.agentCode === undefined
|
|
||||||
? undefined
|
|
||||||
: dto.agentCode?.trim() || null,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user