feat: manage expert access and merge Parsian expert seeds

Allow role-specific identities and admin account activation, support national-code-only claim experts, and remove required voice from v4/v5. Add a combined, resumable Parsian seed that preserves existing accounts and merges locations and product codes.
This commit is contained in:
SepehrYahyaee
2026-11-18 17:16:26 +03:30
parent 2cdbc35d2e
commit 83376df4ad
38 changed files with 3608 additions and 136 deletions
+52 -29
View File
@@ -114,27 +114,24 @@ export class ManagementService {
);
}
private async uniqueLogin(role: string, values: any, ownId?: string) {
const national = values.nationalCode;
const email = values.email;
for (const otherRole of [...STAFF_ROLES, RoleEnum.COMPANY]) {
const filters: any[] = [];
if (email) filters.push({ email }, { username: email });
// National-code login has no tenant selector: never introduce an ambiguous identifier.
if (national) filters.push({ nationalCode: national });
if (!filters.length) continue;
const existing = await this.repository(otherRole)
.findOne({
$or: filters,
...(ownId && otherRole === role
? { _id: { $ne: this.objectId(ownId) } }
: {}),
})
.lean();
if (existing)
throw new ConflictException(
"این ایمیل یا شناسه ورود قبلاً برای یک حساب ثبت شده است.",
);
}
// The selected role already disambiguates cross-role identities. There is
// no tenant selector, so aliases must remain unique within each role.
const filters: any[] = [];
if (values.email)
filters.push({ email: values.email }, { username: values.email });
if (values.nationalCode)
filters.push({ nationalCode: values.nationalCode });
if (!filters.length) return;
const existing = await this.repository(role)
.findOne({
$or: filters,
...(ownId ? { _id: { $ne: this.objectId(ownId) } } : {}),
})
.lean();
if (existing)
throw new ConflictException(
"این ایمیل یا کد ملی قبلاً برای همین نقش ثبت شده است.",
);
}
private async staffValues(
role: string,
@@ -253,16 +250,11 @@ export class ManagementService {
"Password must contain at least six characters",
);
if (
[RoleEnum.EXPERT, RoleEnum.DAMAGE_EXPERT, RoleEnum.COMPANY].includes(
role as RoleEnum,
) &&
[RoleEnum.EXPERT, RoleEnum.COMPANY].includes(role as RoleEnum) &&
!merged.email
)
throw new BadRequestException("Email is required for this role");
if (
[RoleEnum.EXPERT, RoleEnum.DAMAGE_EXPERT].includes(role as RoleEnum) &&
!merged.mobile
)
if (role === RoleEnum.EXPERT && !merged.mobile)
throw new BadRequestException(
"Mobile is required for blame and claim experts",
);
@@ -363,7 +355,11 @@ export class ManagementService {
const groups = await Promise.all(
roles.map(async (role) =>
(await this.repository(role).find(this.scope(key)).lean()).map(
(row) => ({ ...this.pick(row, STAFF_FIELDS), role }),
(row) => ({
...this.pick(row, STAFF_FIELDS),
role,
...(!account ? { isActive: row.isActive !== false } : {}),
}),
),
),
);
@@ -500,6 +496,33 @@ export class ManagementService {
throw error;
}
}
/** Only exposed by the super-admin controller, never by insurer/self edits. */
async setStaffActive(
clientId: string,
role: string,
id: string,
isActive: boolean,
) {
if (!STAFF_ROLES.includes(role as StaffRole))
throw new BadRequestException("نقش کارشناس معتبر نیست.");
if (typeof isActive !== "boolean")
throw new BadRequestException("وضعیت فعالیت باید فعال یا غیرفعال باشد.");
const key = await this.client(clientId);
const saved: any = await this.repository(role)
.findOneAndUpdate(
{ _id: this.objectId(id), ...this.scope(key) },
{ $set: { isActive } },
{ new: true, runValidators: true },
)
.lean();
if (!saved)
throw new NotFoundException("حساب کارشناس در این شرکت بیمه یافت نشد.");
return {
...this.pick(saved, STAFF_FIELDS),
isActive: saved.isActive !== false,
};
}
async listBranches(clientId: string) {
const key = await this.client(clientId);
return {