SurfSense/surfsense_web/atoms/roles/roles-mutation.atoms.ts
DESKTOP-RTLN3BA\$punk a64c8205fe feat: implement ensure_publication for zero_publication management
- Added the ensure_publication function to create and verify the zero_publication if it is missing, ensuring idempotency during database initialization.
- Integrated ensure_publication into the create_db_and_tables function to prevent zero-cache crash loops on startup.
- Introduced a self-check script to validate the ensure_publication functionality on a create_all-bootstrapped database.
- Updated various components to reflect the transition from search space to workspace, including adjustments in imports and routing paths.
2026-07-05 23:17:13 -07:00

70 lines
2.1 KiB
TypeScript

import { atomWithMutation } from "jotai-tanstack-query";
import { toast } from "sonner";
import type {
CreateRoleRequest,
CreateRoleResponse,
DeleteRoleRequest,
DeleteRoleResponse,
UpdateRoleRequest,
UpdateRoleResponse,
} from "@/contracts/types/roles.types";
import { rolesApiService } from "@/lib/apis/roles-api.service";
import { cacheKeys } from "@/lib/query-client/cache-keys";
import { queryClient } from "@/lib/query-client/client";
export const createRoleMutationAtom = atomWithMutation(() => {
return {
meta: { suppressGlobalErrorToast: true },
mutationFn: async (request: CreateRoleRequest) => {
return rolesApiService.createRole(request);
},
onSuccess: (_: CreateRoleResponse, request: CreateRoleRequest) => {
toast.success("Role created successfully");
queryClient.invalidateQueries({
queryKey: cacheKeys.roles.all(request.workspace_id.toString()),
});
},
onError: () => {
toast.error("Failed to create role");
},
};
});
export const updateRoleMutationAtom = atomWithMutation(() => {
return {
meta: { suppressGlobalErrorToast: true },
mutationFn: async (request: UpdateRoleRequest) => {
return rolesApiService.updateRole(request);
},
onSuccess: (_: UpdateRoleResponse, request: UpdateRoleRequest) => {
toast.success("Role updated successfully");
queryClient.invalidateQueries({
queryKey: cacheKeys.roles.all(request.workspace_id.toString()),
});
queryClient.invalidateQueries({
queryKey: cacheKeys.roles.byId(request.workspace_id.toString(), request.role_id.toString()),
});
},
onError: () => {
toast.error("Failed to update role");
},
};
});
export const deleteRoleMutationAtom = atomWithMutation(() => {
return {
meta: { suppressGlobalErrorToast: true },
mutationFn: async (request: DeleteRoleRequest) => {
return rolesApiService.deleteRole(request);
},
onSuccess: (_: DeleteRoleResponse, request: DeleteRoleRequest) => {
toast.success("Role deleted successfully");
queryClient.invalidateQueries({
queryKey: cacheKeys.roles.all(request.workspace_id.toString()),
});
},
onError: () => {
toast.error("Failed to delete role");
},
};
});