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 { 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 { 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 { await waitForLoadIfLoading(store, "loadAllUsers"); const next = await db.createUser(user); store.dispatch(setUser(next)); return next.uid; }