add trial ended email for cloud

This commit is contained in:
daniel31x13
2025-10-13 17:26:09 -04:00
parent f7e7fda779
commit 4148c0f5fb
17 changed files with 613 additions and 28 deletions

View File

@@ -1,6 +1,6 @@
import { randomBytes } from "crypto";
import { prisma } from "@linkwarden/prisma";
import transporter from "./transporter";
import transporter from "@linkwarden/lib/transporter";
import Handlebars from "handlebars";
import { readFileSync } from "fs";
import path from "path";

View File

@@ -1,7 +1,7 @@
import { readFileSync } from "fs";
import path from "path";
import Handlebars from "handlebars";
import transporter from "./transporter";
import transporter from "@linkwarden/lib/transporter";
type Params = {
parentSubscriptionEmail: string;

View File

@@ -1,6 +1,6 @@
import { randomBytes } from "crypto";
import { prisma } from "@linkwarden/prisma";
import transporter from "./transporter";
import transporter from "@linkwarden/lib/transporter";
import Handlebars from "handlebars";
import { readFileSync } from "fs";
import path from "path";

View File

@@ -1,7 +1,7 @@
import { readFileSync } from "fs";
import path from "path";
import Handlebars from "handlebars";
import transporter from "./transporter";
import transporter from "@linkwarden/lib/transporter";
type Params = {
identifier: string;

View File

@@ -42,7 +42,6 @@
"@types/crypto-js": "^4.1.1",
"@types/formidable": "^3.4.5",
"@types/node": "^20.10.4",
"@types/nodemailer": "^6.4.8",
"@types/papaparse": "^5.3.16",
"@types/react": "18.3.20",
"@types/react-dom": "18.3.7",
@@ -72,7 +71,6 @@
"next-auth": "^4.22.1",
"next-i18next": "^15.3.0",
"node-fetch": "^2.7.0",
"nodemailer": "^6.9.3",
"papaparse": "^5.5.3",
"playwright": "^1.55.0",
"react": "18.3.1",

View File

@@ -121,32 +121,38 @@ export default function Subscribe() {
</div>
</div>
<div className="flex flex-col gap-2 justify-center items-center">
<div className="flex flex-col gap-2 justify-center items-center min-h-36">
<p className="text-3xl">
${plan === Plan.monthly ? "4" : "3"}
<span className="text-base text-neutral">/mo</span>
</p>
<p className="font-semibold">
{plan === Plan.monthly ? t("billed_monthly") : t("billed_yearly")}
</p>
<fieldset className="w-full flex-col flex justify-evenly px-4 pb-4 pt-1 rounded-md border border-neutral-content">
<legend className="w-fit font-extralight px-2 border border-neutral-content rounded-md text-xl">
{t("total")}
</legend>
<p className="text-sm">
{plan === Plan.monthly
? t("total_monthly_desc", {
count: REQUIRE_CC ? 14 : daysLeft,
monthlyPrice: "4",
})
: t("total_annual_desc", {
count: REQUIRE_CC ? 14 : daysLeft,
annualPrice: "36",
})}
</p>
<p className="text-sm">{t("plus_tax")}</p>
</fieldset>
{daysLeft > 0 ? (
<fieldset className="w-full max-h-fit flex-col flex gap-2 px-4 pb-4 pt-2 rounded-md border border-neutral-content">
<legend className="w-fit font-extralight px-2 border border-neutral-content rounded-md text-xl">
{t("total")}
</legend>
<p className="text-sm">
{plan === Plan.monthly
? t("total_monthly_desc", {
count: REQUIRE_CC ? 14 : daysLeft,
monthlyPrice: "4",
})
: t("total_annual_desc", {
count: REQUIRE_CC ? 14 : daysLeft,
annualPrice: "36",
})}
</p>
<p className="text-sm">{t("plus_tax")}</p>
</fieldset>
) : (
<p className="text-xs">{t("plus_tax")}</p>
)}
</div>
<div

View File

@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

View File

@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

View File

@@ -31,6 +31,7 @@
"rss-parser": "^3.13.0",
"socks-proxy-agent": "^8.0.2",
"tsx": "^4.19.3",
"handlebars": "^4.7.8",
"zod": "^3.23.8"
},
"devDependencies": {

View File

@@ -0,0 +1,451 @@
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Email</title>
<style media="all" type="text/css">
@media only screen and (max-width: 640px) {
.main p,
.main td,
.main span {
font-size: 14px !important;
}
.wrapper {
padding: 8px !important;
}
.content {
padding: 0 !important;
}
.container {
padding: 0 !important;
padding-top: 8px !important;
width: 100% !important;
}
.main {
border-left-width: 0 !important;
border-radius: 0 !important;
border-right-width: 0 !important;
}
.btn table {
max-width: 100% !important;
width: 100% !important;
}
.btn a {
font-size: 16px !important;
max-width: 100% !important;
width: 100% !important;
}
}
@media all {
.ExternalClass {
width: 100%;
}
.ExternalClass,
.ExternalClass p,
.ExternalClass span,
.ExternalClass font,
.ExternalClass td,
.ExternalClass div {
line-height: 100%;
}
.apple-link a {
color: inherit !important;
font-family: inherit !important;
font-size: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
text-decoration: none !important;
}
#MessageViewBody a {
color: inherit;
text-decoration: none;
font-size: inherit;
font-family: inherit;
font-weight: inherit;
line-height: inherit;
}
}
</style>
</head>
<body
style="
font-family: Helvetica, sans-serif;
-webkit-font-smoothing: antialiased;
font-size: 16px;
line-height: 1.3;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
background-color: #f8f8f8;
margin: 0;
padding: 0;
"
>
<table
role="presentation"
border="0"
cellpadding="0"
cellspacing="0"
class="body"
style="
border-collapse: separate;
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
background-color: #f8f8f8;
width: 100%;
"
width="100%"
bgcolor="#f8f8f8"
>
<tr>
<td
style="
font-family: Helvetica, sans-serif;
font-size: 16px;
vertical-align: top;
"
valign="top"
>
&nbsp;
</td>
<td
class="container"
style="
font-family: Helvetica, sans-serif;
font-size: 16px;
vertical-align: top;
max-width: 600px;
padding: 0;
padding-top: 24px;
width: 600px;
margin: 0 auto;
"
width="600"
valign="top"
>
<div
class="content"
style="
box-sizing: border-box;
display: block;
margin: 0 auto;
max-width: 600px;
padding: 0;
"
>
<!-- START CENTERED WHITE CONTAINER -->
<span
class="preheader"
style="
color: transparent;
display: none;
height: 0;
max-height: 0;
max-width: 0;
opacity: 0;
overflow: hidden;
mso-hide: all;
visibility: hidden;
width: 0;
"
>
Upgrade your account to continue using Linkwarden.
</span>
<table
role="presentation"
border="0"
cellpadding="0"
cellspacing="0"
class="main"
style="
border-collapse: separate;
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
background: #ffffff;
border: 1px solid #eaebed;
border-radius: 16px;
width: 100%;
"
width="100%"
>
<!-- START MAIN CONTENT AREA -->
<tr>
<td
class="wrapper"
style="
font-family: Helvetica, sans-serif;
font-size: 16px;
vertical-align: top;
box-sizing: border-box;
padding: 24px;
width: fit-content;
"
valign="top"
>
<h1
style="
font-family: Helvetica, sans-serif;
font-size: 24px;
font-weight: bold;
margin: 0;
margin-bottom: 16px;
"
>
Your Linkwarden free trial has ended
</h1>
<p
style="
font-family: Helvetica, sans-serif;
font-size: 14px;
font-weight: normal;
margin: 0;
margin-bottom: 16px;
"
>
Hi {{name}},
</p>
<p
style="
font-family: Helvetica, sans-serif;
font-size: 14px;
font-weight: normal;
margin: 0;
margin-bottom: 16px;
"
>
Thanks for trying Linkwarden! Your free trial has now ended,
and some features are no longer available.
</p>
<p
style="
font-family: Helvetica, sans-serif;
font-size: 14px;
font-weight: normal;
margin: 0;
margin-bottom: 16px;
"
>
To continue saving, organizing, and accessing your links,
please upgrade your account.
</p>
<table
role="presentation"
border="0"
cellpadding="0"
cellspacing="0"
class="btn btn-primary"
style="
border-collapse: separate;
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
box-sizing: border-box;
width: 100%;
min-width: 100%;
"
width="100%"
>
<tbody>
<tr>
<td
align="left"
style="
font-family: Helvetica, sans-serif;
font-size: 16px;
vertical-align: top;
padding-bottom: 16px;
"
valign="top"
>
<table
role="presentation"
border="0"
cellpadding="0"
cellspacing="0"
style="
border-collapse: separate;
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
width: auto;
"
>
<tbody>
<tr>
<td
style="
font-family: Helvetica, sans-serif;
font-size: 13px;
vertical-align: top;
border-radius: 8px;
text-align: center;
background-color: #00335a;
"
valign="top"
align="center"
bgcolor="#0867ec"
>
<a
href="{{url}}"
target="_blank"
style="
border-radius: 8px;
box-sizing: border-box;
cursor: pointer;
display: inline-block;
font-size: 13px;
font-weight: bold;
margin: 0;
padding: 10px 18px;
text-decoration: none;
text-transform: capitalize;
background-color: #00335a;
color: #ffffff;
"
>Upgrade your account</a
>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p
style="
font-family: Helvetica, sans-serif;
font-size: 14px;
font-weight: normal;
margin: 0;
margin-bottom: 16px;
"
>
Your support helps us improve the product and provide you
with the best possible service.
</p>
<p
style="
font-family: Helvetica, sans-serif;
font-size: 14px;
font-weight: normal;
margin: 0;
margin-bottom: 16px;
"
>
Have questions? Just reply, were happy to help!
</p>
<hr
style="
border: none;
border-top: 1px solid #eaebed;
margin-bottom: 24px;
width: 100%;
"
/>
<p
style="
font-family: Helvetica, sans-serif;
font-size: 12px;
font-weight: normal;
margin: 0;
margin-bottom: 5px;
color: #868686;
"
>
If youre having trouble clicking the button, click on the
following link:
</p>
<span
style="
font-family: Helvetica, sans-serif;
font-size: 10px;
font-weight: normal;
margin: 0;
margin-bottom: 16px;
word-break: break-all;
"
>
{{url}}
</span>
</td>
</tr>
<!-- END MAIN CONTENT AREA -->
</table>
<!-- START FOOTER -->
<div
class="footer"
style="
clear: both;
padding-top: 24px;
text-align: center;
width: 100%;
"
>
<table
role="presentation"
border="0"
cellpadding="0"
cellspacing="0"
style="
border-collapse: separate;
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
width: 100%;
"
width="100%"
>
<tr>
<td
class="content-block"
style="vertical-align: top; text-align: center"
valign="top"
align="center"
>
<img
src="https://raw.githubusercontent.com/linkwarden/linkwarden/main/apps/web/public/linkwarden_light.png"
alt="logo"
style="width: 180px; height: auto"
/>
</td>
</tr>
</table>
</div>
<!-- END FOOTER -->
<!-- END CENTERED WHITE CONTAINER -->
</div>
</td>
<td
style="
font-family: Helvetica, sans-serif;
font-size: 16px;
vertical-align: top;
"
valign="top"
>
&nbsp;
</td>
</tr>
</table>
</body>
</html>

View File

@@ -1,6 +1,7 @@
import { startIndexing } from "./workers/linkIndexing";
import { linkProcessing } from "./workers/linkProcessing";
import { startRSSPolling } from "./workers/rssPolling";
import { trialEndEmailWorker } from "./workers/trialEndEmailWorker";
const workerIntervalInSeconds =
Number(process.env.ARCHIVE_SCRIPT_INTERVAL) || 10;
@@ -10,6 +11,7 @@ async function init() {
startRSSPolling();
linkProcessing(workerIntervalInSeconds);
startIndexing(workerIntervalInSeconds);
trialEndEmailWorker();
}
init();

View File

@@ -1,4 +1,3 @@
import { prisma } from "@linkwarden/prisma";
import archiveHandler from "../lib/archiveHandler";
import { LinkWithCollectionOwnerAndTags } from "@linkwarden/types";
import { delay } from "@linkwarden/lib";

View File

@@ -0,0 +1,123 @@
import { prisma } from "@linkwarden/prisma";
import { User } from "@linkwarden/prisma/client";
import transporter from "@linkwarden/lib/transporter";
import Handlebars from "handlebars";
import { readFileSync } from "fs";
import path from "path";
const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));
const batchSize = 10;
const pauseMs = 60000;
/**
* Runs the "trial ended" notifier in batches.
* - Only runs if NEXT_PUBLIC_TRIAL_PERIOD_DAYS > 1
* - Picks users whose createdAt is older than that trial window AND trialEndEmailSent=false
* - Skips users with an active own or parent subscription
* - Sends email via the provided transporter
* - Marks trialEndEmailSent=true for processed users (including those with active subs)
* - Waits for a minute between batches
*/
export async function trialEndEmailWorker() {
const trialDays = Number(process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS);
if (
!Number.isFinite(trialDays) ||
trialDays <= 1 ||
!process.env.STRIPE_SECRET_KEY ||
process.env.NEXT_PUBLIC_REQUIRE_CC === "true"
) {
return;
}
console.log("\x1b[34m%s\x1b[0m", `Starting trial-ended email worker...`);
const cutoff = new Date(Date.now() - trialDays * 24 * 60 * 60 * 1000);
const from = {
name: "Linkwarden",
address: String(process.env.EMAIL_FROM),
};
while (true) {
// 1) Pick a batch of candidates
const candidates = await prisma.user.findMany({
where: {
trialEndEmailSent: false,
createdAt: { lte: cutoff, gte: new Date("2025-09-25") }, // safety upper bound to avoid processing old users
},
orderBy: { createdAt: "asc" },
take: batchSize,
include: {
subscriptions: { select: { active: true } }, // own subscription
parentSubscription: { select: { active: true } }, // family/parent plan
},
});
if (candidates.length === 0) {
await sleep(pauseMs);
continue;
}
const processedIds: number[] = [];
let emailsSent = 0;
// 2) Process the batch (send emails if no active sub)
for (const user of candidates) {
const hasActive =
Boolean(user.subscriptions?.active) ||
Boolean(user.parentSubscription?.active);
// Well mark users as processed at the end of this loop iteration.
// If sending fails, we skip marking so we retry next pass.
if (!hasActive && user.email) {
emailsSent++;
try {
const emailsDir = path.resolve(process.cwd(), "templates");
const templateFile = readFileSync(
path.join(emailsDir, "trialEnded.html"),
"utf8"
);
const emailTemplate = Handlebars.compile(templateFile);
await transporter.sendMail({
from,
to: user.email,
subject: "Your Linkwarden trial has ended",
html: emailTemplate({
name: user.name?.trim() ? user.name.trim() : "there",
url: process.env.BASE_URL,
}),
});
} catch (err) {
console.error(
`[trial-worker] Failed to send trial-ended email to user ${user.id}`,
err
);
// Do not mark as processed so it can be retried on the next batch run
await sleep(pauseMs);
continue;
}
}
// Whether we emailed or skipped (active sub, or missing email), mark processed
processedIds.push(user.id);
}
// 3) Mark processed users so we don't pick them again
if (processedIds.length) {
await prisma.user.updateMany({
where: { id: { in: processedIds } },
data: { trialEndEmailSent: true },
});
console.log(
"\x1b[34m%s\x1b[0m",
`Marked off ${processedIds.length} users' trialEndEmailSent to true. Emails sent: ${emailsSent}`
);
}
// 4) Pause before the next batch
await sleep(pauseMs);
}
}

View File

@@ -11,6 +11,8 @@
"meilisearch": "^0.48.2",
"rss-parser": "^3.13.0",
"clsx": "^2.1.1",
"tailwind-merge": "^3.3.0"
"tailwind-merge": "^3.3.0",
"nodemailer": "^6.9.3",
"@types/nodemailer": "^6.4.8"
}
}

View File

@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "trialEndEmailSent" BOOLEAN NOT NULL DEFAULT false;

View File

@@ -69,6 +69,7 @@ model User {
dashboardSections DashboardSection[]
lastPickedAt DateTime?
acceptPromotionalEmails Boolean @default(false)
trialEndEmailSent Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}