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
+82 -82
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
@@ -16,102 +16,102 @@
*/
import { Conflict } from 'http-errors'
import { Database } from '../../database'
import { Database, Transaction } from '../../database'
import { generateVersionId } from '../../util/token'
import { WebsocketApi } from '../../websocket'
import { notifyClientsAboutChanges } from '../websocket'
import { notifyClientsAboutChangesDelayed } from '../websocket'
export async function removeDevice ({ database, familyId, deviceId, websocket }: {
export async function removeDevice ({ database, familyId, deviceId, websocket, transaction }: {
database: Database
familyId: string
deviceId: string
websocket: WebsocketApi
transaction: Transaction
}) {
const { oldDeviceAuthToken } = await database.transaction(async (transaction) => {
const deviceEntry = await database.device.findOne({
where: {
familyId,
deviceId
},
transaction
})
if (!deviceEntry) {
throw new Conflict()
}
// remove app entries
await database.app.destroy({
where: {
familyId,
deviceId
},
transaction
})
await database.appActivity.destroy({
where: {
familyId,
deviceId
},
transaction
})
// remove as current device
await database.user.update({
currentDevice: ''
}, {
where: {
familyId,
currentDevice: deviceId
},
transaction
})
// add to old devices if it is not yet there (it could be there if it reported a uninstall)
const oldOldDeviceEntry = await database.oldDevice.findOne({
where: {
deviceAuthToken: deviceEntry.deviceAuthToken
},
transaction
})
if (!oldOldDeviceEntry) {
await database.oldDevice.create({
deviceAuthToken: deviceEntry.deviceAuthToken
}, {
transaction
})
}
// remove from the device list
await deviceEntry.destroy({ transaction })
// invalidiate the caches
await database.family.update({
deviceListVersion: generateVersionId(),
// the device could have become unassigned during this
userListVersion: generateVersionId()
}, {
where: {
familyId: deviceEntry.familyId
},
transaction
})
return { oldDeviceAuthToken: deviceEntry.deviceAuthToken }
const deviceEntry = await database.device.findOne({
where: {
familyId,
deviceId
},
transaction
})
await notifyClientsAboutChanges({
if (!deviceEntry) {
throw new Conflict()
}
// remove app entries
await database.app.destroy({
where: {
familyId,
deviceId
},
transaction
})
await database.appActivity.destroy({
where: {
familyId,
deviceId
},
transaction
})
// remove as current device
await database.user.update({
currentDevice: ''
}, {
where: {
familyId,
currentDevice: deviceId
},
transaction
})
// add to old devices if it is not yet there (it could be there if it reported a uninstall)
const oldOldDeviceEntry = await database.oldDevice.findOne({
where: {
deviceAuthToken: deviceEntry.deviceAuthToken
},
transaction
})
if (!oldOldDeviceEntry) {
await database.oldDevice.create({
deviceAuthToken: deviceEntry.deviceAuthToken
}, {
transaction
})
}
// remove from the device list
await deviceEntry.destroy({ transaction })
// invalidiate the caches
await database.family.update({
deviceListVersion: generateVersionId(),
// the device could have become unassigned during this
userListVersion: generateVersionId()
}, {
where: {
familyId: deviceEntry.familyId
},
transaction
})
await notifyClientsAboutChangesDelayed({
database,
websocket,
familyId,
sourceDeviceId: null,
isImportant: false
isImportant: false,
transaction
})
websocket.triggerSyncByDeviceAuthToken({
deviceAuthToken: oldDeviceAuthToken,
isImportant: true
transaction.afterCommit(() => {
websocket.triggerSyncByDeviceAuthToken({
deviceAuthToken: deviceEntry.deviceAuthToken,
isImportant: true
})
})
}