CI / install-and-build (pull_request) Successful in 1m29s
CI / format (pull_request) Successful in 51s
CI / typecheck-source (pull_request) Successful in 41s
CI / typecheck-tests (pull_request) Successful in 39s
CI / test (pull_request) Successful in 51s
CI / lint (pull_request) Successful in 21s
Co-Authored-By: gpt-5.6-terra <noreply@openai.com>
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import type { DbInterface } from "../../../db/types/DbInterface.js";
|
|
import type { CreateUser, UpdateUser } from "../../../db/types/User.js";
|
|
import { waitForLoadIfLoading } from "../load.js";
|
|
import { clearLoading } from "../slices/loading.js";
|
|
import { delUser, getUser, setUser } from "../slices/users.js";
|
|
import type { Store } from "../store.js";
|
|
|
|
export async function deleteUserById(
|
|
uid: string,
|
|
{ store, db }: { store: Store; db: DbInterface },
|
|
): Promise<void> {
|
|
await Promise.all([
|
|
waitForLoadIfLoading(store, "loadAllUsers"),
|
|
waitForLoadIfLoading(store, `loadUserById:${uid}`),
|
|
]);
|
|
|
|
const user = getUser(store.getState(), uid);
|
|
await db.deleteUserById(uid);
|
|
store.dispatch(delUser(uid));
|
|
store.dispatch(clearLoading(`loadUserById:${uid}`));
|
|
if (user && "email" in user)
|
|
store.dispatch(clearLoading(`loadUserByEmail:${user.email}`));
|
|
}
|
|
|
|
export async function updateUser(
|
|
user: UpdateUser,
|
|
{ store, db }: { store: Store; db: DbInterface },
|
|
): Promise<void> {
|
|
await Promise.all([
|
|
waitForLoadIfLoading(store, "loadAllUsers"),
|
|
waitForLoadIfLoading(store, `loadUserById:${user.uid}`),
|
|
]);
|
|
|
|
const current = getUser(store.getState(), user.uid);
|
|
const next = await db.updateUser(user);
|
|
store.dispatch(setUser(next));
|
|
store.dispatch(clearLoading(`loadUserById:${user.uid}`));
|
|
if (current && "email" in current)
|
|
store.dispatch(clearLoading(`loadUserByEmail:${current.email}`));
|
|
}
|
|
|
|
export async function createUser(
|
|
user: CreateUser,
|
|
{ store, db }: { store: Store; db: DbInterface },
|
|
): Promise<string> {
|
|
await waitForLoadIfLoading(store, "loadAllUsers");
|
|
|
|
const next = await db.createUser(user);
|
|
store.dispatch(setUser(next));
|
|
return next.uid;
|
|
}
|