forest-express
Version:
Official package for all Forest Express Lianas
30 lines (28 loc) • 1.43 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
function serializeIntercomAttributes(attributes, collectionName, meta) {
var type = "".concat(collectionName, "_intercom_attributes");
var unixTimestampToDateOrNull = function unixTimestampToDateOrNull(unixTimestamp) {
return unixTimestamp && new Date(unixTimestamp * 1000);
};
// Attributes keys ending with `_at` are unix timestamp
// thus they need to be converted to js Date
Object.entries(attributes).forEach(function (_ref) {
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
attributeKey = _ref2[0],
attributeValue = _ref2[1];
if (attributeKey.endsWith('_at')) {
attributes[attributeKey] = unixTimestampToDateOrNull(attributeValue);
}
});
return new JSONAPISerializer(type, attributes, {
attributes: ['email', 'name', 'role', 'companies', 'tags', 'platform', 'browser', 'city', 'country', 'signed_up_at', 'last_request_at', 'last_seen_at', 'last_replied_at', 'last_contacted_at', 'last_email_opened_at', 'last_email_clicked_at', 'created_at', 'updated_at'],
keyForAttribute: function keyForAttribute(key) {
return key;
},
meta: meta
});
}
module.exports = serializeIntercomAttributes;