forked from Yara724/api
YARA-850
This commit is contained in:
@@ -298,6 +298,48 @@ export function partIdentityKey(p: DamageSelectedPartV2): string {
|
||||
return `${p.side}|${p.name}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a client `partId` from claim detail / expert reply to a selected part row.
|
||||
* Accepts `partIdentityKey` values (`id:12`, `left|backfender`), `id:12`, catalog id
|
||||
* when unique, or `index:N` for the Nth entry in `selectedParts`.
|
||||
*/
|
||||
export function resolveSelectedPartByPartId(
|
||||
partId: string,
|
||||
selected: DamageSelectedPartV2[],
|
||||
): DamageSelectedPartV2 | undefined {
|
||||
const raw = String(partId ?? "").trim();
|
||||
if (!raw || !selected.length) return undefined;
|
||||
|
||||
for (const p of selected) {
|
||||
if (partIdentityKey(p) === raw) return p;
|
||||
}
|
||||
|
||||
const idColon = raw.match(/^id:(\d+)$/i);
|
||||
if (idColon) {
|
||||
const id = Number(idColon[1]);
|
||||
return selected.find((p) => p.id === id);
|
||||
}
|
||||
|
||||
const indexColon = raw.match(/^index:(\d+)$/i);
|
||||
if (indexColon) {
|
||||
const i = Number(indexColon[1]);
|
||||
if (Number.isInteger(i) && i >= 0 && i < selected.length) {
|
||||
return selected[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (/^\d+$/.test(raw)) {
|
||||
const n = Number(raw);
|
||||
const byCatalogId = selected.filter((p) => p.id === n);
|
||||
if (byCatalogId.length === 1) return byCatalogId[0];
|
||||
if (Number.isInteger(n) && n >= 0 && n < selected.length) {
|
||||
return selected[n];
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Same canonical identity as `partIdentityKey`, but derived directly from a
|
||||
* unified `carPartDamage` snapshot (the shape we persist on
|
||||
|
||||
Reference in New Issue
Block a user