mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
prepare new database abstraction layer
This commit is contained in:
+21
-20
@@ -19,7 +19,7 @@ import { json } from 'body-parser'
|
||||
import { Router } from 'express'
|
||||
import { BadRequest, Conflict } from 'http-errors'
|
||||
import * as Sequelize from 'sequelize'
|
||||
import { Database } from '../database'
|
||||
import { SimpleDatabase } from '../database/simple'
|
||||
import { addPurchase, canDoNextPurchase } from '../function/purchase'
|
||||
import { getStatusMessage, setStatusMessage } from '../function/statusmessage'
|
||||
import { EventHandler } from '../monitoring/eventhandler'
|
||||
@@ -28,7 +28,7 @@ import { verifyIdentitifyToken, TokenValidationException } from '../util/identit
|
||||
import { WebsocketApi } from '../websocket'
|
||||
|
||||
export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
database: Database
|
||||
database: SimpleDatabase
|
||||
websocket: WebsocketApi
|
||||
eventHandler: EventHandler
|
||||
}) => {
|
||||
@@ -60,7 +60,9 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
|
||||
router.get('/status-message', async (_, res, next) => {
|
||||
try {
|
||||
const currentStatusMessage = await getStatusMessage({ database })
|
||||
const currentStatusMessage = await database.transaction(async (transaction) => {
|
||||
return getStatusMessage({ transaction })
|
||||
})
|
||||
|
||||
res.json({
|
||||
statusMessage: currentStatusMessage
|
||||
@@ -102,12 +104,12 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
}
|
||||
|
||||
await database.transaction(async (transaction) => {
|
||||
const userEntryUnsafe = await database.user.findOne({
|
||||
const userEntryUnsafe = await transaction.legacy.database.user.findOne({
|
||||
where: {
|
||||
mail
|
||||
},
|
||||
attributes: ['familyId'],
|
||||
transaction
|
||||
transaction: transaction.legacy.transaction
|
||||
})
|
||||
|
||||
if (!userEntryUnsafe) {
|
||||
@@ -119,13 +121,12 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
}
|
||||
|
||||
await addPurchase({
|
||||
database,
|
||||
transaction,
|
||||
familyId: userEntry.familyId,
|
||||
type,
|
||||
service: 'directpurchase',
|
||||
transactionId: 'legacyunlock-' + type + '-' + generatePurchaseId(),
|
||||
websocket,
|
||||
transaction
|
||||
websocket
|
||||
})
|
||||
})
|
||||
|
||||
@@ -165,14 +166,14 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
}
|
||||
|
||||
const response = await database.transaction(async (transaction) => {
|
||||
const userValid = await database.user.count({
|
||||
const userValid = await transaction.legacy.database.user.count({
|
||||
where: {
|
||||
familyId: tokenContent.familyId,
|
||||
userId: tokenContent.userId,
|
||||
mail: tokenContent.mail,
|
||||
type: 'parent'
|
||||
},
|
||||
transaction
|
||||
transaction: transaction.legacy.transaction
|
||||
})
|
||||
|
||||
if (!userValid) return {
|
||||
@@ -185,7 +186,7 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
|
||||
if (tokenContent.mail !== '') mailToReturn = tokenContent.mail
|
||||
else {
|
||||
const userEntryWithMail = await database.user.findOne({
|
||||
const userEntryWithMail = await transaction.legacy.database.user.findOne({
|
||||
where: {
|
||||
familyId: tokenContent.familyId,
|
||||
mail: {
|
||||
@@ -193,7 +194,7 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
},
|
||||
type: 'parent'
|
||||
},
|
||||
transaction
|
||||
transaction: transaction.legacy.transaction
|
||||
})
|
||||
|
||||
if (!userEntryWithMail) return {
|
||||
@@ -207,11 +208,12 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
|
||||
let wasAlreadyExecuted: boolean
|
||||
|
||||
const oldPurchaseByPurchaseId = await database.purchase.findOne({
|
||||
const oldPurchaseByPurchaseId = await transaction.legacy.database.purchase.findOne({
|
||||
where: {
|
||||
service: 'directpurchase',
|
||||
transactionId: purchaseId
|
||||
}
|
||||
},
|
||||
transaction: transaction.legacy.transaction
|
||||
})
|
||||
|
||||
if (oldPurchaseByPurchaseId === null) wasAlreadyExecuted = false
|
||||
@@ -222,11 +224,11 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
}
|
||||
|
||||
if (!wasAlreadyExecuted) {
|
||||
const familyEntry = await database.family.findOne({
|
||||
const familyEntry = await transaction.legacy.database.family.findOne({
|
||||
where: {
|
||||
familyId: tokenContent.familyId
|
||||
},
|
||||
transaction
|
||||
transaction: transaction.legacy.transaction
|
||||
})
|
||||
|
||||
if (!familyEntry) return {
|
||||
@@ -240,11 +242,11 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
})
|
||||
|
||||
if (!canDoPurchase) {
|
||||
const lastPurchase = await database.purchase.findOne({
|
||||
const lastPurchase = await transaction.legacy.database.purchase.findOne({
|
||||
where: {
|
||||
familyId: tokenContent.familyId
|
||||
},
|
||||
transaction,
|
||||
transaction: transaction.legacy.transaction,
|
||||
order: [['loggedAt', 'DESC']],
|
||||
limit: 1
|
||||
})
|
||||
@@ -263,13 +265,12 @@ export const createAdminRouter = ({ database, websocket, eventHandler }: {
|
||||
|
||||
if (!dryRun) {
|
||||
await addPurchase({
|
||||
database,
|
||||
transaction,
|
||||
familyId: tokenContent.familyId,
|
||||
type,
|
||||
service: 'directpurchase',
|
||||
transactionId: purchaseId,
|
||||
websocket,
|
||||
transaction
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user