Skip to content

Commit

Permalink
feat: fix emails while sending with external credentials (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankParticle authored Aug 26, 2024
1 parent 120a09c commit 352434d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
26 changes: 16 additions & 10 deletions apps/mail-bridge/trpc/routers/sendMailRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ export const sendMailRouter = router({
postalServerUrl = `https://${config.MAILBRIDGE_POSTAL_SERVER_PERSONAL_CREDENTIALS.apiUrl}/api/v1/send/raw`;
postalServerAPIKey =
config.MAILBRIDGE_POSTAL_SERVER_PERSONAL_CREDENTIALS.apiKey;
} else if (sendAsEmailIdentity.externalCredentials) {
postalServerUrl = '';
postalServerAPIKey = '';
} else {
const orgPostalServerResponse = await db.query.postalServers.findFirst({
where: and(
Expand Down Expand Up @@ -726,24 +729,27 @@ export const sendMailRouter = router({
to: [`${convoToAddress}`, ...convoCcAddressesFiltered],
from: `${sendAsEmailIdentity.sendName} <${convoSenderEmailAddress}>`,
headers: emailHeaders,
raw: Buffer.from(rawEmail.asRaw()).toString('base64')
raw: rawEmail.asRaw()
}
}).catch((e) => {
console.error('🚨 error sending email as external identity', e);
return {
success: false
};
});
})
.then((res) => ({ success: true, ...res }))
.catch((e) => {
console.error('🚨 error sending email as external identity', e);
return {
success: false,
messageId: null
};
});

if ('success' in sentEmail && sentEmail.success === false) {
if (sentEmail.success === false) {
console.error(
'🚨 error sending email as external identity',
`send convoEntryId: ${entryId} from convoId: ${convoId}`
);
return {
success: false
};
} else if (!('success' in sentEmail) && sentEmail.messageId) {
} else if (sentEmail.messageId) {
const entryMetadata: ConvoEntryMetadata = {
email: {
to: [convoMetadataToAddress],
Expand All @@ -767,7 +773,7 @@ export const sendMailRouter = router({
};
} else {
console.error(
'🚨 error sending email as external identity, no idea what went wrong. didnt get success or fail',
"🚨 error sending email as external identity, no idea what went wrong. didn't get success or fail",
`send convoEntryId: ${entryId} from convoId: ${convoId}`
);
return {
Expand Down
32 changes: 22 additions & 10 deletions apps/platform/trpc/routers/orgRouter/mail/emailIdentityRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,29 @@ export const emailIdentityRouter = router({
const authorizedSpaceIds: number[] = [];

if (input.spaceShortcode) {
const spaceQueryResponse = await db.query.spaces.findFirst({
where: and(
eq(spaces.orgId, orgId),
eq(spaces.shortcode, input.spaceShortcode)
),
columns: {
id: true
if (input.spaceShortcode === 'personal') {
const personalSpace = await db.query.orgMembers.findFirst({
where: eq(orgMembers.id, orgMemberId),
columns: {
personalSpaceId: true
}
});
if (personalSpace?.personalSpaceId) {
authorizedSpaceIds.push(personalSpace.personalSpaceId);
}
} else {
const spaceQueryResponse = await db.query.spaces.findFirst({
where: and(
eq(spaces.orgId, orgId),
eq(spaces.shortcode, input.spaceShortcode)
),
columns: {
id: true
}
});
if (spaceQueryResponse?.id) {
authorizedSpaceIds.push(spaceQueryResponse.id);
}
});
if (spaceQueryResponse?.id) {
authorizedSpaceIds.push(spaceQueryResponse.id);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export default function CreateConvoForm({
});

const [selectedSpace, setSelectedSpace] = useState<string | null>(
['all', 'personal', null].includes(spaceShortcode) ? null : spaceShortcode
['all', null].includes(spaceShortcode) ? null : spaceShortcode
);
const { data: userEmailIdentities, isLoading: emailIdentitiesLoading } =
platform.org.mail.emailIdentities.getUserEmailIdentities.useQuery(
Expand Down Expand Up @@ -491,6 +491,19 @@ export default function CreateConvoForm({
throw new Error('Please select a space for the conversation.');
}

const resolvedSpaceShortcode =
selectedSpace === 'personal'
? spaces?.find(
(space) => space.publicId === spacesResponse?.personalSpaceId
)?.shortcode
: selectedSpace;

if (!resolvedSpaceShortcode) {
throw new Error(
'Failed to resolve space shortcode. Contact support if this persists.'
);
}

return createConvoFn({
firstMessageType: type,
topic,
Expand All @@ -503,7 +516,7 @@ export default function CreateConvoForm({
message: editorText,
attachments: getTrpcUploadFormat(),
orgShortcode,
spaceShortcode: selectedSpace
spaceShortcode: resolvedSpaceShortcode
});
}

Expand Down Expand Up @@ -692,7 +705,11 @@ export default function CreateConvoForm({
{spaces?.map((space) => (
<SelectItem
key={space.shortcode}
value={space.shortcode}>
value={
space.publicId === spacesResponse?.personalSpaceId
? 'personal'
: space.shortcode
}>
{space.name}
</SelectItem>
))}
Expand Down

0 comments on commit 352434d

Please sign in to comment.