Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/modules/service-catalog/hooks/useItemFormFields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,57 @@ describe("useItemFormFields", () => {
const presentIds = result.current.requestFields.map((f) => f.id);
expect(presentIds).toEqual([1, 6]);
});

it("should filter out category lookup field from requestFields", async () => {
const categoryLookupField = {
id: 8,
type: "lookup",
description: "Category",
title_in_portal: "Category",
editable_in_portal: true,
relationship_target_type:
"zen:custom_object:standard::service_catalog_category",
required_in_portal: false,
active: true,
};

const formResponse = {
ticket_form: {
id: 1,
ticket_field_ids: [1, 2, 8],
active: true,
},
};

const ticketFieldResponse = {
ticket_fields: [textField, lookupField, categoryLookupField],
};

(globalThis.fetch as jest.Mock) = jest.fn((url) => {
return Promise.resolve({
json: () =>
Promise.resolve(
url.includes("/api/v2/ticket_forms/1")
? formResponse
: url.includes(`/api/v2/ticket_fields?locale=${baseLocale}`)
? ticketFieldResponse
: {}
),
status: 200,
ok: true,
});
});

const { result, waitForNextUpdate } = renderHook(() =>
useItemFormFields(serviceCatalogItem, baseLocale)
);

await waitForNextUpdate();

const presentIds = result.current.requestFields.map((f) => f.id);
expect(presentIds).toEqual([1]);
expect(result.current.associatedLookupField).toEqual(
expect.objectContaining({ id: 2, type: "lookup" })
);
});
});
24 changes: 17 additions & 7 deletions src/modules/service-catalog/hooks/useItemFormFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,23 @@ const formatField = (field: TicketField): TicketFieldObject => {
};
};

const HIDDEN_SERVICE_CATALOG_LOOKUP_KEYS = [
"standard::service_catalog_item",
"standard::service_catalog_category",
];

const isAssociatedLookupField = (field: TicketField) => {
const customObjectKey = getCustomObjectKey(
field.relationship_target_type as string
);
if (customObjectKey === "standard::service_catalog_item") {
return true;
}
return false;
return customObjectKey === "standard::service_catalog_item";
};

const isHiddenServiceCatalogLookup = (field: TicketField) => {
const customObjectKey = getCustomObjectKey(
field.relationship_target_type as string
);
return HIDDEN_SERVICE_CATALOG_LOOKUP_KEYS.includes(customObjectKey);
};

const enrichFieldsWithAssetConfig = (
Expand Down Expand Up @@ -183,10 +192,11 @@ const fetchTicketFields = async (
) {
if (
ticketField.type === "lookup" &&
isAssociatedLookupField(ticketField)
isHiddenServiceCatalogLookup(ticketField)
) {
associatedLookupField = ticketField;

if (isAssociatedLookupField(ticketField)) {
associatedLookupField = ticketField;
}
return null;
}
return formatField(ticketField);
Expand Down
Loading