Compare commits

..

5 Commits

Author SHA1 Message Date
Daniel 4e20d71a41 Merge pull request #509 from linkwarden/dev
improved UX + improved performance
2024-03-10 13:39:04 +03:30
Daniel 9fce74971f Merge pull request #500 from linkwarden/dev
update announcement version number
2024-03-07 02:31:21 +03:30
Daniel bde7b9aae0 Merge pull request #497 from linkwarden/dev
improved performance
2024-03-06 17:38:45 +03:30
Daniel 7dd254af48 Merge pull request #495 from linkwarden/dev
more efficient logic for the background script
2024-03-06 02:58:42 +03:30
Daniel 3969cc5abd Merge pull request #494 from linkwarden/dev
v2.5.0
2024-03-05 22:05:54 +03:30
6 changed files with 40 additions and 52 deletions
+6 -7
View File
@@ -32,12 +32,11 @@ export default async function checkSubscriptionByEmail(email: string) {
customer.subscriptions?.data.some((subscription) => { customer.subscriptions?.data.some((subscription) => {
subscription.current_period_end; subscription.current_period_end;
active = active = subscription.items.data.some(
subscription.items.data.some( (e) =>
(e) => (e.price.id === MONTHLY_PRICE_ID && e.price.active === true) ||
(e.price.id === MONTHLY_PRICE_ID && e.price.active === true) || (e.price.id === YEARLY_PRICE_ID && e.price.active === true)
(e.price.id === YEARLY_PRICE_ID && e.price.active === true) );
) || false;
stripeSubscriptionId = subscription.id; stripeSubscriptionId = subscription.id;
currentPeriodStart = subscription.current_period_start * 1000; currentPeriodStart = subscription.current_period_start * 1000;
currentPeriodEnd = subscription.current_period_end * 1000; currentPeriodEnd = subscription.current_period_end * 1000;
@@ -45,7 +44,7 @@ export default async function checkSubscriptionByEmail(email: string) {
}); });
return { return {
active: active || false, active,
stripeSubscriptionId, stripeSubscriptionId,
currentPeriodStart, currentPeriodStart,
currentPeriodEnd, currentPeriodEnd,
+4 -7
View File
@@ -16,11 +16,8 @@ export default async function paymentCheckout(
const isExistingCustomer = listByEmail?.data[0]?.id || undefined; const isExistingCustomer = listByEmail?.data[0]?.id || undefined;
console.log("isExistingCustomer", listByEmail?.data[0]);
const NEXT_PUBLIC_TRIAL_PERIOD_DAYS = const NEXT_PUBLIC_TRIAL_PERIOD_DAYS =
Number(process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS) || 14; process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS;
const session = await stripe.checkout.sessions.create({ const session = await stripe.checkout.sessions.create({
customer: isExistingCustomer ? isExistingCustomer : undefined, customer: isExistingCustomer ? isExistingCustomer : undefined,
line_items: [ line_items: [
@@ -37,9 +34,9 @@ export default async function paymentCheckout(
enabled: true, enabled: true,
}, },
subscription_data: { subscription_data: {
trial_period_days: isExistingCustomer trial_period_days: NEXT_PUBLIC_TRIAL_PERIOD_DAYS
? undefined ? Number(NEXT_PUBLIC_TRIAL_PERIOD_DAYS)
: NEXT_PUBLIC_TRIAL_PERIOD_DAYS, : 14,
}, },
}); });
+17 -15
View File
@@ -17,7 +17,15 @@ export default async function verifySubscription(
const currentDate = new Date(); const currentDate = new Date();
if (!subscription?.active || currentDate > subscription.currentPeriodEnd) { if (
subscription &&
currentDate > subscription.currentPeriodEnd &&
!subscription.active
) {
return null;
}
if (!subscription || currentDate > subscription.currentPeriodEnd) {
const { const {
active, active,
stripeSubscriptionId, stripeSubscriptionId,
@@ -51,21 +59,15 @@ export default async function verifySubscription(
}, },
}) })
.catch((err) => console.log(err)); .catch((err) => console.log(err));
} else if (!active) { }
const subscription = await prisma.subscription.findFirst({
where: {
userId: user.id,
},
});
if (subscription) if (!active) {
await prisma.subscription.delete({ if (user.username)
where: { // await prisma.user.update({
userId: user.id, // where: { id: user.id },
}, // data: { username: null },
}); // });
return null;
return null;
} }
} }
+1 -1
View File
@@ -52,7 +52,7 @@ export default async function verifyUser({
} }
if (STRIPE_SECRET_KEY) { if (STRIPE_SECRET_KEY) {
const subscribedUser = await verifySubscription(user); const subscribedUser = verifySubscription(user);
if (!subscribedUser) { if (!subscribedUser) {
res.status(401).json({ res.status(401).json({
+2 -2
View File
@@ -10,10 +10,10 @@
"seed": "node ./prisma/seed.js" "seed": "node ./prisma/seed.js"
}, },
"scripts": { "scripts": {
"dev": "concurrently -k -P \"next dev {@}\" \"yarn worker:dev\" --", "dev": "concurrently -k \"next dev\" \"yarn worker:dev\"",
"worker:dev": "nodemon --skip-project scripts/worker.ts", "worker:dev": "nodemon --skip-project scripts/worker.ts",
"worker:prod": "ts-node --transpile-only --skip-project scripts/worker.ts", "worker:prod": "ts-node --transpile-only --skip-project scripts/worker.ts",
"start": "concurrently -k -P \"next start {@}\" \"yarn worker:prod\" --", "start": "concurrently \"next start\" \"yarn worker:prod\"",
"build": "next build", "build": "next build",
"lint": "next lint", "lint": "next lint",
"format": "prettier --write \"**/*.{ts,tsx,js,json,md}\"" "format": "prettier --write \"**/*.{ts,tsx,js,json,md}\""
+10 -20
View File
@@ -5,7 +5,6 @@ import { useRouter } from "next/router";
import CenteredForm from "@/layouts/CenteredForm"; import CenteredForm from "@/layouts/CenteredForm";
import { Plan } from "@/types/global"; import { Plan } from "@/types/global";
import AccentSubmitButton from "@/components/AccentSubmitButton"; import AccentSubmitButton from "@/components/AccentSubmitButton";
import useAccountStore from "@/store/account";
export default function Subscribe() { export default function Subscribe() {
const [submitLoader, setSubmitLoader] = useState(false); const [submitLoader, setSubmitLoader] = useState(false);
@@ -13,8 +12,6 @@ export default function Subscribe() {
const [plan, setPlan] = useState<Plan>(1); const [plan, setPlan] = useState<Plan>(1);
const { account } = useAccountStore();
const router = useRouter(); const router = useRouter();
async function submit() { async function submit() {
@@ -30,13 +27,9 @@ export default function Subscribe() {
return ( return (
<CenteredForm <CenteredForm
text={ text={`Start with a ${
account.username process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS || 14
? "" }-day free trial, cancel anytime!`}
: `Start with a ${
process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS || 14
}-day free trial, cancel anytime!`
}
> >
<div className="p-4 mx-auto flex flex-col gap-3 justify-between max-w-[30rem] min-w-80 w-full bg-base-200 rounded-2xl shadow-md border border-neutral-content"> <div className="p-4 mx-auto flex flex-col gap-3 justify-between max-w-[30rem] min-w-80 w-full bg-base-200 rounded-2xl shadow-md border border-neutral-content">
<p className="sm:text-3xl text-2xl text-center font-extralight"> <p className="sm:text-3xl text-2xl text-center font-extralight">
@@ -44,6 +37,7 @@ export default function Subscribe() {
</p> </p>
<div className="divider my-0"></div> <div className="divider my-0"></div>
<div> <div>
<p> <p>
You will be redirected to Stripe, feel free to reach out to us at{" "} You will be redirected to Stripe, feel free to reach out to us at{" "}
@@ -53,6 +47,7 @@ export default function Subscribe() {
in case of any issue. in case of any issue.
</p> </p>
</div> </div>
<div className="flex gap-3 border border-solid border-neutral-content w-4/5 mx-auto p-1 rounded-xl relative"> <div className="flex gap-3 border border-solid border-neutral-content w-4/5 mx-auto p-1 rounded-xl relative">
<button <button
onClick={() => setPlan(Plan.monthly)} onClick={() => setPlan(Plan.monthly)}
@@ -79,6 +74,7 @@ export default function Subscribe() {
25% Off 25% Off
</div> </div>
</div> </div>
<div className="flex flex-col gap-2 justify-center items-center"> <div className="flex flex-col gap-2 justify-center items-center">
<p className="text-3xl"> <p className="text-3xl">
${plan === Plan.monthly ? "4" : "3"} ${plan === Plan.monthly ? "4" : "3"}
@@ -93,20 +89,13 @@ export default function Subscribe() {
</legend> </legend>
<p className="text-sm"> <p className="text-sm">
{account.username {process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS}-day free trial, then $
? "" {plan === Plan.monthly ? "4 per month" : "36 annually"}
: `${process.env.NEXT_PUBLIC_TRIAL_PERIOD_DAYS}-day free trial, then `}
${plan === Plan.monthly ? "4 per month" : "36 annually"}
</p> </p>
<p className="text-sm">+ VAT if applicable</p> <p className="text-sm">+ VAT if applicable</p>
</fieldset> </fieldset>
<p className="text-sm mb-5">
{account.username
? "Please note that since your trial has been previously ended, your subscription will start immediately. You can cancel anytime."
: ""}
</p>
</div> </div>
<AccentSubmitButton <AccentSubmitButton
type="button" type="button"
label="Complete Subscription!" label="Complete Subscription!"
@@ -114,6 +103,7 @@ export default function Subscribe() {
onClick={submit} onClick={submit}
loading={submitLoader} loading={submitLoader}
/> />
<div <div
onClick={() => signOut()} onClick={() => signOut()}
className="w-fit mx-auto cursor-pointer text-neutral font-semibold " className="w-fit mx-auto cursor-pointer text-neutral font-semibold "