open-vault/ui/app/serializers/pki-certificate.js
claire bontempo 42ae96ed1c
UI/ PKI UI Redesign (#12541)
* installs node-forge

* correctly displays and formats cert metadata

* removes labels

* uses helper in hbs file

* adds named arg to helper

* pki-ca-cert displays common name, issue & expiry date

* alphabetizes some attrs

* adds test for date helper
2021-10-04 14:31:36 -07:00

73 lines
2.2 KiB
JavaScript

import RESTSerializer from '@ember-data/serializer/rest';
import { isNone, isBlank } from '@ember/utils';
import { assign } from '@ember/polyfills';
import { decamelize } from '@ember/string';
import { parsePkiCert } from '../helpers/parse-pki-cert';
export default RESTSerializer.extend({
keyForAttribute: function(attr) {
return decamelize(attr);
},
pushPayload(store, payload) {
const transformedPayload = this.normalizeResponse(
store,
store.modelFor(payload.modelName),
payload,
payload.id,
'findRecord'
);
return store.push(transformedPayload);
},
normalizeItems(payload) {
if (payload.data && payload.data.keys && Array.isArray(payload.data.keys)) {
let ret = payload.data.keys.map(key => {
let model = {
id_for_nav: `cert/${key}`,
id: key,
};
if (payload.backend) {
model.backend = payload.backend;
}
return model;
});
return ret;
}
assign(payload, payload.data);
delete payload.data;
return payload;
},
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
const responseJSON = this.normalizeItems(payload);
const { modelName } = primaryModelClass;
let transformedPayload, certMetadata;
// hits cert/list endpoint first which returns an array, only want to parse if response is not an array
if (!Array.isArray(responseJSON)) {
certMetadata = parsePkiCert([responseJSON]);
transformedPayload = { [modelName]: { ...certMetadata, ...responseJSON } };
} else {
transformedPayload = { [modelName]: responseJSON };
}
return this._super(store, primaryModelClass, transformedPayload, id, requestType);
},
serializeAttribute(snapshot, json, key, attributes) {
const val = snapshot.attr(key);
const valHasNotChanged = isNone(snapshot.changedAttributes()[key]);
const valIsBlank = isBlank(val);
if (attributes.options.readOnly) {
return;
}
if (attributes.type === 'object' && val && Object.keys(val).length > 0 && valHasNotChanged) {
return;
}
if (valIsBlank && valHasNotChanged) {
return;
}
this._super(snapshot, json, key, attributes);
},
});