Replace all async reads and writes to the JSON database with the sync reads and writes to prevent a data corruption race condition where two requests are processed at the same time and write to the same file, or one reads while the other writes causing read of partially written data.
20 lines
818 B
TypeScript
20 lines
818 B
TypeScript
/*
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
import { nextAuthenticationMethodId, writeAuthenticationMethods, writeNextAuthenticationMethodId, writeSchedule, writeUsers } from "~/server/database";
|
|
import { generateDemoSchedule, generateDemoAccounts } from "~/server/generate-demo-schedule";
|
|
export default defineEventHandler(async (event) => {
|
|
await requireServerSessionWithAdmin(event);
|
|
const accounts = generateDemoAccounts();
|
|
writeUsers(accounts);
|
|
writeSchedule(generateDemoSchedule());
|
|
writeAuthenticationMethods(accounts.map((user, index) => ({
|
|
id: index,
|
|
userId: user.id,
|
|
provider: "demo",
|
|
slug: user.name!,
|
|
name: user.name!,
|
|
})));
|
|
writeNextAuthenticationMethodId(Math.max(nextAuthenticationMethodId(), accounts.length));
|
|
})
|