cms/app-client/mirage/serializers/discourse.js
author ymh <ymh.work@gmail.com>
Sat, 10 Jun 2017 08:33:03 +0200
changeset 532 1190ea937f2d
parent 474 245b4df137d3
permissions -rw-r--r--
make things work after node 8, npm 5 migration. Migrate to lodash 4

import { JSONAPISerializer } from 'ember-cli-mirage';

import _ from 'lodash';

export default JSONAPISerializer.extend({
    serialize(response, request) {

      let json = JSONAPISerializer.prototype.serialize.apply(this, arguments);

      let qParams = request.queryParams['discourse'];
      if(qParams) {
        json =  { 'data': _.map(json.data.slice(0, json.data.length/Math.pow(2,qParams.length)), (d) => {
          let res = _.clone(d);
          res.attributes.count = Math.max(Math.floor(d.attributes.count / 2), 1);
          return res;
        })};
      }

      return { 'discourses' : _(json.data).map((discourse) => {
        return [
          discourse.id,
          { count: discourse.attributes.count, label: discourse.attributes.label}
        ];
      }).fromPairs().value()};
    }
});