Add user management endpoints and link employees to existing users through `user_id`, including API resources, validation, repository support, and database migrations. Introduce a two-step login flow that checks email first and lets users without a password create one before signing in. Update the employee detail UI with a dedicated user tab and refresh the employee and intervention side navigation to support the new account management flow.
80 lines
1.9 KiB
TypeScript
80 lines
1.9 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { ref, computed } from "vue";
|
|
import UserService from "@/services/user";
|
|
import type {
|
|
CreateUserPayload,
|
|
UpdateUserPayload,
|
|
UserSummary,
|
|
} from "@/services/user";
|
|
|
|
export const useUserStore = defineStore("user", () => {
|
|
const loading = ref(false);
|
|
const error = ref<string | null>(null);
|
|
const searchedUser = ref<UserSummary | null>(null);
|
|
|
|
const isLoading = computed(() => loading.value);
|
|
const currentSearchUser = computed(() => searchedUser.value);
|
|
|
|
const searchUserByEmail = async (email: string) => {
|
|
loading.value = true;
|
|
error.value = null;
|
|
|
|
try {
|
|
const user = await UserService.searchUserByEmail(email);
|
|
searchedUser.value = user;
|
|
return user;
|
|
} catch (err: any) {
|
|
error.value =
|
|
err.response?.data?.message || err.message || "Failed to search user";
|
|
throw err;
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
};
|
|
|
|
const createUser = async (payload: CreateUserPayload) => {
|
|
loading.value = true;
|
|
error.value = null;
|
|
|
|
try {
|
|
const user = await UserService.createUser(payload);
|
|
searchedUser.value = user;
|
|
return user;
|
|
} catch (err: any) {
|
|
error.value =
|
|
err.response?.data?.message || err.message || "Failed to create user";
|
|
throw err;
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
};
|
|
|
|
const updateUser = async (payload: UpdateUserPayload) => {
|
|
loading.value = true;
|
|
error.value = null;
|
|
|
|
try {
|
|
const user = await UserService.updateUser(payload);
|
|
searchedUser.value = user;
|
|
return user;
|
|
} catch (err: any) {
|
|
error.value =
|
|
err.response?.data?.message || err.message || "Failed to update user";
|
|
throw err;
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
};
|
|
|
|
return {
|
|
loading,
|
|
error,
|
|
searchedUser,
|
|
isLoading,
|
|
currentSearchUser,
|
|
searchUserByEmail,
|
|
createUser,
|
|
updateUser,
|
|
};
|
|
});
|