send email (#10729)

workflow update to allow microsoft send email
- also handle the case were permissions are not enough
- update the redirection in case the user clicks on new account because
it's not anylonger as easy as simply google

fixes https://github.com/twentyhq/core-team-issues/issues/540
This commit is contained in:
Guillim
2025-03-07 18:29:35 +01:00
committed by GitHub
parent 18de7efaaa
commit 5056d3e53f

View File

@ -1,9 +1,12 @@
import { GMAIL_SEND_SCOPE } from '@/accounts/constants/GmailSendScope'; import { GMAIL_SEND_SCOPE } from '@/accounts/constants/GmailSendScope';
import { MICROSOFT_SEND_SCOPE } from '@/accounts/constants/MicrosoftSendScope';
import { ConnectedAccount } from '@/accounts/types/ConnectedAccount'; import { ConnectedAccount } from '@/accounts/types/ConnectedAccount';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState'; import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { useCommandMenu } from '@/command-menu/hooks/useCommandMenu';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords'; import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { FormTextFieldInput } from '@/object-record/record-field/form-types/components/FormTextFieldInput'; import { FormTextFieldInput } from '@/object-record/record-field/form-types/components/FormTextFieldInput';
import { useTriggerApisOAuth } from '@/settings/accounts/hooks/useTriggerApiOAuth'; import { useTriggerApisOAuth } from '@/settings/accounts/hooks/useTriggerApiOAuth';
import { SettingsPath } from '@/types/SettingsPath';
import { Select, SelectOption } from '@/ui/input/components/Select'; import { Select, SelectOption } from '@/ui/input/components/Select';
import { workflowIdState } from '@/workflow/states/workflowIdState'; import { workflowIdState } from '@/workflow/states/workflowIdState';
import { WorkflowSendEmailAction } from '@/workflow/types/Workflow'; import { WorkflowSendEmailAction } from '@/workflow/types/Workflow';
@ -14,10 +17,15 @@ import { WorkflowVariablePicker } from '@/workflow/workflow-variables/components
import { useTheme } from '@emotion/react'; import { useTheme } from '@emotion/react';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { useRecoilValue } from 'recoil'; import { useRecoilValue } from 'recoil';
import { ConnectedAccountProvider, isDefined } from 'twenty-shared'; import {
assertUnreachable,
ConnectedAccountProvider,
isDefined,
} from 'twenty-shared';
import { IconPlus, useIcons } from 'twenty-ui'; import { IconPlus, useIcons } from 'twenty-ui';
import { JsonValue } from 'type-fest'; import { JsonValue } from 'type-fest';
import { useDebouncedCallback } from 'use-debounce'; import { useDebouncedCallback } from 'use-debounce';
import { useNavigateSettings } from '~/hooks/useNavigateSettings';
type WorkflowEditActionFormSendEmailProps = { type WorkflowEditActionFormSendEmailProps = {
action: WorkflowSendEmailAction; action: WorkflowSendEmailAction;
@ -66,12 +74,28 @@ export const WorkflowEditActionFormSendEmail = ({
if (!isDefined(connectedAccount)) { if (!isDefined(connectedAccount)) {
return; return;
} }
const scopes = connectedAccount.scopes; const scopes = connectedAccount.scopes;
if (
!isDefined(scopes) || const hasSendScope = (
!isDefined(scopes.find((scope) => scope === GMAIL_SEND_SCOPE)) connectedAccount: ConnectedAccount,
) { scopes: string[],
await triggerApisOAuth(ConnectedAccountProvider.GOOGLE, { ): boolean => {
switch (connectedAccount.provider) {
case ConnectedAccountProvider.GOOGLE:
return scopes.some((scope) => scope === GMAIL_SEND_SCOPE);
case ConnectedAccountProvider.MICROSOFT:
return scopes.some((scope) => scope === MICROSOFT_SEND_SCOPE);
default:
assertUnreachable(
connectedAccount.provider,
'Provider not yet supported for sending emails',
);
}
};
if (!isDefined(scopes) || !hasSendScope(connectedAccount, scopes)) {
await triggerApisOAuth(connectedAccount.provider, {
redirectLocation: redirectUrl, redirectLocation: redirectUrl,
loginHint: connectedAccount.handle, loginHint: connectedAccount.handle,
}); });
@ -79,7 +103,7 @@ export const WorkflowEditActionFormSendEmail = ({
}; };
const saveAction = useDebouncedCallback( const saveAction = useDebouncedCallback(
async (formData: SendEmailFormData, checkScopes = false) => { async (formData: SendEmailFormData) => {
if (actionOptions.readonly === true) { if (actionOptions.readonly === true) {
return; return;
} }
@ -97,9 +121,7 @@ export const WorkflowEditActionFormSendEmail = ({
}, },
}); });
if (checkScopes === true) { await checkConnectedAccountScopes(formData.connectedAccountId);
await checkConnectedAccountScopes(formData.connectedAccountId);
}
}, },
1_000, 1_000,
); );
@ -169,7 +191,9 @@ export const WorkflowEditActionFormSendEmail = ({
const headerTitle = isDefined(action.name) ? action.name : 'Send Email'; const headerTitle = isDefined(action.name) ? action.name : 'Send Email';
const headerIcon = getActionIcon(action.type); const headerIcon = getActionIcon(action.type);
const navigate = useNavigateSettings();
const { closeCommandMenu } = useCommandMenu();
return ( return (
!loading && ( !loading && (
<> <>
@ -199,10 +223,10 @@ export const WorkflowEditActionFormSendEmail = ({
value={formData.connectedAccountId} value={formData.connectedAccountId}
options={connectedAccountOptions} options={connectedAccountOptions}
callToActionButton={{ callToActionButton={{
onClick: () => onClick: () => {
triggerApisOAuth(ConnectedAccountProvider.GOOGLE, { closeCommandMenu();
redirectLocation: redirectUrl, navigate(SettingsPath.NewAccount);
}), },
Icon: IconPlus, Icon: IconPlus,
text: 'Add account', text: 'Add account',
}} }}