mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2025-10-03 09:49:32 +02:00
Limit used time to the length of the day/ time slot
This commit is contained in:
parent
15b576a2d7
commit
9730d13b2e
2 changed files with 10 additions and 4 deletions
|
@ -26,6 +26,9 @@ export const getRoundedTimestamp = () => {
|
||||||
return now - (now % (1000 * 60 * 60 * 24 * 2 /* 2 days */))
|
return now - (now % (1000 * 60 * 60 * 24 * 2 /* 2 days */))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dayLengthInMinutes = MinuteOfDay.LENGTH
|
||||||
|
const dayLengthInMs = dayLengthInMinutes * 1000 * 60
|
||||||
|
|
||||||
export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
|
export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
|
||||||
deviceId: string
|
deviceId: string
|
||||||
action: AddUsedTimeAction
|
action: AddUsedTimeAction
|
||||||
|
@ -63,7 +66,7 @@ export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
|
||||||
if (action.timeToAdd !== 0) {
|
if (action.timeToAdd !== 0) {
|
||||||
// try to update first
|
// try to update first
|
||||||
const [updatedRows] = await cache.database.usedTime.update({
|
const [updatedRows] = await cache.database.usedTime.update({
|
||||||
usedTime: Sequelize.literal(`usedTime + ${action.timeToAdd}`) as any,
|
usedTime: Sequelize.literal(`MAX(0, MIN(usedTime + ${action.timeToAdd}, ${dayLengthInMs}))`) as any,
|
||||||
lastUpdate: roundedTimestamp
|
lastUpdate: roundedTimestamp
|
||||||
}, {
|
}, {
|
||||||
where: {
|
where: {
|
||||||
|
@ -82,7 +85,7 @@ export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
|
||||||
familyId: cache.familyId,
|
familyId: cache.familyId,
|
||||||
categoryId: categoryId,
|
categoryId: categoryId,
|
||||||
dayOfEpoch: action.dayOfEpoch,
|
dayOfEpoch: action.dayOfEpoch,
|
||||||
usedTime: action.timeToAdd,
|
usedTime: Math.min(action.timeToAdd, dayLengthInMs),
|
||||||
lastUpdate: roundedTimestamp,
|
lastUpdate: roundedTimestamp,
|
||||||
startMinuteOfDay: MinuteOfDay.MIN,
|
startMinuteOfDay: MinuteOfDay.MIN,
|
||||||
endMinuteOfDay: MinuteOfDay.MAX
|
endMinuteOfDay: MinuteOfDay.MAX
|
||||||
|
|
|
@ -64,9 +64,12 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
|
||||||
|
|
||||||
// tslint:disable-next-line:no-inner-declarations
|
// tslint:disable-next-line:no-inner-declarations
|
||||||
async function handle (start: number, end: number) {
|
async function handle (start: number, end: number) {
|
||||||
|
const lengthInMinutes = (end - start) + 1
|
||||||
|
const lengthInMs = lengthInMinutes * 1000 * 60
|
||||||
|
|
||||||
// try to update first
|
// try to update first
|
||||||
const [updatedRows] = await cache.database.usedTime.update({
|
const [updatedRows] = await cache.database.usedTime.update({
|
||||||
usedTime: Sequelize.literal(`usedTime + ${item.timeToAdd}`) as any,
|
usedTime: Sequelize.literal(`MAX(0, MIN(usedTime + ${item.timeToAdd}, ${lengthInMs}))`) as any,
|
||||||
lastUpdate: roundedTimestampForUsedTime
|
lastUpdate: roundedTimestampForUsedTime
|
||||||
}, {
|
}, {
|
||||||
where: {
|
where: {
|
||||||
|
@ -85,7 +88,7 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
|
||||||
familyId: cache.familyId,
|
familyId: cache.familyId,
|
||||||
categoryId: item.categoryId,
|
categoryId: item.categoryId,
|
||||||
dayOfEpoch: action.dayOfEpoch,
|
dayOfEpoch: action.dayOfEpoch,
|
||||||
usedTime: item.timeToAdd,
|
usedTime: Math.min(item.timeToAdd, lengthInMs),
|
||||||
lastUpdate: roundedTimestampForUsedTime,
|
lastUpdate: roundedTimestampForUsedTime,
|
||||||
startMinuteOfDay: start,
|
startMinuteOfDay: start,
|
||||||
endMinuteOfDay: end
|
endMinuteOfDay: end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue