mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Add task completion mails
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
import { Conflict } from 'http-errors'
|
||||
import { NewDeviceInfo, ParentPassword } from '../../api/schema'
|
||||
import { Database } from '../../database'
|
||||
import { maxMailNotificationFlags } from '../../database/user'
|
||||
import {
|
||||
generateAuthToken, generateFamilyId, generateIdWithinFamily, generateVersionId
|
||||
} from '../../util/token'
|
||||
@@ -82,7 +83,7 @@ export const createFamily = async ({ database, mailAuthToken, firstParentDevice,
|
||||
currentDevice: '',
|
||||
categoryForNotAssignedApps: '',
|
||||
relaxPrimaryDeviceRule: false,
|
||||
mailNotificationFlags: 1, // enable warning notifications
|
||||
mailNotificationFlags: maxMailNotificationFlags,
|
||||
blockedTimes: '',
|
||||
flags: '0'
|
||||
}, { transaction })
|
||||
|
||||
@@ -16,8 +16,9 @@
|
||||
*/
|
||||
|
||||
import { MarkTaskPendingAction } from '../../../../action'
|
||||
import { sendTaskDoneMails } from '../../../warningmail/taskdone'
|
||||
import { Cache } from '../cache'
|
||||
import { IllegalStateException, SourceDeviceNotFoundException } from '../exception/illegal-state'
|
||||
import { IllegalStateException, SourceDeviceNotFoundException, SourceUserNotFoundException } from '../exception/illegal-state'
|
||||
import { MissingTaskException } from '../exception/missing-item'
|
||||
|
||||
export async function dispatchMarkTaskPendingAction ({ action, cache, deviceId }: {
|
||||
@@ -31,14 +32,15 @@ export async function dispatchMarkTaskPendingAction ({ action, cache, deviceId }
|
||||
taskId: action.taskId
|
||||
},
|
||||
transaction: cache.transaction,
|
||||
attributes: ['categoryId', 'pendingRequest']
|
||||
attributes: ['categoryId', 'pendingRequest', 'taskTitle']
|
||||
})
|
||||
|
||||
if (taskInfoUnsafe === null) throw new MissingTaskException()
|
||||
|
||||
const taskInfo = {
|
||||
categoryId: taskInfoUnsafe.categoryId,
|
||||
pendingRequest: taskInfoUnsafe.pendingRequest
|
||||
pendingRequest: taskInfoUnsafe.pendingRequest,
|
||||
taskTitle: taskInfoUnsafe.taskTitle
|
||||
}
|
||||
|
||||
if (taskInfo.pendingRequest !== 0) return // review already requested
|
||||
@@ -75,6 +77,19 @@ export async function dispatchMarkTaskPendingAction ({ action, cache, deviceId }
|
||||
throw new IllegalStateException({ staticMessage: 'Can not mark task pending for other user than the current user' })
|
||||
}
|
||||
|
||||
const childInfoUnsafe = await cache.database.user.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
userId: categoryInfo.childId
|
||||
},
|
||||
attributes: ['name'],
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
if (childInfoUnsafe === null) throw new SourceUserNotFoundException()
|
||||
|
||||
const childInfo = { name: childInfoUnsafe.name }
|
||||
|
||||
await cache.database.childTask.update({ pendingRequest: true }, {
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
@@ -84,4 +99,12 @@ export async function dispatchMarkTaskPendingAction ({ action, cache, deviceId }
|
||||
})
|
||||
|
||||
cache.categoriesWithModifiedTasks.add(taskInfo.categoryId)
|
||||
|
||||
await sendTaskDoneMails({
|
||||
database: cache.database,
|
||||
transaction: cache.transaction,
|
||||
familyId: cache.familyId,
|
||||
childName: childInfo.name,
|
||||
taskTitle: taskInfo.taskTitle
|
||||
})
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
import { Database, Transaction, warpPromiseReturner } from '../../database'
|
||||
import { mailNotificationFlags } from '../../database/user'
|
||||
import { sendManipulationWarningMail } from '../../util/mail'
|
||||
import { canSendWarningMail } from '../../util/ratelimit-warningmail'
|
||||
|
||||
@@ -35,7 +36,7 @@ export const sendManipulationWarnings = async ({ database, familyId, deviceName,
|
||||
|
||||
const targetMailAddresses = parentEntries
|
||||
.filter((item) => item.mail !== '')
|
||||
.filter((item) => (item.mailNotificationFlags & 1) === 1)
|
||||
.filter((item) => (item.mailNotificationFlags & mailNotificationFlags.warnings) === mailNotificationFlags.warnings)
|
||||
.map((item) => item.mail)
|
||||
|
||||
transaction.afterCommit(warpPromiseReturner(async () => {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Database, Transaction, warpPromiseReturner } from '../../database'
|
||||
import { mailNotificationFlags } from '../../database/user'
|
||||
import { sendTaskDoneMail } from '../../util/mail'
|
||||
import { canSendTaskDoneMail } from '../../util/ratelimit-taskdonemail'
|
||||
|
||||
export const sendTaskDoneMails = async ({ database, familyId, childName, taskTitle, transaction }: {
|
||||
database: Database
|
||||
familyId: string
|
||||
childName: string
|
||||
taskTitle: string
|
||||
transaction: Transaction
|
||||
}) => {
|
||||
const parentEntries = await database.user.findAll({
|
||||
where: {
|
||||
familyId,
|
||||
type: 'parent'
|
||||
},
|
||||
transaction
|
||||
})
|
||||
|
||||
const targetMailAddresses = parentEntries
|
||||
.filter((item) => item.mail !== '')
|
||||
.filter((item) => (item.mailNotificationFlags & mailNotificationFlags.tasks) === mailNotificationFlags.tasks)
|
||||
.map((item) => item.mail)
|
||||
|
||||
transaction.afterCommit(warpPromiseReturner(async () => {
|
||||
await Promise.all(
|
||||
targetMailAddresses.map(async (receiver) => {
|
||||
if (await canSendTaskDoneMail(receiver)) {
|
||||
await sendTaskDoneMail({ receiver, child: childName, task: taskTitle })
|
||||
}
|
||||
})
|
||||
)
|
||||
}))
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
import { Database, Transaction, warpPromiseReturner } from '../../database'
|
||||
import { mailNotificationFlags } from '../../database/user'
|
||||
import { sendUninstallWarningMail } from '../../util/mail'
|
||||
import { canSendWarningMail } from '../../util/ratelimit-warningmail'
|
||||
|
||||
@@ -35,7 +36,7 @@ export const sendUninstallWarnings = async ({ database, familyId, deviceName, tr
|
||||
|
||||
const targetMailAddresses = parentEntries
|
||||
.filter((item) => item.mail !== '')
|
||||
.filter((item) => (item.mailNotificationFlags & 1) === 1)
|
||||
.filter((item) => (item.mailNotificationFlags & mailNotificationFlags.warnings) === mailNotificationFlags.warnings)
|
||||
.map((item) => item.mail)
|
||||
|
||||
transaction.afterCommit(warpPromiseReturner(async () => {
|
||||
|
||||
Reference in New Issue
Block a user