Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServiceNow action improvements #60052

Merged
merged 18 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 15 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
*/

import { zipWith } from 'lodash';
import { Incident, CommentResponse } from './lib/types';
import { CommentResponse } from './lib/types';
import {
ActionHandlerArguments,
UpdateParamsType,
UpdateActionHandlerArguments,
IncidentCreationResponse,
CommentType,
CommentsZipped,
HandlerResponse,
Comment,
SimpleComment,
CreateHandlerArguments,
UpdateHandlerArguments,
IncidentHandlerArguments,
} from './types';
import { ServiceNow } from './lib';
import { transformFields, prepareFieldsForTransformation, transformComments } from './helpers';

const createComments = async (
export const createComments = async (
serviceNow: ServiceNow,
incidentId: string,
key: string,
comments: CommentType[]
): Promise<CommentsZipped[]> => {
comments: Comment[]
): Promise<SimpleComment[]> => {
const createdComments = await serviceNow.batchCreateComments(incidentId, comments, key);

return zipWith(comments, createdComments, (a: CommentType, b: CommentResponse) => ({
return zipWith(comments, createdComments, (a: Comment, b: CommentResponse) => ({
commentId: a.commentId,
pushedDate: b.pushedDate,
}));
Expand All @@ -35,16 +36,30 @@ export const handleCreateIncident = async ({
params,
comments,
mapping,
}: ActionHandlerArguments): Promise<IncidentCreationResponse> => {
const paramsAsIncident = params as Incident;
}: CreateHandlerArguments): Promise<HandlerResponse> => {
const fields = prepareFieldsForTransformation({
params,
mapping,
});

const incident = transformFields({
params,
fields,
});

const { incidentId, number, pushedDate } = await serviceNow.createIncident({
...paramsAsIncident,
...incident,
});

const res: IncidentCreationResponse = { incidentId, number, pushedDate };
const res: HandlerResponse = { incidentId, number, pushedDate };

if (comments && Array.isArray(comments) && comments.length > 0) {
if (
comments &&
Array.isArray(comments) &&
comments.length > 0 &&
mapping.get('comments').actionType !== 'nothing'
) {
comments = transformComments(comments, params, ['informationAdded']);
res.comments = [
...(await createComments(serviceNow, incidentId, mapping.get('comments').target, comments)),
];
Expand All @@ -59,20 +74,51 @@ export const handleUpdateIncident = async ({
params,
comments,
mapping,
}: UpdateActionHandlerArguments): Promise<IncidentCreationResponse> => {
const paramsAsIncident = params as UpdateParamsType;
}: UpdateHandlerArguments): Promise<HandlerResponse> => {
const currentIncident = await serviceNow.getIncident(incidentId);
const fields = prepareFieldsForTransformation({
params,
mapping,
defaultPipes: ['informationUpdated'],
});

const incident = transformFields({
params,
fields,
currentIncident,
});

const { number, pushedDate } = await serviceNow.updateIncident(incidentId, {
...paramsAsIncident,
...incident,
});

const res: IncidentCreationResponse = { incidentId, number, pushedDate };
const res: HandlerResponse = { incidentId, number, pushedDate };

if (comments && Array.isArray(comments) && comments.length > 0) {
if (
comments &&
Array.isArray(comments) &&
comments.length > 0 &&
mapping.get('comments').actionType !== 'nothing'
) {
comments = transformComments(comments, params, ['informationAdded']);
res.comments = [
...(await createComments(serviceNow, incidentId, mapping.get('comments').target, comments)),
];
}

return { ...res };
};

export const handleIncident = async ({
incidentId,
serviceNow,
params,
comments,
mapping,
}: IncidentHandlerArguments): Promise<HandlerResponse> => {
if (!incidentId) {
return await handleCreateIncident({ serviceNow, params, comments, mapping });
} else {
return await handleUpdateIncident({ incidentId, serviceNow, params, comments, mapping });
}
};
Loading