2023-05-11 04:03:20 +08:00
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import Timer from '/imports/api/timer';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
2020-04-26 03:03:35 +08:00
|
|
|
import { getDefaultTime } from '/imports/api/timer/server/helpers';
|
2023-05-11 04:03:20 +08:00
|
|
|
|
|
|
|
const getActivateModifier = () => {
|
2020-04-26 03:03:35 +08:00
|
|
|
const time = getDefaultTime();
|
|
|
|
check(time, Number);
|
|
|
|
|
2023-05-11 04:03:20 +08:00
|
|
|
return {
|
|
|
|
$set: {
|
|
|
|
stopwatch: true,
|
|
|
|
active: true,
|
|
|
|
running: false,
|
2020-04-26 03:03:35 +08:00
|
|
|
time,
|
2023-05-11 04:03:20 +08:00
|
|
|
accumulated: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDeactivateModifier = () => {
|
|
|
|
return {
|
2020-06-13 02:00:24 +08:00
|
|
|
$set: {
|
|
|
|
active: false,
|
|
|
|
running: false,
|
|
|
|
},
|
2023-05-11 04:03:20 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const getResetModifier = () => {
|
|
|
|
return {
|
|
|
|
$set: {
|
|
|
|
accumulated: 0,
|
2020-04-26 03:03:35 +08:00
|
|
|
timestamp: Date.now(),
|
2023-05-11 04:03:20 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-26 03:03:35 +08:00
|
|
|
const getStartModifier = () => {
|
2023-05-11 04:03:20 +08:00
|
|
|
return {
|
|
|
|
$set: {
|
|
|
|
running: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const getStopModifier = (accumulated) => {
|
|
|
|
return {
|
|
|
|
$set: {
|
|
|
|
running: false,
|
|
|
|
accumulated,
|
|
|
|
timestamp: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-26 03:03:35 +08:00
|
|
|
const getSwitchModifier = (stopwatch) => {
|
2023-05-11 04:03:20 +08:00
|
|
|
return {
|
|
|
|
$set: {
|
2020-04-26 03:03:35 +08:00
|
|
|
stopwatch,
|
|
|
|
running: false,
|
|
|
|
accumulated: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const getSetModifier = (time) => {
|
|
|
|
return {
|
|
|
|
$set: {
|
|
|
|
running: false,
|
|
|
|
accumulated: 0,
|
|
|
|
timestamp: 0,
|
|
|
|
time,
|
2023-05-11 04:03:20 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-26 03:03:35 +08:00
|
|
|
|
|
|
|
export default function updateTimer(action, meetingId, time = 0, stopwatch = true, accumulated = 0) {
|
2023-05-11 04:03:20 +08:00
|
|
|
check(action, String);
|
|
|
|
check(meetingId, String);
|
2020-04-26 03:03:35 +08:00
|
|
|
check(time, Number);
|
|
|
|
check(stopwatch, Boolean);
|
|
|
|
check(accumulated, Number);
|
2023-05-11 04:03:20 +08:00
|
|
|
|
|
|
|
const selector = {
|
|
|
|
meetingId,
|
|
|
|
};
|
|
|
|
|
|
|
|
let modifier;
|
|
|
|
|
|
|
|
switch(action) {
|
|
|
|
case 'activate':
|
|
|
|
modifier = getActivateModifier();
|
|
|
|
break;
|
|
|
|
case 'deactivate':
|
|
|
|
modifier = getDeactivateModifier();
|
|
|
|
break;
|
|
|
|
case 'reset':
|
|
|
|
modifier = getResetModifier();
|
|
|
|
break;
|
|
|
|
case 'start':
|
2020-04-26 03:03:35 +08:00
|
|
|
modifier = getStartModifier();
|
2023-05-11 04:03:20 +08:00
|
|
|
break;
|
|
|
|
case 'stop':
|
|
|
|
modifier = getStopModifier(accumulated);
|
|
|
|
break;
|
2020-04-26 03:03:35 +08:00
|
|
|
case 'switch':
|
|
|
|
modifier = getSwitchModifier(stopwatch);
|
|
|
|
break;
|
|
|
|
case 'set':
|
|
|
|
modifier = getSetModifier(time);
|
|
|
|
break;
|
2023-05-11 04:03:20 +08:00
|
|
|
default:
|
|
|
|
Logger.error(`Unhandled timer action=${action}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cb = (err) => {
|
|
|
|
if (err) {
|
|
|
|
return Logger.error(`Updating timer at collection: ${err}`);
|
|
|
|
}
|
|
|
|
|
2020-04-27 01:44:01 +08:00
|
|
|
return Logger.debug(`Updated timer action=${action} meetingId=${meetingId}`);
|
2023-05-11 04:03:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return Timer.update(selector, modifier, cb);
|
|
|
|
}
|