1window.angular && (function(angular) {
2  'use strict';
3
4  angular.module('app.common.directives').directive('certificate', [
5    'APIUtils',
6    function(APIUtils) {
7      return {
8        'restrict': 'E',
9        'template': require('./certificate.html'),
10        'scope': {'cert': '=', 'reload': '&'},
11        'controller': [
12          '$scope', 'APIUtils', 'toastService', 'Constants',
13          function($scope, APIUtils, toastService, Constants) {
14            var certificateType = 'PEM';
15            var availableCertificateTypes = Constants.CERTIFICATE_TYPES;
16
17            /**
18             * This function is needed to map the backend Description to what
19             * should appear in the GUI. This is needed specifically for CA
20             * certificate types. The backend description for the certificate
21             * type is 'TrustStore Certificate', this function will make sure we
22             * display 'CA Certificate' on the frontend
23             * @param {string} : certificate Description property
24             * @returns {string} : certificate name that should appear on GUI
25             */
26            $scope.getCertificateName = function(certificateDescription) {
27              var matched =
28                  availableCertificateTypes.find(function(certificate) {
29                    return certificate.Description === certificateDescription;
30                  });
31              if (matched === undefined) {
32                return '';
33              } else {
34                return matched.name;
35              }
36            };
37
38            $scope.replaceCertificate = function(certificate) {
39              $scope.loading = true;
40              if (certificate.file.name.split('.').pop() !==
41                  certificateType.toLowerCase()) {
42                toastService.error(
43                    'Certificate must be replaced with a .pem file.');
44                return;
45              }
46              var file =
47                  document
48                      .getElementById(
49                          'upload_' + certificate.Description + certificate.Id)
50                      .files[0];
51              var reader = new FileReader();
52              reader.onloadend = function(e) {
53                var data = {};
54                data.CertificateString = e.target.result;
55                data.CertificateUri = {'@odata.id': certificate['@odata.id']};
56                data.CertificateType = certificateType;
57                APIUtils.replaceCertificate(data).then(
58                    function(data) {
59                      $scope.loading = false;
60                      toastService.success(
61                          $scope.getCertificateName(certificate.Description) +
62                          ' was replaced.');
63                      $scope.reload();
64                    },
65                    function(error) {
66                      console.log(error);
67                      $scope.loading = false;
68                      toastService.error(
69                          'Unable to replace ' +
70                          $scope.getCertificateName(certificate.Description));
71                    });
72              };
73              reader.readAsBinaryString(file);
74            };
75          }
76        ]
77      };
78    }
79  ]);
80})(window.angular);
81