summaryrefslogtreecommitdiffstats
path: root/ui/imports/api/messages/server/methods.js
blob: 540c0a182e59f95f8620c27f266c958f2192a7e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) and others /
//                                                                                      /
// All rights reserved. This program and the accompanying materials                     /
// are made available under the terms of the Apache License, Version 2.0                /
// which accompanies this distribution, and is available at                             /
// http://www.apache.org/licenses/LICENSE-2.0                                           /
/////////////////////////////////////////////////////////////////////////////////////////
import * as R from 'ramda';
import { Messages } from '/imports/api/messages/messages';

Meteor.methods({
  'messages/get?level&env&page&amountPerPage&sortField&sortDirection': function (
    level, env, page, amountPerPage, sortField, sortDirection) {

    logMethodCall('messages/get?level&env&page&amountPerPage&sortField&sortDirection', 
      {level, env, page, amountPerPage});

    this.unblock();

    let skip = (page - 1) * amountPerPage;

    let query = {};
    let sortParams = {};

    query = R.ifElse(R.isNil, R.always(query),R.assoc('environment', R.__, query))(env);
    query = R.ifElse(R.isNil, R.always(query),R.assoc('level', R.__, query))(level);

    sortParams = R.ifElse(R.isNil, R.always(sortParams), 
      R.assoc(R.__, sortDirection, sortParams))(sortField);

    console.log('sort params:', sortParams);

    let qParams = {
      limit: amountPerPage,
      skip: skip,
      sort: sortParams,
    };

    return Messages.find(query, qParams).fetch();
  }
});

function logMethodCall(name, args) {
  console.log(`method call: ${name}`);
  R.forEachObjIndexed((value, key) => {
    console.log(`${key}: ${R.toString(value)}`);
  }, args);
}