Skip to content

Commit

Permalink
cleanup and added link to API documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic committed Nov 22, 2019
1 parent ced5899 commit b67409b
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ export const caseSavedObjectMappings: {
},
},
},
comments: {
type: 'keyword',
},
creation_date: {
type: 'date',
},
Expand All @@ -45,7 +42,7 @@ export const caseSavedObjectMappings: {
last_edit_date: {
type: 'date',
},
name: {
title: {
type: 'keyword',
},
reporter: {
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/case/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Case Workflow

*Experimental Feature*

Elastic is developing a Case Management Workflow. Follow our progress:

- [Case API Documentation](https://documenter.getpostman.com/view/172706/SW7c2SuF?version=latest)
- [Github Meta](https://github.com/elastic/kibana/issues/50103)

33 changes: 17 additions & 16 deletions x-pack/plugins/case/server/routes/api/delete_case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,35 @@ export function initDeleteCaseApi({ caseService, router }: RouteDeps) {
},
},
async (context, request, response) => {
let theCase;
let allCaseComments;
try {
theCase = await caseService.getCase({
await caseService.deleteCase({
client: context.core.savedObjects.client,
caseId: request.params.id,
});
} catch (error) {
return response.customError(wrapError(error));
}
try {
const comments = theCase.attributes.comments;
await Promise.all(
comments.map((commentId: string) =>
caseService.deleteComment({
client: context.core.savedObjects.client,
commentId,
})
)
);
allCaseComments = await caseService.getAllCaseComments({
client: context.core.savedObjects.client,
caseId: request.params.id,
});
} catch (error) {
return response.customError(wrapError(error));
}
try {
await caseService.deleteCase({
client: context.core.savedObjects.client,
caseId: request.params.id,
});
return response.ok({ body: { deletedCase: true } });
if (allCaseComments.saved_objects.length > 0) {
await Promise.all(
allCaseComments.saved_objects.map(({ id }) =>
caseService.deleteComment({
client: context.core.savedObjects.client,
commentId: id,
})
)
);
}
return response.noContent();
} catch (error) {
return response.customError(wrapError(error));
}
Expand Down
25 changes: 2 additions & 23 deletions x-pack/plugins/case/server/routes/api/delete_comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { schema } from '@kbn/config-schema';
import { RouteDeps } from '.';
import { formatUpdatedCase, wrapError } from './utils';
import { wrapError } from './utils';

export function initDeleteCommentApi({ caseService, router }: RouteDeps) {
router.delete(
Expand All @@ -21,33 +21,12 @@ export function initDeleteCommentApi({ caseService, router }: RouteDeps) {
},
async (context, request, response) => {
const client = context.core.savedObjects.client;
let theCase;
try {
await caseService.deleteComment({
client,
commentId: request.params.comment_id,
});
} catch (error) {
return response.customError(wrapError(error));
}
try {
theCase = await caseService.getCase({
client,
caseId: request.params.case_id,
});
} catch (error) {
return response.customError(wrapError(error));
}
try {
const comments = theCase.attributes!.comments.filter(
(comment: string) => comment !== request.params.comment_id
);
const updatedCase = await caseService.updateCase({
client: context.core.savedObjects.client,
caseId: request.params.case_id,
updatedAttributes: formatUpdatedCase({ comments }),
});
return response.ok({ body: { deleted: true, updatedCase } });
return response.noContent();
} catch (error) {
return response.customError(wrapError(error));
}
Expand Down
13 changes: 11 additions & 2 deletions x-pack/plugins/case/server/routes/api/get_case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ export function initGetCaseApi({ caseService, router }: RouteDeps) {
},
},
async (context, request, response) => {
let theCase;
try {
const theCase = await caseService.getCase({
theCase = await caseService.getCase({
client: context.core.savedObjects.client,
caseId: request.params.id,
});
return response.ok({ body: theCase });
} catch (error) {
return response.customError(wrapError(error));
}
try {
const theComments = await caseService.getAllCaseComments({
client: context.core.savedObjects.client,
caseId: request.params.id,
});
return response.ok({ body: { case: theCase, comments: theComments } });
} catch (error) {
return response.customError(wrapError(error));
}
Expand Down
31 changes: 5 additions & 26 deletions x-pack/plugins/case/server/routes/api/post_comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { schema } from '@kbn/config-schema';
import { formatUpdatedCase, formatNewComment, wrapError } from './utils';
import { formatNewComment, wrapError } from './utils';
import { NewCommentSchema } from './schema';
import { RouteDeps } from '.';

Expand All @@ -23,11 +23,10 @@ export function initPostCommentApi({ caseService, router }: RouteDeps) {
async (context, request, response) => {
let user;
let newComment;
let theCase;
try {
user = await caseService.getUser({ request, response });
} catch (error) {
return response.customError(wrapError(error));
} catch (e) {
return e;
}
try {
newComment = await caseService.postNewComment({
Expand All @@ -39,28 +38,8 @@ export function initPostCommentApi({ caseService, router }: RouteDeps) {
case_workflow_id: request.params.id,
}),
});
} catch (error) {
return response.customError(wrapError(error));
}
try {
theCase = await caseService.getCase({
client: context.core.savedObjects.client,
caseId: request.params.id,
});
} catch (error) {
return response.customError(wrapError(error));
}
try {
const updatedCase = await caseService.updateCase({
client: context.core.savedObjects.client,
caseId: request.params.id,
updatedAttributes: formatUpdatedCase({
comments: theCase.attributes!.comments.length
? [...theCase.attributes!.comments, newComment.id]
: [newComment.id],
}),
});
return response.ok({ body: { newComment, updatedCase } });

return response.ok({ body: { newComment } });
} catch (error) {
return response.customError(wrapError(error));
}
Expand Down
16 changes: 7 additions & 9 deletions x-pack/plugins/case/server/routes/api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,25 @@ export const CommentSchema = schema.object({
case_workflow_id: schema.string(),
});

export const UpdatedCommentSchema = schema.object({
comment: schema.string(),
last_edit_date: schema.number(),
});

export const NewCaseSchema = schema.object({
assignees: schema.arrayOf(UserSchema, { defaultValue: [] }),
comments: schema.arrayOf(schema.string(), { defaultValue: [] }),
description: schema.string(),
name: schema.string(),
title: schema.string(),
state: schema.oneOf([schema.literal('open'), schema.literal('closed')], { defaultValue: 'open' }),
tags: schema.arrayOf(schema.string(), { defaultValue: [] }),
case_type: schema.string(),
});

export const UpdatedCaseSchema = schema.object({
assignees: schema.maybe(schema.arrayOf(UserSchema)),
comments: schema.maybe(schema.arrayOf(schema.string())),
description: schema.maybe(schema.string()),
name: schema.maybe(schema.string()),
title: schema.maybe(schema.string()),
state: schema.maybe(schema.oneOf([schema.literal('open'), schema.literal('closed')])),
tags: schema.maybe(schema.arrayOf(schema.string())),
case_type: schema.maybe(schema.string()),
});

export const UpdatedCommentSchema = schema.object({
comment: schema.string(),
last_edit_date: schema.number(),
});
34 changes: 14 additions & 20 deletions x-pack/plugins/case/server/routes/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,11 @@ import {
UserSchema,
} from './schema';

export type NewCaseType = TypeOf<typeof NewCaseSchema>;
export type NewCommentFormatted = TypeOf<typeof CommentSchema>;
export type NewCommentType = TypeOf<typeof NewCommentSchema>;
export type UpdatedCaseTyped = TypeOf<typeof UpdatedCaseSchema>;

export interface UpdatedCaseType {
assignees?: UpdatedCaseTyped['assignees'];
comments?: UpdatedCaseTyped['comments'];
description?: UpdatedCaseTyped['description'];
name?: UpdatedCaseTyped['name'];
state?: UpdatedCaseTyped['state'];
tags?: UpdatedCaseTyped['tags'];
case_type?: UpdatedCaseTyped['case_type'];
}

export type UpdatedCommentType = TypeOf<typeof UpdatedCommentSchema>;

export interface UpdatedCaseFormatted extends UpdatedCaseType {
last_edit_date: number;
}

export type NewCaseType = TypeOf<typeof NewCaseSchema>;

export type UserType = TypeOf<typeof UserSchema>;

export interface NewCaseFormatted extends NewCaseType {
Expand All @@ -42,6 +27,15 @@ export interface NewCaseFormatted extends NewCaseType {
reporter: UserType;
}

export type NewCommentType = TypeOf<typeof NewCommentSchema>;
export interface UpdatedCaseFormatted extends UpdatedCaseType {
last_edit_date: number;
}

export type NewCommentFormatted = TypeOf<typeof CommentSchema>;
export interface UpdatedCaseType {
assignees?: UpdatedCaseTyped['assignees'];
description?: UpdatedCaseTyped['description'];
title?: UpdatedCaseTyped['title'];
state?: UpdatedCaseTyped['state'];
tags?: UpdatedCaseTyped['tags'];
case_type?: UpdatedCaseTyped['case_type'];
}
2 changes: 1 addition & 1 deletion x-pack/plugins/case/server/routes/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export const formatNewComment = ({
username,
case_workflow_id,
}: NewCommentArgs): NewCommentFormatted => ({
...newComment,
creation_date: new Date().valueOf(),
last_edit_date: new Date().valueOf(),
user: { full_name, username },
case_workflow_id,
...newComment,
});

export const formatUpdatedCase = (updateCase: UpdatedCaseType): UpdatedCaseFormatted => ({
Expand Down
14 changes: 9 additions & 5 deletions x-pack/plugins/case/server/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
UpdatedCaseFormatted,
UpdatedCommentType,
} from '../routes/api/types';
import { wrapError } from '../routes/api/utils';
import { PluginSetupContract as SecurityPluginSetup } from '../../../security/server/plugin';

interface ClientArgs {
Expand Down Expand Up @@ -137,13 +136,18 @@ export class CaseService {
user = await authentication!.getCurrentUser(request);
} catch (error) {
this.log.debug(`Error on GET user: ${error}`);
throw response.customError(wrapError(error));
throw error;
}
if (!user) {
this.log.debug(`Error on GET user: Bad User`);
throw response.customError(
wrapError({ name: 'Bad User', message: 'The user is not authenticated' })
);
throw response.badRequest({
body: {
message: 'Bad User - the user is not authenticated',
attributes: {
requestBody: request.body,
},
},
});
}
return user;
},
Expand Down

0 comments on commit b67409b

Please sign in to comment.