Skip to content
Snippets Groups Projects
Commit e42d5012 authored by Martin Schoeler's avatar Martin Schoeler Committed by Rodrigo Nascimento
Browse files

Convert Statistics Package to JS (#6447)

* convert statistics package to js

* amend

* fix code style
parent 36a9c3ad
No related branches found
No related tags found
No related merge requests found
RocketChat.statistics = {}
RocketChat.statistics = {};
......@@ -9,16 +9,15 @@ Package.onUse(function(api) {
api.use([
'mongo',
'ecmascript',
'coffeescript',
'rocketchat:lib'
]);
// Statistics
api.addFiles('lib/rocketchat.coffee', [ 'client', 'server' ]);
api.addFiles('lib/rocketchat.js', [ 'client', 'server' ]);
api.addFiles([
'server/models/Statistics.coffee',
'server/models/Statistics.js',
'server/functions/get.js',
'server/functions/save.coffee',
'server/methods/getStatistics.coffee'
'server/functions/save.js',
'server/methods/getStatistics.js'
], 'server');
});
RocketChat.statistics.save = ->
statistics = RocketChat.statistics.get()
statistics.createdAt = new Date
RocketChat.models.Statistics.insert statistics
return statistics
RocketChat.statistics.save = function() {
const statistics = RocketChat.statistics.get();
statistics.createdAt = new Date;
RocketChat.models.Statistics.insert(statistics);
return statistics;
};
Meteor.methods
getStatistics: (refresh) ->
if not Meteor.userId()
throw new Meteor.Error('error-invalid-user', "Invalid user", { method: 'getStatistics' })
unless RocketChat.authz.hasPermission(Meteor.userId(), 'view-statistics') is true
throw new Meteor.Error('error-not-allowed', "Not allowed", { method: 'getStatistics' })
if refresh
return RocketChat.statistics.save()
else
return RocketChat.models.Statistics.findLast()
Meteor.methods({
getStatistics(refresh) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getStatistics' });
}
if (RocketChat.authz.hasPermission(Meteor.userId(), 'view-statistics') !== true) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'getStatistics' });
}
if (refresh) {
return RocketChat.statistics.save();
} else {
return RocketChat.models.Statistics.findLast();
}
}
});
RocketChat.models.Statistics = new class extends RocketChat.models._Base
constructor: ->
super('statistics')
@tryEnsureIndex { 'createdAt': 1 }
# FIND ONE
findOneById: (_id, options) ->
query =
_id: _id
return @findOne query, options
findLast: ->
options =
sort:
createdAt: -1
limit: 1
return @find({}, options).fetch()?[0]
RocketChat.models.Statistics = new class extends RocketChat.models._Base {
constructor() {
super('statistics');
this.tryEnsureIndex({ 'createdAt': 1 });
}
// FIND ONE
findOneById(_id, options) {
const query = { _id };
return this.findOne(query, options);
}
findLast() {
const options = {
sort: {
createdAt: -1
},
limit: 1
};
const records = this.find({}, options).fetch();
return records && records[0];
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment