improvement: multi organization possibility implemented for users (owners and staffs)
This commit is contained in:
@@ -17,11 +17,18 @@ interface AuthContextType {
|
||||
password: string,
|
||||
name: string,
|
||||
organizationName: string,
|
||||
organizationEmail: string,
|
||||
organizationType: 'CLINIC' | 'LAB'
|
||||
) => Promise<void>;
|
||||
login: (email: string, password: string) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
selectOrganization: (orgId: string) => Promise<void>;
|
||||
createOrganization: (
|
||||
organizationName: string,
|
||||
organizationEmail: string,
|
||||
organizationType: 'CLINIC' | 'LAB',
|
||||
planName?: string,
|
||||
) => Promise<string>;
|
||||
clearError: () => void;
|
||||
}
|
||||
|
||||
@@ -111,6 +118,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
password: string,
|
||||
name: string,
|
||||
organizationName: string,
|
||||
organizationEmail: string,
|
||||
organizationType: 'CLINIC' | 'LAB'
|
||||
) => {
|
||||
try {
|
||||
@@ -122,6 +130,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
password,
|
||||
name,
|
||||
organizationName,
|
||||
organizationEmail,
|
||||
organizationType,
|
||||
});
|
||||
|
||||
@@ -221,6 +230,39 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
const createOrganization = useCallback(async (
|
||||
organizationName: string,
|
||||
organizationEmail: string,
|
||||
organizationType: 'CLINIC' | 'LAB',
|
||||
planName?: string,
|
||||
) => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
const createResponse = await authApi.createOrganization({
|
||||
organizationName,
|
||||
organizationEmail,
|
||||
organizationType,
|
||||
planName,
|
||||
});
|
||||
|
||||
const profileResponse = await authApi.getProfile();
|
||||
if (profileResponse.success) {
|
||||
const { user: userData, organizations: orgs } = normalizeProfilePayload(profileResponse.data);
|
||||
setUser(userData);
|
||||
setOrganizations(orgs);
|
||||
}
|
||||
|
||||
return createResponse.data.organization.id as string;
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Failed to create organization');
|
||||
throw err;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [normalizeProfilePayload]);
|
||||
|
||||
const clearError = useCallback(() => setError(null), []);
|
||||
|
||||
const contextValue = useMemo(
|
||||
@@ -235,6 +277,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
login,
|
||||
logout,
|
||||
selectOrganization,
|
||||
createOrganization,
|
||||
clearError,
|
||||
}),
|
||||
[
|
||||
@@ -248,6 +291,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
login,
|
||||
logout,
|
||||
selectOrganization,
|
||||
createOrganization,
|
||||
clearError,
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user