Skip to content

Commit 05c9bcc

Browse files
fix: add type narrowing for unknown types in routing form attribute handling
- Add proper type guards for field and value properties in fieldValueArray - Narrow unknown types to string and string[][] respectively with runtime checks - Fixes TS18046 errors in getAverageAttributeWeights and getAttributesForVirtualQueues - Maintains type safety while handling routing form attribute weights Co-Authored-By: [email protected] <[email protected]>
1 parent 276dadc commit 05c9bcc

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

packages/features/bookings/lib/getLuckyUser.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -567,17 +567,21 @@ export class LuckyUserService implements ILuckyUserService {
567567
) {
568568
let averageWeightsHosts: { userId: number; weight: number }[] = [];
569569

570-
const fieldValueArray = Object.values(attributesQueryValueChild).map((child) => ({
571-
field: child.properties?.field,
572-
value: child.properties?.value,
573-
}));
570+
const fieldValueArray = Object.values(attributesQueryValueChild).map((child) => {
571+
const props = child.properties;
572+
const field = typeof props?.field === "string" ? props.field : undefined;
573+
const valueRaw = props?.value;
574+
const value =
575+
Array.isArray(valueRaw) && valueRaw.every(Array.isArray) ? (valueRaw as string[][]) : undefined;
576+
return { field, value };
577+
});
574578

575579
fieldValueArray.map((obj) => {
576580
const attributeId = obj.field;
577581
const allRRHostsWeights = new Map<number, number[]>();
578582

579-
if (attributeId === attributeWithWeights.id) {
580-
obj.value.forEach((arrayobj: string[]) => {
583+
if (attributeId === attributeWithWeights.id && obj.value) {
584+
obj.value.forEach((arrayobj) => {
581585
arrayobj.forEach((attributeOption: string) => {
582586
const attributeOptionWithUsers = attributeWithWeights.options.find(
583587
(option) => option.value.toLowerCase() === attributeOption.toLowerCase()
@@ -635,16 +639,20 @@ export class LuckyUserService implements ILuckyUserService {
635639
) {
636640
let selectionOptions: Pick<VirtualQueuesDataType, "fieldOptionData">["fieldOptionData"] | undefined;
637641

638-
const fieldValueArray = Object.values(attributesQueryValueChild).map((child) => ({
639-
field: child.properties?.field,
640-
value: child.properties?.value,
641-
}));
642+
const fieldValueArray = Object.values(attributesQueryValueChild).map((child) => {
643+
const props = child.properties;
644+
const field = typeof props?.field === "string" ? props.field : undefined;
645+
const valueRaw = props?.value;
646+
const value =
647+
Array.isArray(valueRaw) && valueRaw.every(Array.isArray) ? (valueRaw as string[][]) : undefined;
648+
return { field, value };
649+
});
642650

643651
fieldValueArray.some((obj) => {
644652
const attributeId = obj.field;
645653

646-
if (attributeId === attributeWithWeights.id) {
647-
obj.value.some((arrayobj: string[]) => {
654+
if (attributeId === attributeWithWeights.id && obj.value) {
655+
obj.value.some((arrayobj) => {
648656
arrayobj.some((attributeOptionId: string) => {
649657
const content = attributeOptionId.slice(1, -1);
650658

0 commit comments

Comments
 (0)