Advertisement
Savelyev_Vyacheslav

server urgency

Jun 26th, 2023
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;
  2. (() => {
  3.     if(!Object.keys(input).length) return
  4.     const record = getRecord(input.tableName, input.recordId);
  5.     if (input.action === 'INIT') {
  6.         const CLOSED_STATE = 10;
  7.         const COMPLETED_STATE = 7;
  8.  
  9.         const tableNames = ['itsm_incident', 'itsm_request', ...getChildTableName('itsm_request')];
  10.  
  11.         if (!tableNames.includes(input.tableName)) {
  12.             return;
  13.         }
  14.  
  15.         if (!record) {
  16.             return;
  17.         }
  18.  
  19.         if ([CLOSED_STATE, COMPLETED_STATE].includes(record.state)) {
  20.             return;
  21.         }
  22.     }
  23.     data.isShowUrgency = true;
  24.     data.translations =  getTranslations();
  25.     const warningAttributes = getAttributes();
  26.     data.toggleHint = false;
  27.     data.userCanNotSave = true;
  28.     data.urgencyChoices = getChoices(ss.getUser().getDisplayValue('language_id'));
  29.     setWidgetUrgencyValue(record.urgency);
  30.  
  31.     data.currentChoice = data.argencyValueServer.display_value;
  32.     data.infoWarningBox = warningAttributes.isShowWarning.includes(data.argencyValueServer.database_value);
  33.  
  34.     data.isShowWarning = warningAttributes.isShowWarning;
  35.     data.warningPhone = warningAttributes.warningPhone;
  36.     data.warningInfo = warningAttributes.warningInfo;
  37.  
  38.     if (input.action === 'CHANGE_URGENCY') {
  39.         saveUrgency(data.translations.message);
  40.         data.urgencyValueForm = data.urgencyChoices[ Number(input.setUrgency) - 1 ];
  41.         data.infoWarningBox = warningAttributes.isShowWarning.includes( (input.setUrgency) );
  42.         data.action = '';
  43.     }
  44. })();
  45.  
  46. function getTranslations() {
  47.     const sm = new SimpleMessage();
  48.     return {
  49.         change: sm.getMessage('Change'),
  50.         cancel: sm.getMessage('Cancel'),
  51.         save: sm.getMessage('Save'),
  52.         annotationValue: sm.getMessage('Urgency impacts the priority of the ticket processing'),
  53.         title: sm.getMessage('Ticket urgency'),
  54.         hintInfoBold: sm.getMessage('Urgency '),
  55.         hintInfoRegular: sm.getMessage('is a measure defining how soon an issue affects a business'),
  56.         message: `${sm.getMessage('Urgency is set to')} "${input.urgencyValueForm.display_value}"`,
  57.         urgencyNotSet: sm.getMessage('Urgency not set'),
  58.         defaultMessage: sm.getMessage('Contact us in any available way to confirm the information'),
  59.         customMessage: getCustomMessages('Custom warning message for urgency widget'),
  60.     }
  61. }
  62.  
  63. function clientNotReady() {
  64.   return !Object.keys(input).length;
  65. }
  66.  
  67.  
  68. function getChildTableName(tableObj) {
  69.   const request = new SimpleTable(tableObj);
  70.   const childTables = [];
  71.   request.getChildTables().forEach((item) => { childTables.push(item.name) })
  72.   return childTables;
  73. }
  74.  
  75. function setWidgetUrgencyValue(ARGENCY_VALUE_SERVER) {
  76.     const sm = new SimpleMessage();
  77.     if(ARGENCY_VALUE_SERVER) {
  78.       data.argencyValueServer = data.urgencyChoices[ARGENCY_VALUE_SERVER - 1];
  79.       data.urgencyValueForm = data.argencyValueServer;
  80.     } else {
  81.       data.argencyValueServer = {"database_value": '-1000',"display_value": data.translations.urgencyNotSet}
  82.       data.urgencyValueForm = {"database_value": '-1000',"display_value": data.translations.urgencyNotSet}
  83.     }
  84. }
  85.  
  86. function getCustomMessages(sourceMessagesWarning) {
  87.   const source_message = new SimpleRecord('source_message');
  88.   source_message.get('message', sourceMessagesWarning);
  89.   const message = new SimpleRecord('message');
  90.   message.addQuery('source_message_id', source_message.sys_id);
  91.   message.addQuery('language', getLanguage());
  92.   message.query();
  93.   message.next();
  94.   return message.translation;
  95. }
  96.  
  97. function getAttributes() {
  98.     const warningAttributes = {};
  99.     const sm = new SimpleMessage();
  100.     const defaultMessage = data.translations.defaultMessage;
  101.     const customMessage = data.translations.customMessage;
  102.  
  103.     if (isShowWarning(warningAttributes, defaultMessage)) {
  104.         setAttributesWarning(warningAttributes, defaultMessage, customMessage);
  105.     }
  106.  
  107.     return warningAttributes;
  108.  
  109.     function isShowWarning(warningAttributes, defaultMessage) {
  110.         const isShowWarning = `${options.isShowWarning}`;
  111.         warningAttributes.isShowWarning = [];
  112.         if (isShowWarning === 'true' && defaultMessage != '') return true;
  113.         return false;
  114.     }
  115.  
  116.     function setAttributesWarning(warningAttributes, defaultMessage, customMessage) {
  117.         warningAttributes.warningPhone = '';
  118.         const propertyPhone = ss.getProperty('itsm.notification.contact.phone');
  119.         const optionText = `${options.sourceMessage}`;
  120.         const optionPhone = `${options.phone}`;
  121.         warningAttributes.isShowWarning = ['4'];
  122.         if (optionPhone == 'true') {
  123.             warningAttributes.warningPhone = propertyPhone;
  124.         }
  125.  
  126.         if (optionText != 'Custom warning message for urgency widget' || customMessage == '') {
  127.             warningAttributes.warningInfo = defaultMessage;
  128.             return true;
  129.         }
  130.  
  131.         warningAttributes.warningInfo = customMessage;
  132.     }
  133. }
  134.  
  135. function getLanguage() {
  136.     const user = new SimpleRecord('user');
  137.     user.get(`${ss.getUser()}`);
  138.     return user.getDisplayValue('language_id') === 'English' ? 'en' : 'ru';
  139. }
  140.  
  141. function getChoices(language) {
  142.     const TASK_TABLE_ID = '155931135900000083';
  143.     const URGENCY_COLUMN_ID = '155931135900001088';
  144.  
  145.     language = language === 'English' ? 'en' : 'ru';
  146.     const options = [];
  147.  
  148.     const choice = new SimpleRecord('sys_choice')
  149.     choice.addQuery('column_id', URGENCY_COLUMN_ID);
  150.     choice.addQuery('table_id', TASK_TABLE_ID);
  151.     choice.addQuery('language', language);
  152.     choice.orderBy('order');
  153.     choice.selectAttributes(['value', 'title'])
  154.     choice.query();
  155.  
  156.     while (choice.next()) {
  157.         options.push({
  158.             database_value: choice.getValue('value'),
  159.             display_value: choice.getValue('title'),
  160.         })
  161.     }
  162.  
  163.     return options;
  164. }
  165.  
  166. function saveUrgency(message) {
  167.     const record = new SimpleRecord(input.tableName);
  168.     record.get(input.recordId);
  169.     record.urgency = input.setUrgency;
  170.     const save = record.update();
  171.     if (save != 0) {
  172.         ss.addSuccessMessage(message)
  173.         data.argencyValueServer = data.urgencyChoices[ Number(input.setUrgency) - 1 ];
  174.         data.currentChoice = data.argencyValueServer.display_value;
  175.     }
  176. }
  177.  
  178. function getRecord(tableName, recordId) {
  179.     const record = new SimpleRecord(tableName);
  180.     record.get(recordId);
  181.     return record;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement