open-health/imports/api/logEntry.js

57 lines
No EOL
1.7 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import dayjs from 'dayjs';
export const LogEntry = new Mongo.Collection('logEntry');
LogEntry.allow({
insert: function(userId, doc){
// if use id exists, allow insert
return !!userId;
},
});
Meteor.methods({
async 'add.logEntry' (exerciseId, exerciseName, exerciseWeight) {
check(exerciseId, String);
check(exerciseName, String);
check(exerciseWeight, Number);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add log entries. Make sure you are logged in with valid user credentials.');
}
let rightNow = new Date();
let dateAdded = dayjs(rightNow).format('YYYY-MM-DD');
let timeAdded = dayjs(rightNow).format("HH:MM:SS");
return await LogEntry.insertAsync({
exerciseId: exerciseId,
exerciseName: exerciseName,
exerciseWeight: exerciseWeight,
dateAdded: dateAdded,
timeAdded: timeAdded,
addedBy: this.userId,
});
},
async 'add.setToLog' (logId, setNo, repCount) {
check(logId, String);
check(setNo, Number);
check(repCount, Number);
if (!this.userId) {
throw new Meteor.Error('You are not allowed to add sets to log entries. Make sure you are logged in with valid user credentials.');
}
return await LogEntry.updateAsync({ _id: logId }, {
$addToSet: {
sets:
{
setNumber: setNo,
repCount: repCount,
}
}
});
},
});