Skip to content
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
Binary file added .DS_Store
Binary file not shown.
429 changes: 429 additions & 0 deletions api/bun.lock

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions api/controllers/ApiKeyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ export async function getApiKeyById(apiKeyId: string) {
return await ApiKey.findById(apiKeyId);
}

export async function getApiKeyByIdAndOrganizationId(
apiKeyId: string,
organizationId: string
) {
export async function getApiKeyByIdAndOrganizationId({
apiKeyId,
organizationId,
}: {
apiKeyId: string;
organizationId: string;
}) {
return await ApiKey.findOne({ _id: apiKeyId, organizationId });
}

Expand All @@ -39,3 +42,13 @@ export async function activateApiKey(apiKeyId: string, active: boolean) {
export async function getApiKeyByKey(key: string) {
return await ApiKey.findOne({ key });
}

export async function deleteApiKey({
apiKeyId,
organizationId,
}: {
apiKeyId: string;
organizationId: string;
}) {
return await ApiKey.findOneAndDelete({ _id: apiKeyId, organizationId });
}
15 changes: 15 additions & 0 deletions api/controllers/DNSController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { resolveMx } from 'node:dns/promises';
import type { MxRecord } from 'node:dns';

export async function getDNSMXRecords({
domain,
}: {
domain: string;
}): Promise<MxRecord[]> {
try {
return await resolveMx(domain);
} catch (error) {
console.error('Error resolving MX records:', error);
return [];
}
}
98 changes: 98 additions & 0 deletions api/controllers/DomainController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import mongoose, { type HydratedDocument } from "mongoose";
import Domain from "../db/mongo/schemas/Domain";
import { getDNSMXRecords } from "./DNSController";
import type IDomain from "../models/Domain";

export async function createDomain({
organizationId,
name,
}: {
organizationId: string;
name: string;
}) {
const domain = new Domain();
domain.organizationId = new mongoose.Types.ObjectId(organizationId);
domain.name = name;
await domain.save();
return domain;
}

export async function getDomainsByOrganizationId({
organizationId,
}: {
organizationId: string;
}) {
return await Domain.find({
organizationId: new mongoose.Types.ObjectId(organizationId),
});
}

export async function getDomainByOrganizationIdAndName({
organizationId,
name,
}: {
organizationId: string;
name: string;
}) {
return await Domain.findOne({
organizationId: new mongoose.Types.ObjectId(organizationId),
name,
});
}

export async function getVerifiedDomainByOrganizationIdAndName({
organizationId,
name,
}: {
organizationId: string;
name: string;
}) {
return await Domain.findOne({
organizationId: new mongoose.Types.ObjectId(organizationId),
name: name,
verified: true,
});
}

export async function deleteDomainByOrganizationIdAndName({
organizationId,
name,
}: {
organizationId: string;
name: string;
}) {
return await Domain.findOneAndDelete({
organizationId: new mongoose.Types.ObjectId(organizationId),
name: name,
});
}

export async function verifyDomainDNS({
domain,
}: {
domain: HydratedDocument<IDomain>;
}): Promise<{ verified: boolean; domain: HydratedDocument<IDomain> }> {
const mxRecords = await getDNSMXRecords({ domain: domain.name });

let mxRecordFound;
if (mxRecords && Array.isArray(mxRecords)) {
mxRecordFound = domain.records.find(
(domainRecord) =>
domainRecord.type === "MX" &&
mxRecords.find((dnsRecord) => {
return (
typeof dnsRecord.exchange === "string" &&
dnsRecord.exchange.toLowerCase() === domainRecord.value.toLowerCase()
);
})
);
}

if (mxRecordFound) {
mxRecordFound.status = "verified";
}
domain.verified = mxRecordFound ? true : false;
await domain.save();

return { verified: domain.verified, domain };
}
13 changes: 13 additions & 0 deletions api/controllers/InboxController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ export async function getInboxByOrganizationIdAndInboxId({
});
}

export async function deleteInboxByOrganizationIdAndInboxId({
organizationId,
inboxId,
}: {
organizationId: string;
inboxId: string;
}) {
return await Inbox.findOneAndDelete({
organizationId: new mongoose.Types.ObjectId(organizationId),
_id: new mongoose.Types.ObjectId(inboxId),
});
}

export async function getInboxByEmail(email: string) {
return await Inbox.findOne({ email });
}
Expand Down
77 changes: 71 additions & 6 deletions api/controllers/MessageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ export async function createMessage({
toInboxId,
from,
to,
cc,
bcc,
labels,
externalMessageId,
subject,
text,
html,
attachments,
status,
}: {
organizationId: string;
Expand All @@ -22,25 +26,54 @@ export async function createMessage({
fromInboxId?: string;
toInboxId?: string;
from: string;
to: string;
to?: string[];
cc?: string[];
bcc?: string[];
labels?: string[];
externalMessageId?: string;
subject: string;
text: string;
html: string;
attachments?: {
content: string;
name?: string;
contentType?: string;
}[];
status?: (typeof MessageStatus)[number];
}) {
const message = new Message();
message.organizationId = new mongoose.Types.ObjectId(organizationId);
message.inboxId = new mongoose.Types.ObjectId(inboxId);
message.threadId = new mongoose.Types.ObjectId(threadId);
message.fromInboxId = fromInboxId ? new mongoose.Types.ObjectId(fromInboxId) : undefined;
message.toInboxId = toInboxId ? new mongoose.Types.ObjectId(toInboxId) : undefined;
message.fromInboxId = fromInboxId
? new mongoose.Types.ObjectId(fromInboxId)
: undefined;
message.toInboxId = toInboxId
? new mongoose.Types.ObjectId(toInboxId)
: undefined;
message.from = from;
message.to = to;
message.to = to ?? [];
message.cc = cc ?? [];
message.bcc = bcc ?? [];
message.labels = labels ?? [];
message.externalMessageId = externalMessageId;
message.subject = subject;
message.text = text;
message.html = html;
message.attachments = new mongoose.Types.Array<{
content: string;
name?: string;
contentType?: string;
}>();
if (attachments) {
for (const attachment of attachments) {
message.attachments.push({
content: attachment.content,
name: attachment.name,
contentType: attachment.contentType,
});
}
}
message.status = status;
await message.save();
return message;
Expand All @@ -50,10 +83,42 @@ export async function getMessageById(messageId: string) {
return await Message.findById(messageId);
}

export async function getMessagesByInboxId(inboxId: string) {
return await Message.find({ inboxId: new mongoose.Types.ObjectId(inboxId) });
export async function getMessages({
inboxId,
query,
}: {
inboxId: string;
query?: string;
}) {
const filter: any = {
inboxId: new mongoose.Types.ObjectId(inboxId),
};

if (query) {
filter.$or = [
{ subject: { $regex: query, $options: "i" } },
{ text: { $regex: query, $options: "i" } },
{ html: { $regex: query, $options: "i" } },
{ to: { $regex: query, $options: "i" } },
{ cc: { $regex: query, $options: "i" } },
{ bcc: { $regex: query, $options: "i" } },
{ labels: { $regex: query, $options: "i" } },
];
}

return await Message.find(filter);
}

export async function getMessageByExternalMessageId(externalMessageId: string) {
return await Message.findOne({ externalMessageId });
}

export async function deleteMessageById(messageId: string) {
return await Message.findByIdAndDelete(messageId);
}

export async function deleteMessagesByInboxId(inboxId: string) {
return await Message.deleteMany({
inboxId: new mongoose.Types.ObjectId(inboxId),
});
}
2 changes: 1 addition & 1 deletion api/controllers/PassportController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const passportBearerStrategy = new BearerStrategy(
}
const organizations = await getOrganizationsByUserId(user?.id);
return done(null, {
...user,
...(user.toJSON()),
organizations,
});
} catch (err) {
Expand Down
Loading