Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Um email invite fix #2833

Merged
merged 6 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/editor-ui/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,14 @@ export interface IUserResponse {
personalizationAnswers?: IPersonalizationSurveyAnswers | null;
}

export interface IInviteResponse {
user: {
id: string;
email: string;
};
error?: string;
}
BHesseldieck marked this conversation as resolved.
Show resolved Hide resolved

export interface IUser extends IUserResponse {
isDefaultUser: boolean;
isCurrentUser: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/editor-ui/src/api/users.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPersonalizationSurveyAnswers, IRestApiContext, IUserResponse } from '@/Interface';
import { IInviteResponse, IPersonalizationSurveyAnswers, IRestApiContext, IUserResponse } from '@/Interface';
import { IDataObject } from 'n8n-workflow';
import { makeRestApiRequest } from './helpers';

Expand Down Expand Up @@ -59,7 +59,7 @@ export function getUsers(context: IRestApiContext): Promise<IUserResponse[]> {
return makeRestApiRequest(context, 'GET', '/users');
}

export function inviteUsers(context: IRestApiContext, params: Array<{email: string}>): Promise<Array<Partial<IUserResponse>>> {
export function inviteUsers(context: IRestApiContext, params: Array<{email: string}>): Promise<IInviteResponse[]> {
return makeRestApiRequest(context, 'POST', '/users', params as unknown as IDataObject);
}

Expand Down
40 changes: 29 additions & 11 deletions packages/editor-ui/src/components/InviteUsersModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import mixins from "vue-typed-mixins";
import { showMessage } from "@/components/mixins/showMessage";
import Modal from "./Modal.vue";
import Vue from "vue";
import { IFormInputs, IUserResponse } from "@/Interface";
import { IFormInputs, IInviteResponse, IUserResponse } from "@/Interface";
import { VALID_EMAIL_REGEX, INVITE_USER_MODAL_KEY } from "@/constants";
import { ROLE } from "@/modules/userHelpers";

Expand Down Expand Up @@ -140,20 +140,38 @@ export default mixins(showMessage).extend({
throw new Error('No users to invite');
}

const invited: Array<Partial<IUserResponse>> = await this.$store.dispatch('users/inviteUsers', emails);
const invitedEmails = invited.reduce((accu, user) => {
if (user.email) {
return accu ? `${accu}, ${user.email}` : user.email;
const invited: IInviteResponse[] = await this.$store.dispatch('users/inviteUsers', emails);
const invitedEmails = invited.reduce((accu, {user, error}) => {
if (error) {
accu.error.push(user.email);
}
else {
accu.success.push(user.email);
}
return accu;
}, '');

this.$showMessage({
type: 'success',
title: `User${invited.length > 1 ? 's' : ''} invited successfully`,
message: `An invite email was sent to ${invitedEmails}`,
}, {
success: [] as string[],
error: [] as string[],
});

if (invitedEmails.success.length) {
this.$showMessage({
type: 'success',
title: `User${invitedEmails.success.length > 1 ? 's' : ''} invited successfully`,
message: `An invite email was sent to ${invitedEmails.success.join(',')}`,
});
}

if (invitedEmails.error.length) {
setTimeout(() => {
this.$showMessage({ // notifications stack on top of each other otherwise
type: 'error',
title: `User${invitedEmails.error.length > 1 ? 's' : ''} could not be invited`,
message: `Could not invite ${invitedEmails.error.join(',')}`,
});
}, 0);
}

this.modalBus.$emit('close');

} catch (error) {
Expand Down
5 changes: 3 additions & 2 deletions packages/editor-ui/src/modules/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PERSONALIZATION_MODAL_KEY } from '@/constants';
import Vue from 'vue';
import { ActionContext, Module } from 'vuex';
import {
IInviteResponse,
IPermissions,
IPersonalizationSurveyAnswers,
IRootState,
Expand Down Expand Up @@ -178,9 +179,9 @@ const module: Module<IUsersState, IRootState> = {
const users = await getUsers(context.rootGetters.getRestApiContext);
context.commit('addUsers', users);
},
async inviteUsers(context: ActionContext<IUsersState, IRootState>, params: Array<{email: string}>): Promise<Array<Partial<IUserResponse>>> {
async inviteUsers(context: ActionContext<IUsersState, IRootState>, params: Array<{email: string}>): Promise<IInviteResponse[]> {
const users = await inviteUsers(context.rootGetters.getRestApiContext, params);
context.commit('addUsers', users);
context.commit('addUsers', users.map(({user}) => user));
return users;
},
async reinviteUser(context: ActionContext<IUsersState, IRootState>, params: {id: string}) {
Expand Down