2016-11-14 16:16:51 +03:00
|
|
|
/* eslint-disable camelcase */
|
2016-05-24 15:06:59 +03:00
|
|
|
import SlackObject from 'ghost-admin/models/slack-integration';
|
2020-01-16 20:01:12 +03:00
|
|
|
import Transform from '@ember-data/serializer/transform';
|
2018-07-02 16:57:33 +03:00
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
|
|
|
import {isEmpty} from '@ember/utils';
|
2016-03-29 11:40:44 +03:00
|
|
|
|
|
|
|
export default Transform.extend({
|
|
|
|
deserialize(serialized) {
|
2018-07-02 16:57:33 +03:00
|
|
|
let settingsArray;
|
2016-03-29 11:40:44 +03:00
|
|
|
try {
|
|
|
|
settingsArray = JSON.parse(serialized) || [];
|
|
|
|
} catch (e) {
|
|
|
|
settingsArray = [];
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:57:33 +03:00
|
|
|
if (isEmpty(settingsArray)) {
|
2019-01-21 15:26:21 +03:00
|
|
|
settingsArray.push({url: '', username: ''});
|
2018-07-02 16:57:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let slackObjs = settingsArray.map(itemDetails => SlackObject.create(itemDetails));
|
|
|
|
|
|
|
|
return slackObjs;
|
2016-03-29 11:40:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
serialize(deserialized) {
|
|
|
|
let settingsArray;
|
2016-06-30 13:21:47 +03:00
|
|
|
if (isEmberArray(deserialized)) {
|
2016-03-29 11:40:44 +03:00
|
|
|
settingsArray = deserialized.map((item) => {
|
|
|
|
let url = (item.get('url') || '').trim();
|
2019-01-21 15:26:21 +03:00
|
|
|
let username = (item.get('username') || '').trim();
|
2018-07-02 16:57:33 +03:00
|
|
|
if (url) {
|
2019-01-21 15:26:21 +03:00
|
|
|
return {url, username};
|
2018-07-02 16:57:33 +03:00
|
|
|
}
|
2016-03-29 11:40:44 +03:00
|
|
|
}).compact();
|
|
|
|
} else {
|
|
|
|
settingsArray = [];
|
|
|
|
}
|
|
|
|
return JSON.stringify(settingsArray);
|
|
|
|
}
|
|
|
|
});
|