feat(email): send trial ending soon mails
This commit is contained in:
@@ -28,6 +28,7 @@ COPY apps/worker/package.json ./apps/worker/
|
|||||||
# Packages
|
# Packages
|
||||||
COPY packages/db/package.json ./packages/db/
|
COPY packages/db/package.json ./packages/db/
|
||||||
COPY packages/json/package.json ./packages/json/
|
COPY packages/json/package.json ./packages/json/
|
||||||
|
COPY packages/email/package.json ./packages/email/
|
||||||
COPY packages/redis/package.json ./packages/redis/
|
COPY packages/redis/package.json ./packages/redis/
|
||||||
COPY packages/queue/package.json ./packages/queue/
|
COPY packages/queue/package.json ./packages/queue/
|
||||||
COPY packages/logger/package.json ./packages/logger/
|
COPY packages/logger/package.json ./packages/logger/
|
||||||
@@ -73,6 +74,7 @@ COPY --from=build /app/apps/worker ./apps/worker
|
|||||||
# Packages
|
# Packages
|
||||||
COPY --from=build /app/packages/db ./packages/db
|
COPY --from=build /app/packages/db ./packages/db
|
||||||
COPY --from=build /app/packages/json ./packages/json
|
COPY --from=build /app/packages/json ./packages/json
|
||||||
|
COPY --from=build /app/packages/email ./packages/email
|
||||||
COPY --from=build /app/packages/redis ./packages/redis
|
COPY --from=build /app/packages/redis ./packages/redis
|
||||||
COPY --from=build /app/packages/logger ./packages/logger
|
COPY --from=build /app/packages/logger ./packages/logger
|
||||||
COPY --from=build /app/packages/queue ./packages/queue
|
COPY --from=build /app/packages/queue ./packages/queue
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
"@openpanel/logger": "workspace:*",
|
"@openpanel/logger": "workspace:*",
|
||||||
"@openpanel/queue": "workspace:*",
|
"@openpanel/queue": "workspace:*",
|
||||||
"@openpanel/redis": "workspace:*",
|
"@openpanel/redis": "workspace:*",
|
||||||
|
"@openpanel/email": "workspace:*",
|
||||||
"bullmq": "^5.8.7",
|
"bullmq": "^5.8.7",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"prom-client": "^15.1.3",
|
"prom-client": "^15.1.3",
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { Worker } from 'bullmq';
|
|||||||
import {
|
import {
|
||||||
cronQueue,
|
cronQueue,
|
||||||
eventsQueue,
|
eventsQueue,
|
||||||
|
miscQueue,
|
||||||
notificationQueue,
|
notificationQueue,
|
||||||
sessionsQueue,
|
sessionsQueue,
|
||||||
} from '@openpanel/queue';
|
} from '@openpanel/queue';
|
||||||
@@ -13,6 +14,7 @@ import { performance } from 'node:perf_hooks';
|
|||||||
import { setTimeout as sleep } from 'node:timers/promises';
|
import { setTimeout as sleep } from 'node:timers/promises';
|
||||||
import { cronJob } from './jobs/cron';
|
import { cronJob } from './jobs/cron';
|
||||||
import { eventsJob } from './jobs/events';
|
import { eventsJob } from './jobs/events';
|
||||||
|
import { miscJob } from './jobs/misc';
|
||||||
import { notificationJob } from './jobs/notification';
|
import { notificationJob } from './jobs/notification';
|
||||||
import { sessionsJob } from './jobs/sessions';
|
import { sessionsJob } from './jobs/sessions';
|
||||||
import { logger } from './utils/logger';
|
import { logger } from './utils/logger';
|
||||||
@@ -35,12 +37,14 @@ export async function bootWorkers() {
|
|||||||
notificationJob,
|
notificationJob,
|
||||||
workerOptions,
|
workerOptions,
|
||||||
);
|
);
|
||||||
|
const miscWorker = new Worker(miscQueue.name, miscJob, workerOptions);
|
||||||
|
|
||||||
const workers = [
|
const workers = [
|
||||||
sessionsWorker,
|
sessionsWorker,
|
||||||
eventsWorker,
|
eventsWorker,
|
||||||
cronWorker,
|
cronWorker,
|
||||||
notificationWorker,
|
notificationWorker,
|
||||||
|
miscWorker,
|
||||||
];
|
];
|
||||||
|
|
||||||
workers.forEach((worker) => {
|
workers.forEach((worker) => {
|
||||||
@@ -105,12 +109,7 @@ export async function bootWorkers() {
|
|||||||
try {
|
try {
|
||||||
const time = performance.now();
|
const time = performance.now();
|
||||||
await waitForQueueToEmpty(cronQueue);
|
await waitForQueueToEmpty(cronQueue);
|
||||||
await Promise.all([
|
await Promise.all(workers.map((worker) => worker.close()));
|
||||||
cronWorker.close(),
|
|
||||||
eventsWorker.close(),
|
|
||||||
sessionsWorker.close(),
|
|
||||||
notificationWorker.close(),
|
|
||||||
]);
|
|
||||||
logger.info('workers closed successfully', {
|
logger.info('workers closed successfully', {
|
||||||
elapsed: performance.now() - time,
|
elapsed: performance.now() - time,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { createInitialSalts } from '@openpanel/db';
|
|||||||
import {
|
import {
|
||||||
cronQueue,
|
cronQueue,
|
||||||
eventsQueue,
|
eventsQueue,
|
||||||
|
miscQueue,
|
||||||
notificationQueue,
|
notificationQueue,
|
||||||
sessionsQueue,
|
sessionsQueue,
|
||||||
} from '@openpanel/queue';
|
} from '@openpanel/queue';
|
||||||
@@ -36,6 +37,7 @@ async function start() {
|
|||||||
new BullMQAdapter(sessionsQueue),
|
new BullMQAdapter(sessionsQueue),
|
||||||
new BullMQAdapter(cronQueue),
|
new BullMQAdapter(cronQueue),
|
||||||
new BullMQAdapter(notificationQueue),
|
new BullMQAdapter(notificationQueue),
|
||||||
|
new BullMQAdapter(miscQueue),
|
||||||
],
|
],
|
||||||
serverAdapter: serverAdapter,
|
serverAdapter: serverAdapter,
|
||||||
});
|
});
|
||||||
|
|||||||
50
apps/worker/src/jobs/misc.trail-ending-soon.ts
Normal file
50
apps/worker/src/jobs/misc.trail-ending-soon.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { db } from '@openpanel/db';
|
||||||
|
import { sendEmail } from '@openpanel/email';
|
||||||
|
import type { MiscQueuePayloadTrialEndingSoon } from '@openpanel/queue';
|
||||||
|
import type { Job } from 'bullmq';
|
||||||
|
|
||||||
|
export async function trialEndingSoonJob(
|
||||||
|
job: Job<MiscQueuePayloadTrialEndingSoon>,
|
||||||
|
) {
|
||||||
|
const { organizationId } = job.data.payload;
|
||||||
|
|
||||||
|
const organization = await db.organization.findUnique({
|
||||||
|
where: {
|
||||||
|
id: organizationId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
createdBy: {
|
||||||
|
select: {
|
||||||
|
email: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
projects: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!organization) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const project = organization.projects[0];
|
||||||
|
|
||||||
|
if (!organization.createdBy?.email) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!project) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sendEmail('trial-ending-soon', {
|
||||||
|
to: organization.createdBy?.email,
|
||||||
|
data: {
|
||||||
|
organizationName: organization.name,
|
||||||
|
url: `https://dashboard.openpanel.dev/${organization.id}/${project.id}/settings/organization?tab=billing`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
13
apps/worker/src/jobs/misc.ts
Normal file
13
apps/worker/src/jobs/misc.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import type { Job } from 'bullmq';
|
||||||
|
|
||||||
|
import type { MiscQueuePayloadTrialEndingSoon } from '@openpanel/queue';
|
||||||
|
|
||||||
|
import { trialEndingSoonJob } from './misc.trail-ending-soon';
|
||||||
|
|
||||||
|
export async function miscJob(job: Job<MiscQueuePayloadTrialEndingSoon>) {
|
||||||
|
switch (job.data.type) {
|
||||||
|
case 'trialEndingSoon': {
|
||||||
|
return await trialEndingSoonJob(job);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,8 +22,8 @@ export function Footer() {
|
|||||||
|
|
||||||
<br />
|
<br />
|
||||||
|
|
||||||
<Row>
|
<Row className="mt-4">
|
||||||
<Column className="align-middle w-[40px]">
|
<Column className="w-8">
|
||||||
<Link href="https://git.new/openpanel">
|
<Link href="https://git.new/openpanel">
|
||||||
<Img
|
<Img
|
||||||
src={`${baseUrl}/icons/github.png`}
|
src={`${baseUrl}/icons/github.png`}
|
||||||
@@ -33,7 +33,7 @@ export function Footer() {
|
|||||||
/>
|
/>
|
||||||
</Link>
|
</Link>
|
||||||
</Column>
|
</Column>
|
||||||
<Column className="align-middle w-[40px]">
|
<Column className="w-8">
|
||||||
<Link href="https://x.com/openpaneldev">
|
<Link href="https://x.com/openpaneldev">
|
||||||
<Img
|
<Img
|
||||||
src={`${baseUrl}/icons/x.png`}
|
src={`${baseUrl}/icons/x.png`}
|
||||||
@@ -43,8 +43,7 @@ export function Footer() {
|
|||||||
/>
|
/>
|
||||||
</Link>
|
</Link>
|
||||||
</Column>
|
</Column>
|
||||||
|
<Column className="w-8">
|
||||||
<Column className="align-middle">
|
|
||||||
<Link href="https://go.openpanel.dev/discord">
|
<Link href="https://go.openpanel.dev/discord">
|
||||||
<Img
|
<Img
|
||||||
src={`${baseUrl}/icons/discord.png`}
|
src={`${baseUrl}/icons/discord.png`}
|
||||||
@@ -54,8 +53,7 @@ export function Footer() {
|
|||||||
/>
|
/>
|
||||||
</Link>
|
</Link>
|
||||||
</Column>
|
</Column>
|
||||||
|
<Column className="w-auto">
|
||||||
<Column className="align-middle">
|
|
||||||
<Link href="mailto:hello@openpanel.dev">
|
<Link href="mailto:hello@openpanel.dev">
|
||||||
<Img
|
<Img
|
||||||
src={`${baseUrl}/icons/email.png`}
|
src={`${baseUrl}/icons/email.png`}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { EmailInvite, zEmailInvite } from './email-invite';
|
|||||||
import EmailResetPassword, {
|
import EmailResetPassword, {
|
||||||
zEmailResetPassword,
|
zEmailResetPassword,
|
||||||
} from './email-reset-password';
|
} from './email-reset-password';
|
||||||
|
import TrailEndingSoon, { zTrailEndingSoon } from './trial-ending-soon';
|
||||||
|
|
||||||
export const templates = {
|
export const templates = {
|
||||||
invite: {
|
invite: {
|
||||||
@@ -17,6 +18,12 @@ export const templates = {
|
|||||||
Component: EmailResetPassword,
|
Component: EmailResetPassword,
|
||||||
schema: zEmailResetPassword,
|
schema: zEmailResetPassword,
|
||||||
},
|
},
|
||||||
|
'trial-ending-soon': {
|
||||||
|
subject: (data: z.infer<typeof zTrailEndingSoon>) =>
|
||||||
|
'Your trial is ending soon',
|
||||||
|
Component: TrailEndingSoon,
|
||||||
|
schema: zTrailEndingSoon,
|
||||||
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export type Templates = typeof templates;
|
export type Templates = typeof templates;
|
||||||
|
|||||||
73
packages/email/src/emails/trial-ending-soon.tsx
Normal file
73
packages/email/src/emails/trial-ending-soon.tsx
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import { Button, Hr, Link, Text } from '@react-email/components';
|
||||||
|
import React from 'react';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { Layout } from '../components/layout';
|
||||||
|
|
||||||
|
export const zTrailEndingSoon = z.object({
|
||||||
|
url: z.string(),
|
||||||
|
organizationName: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type Props = z.infer<typeof zTrailEndingSoon>;
|
||||||
|
export default TrailEndingSoon;
|
||||||
|
export function TrailEndingSoon({
|
||||||
|
organizationName = 'Acme Co',
|
||||||
|
url = 'https://openpanel.dev',
|
||||||
|
}: Props) {
|
||||||
|
const newUrl = new URL(url);
|
||||||
|
newUrl.searchParams.set('utm_source', 'email');
|
||||||
|
newUrl.searchParams.set('utm_medium', 'email');
|
||||||
|
newUrl.searchParams.set('utm_campaign', 'trial-ending-soon');
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
<Text>Your trial period is ending soon for {organizationName}!</Text>
|
||||||
|
<Text>
|
||||||
|
When your trial ends, you'll still receive incoming events but you won't
|
||||||
|
be able to see them in the dashboard until you upgrade.
|
||||||
|
</Text>
|
||||||
|
<Text>
|
||||||
|
<Link href={newUrl.toString()}>Upgrade to a paid plan</Link>
|
||||||
|
</Text>
|
||||||
|
<Hr />
|
||||||
|
<Text style={{ fontWeight: 'bold' }}>
|
||||||
|
Discover what you can do with OpenPanel:
|
||||||
|
</Text>
|
||||||
|
<Text>
|
||||||
|
🎯 <strong>Create Custom Funnels</strong> - Track user progression
|
||||||
|
through your key conversion paths and identify where users drop off
|
||||||
|
</Text>
|
||||||
|
<Text>
|
||||||
|
📈 <strong>User Retention Analysis</strong> - Understand how well you're
|
||||||
|
keeping users engaged over time with beautiful retention graphs
|
||||||
|
</Text>
|
||||||
|
<Text>
|
||||||
|
🗺️ <strong>User Journey Mapping</strong> - Follow individual user paths
|
||||||
|
through your application to understand their behavior and optimize their
|
||||||
|
experience
|
||||||
|
</Text>
|
||||||
|
<Text>
|
||||||
|
🔬 <strong>A/B Testing Analysis</strong> - Measure the impact of your
|
||||||
|
product changes with detailed conversion metrics and statistical
|
||||||
|
significance
|
||||||
|
</Text>
|
||||||
|
<Text>
|
||||||
|
📊 <strong>Custom Event Tracking</strong> - Track any user interaction
|
||||||
|
that matters to your business with our flexible event system
|
||||||
|
</Text>
|
||||||
|
<Text>
|
||||||
|
<Button
|
||||||
|
href={newUrl.toString()}
|
||||||
|
style={{
|
||||||
|
backgroundColor: '#0070f3',
|
||||||
|
color: 'white',
|
||||||
|
padding: '12px 20px',
|
||||||
|
borderRadius: '5px',
|
||||||
|
textDecoration: 'none',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Upgrade Now to Unlock All Features
|
||||||
|
</Button>
|
||||||
|
</Text>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -76,6 +76,15 @@ export type CronQueuePayload =
|
|||||||
| CronQueuePayloadPing
|
| CronQueuePayloadPing
|
||||||
| CronQueuePayloadProject;
|
| CronQueuePayloadProject;
|
||||||
|
|
||||||
|
export type MiscQueuePayloadTrialEndingSoon = {
|
||||||
|
type: 'trialEndingSoon';
|
||||||
|
payload: {
|
||||||
|
organizationId: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type MiscQueuePayload = MiscQueuePayloadTrialEndingSoon;
|
||||||
|
|
||||||
export type CronQueueType = CronQueuePayload['type'];
|
export type CronQueueType = CronQueuePayload['type'];
|
||||||
|
|
||||||
export const eventsQueue = new Queue<EventsQueuePayload>('events', {
|
export const eventsQueue = new Queue<EventsQueuePayload>('events', {
|
||||||
@@ -107,6 +116,13 @@ export const cronQueue = new Queue<CronQueuePayload>('cron', {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const miscQueue = new Queue<MiscQueuePayload>('misc', {
|
||||||
|
connection: getRedisQueue(),
|
||||||
|
defaultJobOptions: {
|
||||||
|
removeOnComplete: 10,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export type NotificationQueuePayload = {
|
export type NotificationQueuePayload = {
|
||||||
type: 'sendNotification';
|
type: 'sendNotification';
|
||||||
payload: {
|
payload: {
|
||||||
@@ -123,3 +139,18 @@ export const notificationQueue = new Queue<NotificationQueuePayload>(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export function addTrialEndingSoonJob(organizationId: string, delay: number) {
|
||||||
|
return miscQueue.add(
|
||||||
|
'misc',
|
||||||
|
{
|
||||||
|
type: 'trialEndingSoon',
|
||||||
|
payload: {
|
||||||
|
organizationId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
delay,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { zOnboardingProject } from '@openpanel/validation';
|
|||||||
|
|
||||||
import { hashPassword } from '@openpanel/common/server';
|
import { hashPassword } from '@openpanel/common/server';
|
||||||
import { addDays } from 'date-fns';
|
import { addDays } from 'date-fns';
|
||||||
|
import { addTrialEndingSoonJob, miscQueue } from '../../../queue';
|
||||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from '../trpc';
|
import { createTRPCRouter, protectedProcedure, publicProcedure } from '../trpc';
|
||||||
|
|
||||||
async function createOrGetOrganization(
|
async function createOrGetOrganization(
|
||||||
@@ -18,16 +19,27 @@ async function createOrGetOrganization(
|
|||||||
return await getOrganizationBySlug(input.organizationId);
|
return await getOrganizationBySlug(input.organizationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const TRIAL_DURATION_IN_DAYS = 30;
|
||||||
|
|
||||||
if (input.organization) {
|
if (input.organization) {
|
||||||
return db.organization.create({
|
const organization = await db.organization.create({
|
||||||
data: {
|
data: {
|
||||||
id: await getId('organization', input.organization),
|
id: await getId('organization', input.organization),
|
||||||
name: input.organization,
|
name: input.organization,
|
||||||
createdByUserId: user.id,
|
createdByUserId: user.id,
|
||||||
subscriptionEndsAt: addDays(new Date(), 30),
|
subscriptionEndsAt: addDays(new Date(), TRIAL_DURATION_IN_DAYS),
|
||||||
subscriptionStatus: 'trialing',
|
subscriptionStatus: 'trialing',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!process.env.SELF_HOSTED) {
|
||||||
|
await addTrialEndingSoonJob(
|
||||||
|
organization.id,
|
||||||
|
1000 * 60 * 60 * 24 * TRIAL_DURATION_IN_DAYS * 0.9,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return organization;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
1048
pnpm-lock.yaml
generated
1048
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user