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
}
@@ -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
@@ -23,6 +23,7 @@ export const logoutAtPrimaryDevice = async ({ deviceAuthToken, database, websock
deviceAuthToken: string
database: Database
websocket: WebsocketApi
// no transaction here because this is directly called from an API endpoint
}) => {
await database.transaction(async (transaction) => {
const ownDeviceEntryUnsafe = await database.device.findOne({
+12 -30
View File
@@ -21,7 +21,7 @@ import { config } from '../../config'
import { Database } from '../../database'
import { generateVersionId } from '../../util/token'
import { WebsocketApi } from '../../websocket'
import { notifyClientsAboutChanges } from '../websocket'
import { notifyClientsAboutChangesDelayed } from '../websocket'
export const setPrimaryDevice = async ({ database, websocket, deviceAuthToken, currentUserId, action }: {
database: Database
@@ -29,12 +29,9 @@ export const setPrimaryDevice = async ({ database, websocket, deviceAuthToken, c
deviceAuthToken: string
currentUserId: string
action: 'set this device' | 'unset this device'
// no transaction here because this is directly called from an API endpoint
}): Promise<'assigned to other device' | 'requires full version' | 'success'> => {
const response = await database.transaction(async (transaction): Promise<{
response: 'assigned to other device' | 'requires full version' | 'success',
sourceDeviceId: string,
familyId: string
}> => {
return database.transaction(async (transaction): Promise<'assigned to other device' | 'requires full version' | 'success'> => {
const deviceEntryUnsafe = await database.device.findOne({
where: {
deviceAuthToken
@@ -106,22 +103,14 @@ export const setPrimaryDevice = async ({ database, websocket, deviceAuthToken, c
}
if (!(familyEntry.hasFullVersion || config.alwaysPro)) {
return {
response: 'requires full version',
sourceDeviceId: deviceEntry.deviceId,
familyId: deviceEntry.familyId
}
return 'requires full version'
}
}
if (action === 'set this device') {
// check that no other device is selected
if (userDeviceEntries.find((item) => item.deviceId === userEntry.currentDevice)) {
return {
response: 'assigned to other device',
sourceDeviceId: deviceEntry.deviceId,
familyId: deviceEntry.familyId
}
return 'assigned to other device'
}
// update
@@ -171,23 +160,16 @@ export const setPrimaryDevice = async ({ database, websocket, deviceAuthToken, c
}
})
return {
response: 'success',
sourceDeviceId: deviceEntry.deviceId,
familyId: deviceEntry.familyId
}
})
if (response.response === 'success') {
// trigger sync
await notifyClientsAboutChanges({
familyId: response.familyId,
sourceDeviceId: response.sourceDeviceId,
await notifyClientsAboutChangesDelayed({
familyId: deviceEntry.familyId,
sourceDeviceId: deviceEntry.deviceId,
websocket,
database,
isImportant: false // the source device knows it already
isImportant: false, // the source device knows it already
transaction
})
}
return response.response
return 'success'
})
}