forked from Yara724/api
YARA-850
This commit is contained in:
37
src/helpers/outer-damage-parts-resolve.spec.ts
Normal file
37
src/helpers/outer-damage-parts-resolve.spec.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import {
|
||||
partIdentityKey,
|
||||
resolveSelectedPartByPartId,
|
||||
type DamageSelectedPartV2,
|
||||
} from "./outer-damage-parts";
|
||||
|
||||
describe("resolveSelectedPartByPartId", () => {
|
||||
const selected: DamageSelectedPartV2[] = [
|
||||
{
|
||||
id: 12,
|
||||
name: "backfender",
|
||||
side: "left",
|
||||
label_fa: "گلگیر",
|
||||
catalogKey: "left_backfender",
|
||||
},
|
||||
{
|
||||
id: null,
|
||||
name: "hood",
|
||||
side: "front",
|
||||
label_fa: "کاپوت",
|
||||
},
|
||||
];
|
||||
|
||||
it("resolves by partIdentityKey", () => {
|
||||
expect(
|
||||
resolveSelectedPartByPartId(partIdentityKey(selected[0]), selected),
|
||||
).toBe(selected[0]);
|
||||
});
|
||||
|
||||
it("resolves by id:N catalog prefix", () => {
|
||||
expect(resolveSelectedPartByPartId("id:12", selected)).toBe(selected[0]);
|
||||
});
|
||||
|
||||
it("resolves by index:N", () => {
|
||||
expect(resolveSelectedPartByPartId("index:1", selected)).toBe(selected[1]);
|
||||
});
|
||||
});
|
||||
@@ -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