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

fix: handle form emails being identities #775

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
152 changes: 87 additions & 65 deletions apps/mail-bridge/queue/mail-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import {
convoToSpaces,
convos,
emailIdentities,
orgMembers,
orgs,
postalServers,
spaceMembers,
teams,
type ConvoEntryMetadata
} from '@u22n/database/schema';
import {
Expand Down Expand Up @@ -797,71 +800,90 @@ export const worker = createWorker<MailProcessorJobData>(
// @ts-expect-error we check and define earlier up
fromAddressParticipantId = contactParticipant.id;
} else if (fromAddressPlatformObject.type === 'emailIdentity') {
await discord.info(
`🚪 Adding participants from internal email identity with messages sent from external email services not supported yet, convoId: ${convoId}, fromAddressParticipantId: ${fromAddressParticipantId}`
);
//! TODO: How do we handle adding the participant to the convo if we only track spaces?
//! leave code below for ref and quick revert if needed
// we need to get the first person/team in the routing rule and add them to the convo
// const emailIdentityParticipant =
// await db.query.emailIdentities.findFirst({
// where: and(
// eq(emailIdentities.orgId, orgId),
// eq(emailIdentities.id, fromAddressPlatformObject?.id)
// ),
// columns: {
// id: true
// },
// with: {
// routingRules: {
// columns: {
// id: true
// },
// with: {
// destinations: {
// columns: {
// spaceId: true,
// }
// }
// }
// }
// }
// });
// const firstDestination =
// // @ts-expect-error, taken form old code, will rewrite later
// emailIdentityParticipant.routingRules.destinations[0]!;
// let convoParticipantFromAddressIdentity;
// if (firstDestination.orgMemberId) {
// convoParticipantFromAddressIdentity =
// await db.query.convoParticipants.findFirst({
// where: and(
// eq(convoParticipants.orgId, orgId),
// eq(convoParticipants.convoId, convoId),

// eq(
// convoParticipants.orgMemberId,
// firstDestination.orgMemberId
// )
// ),
// columns: {
// id: true
// }
// });
// } else if (firstDestination.teamId) {
// convoParticipantFromAddressIdentity =
// await db.query.convoParticipants.findFirst({
// where: and(
// eq(convoParticipants.orgId, orgId),
// eq(convoParticipants.convoId, convoId || 0),
// eq(convoParticipants.teamId, firstDestination.teamId)
// ),
// columns: {
// id: true
// }
// });
// }

// fromAddressParticipantId = convoParticipantFromAddressIdentity.id;
const emailIdentity = await db.query.emailIdentities.findFirst({
where: and(
eq(emailIdentities.orgId, orgId),
eq(emailIdentities.id, fromAddressPlatformObject?.id)
),
columns: {
id: true
},
with: {
authorizedSenders: {
with: {
orgMember: true,
team: true,
space: true
}
}
}
});

if (!emailIdentity) {
throw new Error('No email identity participant found');
}

const ownerMember = await db.query.orgMembers.findFirst({
where: and(
eq(orgMembers.orgId, orgId),
eq(orgMembers.defaultEmailIdentityId, emailIdentity.id)
)
});

if (ownerMember) {
fromAddressParticipantId = ownerMember.id;
} else {
const ownerTeam = await db.query.teams.findFirst({
where: and(
eq(teams.orgId, orgId),
eq(teams.defaultEmailIdentityId, emailIdentity.id)
)
});

if (ownerTeam) {
fromAddressParticipantId = ownerTeam.id;
}
}

// if we still don't have a participant, then we need to find the first person/team in the authorized senders or first member of the first space
if (!fromAddressParticipantId) {
if (emailIdentity.authorizedSenders.length) {
const firstAuthorizedSender =
emailIdentity.authorizedSenders[0];
if (firstAuthorizedSender?.orgMember) {
fromAddressParticipantId = firstAuthorizedSender.orgMember.id;
}
if (firstAuthorizedSender?.team) {
fromAddressParticipantId = firstAuthorizedSender.team.id;
}
if (firstAuthorizedSender?.space) {
const spaceMember = await db.query.spaceMembers.findFirst({
where: and(
eq(spaceMembers.spaceId, firstAuthorizedSender.space.id)
),
with: {
orgMember: true,
team: true
}
});
if (spaceMember?.team) {
fromAddressParticipantId = spaceMember.team.id;
} else if (spaceMember?.orgMember) {
fromAddressParticipantId = spaceMember.orgMember.id;
}
}
} else {
throw new Error(
'No authorized senders found for email identity'
);
}
}

if (!fromAddressParticipantId) {
throw new Error(
`Failed to find a from address participant for email identity ${fromAddressPlatformObject?.email}`
);
}
}
}

Expand Down
Loading