Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,17 @@
// @vitest-environment node
import { describe, expect, test } from "vitest";
import { readRepoFile } from "~/utils/testing/repoPaths";
const source = readRepoFile("apps/web/app/modules/emailMarketing/db/campaigns.db.server.ts");
describe("campaign db tenant scope", () => {
test("updates campaigns and recipient replacement through tenant scope", () => {
expect(source).toContain("export async function updateCampaign(");
expect(source).toContain("tenantId: string | null,");
expect(source).toContain("db.campaign.updateMany");
expect(source).toContain("where: {\n id,\n tenantId,");
expect(source).toContain('throw new Error("Campaign not found for tenant scope.")');
expect(source).toContain("db.outboundEmail.deleteMany");
expect(source).toContain("campaignId: id,\n tenantId,");
});
});
@@ -0,0 +1,173 @@
import type { Campaign, EmailSender, OutboundEmail, OutboundEmailClick, OutboundEmailOpen } from "@toolctrl/db/prisma-browser";
import UserModelHelper from "~/utils/helpers/models/UserModelHelper";
import { db } from "~/utils/db.server";
import { RowWithValues } from "~/utils/db/entities/rows.db.server";
export type CampaignWithDetails = Campaign & {
emailSender: EmailSender;
recipients: (OutboundEmail & {
campaign: Campaign | null;
opens: OutboundEmailOpen[];
clicks: OutboundEmailClick[];
contactRow: RowWithValues | null;
})[];
};
export async function getAllCampaigns(tenantId: string | null, status?: string): Promise<CampaignWithDetails[]> {
return await db.campaign.findMany({
where: {
tenantId,
status,
},
include: {
emailSender: true,
recipients: {
include: {
campaign: true,
opens: true,
clicks: true,
contactRow: {
include: {
createdByUser: { select: UserModelHelper.selectSimpleUserProperties },
createdByApiKey: true,
values: { include: { media: true, multiple: true, range: true } },
},
},
},
},
},
orderBy: {
sentAt: "desc",
},
});
}
export async function getCampaign(id: string, tenantId: string | null): Promise<CampaignWithDetails | null> {
return await db.campaign.findFirst({
where: {
id,
tenantId,
},
include: {
emailSender: true,
recipients: {
include: {
campaign: true,
opens: true,
clicks: true,
contactRow: {
include: {
createdByUser: { select: UserModelHelper.selectSimpleUserProperties },
createdByApiKey: true,
values: { include: { media: true, multiple: true, range: true } },
},
},
},
},
},
});
}
export async function createCampaign(
data: {
emailSenderId: string;
tenantId: string | null;
name: string;
subject: string;
htmlBody: string;
textBody: string | undefined;
track: boolean;
},
recipients: { fromSenderId: string; email: string; contactRowId: string }[]
): Promise<Campaign> {
const item = await db.campaign.create({
data: {
...data,
recipients: {
create: recipients.map((recipient) => {
return {
tenantId: data.tenantId,
contactRowId: recipient.contactRowId,
email: recipient.email,
fromSenderId: recipient.fromSenderId,
};
}),
},
},
});
return item;
}
export async function updateCampaign(
id: string,
tenantId: string | null,
data: {
name?: string;
subject?: string;
htmlBody?: string;
textBody?: string | undefined;
status?: string;
track?: boolean;
sentAt?: Date;
},
recipients?: { fromSenderId: string; email: string; contactRowId: string }[]
): Promise<Campaign> {
const result = await db.campaign.updateMany({
where: {
id,
tenantId,
},
data,
});
if (result.count !== 1) {
throw new Error("Campaign not found for tenant scope.");
}
const item = await db.campaign.findFirstOrThrow({
where: {
id,
tenantId,
},
});
if (recipients) {
await db.outboundEmail.deleteMany({
where: {
campaignId: id,
tenantId,
},
});
await db.outboundEmail.createMany({
data: recipients.map((recipient) => {
return {
tenantId: item.tenantId,
campaignId: id,
contactRowId: recipient.contactRowId,
email: recipient.email,
fromSenderId: recipient.fromSenderId,
};
}),
});
}
return item;
}
export async function deleteCampaign(id: string, tenantId: string | null) {
return await db.campaign.deleteMany({
where: {
id,
tenantId,
},
});
}
export async function groupCampaigns(tenantId: string | null) {
return await db.campaign.groupBy({
by: ["status"],
where: {
tenantId,
},
_count: {
_all: true,
},
});
}
@@ -0,0 +1,115 @@
import type { EmailSender } from "@toolctrl/db/prisma-browser";
import { db } from "~/utils/db.server";
export type EmailSenderWithoutApiKey = Omit<EmailSender, "apiKey" | "apiSecret">;
export async function getAllEmailSenders(tenantId: string | null): Promise<EmailSenderWithoutApiKey[]> {
return await db.emailSender.findMany({
where: {
tenantId,
},
select: {
id: true,
tenantId: true,
provider: true,
stream: true,
fromName: true,
fromEmail: true,
replyToEmail: true,
},
orderBy: [
{
fromName: "asc",
},
],
});
}
export async function getEmailSender(id: string, tenantId: string | null): Promise<EmailSender | null> {
return await db.emailSender.findFirst({
where: {
id,
tenantId,
},
});
}
export async function getEmailSenderWithoutApiKey(id: string, tenantId: string | null): Promise<EmailSenderWithoutApiKey | null> {
return await db.emailSender.findFirst({
where: {
id,
tenantId,
},
select: {
id: true,
tenantId: true,
provider: true,
stream: true,
fromName: true,
fromEmail: true,
replyToEmail: true,
},
});
}
export async function createEmailSender(data: {
tenantId: string | null;
provider: string;
stream: string;
apiKey: string;
apiSecret?: string;
fromEmail: string;
fromName?: string;
replyToEmail?: string;
}): Promise<EmailSender> {
const item = await db.emailSender.create({
data,
});
return item;
}
export async function updateEmailSender(
id: string,
tenantId: string | null,
data: {
provider: string;
stream: string;
fromEmail: string;
fromName?: string;
replyToEmail?: string;
}
): Promise<EmailSender> {
const result = await db.emailSender.updateMany({
where: { id, tenantId },
data,
});
if (result.count !== 1) {
throw new Error("Email sender not found");
}
const item = await getEmailSender(id, tenantId);
if (!item) {
throw new Error("Email sender not found");
}
return item;
}
export async function deleteEmailSender(id: string, tenantId: string | null): Promise<EmailSender> {
const existing = await getEmailSender(id, tenantId);
if (!existing) {
throw new Error("Email sender not found");
}
await db.outboundEmail.deleteMany({
where: { fromSenderId: id, tenantId },
});
const result = await db.emailSender.deleteMany({
where: { id, tenantId },
});
if (result.count !== 1) {
throw new Error("Email sender not found");
}
return existing;
}
@@ -0,0 +1,203 @@
import type { Campaign, OutboundEmail, OutboundEmailClick, OutboundEmailOpen, Prisma } from "@toolctrl/db/prisma-browser";
import Constants from "~/application/Constants";
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
import UserModelHelper from "~/utils/helpers/models/UserModelHelper";
import { db } from "~/utils/db.server";
import { RowWithValues } from "~/utils/db/entities/rows.db.server";
export type OutboundEmailWithDetails = OutboundEmail & {
campaign: Campaign | null;
opens: OutboundEmailOpen[];
clicks: OutboundEmailClick[];
contactRow: RowWithValues | null;
};
export async function getAllOutboundEmails(
tenantId: string | null,
options: {
where?: Prisma.OutboundEmailWhereInput;
pagination: { page: number; pageSize: number };
}
): Promise<{ items: OutboundEmailWithDetails[]; pagination: PaginationDto }> {
const items = await db.outboundEmail.findMany({
take: options.pagination.pageSize,
skip: options.pagination.pageSize * (options.pagination.page - 1),
where: {
tenantId,
...options.where,
},
include: {
campaign: true,
opens: true,
clicks: true,
contactRow: {
include: {
createdByUser: { select: UserModelHelper.selectSimpleUserProperties },
createdByApiKey: true,
values: { include: { media: true, multiple: true, range: true } },
},
},
},
orderBy: [{ sentAt: "desc" }],
});
const totalItems = await db.outboundEmail.count({
where: {
tenantId,
...options.where,
},
});
return {
items,
pagination: {
page: options.pagination?.page ?? 1,
pageSize: options.pagination.pageSize ?? Constants.DEFAULT_PAGE_SIZE,
totalItems,
totalPages: Math.ceil(totalItems / (options.pagination?.pageSize ?? Constants.DEFAULT_PAGE_SIZE)),
},
};
}
export async function findOutboundEmails(
tenantId: string | null,
options: {
where?: Prisma.OutboundEmailWhereInput;
}
) {
return await db.outboundEmail.findMany({
where: {
tenantId,
...options.where,
},
include: {
campaign: true,
opens: true,
clicks: true,
contactRow: {
include: {
createdByUser: { select: { id: true, email: true, firstName: true, lastName: true, phone: true } },
values: { include: { media: true } },
},
},
},
orderBy: [{ sentAt: "desc" }],
});
}
export async function getOutboundEmail(id: string) {
return await db.outboundEmail.findFirst({
where: {
id,
},
include: {
campaign: true,
opens: true,
clicks: true,
},
});
}
export async function createOutboundEmailTest(data: {
tenantId: string | null;
contactRowId?: string;
email: string;
fromSenderId: string;
}): Promise<OutboundEmail> {
const item = await db.outboundEmail.create({
data: {
...data,
isPreview: true,
},
});
return item;
}
export async function createOutboundEmail(data: {
tenantId: string | null;
contactRowId: string;
email: string;
fromSenderId: string;
campaignId?: string;
}): Promise<OutboundEmail> {
const item = await db.outboundEmail.create({
data,
});
return item;
}
export async function updateOutboundEmail(
id: string,
data: {
error?: string;
sentAt?: Date;
deliveredAt?: Date;
bouncedAt?: Date;
spamComplainedAt?: Date;
unsubscribedAt?: Date;
}
) {
const item = await db.outboundEmail.update({
where: {
id,
},
data,
});
return item;
}
export async function openedOutboundEmail(outboundEmailId: string, data: { firstOpen: boolean }) {
const item = await db.outboundEmailOpen.create({
data: {
outboundEmailId,
firstOpen: data.firstOpen,
},
});
return item;
}
export async function clickedOutboundEmail(
outboundEmailId: string,
data: {
link: string;
}
) {
const item = await db.outboundEmailClick.create({
data: {
outboundEmailId,
link: data.link,
},
});
return item;
}
export async function unsubscribeCampaignRecipient(
id: string,
data: {
unsubscribedAt: Date;
}
) {
const item = await db.outboundEmail.update({
where: {
id,
},
data,
});
return item;
}
export async function countOutboundEmails(
tenantId: string | null,
options?: {
where: Prisma.OutboundEmailWhereInput;
}
): Promise<number> {
return await db.outboundEmail.count({
where: {
tenantId,
...options?.where,
},
});
}