tasko/actions/stripe-redirect/index.ts
Ahmad 7578b189ef Optimize performance and reduce bundle sizes
## AI Generated code and Descriptions
Improve website performance and efficiency by reducing bundle sizes and implementing caching strategies.

* **next.config.ts**: Add `compression` middleware to enable gzip compression for responses.
* **Lazy Loading**: Implement lazy loading for images in `app/(platform)/(dashboard)/board/[boardId]/_components/board-update-image.tsx` using the `loading="lazy"` attribute. Add `react-lazyload` library for lazy loading components in `app/(platform)/(dashboard)/board/[boardId]/_components/list-item.tsx` and wrap list items with `LazyLoad` component.
* **Database Query Optimization**: Optimize database queries in `actions/copy-card/index.ts`, `actions/copy-list/index.ts`, `actions/create-board/index.ts`, `actions/create-card/index.ts`, `actions/create-list/index.ts`, `actions/delete-board/index.ts`, `actions/delete-card/index.ts`, `actions/delete-list/index.ts`, `actions/stripe-redirect/index.ts`, `actions/update-board/index.ts`, `actions/update-card-order/index.ts`, `actions/update-card/index.ts`, and `actions/update-list/index.ts` by adding appropriate indexes and using `select` to fetch only required fields.
* **Dependencies**: Update `package.json` to include `compression` and `react-lazyload` dependencies.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ahmadk953/tasko?shareId=XXXX-XXXX-XXXX-XXXX).
2024-12-18 21:06:08 -05:00

79 lines
2.1 KiB
TypeScript

'use server';
import { auth, currentUser } from '@clerk/nextjs/server';
import { revalidatePath } from 'next/cache';
import { db } from '@/lib/db';
import { createSafeAction } from '@/lib/create-safe-action';
import { absoluteUrl } from '@/lib/utils';
import { stripe } from '@/lib/stripe';
import { InputType, ReturnType } from './types';
import { StripeRedirect } from './schema';
const handler = async (data: InputType): Promise<ReturnType> => {
const { userId, orgId } = await auth();
const user = await currentUser();
if (!userId || !orgId || !user) return { error: 'Unauthorized' };
const settingsUrl = absoluteUrl(`/organization/${orgId}`);
let url = '';
try {
const orgSubscription = await db.orgSubscription.findUnique({
where: { orgId },
select: {
stripeCustomerId: true,
},
});
if (orgSubscription?.stripeCustomerId) {
const stripeSession = await stripe.billingPortal.sessions.create({
customer: orgSubscription.stripeCustomerId,
return_url: settingsUrl,
});
url = stripeSession.url;
} else {
/* @ts-ignore */
const stripeSession = await stripe.checkout.sessions.create({
success_url: settingsUrl,
cancel_url: settingsUrl,
payment_method_types: ['card', 'paypal'],
mode: 'subscription',
billing_address_collection: 'auto',
customer_email: user.emailAddresses[0].emailAddress,
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: 'Tasko Pro',
description: 'Unlimited boards for your organization',
},
unit_amount_decimal: 99,
recurring: { interval: 'month' },
},
quantity: 1,
},
],
metadata: {
orgId,
},
});
url = stripeSession.url ?? '';
}
} catch (error) {
return {
error: 'Something went wrong',
};
}
revalidatePath(`/organization/${orgId}`);
return { data: url };
};
export const stripeRedirect = createSafeAction(StripeRedirect, handler);