339 lines
16 KiB
JavaScript
339 lines
16 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('knowledgeArticleModule')
|
|
.service('knowledgeArticleService', ['$resource', '$filter', 'configurationModel', 'systemAlertService', 'searchModel', function ($resource, $filter, configurationModel, systemAlertService, searchModel) {
|
|
var resource = $resource('/smartit/rest/knowledge/:id', {}, {
|
|
getArticleById: { method: 'GET' /*, url: 'mocks/knowledge-article.json'*/, isArray: true },
|
|
incrementViewCount: { method: 'PUT', url: '/smartit/rest/knowledge/incrementViewCount/:id', isArray: true },
|
|
rateArticle: { method: 'PUT', url: '/smartit/rest/knowledge/:operation/:id', isArray: true },
|
|
getArticleRevisions: { method: 'GET', url: '/smartit/rest/knowledge/revisions/:id', isArray: true },
|
|
getDraft: { method: 'GET', url: '/smartit/rest/knowledge/draft', isArray: true },
|
|
getProblemDraft: {
|
|
method: 'POST',
|
|
url: '/smartit/rest/ticket/create/knowledge/from/problem/:id',
|
|
isArray: true
|
|
},
|
|
getKnownErrorDraft: {
|
|
method: 'POST',
|
|
url: '/smartit/rest/ticket/create/knowledge/from/knownerror/:id',
|
|
isArray: true
|
|
},
|
|
getIncidentDraft: {
|
|
method: 'POST',
|
|
url: '/smartit/rest/ticket/create/knowledge/from/incident/:id',
|
|
isArray: true
|
|
},
|
|
getWorkorderDraft: {
|
|
method: 'POST',
|
|
url: '/smartit/rest/ticket/create/knowledge/from/workorder/:id',
|
|
isArray: true
|
|
},
|
|
getVisibility: {
|
|
method: 'GET',
|
|
url: '/smartit/rest/knowledge/visibility',
|
|
//url: 'mocks/ka-metadata-visibility.json',
|
|
isArray: true
|
|
},
|
|
createArticle: {
|
|
method: 'POST',
|
|
url: '/smartit/rest/knowledge/createknowledge/user/:loginId',
|
|
isArray: true,
|
|
transformRequest: function (data) {
|
|
data.status = data.status.value;
|
|
return angular.toJson(data);
|
|
}
|
|
},
|
|
//TODO: finish it when backend will ready
|
|
updateArticle: { method: 'PUT', url: '/smartit/rest/knowledge/all/:id', isArray: true },
|
|
getAutoSavedCopy: {
|
|
url: '/smartit/rest/knowledge/save',
|
|
method: 'GET',
|
|
isArray: false
|
|
},
|
|
createAutoSavedCopy: {
|
|
url: '/smartit/rest/knowledge/save',
|
|
method: 'POST',
|
|
isArray: false
|
|
},
|
|
removeAutoSavedCopy: {
|
|
url: '/smartit/rest/knowledge/save',
|
|
method: 'DELETE',
|
|
isArray: true
|
|
},
|
|
checkEditStatus: {
|
|
url: '/smartit/rest/knowledge/save/status',
|
|
method: 'GET',
|
|
isArray: false
|
|
},
|
|
getTemplates: {
|
|
url: '/smartit/rest/knowledge/templates',
|
|
method: 'GET',
|
|
isArray: true
|
|
},
|
|
updateTemplateStyles: {
|
|
method: 'PUT',
|
|
url: '/smartit/rest/knowledge/templatestyles/update',
|
|
isArray: false
|
|
},
|
|
getPotentialPrimary: {
|
|
method: 'POST',
|
|
url: '/smartit/rest/foundation/primary',
|
|
isArray: true
|
|
},
|
|
getAssessmentQuestions: {
|
|
method: 'GET',
|
|
url: '/smartit/rest/knowledge/assessment/questions',
|
|
isArray: true
|
|
},
|
|
submitAssessment: {
|
|
method: 'POST',
|
|
url: '/smartit/rest/knowledge/assessment',
|
|
isArray: false
|
|
},
|
|
getQuestionSetList: {
|
|
method: 'GET',
|
|
url: '/smartit/rest/knowledge/assessment/questionsetlist',
|
|
isArray: true
|
|
},
|
|
getQuestionSetById: {
|
|
method: 'GET',
|
|
url: '/smartit/rest/knowledge/assessment/questionset/draft',
|
|
isArray: true
|
|
},
|
|
deleteQuestionSetById: {
|
|
method: 'DELETE',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
data: '',
|
|
url: '/smartit/rest/knowledge/assessment/deletequestionset'
|
|
},
|
|
createQuestionSet: {
|
|
method: 'POST',
|
|
url: '/smartit/rest/knowledge/assessment/createquestionset',
|
|
isArray: true
|
|
},
|
|
updateQuestionSet: {
|
|
method: 'PUT',
|
|
url: '/smartit/rest/knowledge/assessment/updateassessment/:id',
|
|
isArray: true
|
|
},
|
|
getLatestVersionId: { method: 'GET', url: '/smartit/rest/knowledge/link/:id', isArray: true }
|
|
}), serializationMap = configurationModel.get('knowledgeArticle.categorySerializationMap');
|
|
this.getArticleById = function (id, preventIncrement) {
|
|
return resource.getArticleById({ id: id, preventIncrement: preventIncrement }).$promise.then(function (result) {
|
|
return convertArticle(new KnowledgeArticleVO().build(result[0].items[0]));
|
|
});
|
|
};
|
|
this.getLatestVersionId = function (articleId) {
|
|
return resource.getLatestVersionId({ id: articleId }).$promise.then(function (result) {
|
|
return result[0].items[0];
|
|
});
|
|
};
|
|
function convertArticle(article) {
|
|
_.forEach(serializationMap, function (field) {
|
|
var primary = [];
|
|
if (!_.isEmpty(article[field.primary])) {
|
|
if (_.isArray(article[field.primary])) {
|
|
primary = _.map(article[field.primary], function (field) {
|
|
field.primary = true;
|
|
return field;
|
|
});
|
|
}
|
|
else {
|
|
primary = [angular.extend({ primary: true }, article[field.primary])];
|
|
}
|
|
}
|
|
article[field.all] = primary.concat(article[field.additional]);
|
|
});
|
|
return article;
|
|
}
|
|
this.incrementViewCount = function (id, source) {
|
|
return resource.incrementViewCount({ id: id }, { incrementCount: 1, source: source });
|
|
};
|
|
this.rateArticle = function (id, vote) {
|
|
var operation = vote ? 'useCount' : 'notUsefulCount';
|
|
return resource.rateArticle({ id: id, operation: operation }, {}).$promise;
|
|
};
|
|
this.getArticleRevisions = function (id) {
|
|
return resource.getArticleRevisions({ id: id }).$promise.then(function (result) {
|
|
return prepareRevisions(result[0].items || []);
|
|
});
|
|
};
|
|
function prepareRevisions(revisions) {
|
|
_.forEach(revisions, function (revision) {
|
|
revision.label = $filter('i18n')('common.labels.version') + ' ' + revision.version + ' - ' + $filter('datePreConfigTimezone')(revision.createDate, 'mediumDate');
|
|
});
|
|
return revisions;
|
|
}
|
|
this.getDraft = function (id) {
|
|
return resource.getDraft({ templateId: id }).$promise.then(function (result) {
|
|
result[0].items[0].isDraft = true;
|
|
return convertArticle(new KnowledgeArticleVO().build(result[0].items[0]));
|
|
});
|
|
};
|
|
this.getProblemDraft = function (id, templateId) {
|
|
return resource.getProblemDraft({ id: id, templateId: templateId }, {}).$promise.then(function (result) {
|
|
result[0].items[0].isDraft = true;
|
|
return convertArticle(new KnowledgeArticleVO().build(result[0].items[0]));
|
|
});
|
|
};
|
|
this.getKnownErrorDraft = function (id, templateId) {
|
|
return resource.getKnownErrorDraft({ id: id, templateId: templateId }, {}).$promise.then(function (result) {
|
|
result[0].items[0].isDraft = true;
|
|
return convertArticle(new KnowledgeArticleVO().build(result[0].items[0]));
|
|
});
|
|
};
|
|
this.getIncidentDraft = function (id, templateId) {
|
|
return resource.getIncidentDraft({ id: id, templateId: templateId }, {}).$promise.then(function (result) {
|
|
result[0].items[0].isDraft = true;
|
|
return convertArticle(new KnowledgeArticleVO().build(result[0].items[0]));
|
|
});
|
|
};
|
|
this.getWorkorderDraft = function (id, templateId) {
|
|
return resource.getWorkorderDraft({ id: id, templateId: templateId }, {}).$promise.then(function (result) {
|
|
result[0].items[0].isDraft = true;
|
|
return convertArticle(new KnowledgeArticleVO().build(result[0].items[0]));
|
|
});
|
|
};
|
|
this.getVisibility = function (companyName, chunkSize) {
|
|
var params = {
|
|
companyName: companyName,
|
|
chunkInfo: { startIndex: 0, chunkSize: chunkSize ? chunkSize : searchModel.supportGroupChunkSize }
|
|
};
|
|
return resource.getVisibility(params).$promise.then(function (result) {
|
|
return result[0].items[0];
|
|
});
|
|
};
|
|
this.getVisibilityByText = function (companyName, searchText, chunkSize) {
|
|
var params = {
|
|
companyName: companyName,
|
|
visibilityGroup: searchText,
|
|
chunkInfo: { startIndex: 0, chunkSize: chunkSize ? chunkSize : searchModel.supportGroupChunkSize }
|
|
};
|
|
return resource.getVisibility(params).$promise.then(function (result) {
|
|
return result[0].items[0];
|
|
});
|
|
};
|
|
this.createArticle = function (loginId, params) {
|
|
// to handle loginid containing / . login id needs to be encoded.
|
|
loginId = encodeURIComponent(loginId);
|
|
return resource.createArticle({ loginId: loginId }, params).$promise.then(function (result) {
|
|
return convertArticle(new KnowledgeArticleVO().build(result[0].items[0]));
|
|
});
|
|
};
|
|
this.updateArticle = function (id, data) {
|
|
return resource.updateArticle({ id: id }, data).$promise.then(function (result) {
|
|
var statusInfo = result[0].items[0].statusInfo;
|
|
if (statusInfo.number === 3000) {
|
|
systemAlertService.warning({
|
|
title: $filter('i18n')('common.notification.dirty.title'),
|
|
text: statusInfo.message,
|
|
hide: 10000
|
|
});
|
|
}
|
|
return convertArticle(new KnowledgeArticleVO().build(result[0].items[0].responseObject));
|
|
});
|
|
};
|
|
this.getAutoSavedCopy = function (articleId) {
|
|
return resource.getAutoSavedCopy({ knowledgeArticleId: articleId }).$promise.then(function (result) {
|
|
return (result.content && result.content.length) ? _.chain(result.content).sortBy('order').map(function (item) {
|
|
return _.pick(item, ['name', 'snippet', 'createDate', 'modifiedDate']);
|
|
}).value() : [];
|
|
});
|
|
};
|
|
this.autoSave = function (article) {
|
|
var params = {
|
|
knowledgeArticleId: article.uuid,
|
|
content: _.map(article.content, function (item) {
|
|
return _.omit(item, ['label', 'embeddedImageLimit']);
|
|
})
|
|
};
|
|
return resource.createAutoSavedCopy({}, params).$promise.then(function (result) {
|
|
return result.content;
|
|
});
|
|
};
|
|
this.removeAutoSavedCopy = function (articleId) {
|
|
return resource.removeAutoSavedCopy({ knowledgeArticleId: articleId }).$promise;
|
|
};
|
|
this.checkEditStatus = function (articleId) {
|
|
return resource.checkEditStatus({ knowledgeArticleId: articleId }).$promise.then(function (result) {
|
|
return result.content;
|
|
});
|
|
};
|
|
this.getKnowledgeArticleTemplates = function () {
|
|
return resource.getTemplates().$promise.then(function (result) {
|
|
return _.map(result[0].items[0].objects, function (template) {
|
|
template.templateObject.sections = _.sortBy(template.templateObject.sections, 'order');
|
|
template.label = template.templateObject.label || template.name;
|
|
return template;
|
|
});
|
|
});
|
|
};
|
|
this.updateTemplateStyles = function (data) {
|
|
return resource.updateTemplateStyles(data).$promise.then(function (result) {
|
|
return (result);
|
|
});
|
|
};
|
|
this.getPotentialPrimary = function (data) {
|
|
return resource.getPotentialPrimary(data).$promise.then(function (result) {
|
|
return (_.omit(result[0].items[0], 'company'));
|
|
});
|
|
};
|
|
this.getAssessmentQuestions = function (companyName, organization) {
|
|
return resource.getAssessmentQuestions({
|
|
companyName: companyName,
|
|
organization: organization
|
|
}).$promise.then(processQuestions);
|
|
};
|
|
this.submitAssessment = function (articleId, answers) {
|
|
var params = {
|
|
articleId: articleId,
|
|
answerList: answers
|
|
};
|
|
return resource.submitAssessment(params).$promise.then(function (result) {
|
|
return result;
|
|
});
|
|
};
|
|
this.getQuestionSetList = function () {
|
|
return resource.getQuestionSetList().$promise.then(function (data) {
|
|
return data;
|
|
});
|
|
};
|
|
this.getQuestionSetById = function (questionSetId) {
|
|
return resource.getQuestionSetById({ id: questionSetId }).$promise.then(function (data) {
|
|
var questionList = data[0].items[0].objects.map(function (item) {
|
|
return new AssessmentQuestionVO().build(item);
|
|
});
|
|
return questionList;
|
|
});
|
|
};
|
|
this.deleteQuestionSetById = function (questionSetId) {
|
|
return resource.deleteQuestionSetById({ id: questionSetId }).$promise.then(function (data) {
|
|
return data;
|
|
});
|
|
};
|
|
this.createQuestionSet = function (postData) {
|
|
return resource.createQuestionSet(postData).$promise.then(function (data) {
|
|
return data;
|
|
});
|
|
};
|
|
this.updateQuestionSet = function (questionSetId, postData) {
|
|
return resource.updateQuestionSet({ id: questionSetId }, postData).$promise.then(function (data) {
|
|
return data;
|
|
});
|
|
};
|
|
/**
|
|
* Process Question list data from server
|
|
*
|
|
* @param {Array} Object
|
|
* @returns {Object} Object with assessment question vo
|
|
*/
|
|
function processQuestions(data) {
|
|
var questionListObj = data[0].items[0].knowledgeQuestionList.map(function (item) {
|
|
return new AssessmentQuestionVO().build(item);
|
|
});
|
|
return questionListObj;
|
|
}
|
|
}]);
|
|
}());
|