import {
UserGroup,
changePassword,
- createUser,
deleteUser,
getIdentityProviders,
getUserGroups,
getUsers,
+ postUser,
updateUser,
} from '../users';
jest.mocked(getSystemInfo).mockImplementation(this.handleGetSystemInfo);
jest.mocked(getIdentityProviders).mockImplementation(this.handleGetIdentityProviders);
jest.mocked(getUsers).mockImplementation((p) => this.handleGetUsers(p));
- jest.mocked(createUser).mockImplementation(this.handleCreateUser);
+ jest.mocked(postUser).mockImplementation(this.handlePostUser);
jest.mocked(updateUser).mockImplementation(this.handleUpdateUser);
jest.mocked(getUserGroups).mockImplementation(this.handleGetUserGroups);
jest.mocked(addUserToGroup).mockImplementation(this.handleAddUserToGroup);
});
};
- handleCreateUser = (data: {
+ handlePostUser = (data: {
email?: string;
local?: boolean;
login: string;
name: string;
password?: string;
- scmAccount: string[];
+ scmAccounts: string[];
}) => {
- const { email, local, login, name, scmAccount } = data;
- if (scmAccount.some((a) => isEmpty(a.trim()))) {
+ const { email, local, login, name, scmAccounts } = data;
+ if (scmAccounts.some((a) => isEmpty(a.trim()))) {
return Promise.reject({
status: 400,
- json: () => Promise.resolve({ errors: [{ msg: 'Error: Empty SCM' }] }),
+ json: () => Promise.resolve({ message: 'Error: Empty SCM' }),
});
}
const newUser = mockRestUser({
local,
login,
name,
- scmAccounts: scmAccount,
+ scmAccounts,
});
this.users.push(newUser);
return this.reply(undefined);
const index = this.users.findIndex((u) => u.login === data.login);
const user = this.users.splice(index, 1)[0];
user.active = false;
- return this.reply({ user });
+ return this.reply(undefined);
};
reset = () => {
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '../helpers/error';
-import { deleteJSON, getJSON, HttpStatus, parseJSON, post, postJSON } from '../helpers/request';
+import {
+ deleteJSON,
+ getJSON,
+ HttpStatus,
+ parseJSON,
+ post,
+ postJSON,
+ postJSONBody,
+} from '../helpers/request';
import { IdentityProvider, Paging } from '../types/types';
import {
ChangePasswordResults,
HomePage,
NoticeType,
RestUserBase,
- RestUserDetailed,
User,
} from '../types/users';
return getJSON('/api/v2/users', data).catch(throwGlobalError);
}
-export function createUser(data: {
+export function postUser(data: {
email?: string;
- local?: boolean;
login: string;
name: string;
password?: string;
- scmAccount: string[];
+ scmAccounts: string[];
}): Promise<void | Response> {
- return post('/api/users/create', data);
+ return postJSONBody('/api/v2/users', data);
}
export function updateUser(data: {
}: {
login: string;
anonymize?: boolean;
-}): Promise<{ user: RestUserDetailed }> {
+}): Promise<void | Response> {
return deleteJSON(`/api/v2/users/${login}`, { anonymize }).catch(throwGlobalError);
}
import Avatar from '../../../components/ui/Avatar';
import { translate, translateWithParameters } from '../../../helpers/l10n';
import { Issue } from '../../../types/types';
-import { RestUser, isLoggedIn, isUserActive } from '../../../types/users';
+import { RestUser, UserActive, isLoggedIn, isUserActive } from '../../../types/users';
import { searchAssignees } from '../utils';
// exported for test
inputId: string;
}
-function userToOption(user: RestUser) {
+function userToOption(user: RestUser | UserActive) {
const userInfo = user.name || user.login;
return {
value: user.login,
isLoggedIn(currentUser) && issues.some((issue) => currentUser.login !== issue.assignee);
const defaultOptions = allowCurrentUserSelection
- ? [UNASSIGNED, userToOption(currentUser as unknown as RestUser)]
+ ? [UNASSIGNED, userToOption(currentUser)]
: [UNASSIGNED];
const controlLabel = assignee ? (
import MandatoryFieldsExplanation from '../../../components/ui/MandatoryFieldsExplanation';
import { throwGlobalError } from '../../../helpers/error';
import { translate, translateWithParameters } from '../../../helpers/l10n';
-import { parseError } from '../../../helpers/request';
-import { useCreateUserMutation, useUpdateUserMutation } from '../../../queries/users';
+import { parseMessage } from '../../../helpers/request';
+import { usePostUserMutation, useUpdateUserMutation } from '../../../queries/users';
import { RestUserDetailed } from '../../../types/users';
import UserScmAccountInput from './UserScmAccountInput';
export default function UserForm(props: Props) {
const { user } = props;
- const { mutate: createUser } = useCreateUserMutation();
+ const { mutate: createUser } = usePostUserMutation();
const { mutate: updateUser } = useUpdateUserMutation();
const [email, setEmail] = React.useState<string>(user?.email ?? '');
if (![BAD_REQUEST, INTERNAL_SERVER_ERROR].includes(response.status)) {
throwGlobalError(response);
} else {
- parseError(response).then((errorMsg) => setError(errorMsg), throwGlobalError);
+ parseMessage(response).then((errorMsg) => setError(errorMsg), throwGlobalError);
}
};
login,
name,
password,
- scmAccount: scmAccounts,
+ scmAccounts,
},
{ onSuccess: props.onClose, onError: handleError }
);
}
/**
- * Parse response of failed request
+ * Parse error response of failed request
*/
export function parseError(response: Response): Promise<string> {
const DEFAULT_MESSAGE = translate('default_error_message');
.catch(() => DEFAULT_MESSAGE);
}
+/**
+ * Parse message response of failed request
+ */
+export function parseMessage(response: Response): Promise<string> {
+ const DEFAULT_MESSAGE = translate('default_error_message');
+ return parseJSON(response)
+ .then(({ message }) => message)
+ .catch(() => DEFAULT_MESSAGE);
+}
+
/**
* Shortcut to do a GET request and return a Response
*/
/**
* Shortcut to do a DELETE request
*/
-export function deleteJSON(url: string, data?: RequestData): Promise<any> {
+export function deleteJSON(url: string, data?: RequestData): Promise<Response> {
return request(url)
.setMethod('DELETE')
.setData(data)
useQueryClient,
} from '@tanstack/react-query';
import { range } from 'lodash';
-import { createUser, deleteUser, getUsers, updateUser } from '../api/users';
+import { deleteUser, getUsers, postUser, updateUser } from '../api/users';
import { RestUserBase } from '../types/users';
const STALE_TIME = 4 * 60 * 1000;
return () => queryClient.invalidateQueries({ queryKey: ['user', 'list'] });
}
-export function useCreateUserMutation() {
+export function usePostUserMutation() {
const queryClient = useQueryClient();
return useMutation({
- mutationFn: async (data: Parameters<typeof createUser>[0]) => {
- await createUser(data);
+ mutationFn: async (data: Parameters<typeof postUser>[0]) => {
+ await postUser(data);
},
onSuccess() {
queryClient.invalidateQueries({ queryKey: ['user', 'list'] });