PromucFlow_constructor/app/client/src/ce/sagas/WorkspaceSagas.ts
Michael Carner a3fe54f217
fix: reset workspace before deleting a workspace (#23782)
## Description
Fixes [#12057](https://github.com/appsmithorg/appsmith/issues/12057)

In specific scenarios, the currentWorkspace is not up to date and uses a
previous workspace, which can cause certain API calls to fail. The end
result is the user sees a "No Resource Found" error with no clear
explanation.

This changes adds a new ReduxAction RESET_CURRENT_WORKSPACE with returns
it to it's initial value. The only place I've added that is at the
begining of deleteWorkspaceSaga()

#### PR fixes following issue(s)
Fixes # (12057)

#### Type of change
- Bug fix (non-breaking change which fixes an issue)

## Testing

#### How Has This Been Tested?
- [x] Manual


## Checklist:
#### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] My changes generate no new warnings
- [x] New and existing unit tests pass locally with my changes


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Test-plan-implementation#speedbreaker-features-to-consider-for-every-change)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans/_edit#areas-of-interest)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-06-02 17:50:53 +05:30

370 lines
10 KiB
TypeScript

import { call, put, select } from "redux-saga/effects";
import type {
ReduxAction,
ReduxActionWithPromise,
} from "@appsmith/constants/ReduxActionConstants";
import {
ReduxActionTypes,
ReduxActionErrorTypes,
} from "@appsmith/constants/ReduxActionConstants";
import {
validateResponse,
callAPI,
getResponseErrorMessage,
} from "sagas/ErrorSagas";
import type {
FetchWorkspaceRolesResponse,
SaveWorkspaceRequest,
FetchWorkspaceRequest,
FetchWorkspaceResponse,
CreateWorkspaceRequest,
FetchAllUsersResponse,
FetchAllUsersRequest,
FetchAllRolesResponse,
DeleteWorkspaceUserRequest,
ChangeUserRoleRequest,
FetchAllRolesRequest,
SaveWorkspaceLogo,
} from "@appsmith/api/WorkspaceApi";
import WorkspaceApi from "@appsmith/api/WorkspaceApi";
import type { ApiResponse } from "api/ApiResponses";
import { getCurrentWorkspace } from "@appsmith/selectors/workspaceSelectors";
import { getCurrentUser } from "selectors/usersSelectors";
import type { Workspace } from "@appsmith/constants/workspaceConstants";
import history from "utils/history";
import { APPLICATIONS_URL } from "constants/routes";
import { getAllApplications } from "@appsmith/actions/applicationActions";
import log from "loglevel";
import type { User } from "constants/userConstants";
import {
createMessage,
DELETE_WORKSPACE_SUCCESSFUL,
} from "@appsmith/constants/messages";
import { toast } from "design-system";
import { resetCurrentWorkspace } from "../actions/workspaceActions";
export function* fetchRolesSaga() {
try {
const response: FetchWorkspaceRolesResponse = yield call(
WorkspaceApi.fetchRoles,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_WORKSPACE_ROLES_SUCCESS,
payload: response.data,
});
}
} catch (error) {
log.error(error);
yield put({
type: ReduxActionErrorTypes.FETCH_WORKSPACE_ROLES_ERROR,
payload: {
error,
},
});
}
}
export function* fetchWorkspaceSaga(
action: ReduxAction<FetchWorkspaceRequest>,
) {
try {
const request: FetchWorkspaceRequest = action.payload;
const response: FetchWorkspaceResponse = yield call(
WorkspaceApi.fetchWorkspace,
request,
);
const isValidResponse: boolean = yield request.skipValidation ||
validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_WORKSPACE_SUCCESS,
payload: response.data || {},
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.FETCH_WORKSPACE_ERROR,
payload: {
error,
},
});
}
}
export function* fetchAllUsersSaga(action: ReduxAction<FetchAllUsersRequest>) {
try {
const request: FetchAllUsersRequest = action.payload;
const response: FetchAllUsersResponse = yield call(
WorkspaceApi.fetchAllUsers,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
const users = response.data.map((user) => ({
...user,
isDeleting: false,
isChangingRole: false,
}));
yield put({
type: ReduxActionTypes.FETCH_ALL_USERS_SUCCESS,
payload: users,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.FETCH_ALL_USERS_ERROR,
payload: {
error,
},
});
}
}
export function* changeWorkspaceUserRoleSaga(
action: ReduxAction<ChangeUserRoleRequest>,
) {
try {
const request: ChangeUserRoleRequest = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.changeWorkspaceUserRole,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.CHANGE_WORKSPACE_USER_ROLE_SUCCESS,
payload: response.data,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.CHANGE_WORKSPACE_USER_ROLE_ERROR,
payload: {
error,
},
});
}
}
export function* deleteWorkspaceUserSaga(
action: ReduxAction<DeleteWorkspaceUserRequest>,
) {
try {
const request: DeleteWorkspaceUserRequest = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.deleteWorkspaceUser,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
const currentUser: User | undefined = yield select(getCurrentUser);
if (currentUser?.username == action.payload.username) {
history.replace(APPLICATIONS_URL);
} else {
yield put({
type: ReduxActionTypes.DELETE_WORKSPACE_USER_SUCCESS,
payload: {
username: action.payload.username,
},
});
}
//@ts-expect-error: response is of type unknown
toast.show(`${response.data.username} has been removed successfully`, {
kind: "success",
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.DELETE_WORKSPACE_USER_ERROR,
payload: {
error,
},
});
}
}
export function* fetchAllRolesSaga(action: ReduxAction<FetchAllRolesRequest>) {
try {
const request: FetchAllRolesRequest = action.payload;
const response: FetchAllRolesResponse = yield call(
WorkspaceApi.fetchAllRoles,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_ALL_ROLES_SUCCESS,
payload: response.data,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.FETCH_ALL_ROLES_ERROR,
});
}
}
export function* saveWorkspaceSaga(action: ReduxAction<SaveWorkspaceRequest>) {
try {
const request: SaveWorkspaceRequest = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.saveWorkspace,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.SAVE_WORKSPACE_SUCCESS,
payload: request,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.SAVE_WORKSPACE_ERROR,
payload: {
error: (error as Error).message,
},
});
}
}
export function* deleteWorkspaceSaga(action: ReduxAction<string>) {
try {
yield put({
type: ReduxActionTypes.SAVING_WORKSPACE_INFO,
});
yield put(resetCurrentWorkspace());
const workspaceId: string = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.deleteWorkspace,
workspaceId,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.DELETE_WORKSPACE_SUCCESS,
payload: workspaceId,
});
toast.show(createMessage(DELETE_WORKSPACE_SUCCESSFUL), {
kind: "success",
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.DELETE_WORKSPACE_ERROR,
payload: {
error: (error as Error).message,
},
});
}
}
export function* createWorkspaceSaga(
action: ReduxActionWithPromise<CreateWorkspaceRequest>,
) {
const { name, reject, resolve } = action.payload;
try {
const request: CreateWorkspaceRequest = { name };
const response: ApiResponse = yield callAPI(
WorkspaceApi.createWorkspace,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (!isValidResponse) {
const errorMessage: string | undefined = yield getResponseErrorMessage(
response,
);
yield call(reject, { _error: errorMessage });
} else {
yield put({
type: ReduxActionTypes.CREATE_WORKSPACE_SUCCESS,
payload: response.data,
});
yield put(getAllApplications());
yield call(resolve);
}
// get created workspace in focus
// @ts-expect-error: response is of type unknown
const workspaceId = response.data.id;
history.push(`${window.location.pathname}#${workspaceId}`);
} catch (error) {
yield call(reject, { _error: (error as Error).message });
yield put({
type: ReduxActionErrorTypes.CREATE_WORKSPACE_ERROR,
payload: {
error,
},
});
}
}
export function* uploadWorkspaceLogoSaga(
action: ReduxAction<SaveWorkspaceLogo>,
) {
try {
const request = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.saveWorkspaceLogo,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
const allWorkspaces: Workspace[] = yield select(getCurrentWorkspace);
const currentWorkspace = allWorkspaces.filter(
(el: Workspace) => el.id === request.id,
);
if (currentWorkspace.length > 0) {
yield put({
type: ReduxActionTypes.SAVE_WORKSPACE_SUCCESS,
payload: {
id: currentWorkspace[0].id,
// @ts-expect-error: response is of type unknown
logoUrl: response.data.logoUrl,
},
});
toast.show("Logo uploaded successfully", {
kind: "success",
});
}
}
} catch (error) {
log.error("Error occured while uploading the logo", error);
}
}
export function* deleteWorkspaceLogoSaga(action: ReduxAction<{ id: string }>) {
try {
const request = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.deleteWorkspaceLogo,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
const allWorkspaces: Workspace[] = yield select(getCurrentWorkspace);
const currentWorkspace = allWorkspaces.filter(
(el: Workspace) => el.id === request.id,
);
if (currentWorkspace.length > 0) {
yield put({
type: ReduxActionTypes.SAVE_WORKSPACE_SUCCESS,
payload: {
id: currentWorkspace[0].id,
// @ts-expect-error: response is of type unknown
logoUrl: response.data.logoUrl,
},
});
toast.show("Logo removed successfully", {
kind: "success",
});
}
}
} catch (error) {
log.error("Error occured while removing the logo", error);
}
}