parity(admin): D17 — SalesExpertProfile, AdminUser name parts, agent grid fields

Restores the sales-expert fields that were lost when a sales expert became
an AdminUser instead of a type=1 Customer row.

New SalesExpertProfile holds cityId, gramLevel, phone and nationalCode. A
separate table rather than columns on AdminUser, because these mean
something only for role = SALES_EXPERT. gramLevel is DECIMAL(14,4), matched
to Customer.goldCreditLimitGrams so the same credit limit has identical
precision on both tables. phone and nationalCode copy the reference exactly:
nullable, NOT unique, no format rule — createCustomer validates only mobile
and percent, and both fields are required:false on the reference form.
Inventing a uniqueness constraint would reject data the reference accepts.

reagent_id is deliberately NOT ported (D17). Every use of it in the
reference reads a CUSTOMER's reagent_id, which the target already models as
Customer.referredByAgentId. On a sales expert's own form it was a side
effect of the shared customers form: filling it removed that expert from
the selectable-agent list, because queryReagent filters on
whereNull('reagent_id'). The target's equivalent filters on role and active
only, so no agent can be dropped — strictly better than the reference.

AdminUser gains firstName/lastName with the same additive migration,
backfill and sync trigger as Customer, for all admins rather than only
sales experts. This closes D0 requirement 7, and every remaining
`fname: fullName, lname: ''` was fixed — six further sites across
admin-users, admin-comments, admin-documents, admin-orders and
admin-preorders.

listReagents now returns real city_id, city, gram_level, phone and ncode,
and left-joins so an expert without a profile still lists with empty cells
rather than vanishing. The two grid filters map through the relation. The
service upserts a profile on create and on a role change to SALES_EXPERT,
and the migration backfilled one for every existing expert.

ReagentCustomer.vue gains the four fields plus the city -> commission
autofill, ported verbatim including the reference's falsy test so a city
percent of 0 also falls through to the aux lookup (replicated, not fixed).

Tests: new sales-expert round trip 33/33, covering create with every field,
an edit form with each field populated separately, a byte-identical no-op
save, single-field edits, both grid filters with negative cases, the
left-join case and the referrer dropdown. «محمد علی» + «احمدی» survives
intact. Plus jest 32/32, auth-smoke 53/53, customer trigger 9/9, typecheck
clean on api and admin-panel. Migration dry-run twice in a rolled-back
transaction before applying; drift is zero.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
s.esmaeili
2026-09-22 17:50:47 +03:30
co-authored by Claude Opus 5
parent e3af31948e
commit 2194b3d4d9
13 changed files with 905 additions and 39 deletions
+66 -2
View File
@@ -123,11 +123,28 @@ model City {
addresses CustomerAddress[]
branches Branch[]
preOrders PreOrder[]
salesExpertProfiles SalesExpertProfile[]
}
model AdminUser {
id String @id @default(cuid())
fullName String
id String @id @default(cuid())
/// ⚠️ Kept in sync BY A DATABASE TRIGGER — `admin_user_sync_full_name`, from
/// migration 20260922180000. Prisma cannot see it, so a write here may store
/// something other than what you passed:
///
/// * write the parts -> fullName is rebuilt as "first last"
/// * write fullName only -> the parts are re-derived from it
/// * write BOTH in one update -> THE PARTS WIN; the submitted fullName is
/// discarded
///
/// Re-read after writing if you need the stored value. Same arrangement as
/// Customer (D0 requirement 7 — the split applies to ALL admins, not only
/// sales experts).
fullName String
firstName String?
lastName String?
mobile String @unique
passwordHash String
role AdminRole
@@ -164,9 +181,56 @@ model AdminUser {
sentNotifications Notification[] @relation("NotificationAuthor")
uploadedFiles UploadedFile[] @relation("FileUploader")
/// Only ever set for role = SALES_EXPERT (D17).
salesExpertProfile SalesExpertProfile?
@@index([role])
@@index([userTypeId])
@@index([avatarFileId])
@@index([firstName])
@@index([lastName])
}
/// The sales-expert fields legacy kept on the `customers` row (type=1) and that
/// were lost when a sales expert became an AdminUser (D17).
///
/// A separate table rather than columns on AdminUser: these are meaningful only
/// for role = SALES_EXPERT and would be permanently null for every other admin.
/// A row is created for every SALES_EXPERT by the migration, and upserted by the
/// service whenever one is created or a user's role becomes SALES_EXPERT — but
/// readers still left-join, so a missing profile degrades to nulls rather than
/// dropping the agent from the grid.
///
/// `reagent_id` is deliberately NOT here — see D17: in the reference it was a
/// side effect of the shared customers form, and filling it removed that expert
/// from the selectable-agent list (queryReagent, AdminController.php:420).
model SalesExpertProfile {
id String @id @default(cuid())
adminUserId String @unique
adminUser AdminUser @relation(fields: [adminUserId], references: [id], onDelete: Cascade)
/// Legacy customers.city_id — a filterable grid column
/// (ReagentListModel.js:14) and the source of the commission autofill
/// (ReagentCustomer.vue:215-228).
cityId String?
city City? @relation(fields: [cityId], references: [id])
/// Legacy customers.gram_level — the «سطح اعتبار» column, filterable.
/// Decimal(14,4) matches Customer.goldCreditLimitGrams exactly so the same
/// credit limit has identical precision on both tables.
gramLevel Decimal @default(0) @db.Decimal(14, 4)
/// Legacy customers.phone (varchar 15) and ncode (varchar 20). The reference
/// stores both and reads neither back; kept so the form matches it. Neither
/// is unique and neither has a validation rule in the reference — inventing
/// one would reject data the reference accepts.
phone String? @db.VarChar(15)
nationalCode String? @db.VarChar(20)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@index([cityId])
}
model Customer {