Compare commits
2
Commits
5ddb225813
...
bc881a0a8a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc881a0a8a | ||
|
|
5f3b00497e |
+53
-3
@@ -88714,6 +88714,39 @@ var require_brand = __commonJS({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// dist/common/mobile-owner.js
|
||||||
|
var require_mobile_owner = __commonJS({
|
||||||
|
"dist/common/mobile-owner.js"(exports2) {
|
||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports2, "__esModule", { value: true });
|
||||||
|
exports2.CUSTOMER_HOLDS_MOBILE = exports2.EXPERT_HOLDS_MOBILE = void 0;
|
||||||
|
exports2.activeExpertHoldsMobile = activeExpertHoldsMobile;
|
||||||
|
exports2.customerHoldsMobile = customerHoldsMobile;
|
||||||
|
var client_1 = require("@prisma/client");
|
||||||
|
exports2.EXPERT_HOLDS_MOBILE = "\u0627\u06CC\u0646 \u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0642\u0628\u0644\u0627\u064B \u0628\u0631\u0627\u06CC \u06CC\u06A9 \u06A9\u0627\u0631\u0634\u0646\u0627\u0633 \u0641\u0631\u0648\u0634 \u062B\u0628\u062A \u0634\u062F\u0647 \u0627\u0633\u062A";
|
||||||
|
exports2.CUSTOMER_HOLDS_MOBILE = "\u0627\u06CC\u0646 \u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0642\u0628\u0644\u0627\u064B \u0628\u0631\u0627\u06CC \u06CC\u06A9 \u0645\u0634\u062A\u0631\u06CC \u062B\u0628\u062A \u0634\u062F\u0647 \u0627\u0633\u062A";
|
||||||
|
async function activeExpertHoldsMobile(db, mobile, exceptTradingCustomerId) {
|
||||||
|
const row = await db.adminUser.findFirst({
|
||||||
|
where: {
|
||||||
|
mobile,
|
||||||
|
role: client_1.AdminRole.SALES_EXPERT,
|
||||||
|
isActive: true,
|
||||||
|
...exceptTradingCustomerId ? { NOT: { salesExpertProfile: { is: { tradingCustomerId: exceptTradingCustomerId } } } } : {}
|
||||||
|
},
|
||||||
|
select: { id: true }
|
||||||
|
});
|
||||||
|
return row !== null;
|
||||||
|
}
|
||||||
|
async function customerHoldsMobile(db, mobile, exceptCustomerId) {
|
||||||
|
const row = await db.customer.findFirst({
|
||||||
|
where: { mobile, ...exceptCustomerId ? { id: { not: exceptCustomerId } } : {} },
|
||||||
|
select: { id: true }
|
||||||
|
});
|
||||||
|
return row !== null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// dist/modules/customer-auth/customer-auth.service.js
|
// dist/modules/customer-auth/customer-auth.service.js
|
||||||
var require_customer_auth_service = __commonJS({
|
var require_customer_auth_service = __commonJS({
|
||||||
"dist/modules/customer-auth/customer-auth.service.js"(exports2) {
|
"dist/modules/customer-auth/customer-auth.service.js"(exports2) {
|
||||||
@@ -88785,6 +88818,7 @@ var require_customer_auth_service = __commonJS({
|
|||||||
var digits_1 = require_digits();
|
var digits_1 = require_digits();
|
||||||
var sms_service_12 = require_sms_service();
|
var sms_service_12 = require_sms_service();
|
||||||
var brand_1 = require_brand();
|
var brand_1 = require_brand();
|
||||||
|
var mobile_owner_1 = require_mobile_owner();
|
||||||
var TooManyRequestsException = class extends common_12.HttpException {
|
var TooManyRequestsException = class extends common_12.HttpException {
|
||||||
constructor(message) {
|
constructor(message) {
|
||||||
super(message, common_12.HttpStatus.TOO_MANY_REQUESTS);
|
super(message, common_12.HttpStatus.TOO_MANY_REQUESTS);
|
||||||
@@ -88924,7 +88958,7 @@ var require_customer_auth_service = __commonJS({
|
|||||||
const fname = dto.fname.trim();
|
const fname = dto.fname.trim();
|
||||||
const lname = dto.lname.trim();
|
const lname = dto.lname.trim();
|
||||||
const existing = await this.prisma.customer.findUnique({ where: { mobile }, select: { id: true } });
|
const existing = await this.prisma.customer.findUnique({ where: { mobile }, select: { id: true } });
|
||||||
if (existing) {
|
if (existing || await (0, mobile_owner_1.activeExpertHoldsMobile)(this.prisma, mobile)) {
|
||||||
throw new common_12.ConflictException("\u0627\u06CC\u0646 \u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0642\u0628\u0644\u0627\u064B \u062B\u0628\u062A \u0634\u062F\u0647 \u0627\u0633\u062A. \u0627\u0632 \u0628\u062E\u0634 \u0648\u0631\u0648\u062F \u0648\u0627\u0631\u062F \u0634\u0648\u06CC\u062F.");
|
throw new common_12.ConflictException("\u0627\u06CC\u0646 \u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0642\u0628\u0644\u0627\u064B \u062B\u0628\u062A \u0634\u062F\u0647 \u0627\u0633\u062A. \u0627\u0632 \u0628\u062E\u0634 \u0648\u0631\u0648\u062F \u0648\u0627\u0631\u062F \u0634\u0648\u06CC\u062F.");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@@ -96979,6 +97013,7 @@ var require_admin_customers_service = __commonJS({
|
|||||||
var accounting_hooks_service_1 = require_accounting_hooks_service();
|
var accounting_hooks_service_1 = require_accounting_hooks_service();
|
||||||
var accounting_config_service_1 = require_accounting_config_service();
|
var accounting_config_service_1 = require_accounting_config_service();
|
||||||
var brand_1 = require_brand();
|
var brand_1 = require_brand();
|
||||||
|
var mobile_owner_1 = require_mobile_owner();
|
||||||
exports2.CUSTOMER_TYPE_STR = {
|
exports2.CUSTOMER_TYPE_STR = {
|
||||||
SALES_AGENT: " \u06A9\u0627\u0631\u0634\u0646\u0627\u0633 \u0641\u0631\u0648\u0634",
|
SALES_AGENT: " \u06A9\u0627\u0631\u0634\u0646\u0627\u0633 \u0641\u0631\u0648\u0634",
|
||||||
// legacy value only — see CustomerKind's schema comment
|
// legacy value only — see CustomerKind's schema comment
|
||||||
@@ -97629,6 +97664,9 @@ var require_admin_customers_service = __commonJS({
|
|||||||
if (!fullName || !mobile) {
|
if (!fullName || !mobile) {
|
||||||
throw new common_12.BadRequestException("\u0646\u0627\u0645 \u0648 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0627\u0644\u0632\u0627\u0645\u06CC \u0627\u0633\u062A");
|
throw new common_12.BadRequestException("\u0646\u0627\u0645 \u0648 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0627\u0644\u0632\u0627\u0645\u06CC \u0627\u0633\u062A");
|
||||||
}
|
}
|
||||||
|
if (await (0, mobile_owner_1.activeExpertHoldsMobile)(this.prisma, mobile)) {
|
||||||
|
throw new common_12.BadRequestException(mobile_owner_1.EXPERT_HOLDS_MOBILE);
|
||||||
|
}
|
||||||
if (data.percent === void 0 || data.percent === null || data.percent === "") {
|
if (data.percent === void 0 || data.percent === null || data.percent === "") {
|
||||||
throw new common_12.BadRequestException("\u0648\u0627\u0631\u062F \u06A9\u0631\u062F\u0646 percent \u0627\u0644\u0632\u0627\u0645\u06CC \u0627\u0633\u062A");
|
throw new common_12.BadRequestException("\u0648\u0627\u0631\u062F \u06A9\u0631\u062F\u0646 percent \u0627\u0644\u0632\u0627\u0645\u06CC \u0627\u0633\u062A");
|
||||||
}
|
}
|
||||||
@@ -97696,8 +97734,13 @@ var require_admin_customers_service = __commonJS({
|
|||||||
if (data.percent !== void 0 && data.percent !== null && data.percent !== "") {
|
if (data.percent !== void 0 && data.percent !== null && data.percent !== "") {
|
||||||
patch.makingChargeOffsetPercent = new client_1.Prisma.Decimal(String(data.percent));
|
patch.makingChargeOffsetPercent = new client_1.Prisma.Decimal(String(data.percent));
|
||||||
}
|
}
|
||||||
if (data.mobile !== void 0)
|
if (data.mobile !== void 0) {
|
||||||
patch.mobile = (0, digits_1.faToEn)(String(data.mobile));
|
const mobile = (0, digits_1.faToEn)(String(data.mobile));
|
||||||
|
patch.mobile = mobile;
|
||||||
|
if (mobile !== existing.mobile && await (0, mobile_owner_1.activeExpertHoldsMobile)(this.prisma, mobile, id)) {
|
||||||
|
throw new common_12.BadRequestException(mobile_owner_1.EXPERT_HOLDS_MOBILE);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (data.national_code !== void 0)
|
if (data.national_code !== void 0)
|
||||||
patch.nationalCode = nationalCodeOrNull(data.national_code);
|
patch.nationalCode = nationalCodeOrNull(data.national_code);
|
||||||
if (data.city_id !== void 0) {
|
if (data.city_id !== void 0) {
|
||||||
@@ -101292,6 +101335,7 @@ var require_admin_users_service = __commonJS({
|
|||||||
var digits_1 = require_digits();
|
var digits_1 = require_digits();
|
||||||
var acl_constants_1 = require_acl_constants();
|
var acl_constants_1 = require_acl_constants();
|
||||||
var super_admin_1 = require_super_admin();
|
var super_admin_1 = require_super_admin();
|
||||||
|
var mobile_owner_1 = require_mobile_owner();
|
||||||
var STORABLE_RIDS = new Set(acl_constants_1.ALL_RIDS.flatMap((rid) => rid.split(".").map((_2, i, parts) => parts.slice(0, i + 1).join("."))));
|
var STORABLE_RIDS = new Set(acl_constants_1.ALL_RIDS.flatMap((rid) => rid.split(".").map((_2, i, parts) => parts.slice(0, i + 1).join("."))));
|
||||||
var AdminUsersService = class AdminUsersService {
|
var AdminUsersService = class AdminUsersService {
|
||||||
static {
|
static {
|
||||||
@@ -101489,6 +101533,9 @@ var require_admin_users_service = __commonJS({
|
|||||||
const existing = await this.prisma.adminUser.findUnique({ where: { mobile } });
|
const existing = await this.prisma.adminUser.findUnique({ where: { mobile } });
|
||||||
if (existing)
|
if (existing)
|
||||||
throw new common_12.BadRequestException("\u0627\u06CC\u0646 \u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0642\u0628\u0644\u0627\u064B \u062B\u0628\u062A \u0634\u062F\u0647 \u0627\u0633\u062A");
|
throw new common_12.BadRequestException("\u0627\u06CC\u0646 \u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0642\u0628\u0644\u0627\u064B \u062B\u0628\u062A \u0634\u062F\u0647 \u0627\u0633\u062A");
|
||||||
|
if (await (0, mobile_owner_1.customerHoldsMobile)(this.prisma, mobile)) {
|
||||||
|
throw new common_12.BadRequestException(mobile_owner_1.CUSTOMER_HOLDS_MOBILE);
|
||||||
|
}
|
||||||
return this.prisma.adminUser.create({
|
return this.prisma.adminUser.create({
|
||||||
data: {
|
data: {
|
||||||
fullName,
|
fullName,
|
||||||
@@ -101610,6 +101657,9 @@ var require_admin_users_service = __commonJS({
|
|||||||
const clash = await this.prisma.adminUser.findUnique({ where: { mobile } });
|
const clash = await this.prisma.adminUser.findUnique({ where: { mobile } });
|
||||||
if (clash)
|
if (clash)
|
||||||
throw new common_12.BadRequestException("\u0627\u06CC\u0646 \u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0642\u0628\u0644\u0627\u064B \u062B\u0628\u062A \u0634\u062F\u0647 \u0627\u0633\u062A");
|
throw new common_12.BadRequestException("\u0627\u06CC\u0646 \u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06CC\u0644 \u0642\u0628\u0644\u0627\u064B \u062B\u0628\u062A \u0634\u062F\u0647 \u0627\u0633\u062A");
|
||||||
|
if (await (0, mobile_owner_1.customerHoldsMobile)(this.prisma, mobile, existing.salesExpertProfile?.tradingCustomerId)) {
|
||||||
|
throw new common_12.BadRequestException(mobile_owner_1.CUSTOMER_HOLDS_MOBILE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
patch.mobile = mobile;
|
patch.mobile = mobile;
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -819,7 +819,7 @@ const config = {
|
|||||||
"value": "prisma-client-js"
|
"value": "prisma-client-js"
|
||||||
},
|
},
|
||||||
"output": {
|
"output": {
|
||||||
"value": "E:\\tehran_taj-wt\\node_modules\\.pnpm\\@prisma+client@5.22.0_prisma@5.22.0\\node_modules\\@prisma\\client",
|
"value": "E:\\tehran_taj\\node_modules\\.pnpm\\@prisma+client@5.22.0_prisma@5.22.0\\node_modules\\@prisma\\client",
|
||||||
"fromEnvVar": null
|
"fromEnvVar": null
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
@@ -837,10 +837,11 @@ const config = {
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"previewFeatures": [],
|
"previewFeatures": [],
|
||||||
"sourceFilePath": "E:\\tehran_taj-wt\\apps\\api\\prisma\\schema.prisma"
|
"sourceFilePath": "E:\\tehran_taj\\apps\\api\\prisma\\schema.prisma"
|
||||||
},
|
},
|
||||||
"relativeEnvPaths": {
|
"relativeEnvPaths": {
|
||||||
"rootEnvPath": null
|
"rootEnvPath": null,
|
||||||
|
"schemaEnvPath": "../../../../../../apps/api/.env"
|
||||||
},
|
},
|
||||||
"relativePath": "../../../../../../apps/api/prisma",
|
"relativePath": "../../../../../../apps/api/prisma",
|
||||||
"clientVersion": "5.22.0",
|
"clientVersion": "5.22.0",
|
||||||
|
|||||||
+4
-3
@@ -820,7 +820,7 @@ const config = {
|
|||||||
"value": "prisma-client-js"
|
"value": "prisma-client-js"
|
||||||
},
|
},
|
||||||
"output": {
|
"output": {
|
||||||
"value": "E:\\tehran_taj-wt\\node_modules\\.pnpm\\@prisma+client@5.22.0_prisma@5.22.0\\node_modules\\@prisma\\client",
|
"value": "E:\\tehran_taj\\node_modules\\.pnpm\\@prisma+client@5.22.0_prisma@5.22.0\\node_modules\\@prisma\\client",
|
||||||
"fromEnvVar": null
|
"fromEnvVar": null
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
@@ -838,10 +838,11 @@ const config = {
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"previewFeatures": [],
|
"previewFeatures": [],
|
||||||
"sourceFilePath": "E:\\tehran_taj-wt\\apps\\api\\prisma\\schema.prisma"
|
"sourceFilePath": "E:\\tehran_taj\\apps\\api\\prisma\\schema.prisma"
|
||||||
},
|
},
|
||||||
"relativeEnvPaths": {
|
"relativeEnvPaths": {
|
||||||
"rootEnvPath": null
|
"rootEnvPath": null,
|
||||||
|
"schemaEnvPath": "../../../../../../apps/api/.env"
|
||||||
},
|
},
|
||||||
"relativePath": "../../../../../../apps/api/prisma",
|
"relativePath": "../../../../../../apps/api/prisma",
|
||||||
"clientVersion": "5.22.0",
|
"clientVersion": "5.22.0",
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import { AdminRole } from '@prisma/client';
|
||||||
|
import type { PrismaService } from '../prisma/prisma.service';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One mobile, one person — across the two tables the apps sign in from.
|
||||||
|
*
|
||||||
|
* A sales expert (AdminUser) and a customer (Customer) both log in to the apps
|
||||||
|
* with their mobile, but they live in different tables, so the database cannot
|
||||||
|
* make the number unique across them. These checks do it where a number is
|
||||||
|
* typed in: the reference rejected a duplicate the same way (`unique:customers`
|
||||||
|
* on create and update, with the agent a customers row too).
|
||||||
|
*
|
||||||
|
* Plain equality on the unique columns, so both lookups are index hits. The
|
||||||
|
* mobile must already be normalised (`faToEn`), as it is when stored.
|
||||||
|
*
|
||||||
|
* Nothing here reads or changes a row that already exists: callers only ask
|
||||||
|
* when a number is being CREATED or CHANGED, so an existing duplicate keeps
|
||||||
|
* logging in and stays editable.
|
||||||
|
*/
|
||||||
|
type Db = Pick<PrismaService, 'adminUser' | 'customer'>;
|
||||||
|
|
||||||
|
export const EXPERT_HOLDS_MOBILE = 'این شماره موبایل قبلاً برای یک کارشناس فروش ثبت شده است';
|
||||||
|
export const CUSTOMER_HOLDS_MOBILE = 'این شماره موبایل قبلاً برای یک مشتری ثبت شده است';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An ACTIVE sales expert holds this number. Active only, because that is who the
|
||||||
|
* app login treats as an expert (`resolveExpertPrincipal`); a deactivated
|
||||||
|
* expert's number still works as a customer today and must keep doing so.
|
||||||
|
*
|
||||||
|
* `exceptTradingCustomerId` skips the expert whose own trading account that
|
||||||
|
* customer is — the row is theirs, so it may carry their number.
|
||||||
|
*/
|
||||||
|
export async function activeExpertHoldsMobile(
|
||||||
|
db: Db,
|
||||||
|
mobile: string,
|
||||||
|
exceptTradingCustomerId?: string,
|
||||||
|
): Promise<boolean> {
|
||||||
|
const row = await db.adminUser.findFirst({
|
||||||
|
where: {
|
||||||
|
mobile,
|
||||||
|
role: AdminRole.SALES_EXPERT,
|
||||||
|
isActive: true,
|
||||||
|
...(exceptTradingCustomerId
|
||||||
|
? { NOT: { salesExpertProfile: { is: { tradingCustomerId: exceptTradingCustomerId } } } }
|
||||||
|
: {}),
|
||||||
|
},
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
return row !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A customer row (any state) holds this number. Any state, because an expert
|
||||||
|
* whose number sits on an inactive or archived customer cannot sign in at all.
|
||||||
|
*
|
||||||
|
* `exceptCustomerId` is the expert's own trading account.
|
||||||
|
*/
|
||||||
|
export async function customerHoldsMobile(
|
||||||
|
db: Db,
|
||||||
|
mobile: string,
|
||||||
|
exceptCustomerId?: string | null,
|
||||||
|
): Promise<boolean> {
|
||||||
|
const row = await db.customer.findFirst({
|
||||||
|
where: { mobile, ...(exceptCustomerId ? { id: { not: exceptCustomerId } } : {}) },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
return row !== null;
|
||||||
|
}
|
||||||
@@ -11,7 +11,10 @@ import { AdminCustomersService } from './admin-customers.service';
|
|||||||
* explicit OTP-only marker.
|
* explicit OTP-only marker.
|
||||||
*/
|
*/
|
||||||
describe('AdminCustomersService', () => {
|
describe('AdminCustomersService', () => {
|
||||||
function build(current: Record<string, unknown> = { id: 'c1', verificationStep: VerificationStep.STEP_ONE_SUBMITTED }) {
|
function build(
|
||||||
|
current: Record<string, unknown> = { id: 'c1', verificationStep: VerificationStep.STEP_ONE_SUBMITTED },
|
||||||
|
expertHolder: { id: string } | null = null,
|
||||||
|
) {
|
||||||
const writes: Record<string, unknown>[] = [];
|
const writes: Record<string, unknown>[] = [];
|
||||||
const tx = {
|
const tx = {
|
||||||
customer: {
|
customer: {
|
||||||
@@ -26,11 +29,13 @@ describe('AdminCustomersService', () => {
|
|||||||
findUnique: jest.fn(async () => current),
|
findUnique: jest.fn(async () => current),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const prisma = { ...tx, $transaction: jest.fn(async (fn: (t: typeof tx) => unknown) => fn(tx)) };
|
// the cross-table check: is there an active sales expert on this number
|
||||||
const accounting = { customerVerified: jest.fn(), customerChanged: jest.fn() };
|
const adminUser = { findFirst: jest.fn(async () => expertHolder) };
|
||||||
|
const prisma = { ...tx, adminUser, $transaction: jest.fn(async (fn: (t: typeof tx) => unknown) => fn(tx)) };
|
||||||
|
const accounting = { customerVerified: jest.fn(), customerChanged: jest.fn(), customerEdited: jest.fn() };
|
||||||
const sms = { send: jest.fn(async () => undefined) };
|
const sms = { send: jest.fn(async () => undefined) };
|
||||||
const service = new AdminCustomersService(prisma as never, {} as never, sms as never, accounting as never, {} as never);
|
const service = new AdminCustomersService(prisma as never, {} as never, sms as never, accounting as never, {} as never);
|
||||||
return { service, writes, accounting };
|
return { service, writes, accounting, adminUser };
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('verify()', () => {
|
describe('verify()', () => {
|
||||||
@@ -157,4 +162,43 @@ describe('AdminCustomersService', () => {
|
|||||||
expect(JSON.stringify(where)).not.toContain('isActive');
|
expect(JSON.stringify(where)).not.toContain('isActive');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('one mobile, one person (sales experts sign in with theirs too)', () => {
|
||||||
|
const payload = { fname: 'علی', mobile: '09120000000', percent: '3' };
|
||||||
|
|
||||||
|
it('create() refuses a number an active sales expert holds, and stores nothing', async () => {
|
||||||
|
const { service, writes } = build(undefined, { id: 'exp1' });
|
||||||
|
await expect(service.create(payload)).rejects.toThrow('این شماره موبایل قبلاً برای یک کارشناس فروش ثبت شده است');
|
||||||
|
expect(writes).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create() normalises the digits before asking, so a Persian-typed number is checked as stored', async () => {
|
||||||
|
const { service, adminUser } = build();
|
||||||
|
await service.create({ ...payload, mobile: '۰۹۱۲۰۰۰۰۰۰۰' });
|
||||||
|
expect(adminUser.findFirst).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({ where: expect.objectContaining({ mobile: '09120000000', isActive: true }) }),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create() on a free number goes through', async () => {
|
||||||
|
const { service, writes } = build();
|
||||||
|
await service.create(payload);
|
||||||
|
expect(writes).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('update() refuses a CHANGED number an expert holds', async () => {
|
||||||
|
const { service, writes } = build({ id: 'c1', mobile: '09121111111' }, { id: 'exp1' });
|
||||||
|
await expect(service.update('c1', { mobile: '09120000000' })).rejects.toThrow(
|
||||||
|
'این شماره موبایل قبلاً برای یک کارشناس فروش ثبت شده است',
|
||||||
|
);
|
||||||
|
expect(writes).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('update() does not ask when the number is unchanged, so an existing duplicate stays editable', async () => {
|
||||||
|
const { service, writes, adminUser } = build({ id: 'c1', mobile: '09120000000' }, { id: 'exp1' });
|
||||||
|
await service.update('c1', { mobile: '09120000000', fname: 'علی' });
|
||||||
|
expect(adminUser.findFirst).not.toHaveBeenCalled();
|
||||||
|
expect(writes).toHaveLength(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { SmsService } from '../sms/sms.service';
|
|||||||
import { AccountingHooksService } from '../accounting-sync/accounting-hooks.service';
|
import { AccountingHooksService } from '../accounting-sync/accounting-hooks.service';
|
||||||
import { AccountingConfigService } from '../accounting-sync/accounting-config.service';
|
import { AccountingConfigService } from '../accounting-sync/accounting-config.service';
|
||||||
import { brandName, brandSiteUrl } from '../../common/brand';
|
import { brandName, brandSiteUrl } from '../../common/brand';
|
||||||
|
import { activeExpertHoldsMobile, EXPERT_HOLDS_MOBILE } from '../../common/mobile-owner';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wholesale ("عمده") customer lists — legacy views/customer/omde/*.
|
* Wholesale ("عمده") customer lists — legacy views/customer/omde/*.
|
||||||
@@ -818,6 +819,12 @@ export class AdminCustomersService {
|
|||||||
throw new BadRequestException('نام و موبایل الزامی است');
|
throw new BadRequestException('نام و موبایل الزامی است');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// One mobile, one person: an active sales expert signs in with this number
|
||||||
|
// too. (A number another CUSTOMER holds is refused by the unique index.)
|
||||||
|
if (await activeExpertHoldsMobile(this.prisma, mobile)) {
|
||||||
|
throw new BadRequestException(EXPERT_HOLDS_MOBILE);
|
||||||
|
}
|
||||||
|
|
||||||
// D9 — the reference makes `percent` required on create
|
// D9 — the reference makes `percent` required on create
|
||||||
// (AdminController.php:354) with the stock Laravel message, and its
|
// (AdminController.php:354) with the stock Laravel message, and its
|
||||||
// attribute has no Persian translation, so the text is verbatim what the
|
// attribute has no Persian translation, so the text is verbatim what the
|
||||||
@@ -913,7 +920,16 @@ export class AdminCustomersService {
|
|||||||
// submitted mobile entirely on edit (AdminController.php:348); D19 ruled
|
// submitted mobile entirely on edit (AdminController.php:348); D19 ruled
|
||||||
// that lock NOT ported, so the field stays editable here and is normalised
|
// that lock NOT ported, so the field stays editable here and is normalised
|
||||||
// instead. Logged in ISSUES as PERS-01.
|
// instead. Logged in ISSUES as PERS-01.
|
||||||
if (data.mobile !== undefined) patch.mobile = faToEn(String(data.mobile));
|
if (data.mobile !== undefined) {
|
||||||
|
const mobile = faToEn(String(data.mobile));
|
||||||
|
patch.mobile = mobile;
|
||||||
|
// Asked only when the number CHANGES: a customer who already shares a
|
||||||
|
// number with an expert (older data) stays editable and keeps signing in.
|
||||||
|
// A trading account may carry its own expert's number.
|
||||||
|
if (mobile !== existing.mobile && (await activeExpertHoldsMobile(this.prisma, mobile, id))) {
|
||||||
|
throw new BadRequestException(EXPERT_HOLDS_MOBILE);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (data.national_code !== undefined) patch.nationalCode = nationalCodeOrNull(data.national_code);
|
if (data.national_code !== undefined) patch.nationalCode = nationalCodeOrNull(data.national_code);
|
||||||
if (data.city_id !== undefined) {
|
if (data.city_id !== undefined) {
|
||||||
patch.city = data.city_id ? { connect: { id: String(data.city_id) } } : { disconnect: true };
|
patch.city = data.city_id ? { connect: { id: String(data.city_id) } } : { disconnect: true };
|
||||||
|
|||||||
@@ -38,7 +38,12 @@ describe('AdminUsersService — expert edit reaches the trading account', () =>
|
|||||||
},
|
},
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
customer: { update: customerUpdate, findUnique: jest.fn(async () => customerHolder) },
|
customer: {
|
||||||
|
update: customerUpdate,
|
||||||
|
findUnique: jest.fn(async () => customerHolder),
|
||||||
|
// the cross-table check: does a customer (not the expert's own account) hold the number
|
||||||
|
findFirst: jest.fn(async () => customerHolder),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
const prisma = {
|
const prisma = {
|
||||||
...tx,
|
...tx,
|
||||||
@@ -78,11 +83,32 @@ describe('AdminUsersService — expert edit reaches the trading account', () =>
|
|||||||
await free.service.update('exp1', { mobile: '09120001111' });
|
await free.service.update('exp1', { mobile: '09120001111' });
|
||||||
expect(free.customerUpdate).toHaveBeenCalledWith({ where: { id: 'cust1' }, data: { mobile: '09120001111' } });
|
expect(free.customerUpdate).toHaveBeenCalledWith({ where: { id: 'cust1' }, data: { mobile: '09120001111' } });
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('refuses a new mobile a customer already holds, and writes nothing', async () => {
|
||||||
const taken = build({ id: 'someone-else' });
|
const taken = build({ id: 'someone-else' });
|
||||||
await taken.service.update('exp1', { mobile: '09120001111' });
|
await expect(taken.service.update('exp1', { mobile: '09120001111' })).rejects.toThrow(
|
||||||
|
'این شماره موبایل قبلاً برای یک مشتری ثبت شده است',
|
||||||
|
);
|
||||||
expect(taken.customerUpdate).not.toHaveBeenCalled();
|
expect(taken.customerUpdate).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not ask about a mobile that is not changing, so an existing duplicate stays editable', async () => {
|
||||||
|
const { service, customerUpdate } = build({ id: 'someone-else' });
|
||||||
|
await expect(service.update('exp1', { mobile: '09902560722', fname: 'سعید1', lname: 'اسماعیلی1' })).resolves.toBeDefined();
|
||||||
|
expect(customerUpdate).toHaveBeenCalledWith({
|
||||||
|
where: { id: 'cust1' },
|
||||||
|
data: { firstName: 'سعید1', lastName: 'اسماعیلی1' },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('refuses to create an expert on a number a customer holds', async () => {
|
||||||
|
const { service } = build({ id: 'someone-else' });
|
||||||
|
await expect(
|
||||||
|
service.create({ fname: 'علی', lname: 'رضایی', mobile: '09125550000', password: 'secret1', role: 'SALES_EXPERT' }),
|
||||||
|
).rejects.toThrow('این شماره موبایل قبلاً برای یک مشتری ثبت شده است');
|
||||||
|
});
|
||||||
|
|
||||||
it('writes nothing to the trading account when nothing it mirrors changed', async () => {
|
it('writes nothing to the trading account when nothing it mirrors changed', async () => {
|
||||||
const { service, customerUpdate } = build();
|
const { service, customerUpdate } = build();
|
||||||
await service.update('exp1', { address: 'تهران' });
|
await service.update('exp1', { address: 'تهران' });
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import type { TableQueryDto } from '../admin-table/table-query.dto';
|
|||||||
import { faToEn } from '../customer-auth/digits';
|
import { faToEn } from '../customer-auth/digits';
|
||||||
import { ALL_RIDS } from '../auth/acl.constants';
|
import { ALL_RIDS } from '../auth/acl.constants';
|
||||||
import { SUPER_ADMIN_MOBILE, isSuperAdmin } from '../auth/super-admin';
|
import { SUPER_ADMIN_MOBILE, isSuperAdmin } from '../auth/super-admin';
|
||||||
|
import { CUSTOMER_HOLDS_MOBILE, customerHoldsMobile } from '../../common/mobile-owner';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SEC-07: the rids a role may hold — every rid in the catalogue and each of
|
* SEC-07: the rids a role may hold — every rid in the catalogue and each of
|
||||||
@@ -300,6 +301,10 @@ export class AdminUsersService {
|
|||||||
// Unique within the expert table only: the same number may also be an admin.
|
// Unique within the expert table only: the same number may also be an admin.
|
||||||
const existing = await this.prisma.adminUser.findUnique({ where: { mobile } });
|
const existing = await this.prisma.adminUser.findUnique({ where: { mobile } });
|
||||||
if (existing) throw new BadRequestException('این شماره موبایل قبلاً ثبت شده است');
|
if (existing) throw new BadRequestException('این شماره موبایل قبلاً ثبت شده است');
|
||||||
|
// ...and a customer signs in to the same apps with theirs: one mobile, one person.
|
||||||
|
if (await customerHoldsMobile(this.prisma, mobile)) {
|
||||||
|
throw new BadRequestException(CUSTOMER_HOLDS_MOBILE);
|
||||||
|
}
|
||||||
|
|
||||||
return this.prisma.adminUser.create({
|
return this.prisma.adminUser.create({
|
||||||
data: {
|
data: {
|
||||||
@@ -456,6 +461,12 @@ export class AdminUsersService {
|
|||||||
if (mobile !== existing.mobile) {
|
if (mobile !== existing.mobile) {
|
||||||
const clash = await this.prisma.adminUser.findUnique({ where: { mobile } });
|
const clash = await this.prisma.adminUser.findUnique({ where: { mobile } });
|
||||||
if (clash) throw new BadRequestException('این شماره موبایل قبلاً ثبت شده است');
|
if (clash) throw new BadRequestException('این شماره موبایل قبلاً ثبت شده است');
|
||||||
|
// Only a CHANGED number is asked about, so an expert who already shares
|
||||||
|
// theirs with a customer (older data) stays editable. Their own trading
|
||||||
|
// account may hold it.
|
||||||
|
if (await customerHoldsMobile(this.prisma, mobile, existing.salesExpertProfile?.tradingCustomerId)) {
|
||||||
|
throw new BadRequestException(CUSTOMER_HOLDS_MOBILE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
patch.mobile = mobile;
|
patch.mobile = mobile;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import { CUSTOMER_TOKEN_TYPE, EXPERT_TOKEN_TYPE, type CustomerJwtPayload } from
|
|||||||
import { faToEn } from './digits';
|
import { faToEn } from './digits';
|
||||||
import { SmsService } from '../sms/sms.service';
|
import { SmsService } from '../sms/sms.service';
|
||||||
import { brandName } from '../../common/brand';
|
import { brandName } from '../../common/brand';
|
||||||
|
import { activeExpertHoldsMobile } from '../../common/mobile-owner';
|
||||||
import type { RegisterDto } from './dto/register.dto';
|
import type { RegisterDto } from './dto/register.dto';
|
||||||
|
|
||||||
/** Nest 10 has no TooManyRequestsException; 429 with a Persian message. */
|
/** Nest 10 has no TooManyRequestsException; 429 with a Persian message. */
|
||||||
@@ -235,7 +236,7 @@ export class CustomerAuthService {
|
|||||||
const fname = dto.fname.trim();
|
const fname = dto.fname.trim();
|
||||||
const lname = dto.lname.trim();
|
const lname = dto.lname.trim();
|
||||||
const existing = await this.prisma.customer.findUnique({ where: { mobile }, select: { id: true } });
|
const existing = await this.prisma.customer.findUnique({ where: { mobile }, select: { id: true } });
|
||||||
if (existing) {
|
if (existing || (await activeExpertHoldsMobile(this.prisma, mobile))) {
|
||||||
throw new ConflictException('این شماره موبایل قبلاً ثبت شده است. از بخش ورود وارد شوید.');
|
throw new ConflictException('این شماره موبایل قبلاً ثبت شده است. از بخش ورود وارد شوید.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user