Extend transaction usage

This commit is contained in:
Jonas Lochmann
2020-10-02 09:58:13 +02:00
parent 62f4e368a6
commit abc2102da5
47 changed files with 1367 additions and 860 deletions
+8 -12
View File
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
* 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
@@ -21,14 +21,15 @@ import { Database } from '../../database'
import { generateAuthToken, generateVersionId } from '../../util/token'
import { WebsocketApi } from '../../websocket'
import { prepareDeviceEntry } from '../device/prepare-device-entry'
import { notifyClientsAboutChanges } from '../websocket'
import { notifyClientsAboutChangesDelayed } from '../websocket'
export const addChildDevice = async ({ database, websocket, request }: {
database: Database
websocket: WebsocketApi
request: RegisterChildDeviceRequest
// no transaction here because this is directly called from an API endpoint
}) => {
const { response, familyId } = await database.transaction(async (transaction) => {
return database.transaction(async (transaction) => {
const entry = await database.addDeviceToken.findOne({
where: {
token: request.registerToken.toLowerCase()
@@ -63,16 +64,11 @@ export const addChildDevice = async ({ database, websocket, request }: {
transaction
})
await notifyClientsAboutChangesDelayed({ familyId, websocket, database, isImportant: true, sourceDeviceId: deviceId, transaction })
return {
response: {
deviceId,
deviceAuthToken
},
familyId
deviceId,
deviceAuthToken
}
})
await notifyClientsAboutChanges({ familyId, websocket, database, isImportant: true, sourceDeviceId: response.deviceId })
return response
}