| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | import Ember from 'ember'; | 
					
						
							|  |  |  | import { IDatasetComment, IDatasetCommentsGetResponse } from 'wherehows-web/typings/api/datasets/comments'; | 
					
						
							| 
									
										
										
										
											2018-02-21 09:46:04 -08:00
										 |  |  | import { datasetUrlById, datasetUrlByUrn } from 'wherehows-web/utils/api/datasets/shared'; | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | import { ApiStatus } from 'wherehows-web/utils/api/shared'; | 
					
						
							| 
									
										
										
										
											2018-02-21 09:46:04 -08:00
										 |  |  | import { getJSON } from 'wherehows-web/utils/api/fetcher'; | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 09:46:04 -08:00
										 |  |  | const { $: { getJSON: $getJSON, post, ajax } } = Ember; | 
					
						
							| 
									
										
										
										
											2017-09-20 14:25:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TODO:  DSS-6122 Create and move to Error module
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * default message for comment api exception | 
					
						
							|  |  |  |  * @type {string} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const datasetCommentsApiException = 'An error occurred with the comments api'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @type {string} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const csrfToken = '_UNUSED_'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Returns a dataset comment url for a given Id | 
					
						
							|  |  |  |  * @param {number} id the dataset Id | 
					
						
							|  |  |  |  * @return {string} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const datasetCommentsUrlById = (id: number): string => `${datasetUrlById(id)}/comments`; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 09:46:04 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Returns the url for a dataset comment by urn | 
					
						
							|  |  |  |  * @param {string} urn | 
					
						
							|  |  |  |  * @return {string} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const datasetCommentsUrnByUrn = (urn: string): string => `${datasetUrlByUrn(urn)}/comments`; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Gets a specific comment on a dataset | 
					
						
							|  |  |  |  * @param {number} datasetId the id of the dataset | 
					
						
							|  |  |  |  * @param {number} commentId the id of the comment | 
					
						
							|  |  |  |  * @return {string} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const datasetCommentUrlById = (datasetId: number, commentId: number): string => | 
					
						
							|  |  |  |   `${datasetCommentsUrlById(datasetId)}/${commentId}`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * For a given dataset Id, fetches the list of comments associated with the dataset | 
					
						
							|  |  |  |  * @param {number} id the id of the dataset | 
					
						
							|  |  |  |  * @return {Promise<Array<IDatasetComment>>} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-09-19 18:20:03 -07:00
										 |  |  | const readDatasetComments = async (id: number): Promise<Array<IDatasetComment>> => { | 
					
						
							| 
									
										
										
										
											2018-02-21 09:46:04 -08:00
										 |  |  |   const response: IDatasetCommentsGetResponse = await Promise.resolve($getJSON(datasetCommentsUrlById(id))); | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |   const { status, data: { comments } } = response; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (status === ApiStatus.OK) { | 
					
						
							|  |  |  |     return comments; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   throw new Error(datasetCommentsApiException); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 09:46:04 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Reads the dataset comments related to the urn | 
					
						
							|  |  |  |  * @param {string} urn | 
					
						
							|  |  |  |  * @return {Promise<Array<IDatasetComment>>} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const readDatasetCommentsByUrn = async (urn: string): Promise<Array<IDatasetComment>> => { | 
					
						
							|  |  |  |   const { data: { comments } } = await getJSON<Pick<IDatasetCommentsGetResponse, 'data'>>({ | 
					
						
							|  |  |  |     url: datasetCommentsUrnByUrn(urn) | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   return comments; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Posts a new comment on a dataset | 
					
						
							|  |  |  |  * @param {number} id the id of the dataset | 
					
						
							|  |  |  |  * @param {CommentTypeUnion} type the comment type | 
					
						
							|  |  |  |  * @param {string} text the comment | 
					
						
							|  |  |  |  * @return {Promise<void>} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-09-19 18:20:03 -07:00
										 |  |  | const createDatasetComment = async ( | 
					
						
							|  |  |  |   id: number, | 
					
						
							|  |  |  |   { type, text }: Pick<IDatasetComment, 'type' | 'text'> | 
					
						
							|  |  |  | ): Promise<void> => { | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |   const response: { status: ApiStatus } = await Promise.resolve( | 
					
						
							|  |  |  |     post({ | 
					
						
							|  |  |  |       url: datasetCommentsUrlById(id), | 
					
						
							|  |  |  |       headers: { | 
					
						
							|  |  |  |         'Csrf-Token': csrfToken | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       data: { | 
					
						
							|  |  |  |         type, | 
					
						
							|  |  |  |         text | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { status } = response; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Comments api uses the ApiStatus.SUCCESS string literal
 | 
					
						
							|  |  |  |   if (status !== ApiStatus.SUCCESS) { | 
					
						
							|  |  |  |     throw new Error(datasetCommentsApiException); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Deletes a dataset comment using the mid-tier endpoint | 
					
						
							|  |  |  |  * @param {number} datasetId the id of the dataset that has the comment | 
					
						
							|  |  |  |  * @param {number} commentId the id of the comment on the specified dataset | 
					
						
							|  |  |  |  * @return {Promise<void>} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const deleteDatasetComment = async (datasetId: number, commentId: number): Promise<void> => { | 
					
						
							|  |  |  |   const response: { status: ApiStatus } = await Promise.resolve( | 
					
						
							|  |  |  |     ajax({ | 
					
						
							|  |  |  |       url: datasetCommentUrlById(datasetId, commentId), | 
					
						
							|  |  |  |       method: 'DELETE', | 
					
						
							|  |  |  |       headers: { | 
					
						
							|  |  |  |         'Csrf-Token': csrfToken | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       dataType: 'json' | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { status } = response; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (status !== ApiStatus.SUCCESS) { | 
					
						
							|  |  |  |     throw new Error(datasetCommentsApiException); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Modifies a previous comment on this dataset | 
					
						
							|  |  |  |  * @param {number} datasetId the dataset with the comment to be modified | 
					
						
							|  |  |  |  * @param {number} commentId the comment to be modified | 
					
						
							|  |  |  |  * @param {CommentTypeUnion} type the type of the comment | 
					
						
							|  |  |  |  * @param {string} text the updated comment text | 
					
						
							|  |  |  |  * @return {Promise<void>} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-09-19 18:20:03 -07:00
										 |  |  | const updateDatasetComment = async ( | 
					
						
							| 
									
										
										
										
											2017-09-10 19:31:54 -07:00
										 |  |  |   datasetId: number, | 
					
						
							|  |  |  |   commentId: number, | 
					
						
							|  |  |  |   { type, text }: IDatasetComment | 
					
						
							|  |  |  | ): Promise<void> => { | 
					
						
							|  |  |  |   const response: { status: ApiStatus } = await Promise.resolve( | 
					
						
							|  |  |  |     ajax({ | 
					
						
							|  |  |  |       url: datasetCommentUrlById(datasetId, commentId), | 
					
						
							|  |  |  |       method: 'PUT', | 
					
						
							|  |  |  |       headers: { | 
					
						
							|  |  |  |         'Csrf-Token': csrfToken | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       dataType: 'json', | 
					
						
							|  |  |  |       data: { type, text } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { status } = response; | 
					
						
							|  |  |  |   if (status !== ApiStatus.SUCCESS) { | 
					
						
							|  |  |  |     throw new Error(datasetCommentsApiException); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 09:46:04 -08:00
										 |  |  | export { | 
					
						
							|  |  |  |   readDatasetComments, | 
					
						
							|  |  |  |   createDatasetComment, | 
					
						
							|  |  |  |   deleteDatasetComment, | 
					
						
							|  |  |  |   updateDatasetComment, | 
					
						
							|  |  |  |   readDatasetCommentsByUrn | 
					
						
							|  |  |  | }; |