Skip to content

Commit

Permalink
PR feedback 3
Browse files Browse the repository at this point in the history
  • Loading branch information
js-jankisalvi committed Nov 30, 2023
1 parent 6cdc701 commit e890e55
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ describe('ServiceNowITSMParamsFields renders', () => {

expect(editAction.mock.calls[0][1]).toEqual({
incident: { correlation_id: 'updated correlation id' },
comments: [],
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ const ServiceNowParamsFields: React.FunctionComponent<
actionParams.subActionParams ??
({
incident: {},
comments: [],
comments: selectedActionGroupId !== ACTION_GROUP_RECOVERED ? [] : undefined,
} as unknown as ServiceNowITSMActionParams['subActionParams']),
[actionParams.subActionParams]
[actionParams.subActionParams, selectedActionGroupId]
);

const [choices, setChoices] = useState<Fields>(defaultFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,22 +547,6 @@ describe('ServiceNow service', () => {
});
});

test('it should log warning if no incident found', async () => {
requestMock.mockImplementationOnce(() => ({
status: 404,
data: { result: [] },
}));
const res = await service.getIncidentByCorrelationId('custom_correlation_id');

expect(requestMock).toHaveBeenCalledTimes(1);
expect(logger.warn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"No incident found with correlation ID custom_correlation_id.",
]
`);
expect(res).toBe(null);
});

test('it should throw an error', async () => {
requestMock.mockImplementationOnce(() => {
throw new Error('An error has occurred');
Expand Down Expand Up @@ -1095,21 +1079,26 @@ describe('ServiceNow service', () => {
expect(requestMock).toHaveBeenCalledTimes(1);
expect(logger.warn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"Incident is already closed.",
"[ServiceNow][CloseIncident] Incident with correlation_id: null or incidentId: 1 is closed.",
]
`);
});

test('it should return null if found incident with correlation id is null', async () => {
requestMock.mockImplementationOnce(() => ({
data: {
result: [null],
result: [],
},
}));

const res = await service.closeIncident({ incidentId: null, correlationId: 'bar' });

expect(requestMock).toHaveBeenCalledTimes(1);
expect(logger.warn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"[ServiceNow][CloseIncident] No incident found with correlation_id: bar or incidentId: null.",
]
`);
expect(res).toBeNull();
});

Expand Down Expand Up @@ -1176,7 +1165,7 @@ describe('ServiceNow service', () => {
});

mockIncidentResponse(false);
mockImportIncident(true);
mockIncidentResponse(true);
mockIncidentResponse(true);

const res = await service.closeIncident({
Expand Down Expand Up @@ -1208,7 +1197,7 @@ describe('ServiceNow service', () => {
axios,
logger,
configurationUtilities,
url: 'https://example.com/api/now/v2/table/sn_si_incident/undefined',
url: 'https://example.com/api/now/v2/table/sn_si_incident/1',
method: 'get',
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,6 @@ export const createExternalService: ServiceFactory = ({

checkInstance(res);

if (res.status === 404) {
logger.warn(`No incident found with correlation ID ${correlationId}.`);
}

const foundIncident = res.data.result[0] ?? null;

return foundIncident;
Expand All @@ -292,8 +288,17 @@ export const createExternalService: ServiceFactory = ({
incidentToBeClosed = await getIncidentByCorrelationId(correlationId);
}

if (incidentToBeClosed?.state === 7) {
logger.warn(`Incident is already closed.`);
if (incidentToBeClosed === null || incidentToBeClosed?.status === 'error') {
logger.warn(
`[ServiceNow][CloseIncident] No incident found with correlation_id: ${correlationId} or incidentId: ${incidentId}.`
);
return null;
}

if (incidentToBeClosed.state === 7) {
logger.warn(
`[ServiceNow][CloseIncident] Incident with correlation_id: ${correlationId} or incidentId: ${incidentId} is closed.`
);

return {
title: incidentToBeClosed.number,
Expand All @@ -303,10 +308,6 @@ export const createExternalService: ServiceFactory = ({
};
}

if (incidentToBeClosed === null || incidentToBeClosed?.status === 'error') {
return null;
}

const closedIncident = await updateIncident({
incidentId: incidentToBeClosed.sys_id,
incident: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export interface ExternalService {
checkInstance: (res: AxiosResponse) => void;
getApplicationInformation: () => Promise<GetApplicationInfoResponse>;
checkIfApplicationIsInstalled: () => Promise<void>;
getIncidentByCorrelationId: (correlationId: string) => Promise<ServiceNowIncident>;
getIncidentByCorrelationId: (correlationId: string) => Promise<ServiceNowIncident | null>;
}

export type PushToServiceApiParams = ExecutorSubActionPushParams;
Expand Down

0 comments on commit e890e55

Please sign in to comment.