mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-09 23:48:36 +01:00
add pricing
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { TProjectCount } from "@schema/ProjectsCounts";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { UserModel } from "@schema/UserSchema";
|
||||
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
|
||||
import EmailService from '@services/EmailService';
|
||||
import { requireEnv } from "../../shared/utilts/requireEnv";
|
||||
import { TProjectLimit } from "@schema/ProjectsLimits";
|
||||
|
||||
|
||||
EmailService.createTransport(
|
||||
@@ -13,7 +13,7 @@ EmailService.createTransport(
|
||||
requireEnv('EMAIL_PASS'),
|
||||
);
|
||||
|
||||
export async function checkLimitsForEmail(projectCounts: TProjectCount) {
|
||||
export async function checkLimitsForEmail(projectCounts: TProjectLimit) {
|
||||
|
||||
if ((projectCounts.visits + projectCounts.events) >= (projectCounts.limit / 2)) {
|
||||
const notify = await LimitNotifyModel.findOne({ project_id: projectCounts._id });
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
|
||||
import { Router, json } from "express";
|
||||
import { createSessionHash, getIPFromRequest } from "../../utils/Utils";
|
||||
import { checkProjectCount } from "@functions/UtilsProjectCounts";
|
||||
|
||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||
import { EVENT_LOG_LIMIT_PERCENT } from '@data/broker/Limits';
|
||||
@@ -13,6 +12,8 @@ import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { ProjectCountModel } from "@schema/ProjectsCounts";
|
||||
import { checkLimitsForEmail } from "../../Controller";
|
||||
import { ProjectLimitModel } from "@schema/ProjectsLimits";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -55,13 +56,21 @@ router.post('/metrics/push', json(jsonOptions), async (req, res) => {
|
||||
|
||||
const { pid } = req.body;
|
||||
|
||||
const projectCounts = await checkProjectCount(pid);
|
||||
const projectExist = await ProjectModel.exists({ _id: pid });
|
||||
if (!projectExist) return res.status(400).json({ error: 'Project not exist' });
|
||||
|
||||
const projectLimits = await ProjectLimitModel.findOne({ project_id: pid });
|
||||
|
||||
if (!projectLimits) return res.status(400).json({ error: 'No limits found' });
|
||||
|
||||
const TOTAL_COUNT = projectLimits.events + projectLimits.visits;
|
||||
const COUNT_LIMIT = projectLimits.limit;
|
||||
if ((TOTAL_COUNT * EVENT_LOG_LIMIT_PERCENT) > COUNT_LIMIT) {
|
||||
return res.status(200).json({ error: 'Limit reached' });
|
||||
};
|
||||
await checkLimitsForEmail(projectLimits);
|
||||
|
||||
const TOTAL_COUNT = projectCounts.events + projectCounts.visits;
|
||||
const LIMIT = projectCounts.limit;
|
||||
if ((TOTAL_COUNT * EVENT_LOG_LIMIT_PERCENT) > LIMIT) return;
|
||||
|
||||
await checkLimitsForEmail(projectCounts);
|
||||
|
||||
const ip = getIPFromRequest(req);
|
||||
|
||||
@@ -113,7 +122,11 @@ router.post('/metrics/push', json(jsonOptions), async (req, res) => {
|
||||
|
||||
const fieldToInc = type === EventType.VISIT ? 'visits' : 'events';
|
||||
|
||||
await ProjectCountModel.updateOne({ _id: projectCounts._id }, { $inc: { [fieldToInc]: 1 } });
|
||||
await ProjectCountModel.updateOne({ project_id: pid }, { $inc: { [fieldToInc]: 1 } }, { upsert: true });
|
||||
|
||||
await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { [fieldToInc]: 1 } });
|
||||
|
||||
|
||||
|
||||
return res.sendStatus(200);
|
||||
|
||||
|
||||
@@ -5,16 +5,22 @@ export type PricingCardProp = {
|
||||
cost: string,
|
||||
features: string[],
|
||||
desc: string,
|
||||
active: boolean
|
||||
active: boolean,
|
||||
planId: number
|
||||
}
|
||||
|
||||
const props = defineProps<{ data: PricingCardProp }>();
|
||||
|
||||
const activeProject = useActiveProject();
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
function onUpgradeClick() {
|
||||
router.push('/book_demo')
|
||||
async function onUpgradeClick() {
|
||||
const res = await $fetch<string>(`/api/pay/${activeProject.value?._id.toString()}/create`, {
|
||||
...signHeaders({ 'content-type': 'application/json' }),
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ planId: props.data.planId })
|
||||
})
|
||||
if (!res) alert('Something went wrong');
|
||||
window.open(res);
|
||||
}
|
||||
|
||||
</script>
|
||||
@@ -37,7 +43,8 @@ function onUpgradeClick() {
|
||||
<div v-if="data.active" class="text-[1rem] bg-[#1f1f22] rounded-md py-2 text-center">
|
||||
Current active plan
|
||||
</div>
|
||||
<div @click="onUpgradeClick()" v-if="!data.active" class="cursor-pointer text-[1rem] font-semibold bg-[#3a3af5] rounded-md py-2 text-center">
|
||||
<div @click="onUpgradeClick()" v-if="!data.active"
|
||||
class="cursor-pointer text-[1rem] font-semibold bg-[#3a3af5] rounded-md py-2 text-center">
|
||||
Upgrade
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -22,7 +22,8 @@ const starterTierCardData = ref<PricingCardProp>({
|
||||
can experience some data loss.To have a
|
||||
dedicated server we suggest to upgrade the
|
||||
plan to an higher one!`,
|
||||
active: activeProject.value?.premium === false
|
||||
active: activeProject.value?.premium === false,
|
||||
planId: 0
|
||||
});
|
||||
|
||||
const accelerationTierCardData = ref<PricingCardProp>({
|
||||
@@ -39,7 +40,8 @@ const accelerationTierCardData = ref<PricingCardProp>({
|
||||
"Low priority email support"
|
||||
],
|
||||
desc: `Your project is entering a growth phase. We simplify data analysis for you. For more support, try our Expansion plan—it's worth it!`,
|
||||
active: activeProject.value?.premium_type === 1
|
||||
active: activeProject.value?.premium_type === 1,
|
||||
planId: 1
|
||||
});
|
||||
|
||||
const expansionTierCardData = ref<PricingCardProp>({
|
||||
@@ -56,7 +58,8 @@ const expansionTierCardData = ref<PricingCardProp>({
|
||||
"high priority email support"
|
||||
],
|
||||
desc: `We will support you with everything we can offer and give you the full power of our service. If you need more space and are growing, contact us for a custom offer!`,
|
||||
active: activeProject.value?.premium_type === 2
|
||||
active: activeProject.value?.premium_type === 2,
|
||||
planId: 2
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
@@ -75,17 +75,6 @@ function onHideClicked() {
|
||||
|
||||
const activeProject = useActiveProject();
|
||||
|
||||
async function payment() {
|
||||
// const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/create`, {
|
||||
// ...signHeaders({ 'content-type': 'application/json' }),
|
||||
// method: 'POST',
|
||||
// body: JSON.stringify({ planId: 1 })
|
||||
// })
|
||||
// console.log(res);
|
||||
const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/invoices`, signHeaders())
|
||||
console.log(res);
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
@@ -93,7 +82,7 @@ async function payment() {
|
||||
<div class="bg-bg overflow-y-auto w-full h-dvh p-6 gap-6 flex flex-col">
|
||||
|
||||
|
||||
<div @click="payment()" v-if="!isAdminHidden"
|
||||
<div @click="onHideClicked()" v-if="!isAdminHidden"
|
||||
class="bg-menu hover:bg-menu/70 cursor-pointer flex gap-2 rounded-lg w-fit px-6 py-4 text-text-sub">
|
||||
<div class="text-text-sub/90"> <i class="far fa-eye"></i> </div>
|
||||
<div> Nascondi dalla barra </div>
|
||||
|
||||
@@ -107,6 +107,9 @@ function openInvoice(link: string) {
|
||||
</div>
|
||||
<div class="poppins"> {{ daysLeft }} days left </div>
|
||||
</div>
|
||||
<div class="flex justify-center">
|
||||
Subscription: {{ planData.subscription_status }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="my-4 w-full bg-gray-400/30 h-[1px]">
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { ProjectLimitModel } from "@schema/ProjectsLimits";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { checkProjectCount } from '@functions/UtilsProjectCounts';
|
||||
|
||||
export async function getAiChatRemainings(project_id: string) {
|
||||
const counts = await checkProjectCount(project_id)
|
||||
if (!counts) return 0;
|
||||
const chatsRemaining = counts.ai_limit - counts.ai_messages;
|
||||
const limits = await ProjectLimitModel.findOne({ _id: project_id })
|
||||
if (!limits) return 0;
|
||||
const chatsRemaining = limits.ai_limit - limits.ai_messages;
|
||||
if (isNaN(chatsRemaining)) return 0;
|
||||
return chatsRemaining;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ import { OAuth2Client } from 'google-auth-library';
|
||||
import { createUserJwt } from '~/server/AuthManager';
|
||||
import { UserModel } from '@schema/UserSchema';
|
||||
import EmailService from '@services/EmailService';
|
||||
import { ProjectModel } from '@schema/ProjectSchema';
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
const { GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig()
|
||||
|
||||
@@ -11,6 +13,8 @@ const client = new OAuth2Client({
|
||||
clientSecret: GOOGLE_AUTH_CLIENT_SECRET
|
||||
});
|
||||
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const body = await readBody(event)
|
||||
|
||||
@@ -33,8 +37,10 @@ export default defineEventHandler(async event => {
|
||||
|
||||
|
||||
const user = await UserModel.findOne({ email: payload.email });
|
||||
|
||||
if (user) return { error: false, access_token: createUserJwt({ email: user.email, name: user.name }) }
|
||||
|
||||
|
||||
const newUser = new UserModel({
|
||||
email: payload.email,
|
||||
given_name: payload.given_name,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { PREMIUM_PLANS, STRIPE_PLANS } from "@data/PREMIUM_LIMITS";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { getPlanFromId } from "@data/PREMIUM";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
@@ -17,24 +16,22 @@ export default defineEventHandler(async event => {
|
||||
|
||||
const { planId } = body;
|
||||
|
||||
const plan = PREMIUM_PLANS.find(e => e.id == planId);
|
||||
const PLAN = getPlanFromId(planId);
|
||||
|
||||
if (!plan) {
|
||||
if (!PLAN) {
|
||||
console.error('PLAN', planId, 'NOT EXIST');
|
||||
return setResponseStatus(event, 400, 'Plan not exist');
|
||||
}
|
||||
|
||||
const { price } = STRIPE_PLANS[plan.tag];
|
||||
|
||||
const checkout = await StripeService.cretePayment(
|
||||
price,
|
||||
PLAN.PRICE,
|
||||
'https://dashboard.litlyx.com/payment_ok',
|
||||
project_id,
|
||||
project.customer_id
|
||||
);
|
||||
|
||||
if (!checkout) {
|
||||
console.error('Cannot create payment', { plan, price });
|
||||
console.error('Cannot create payment', { plan: PLAN });
|
||||
return setResponseStatus(event, 400, 'Cannot create payment');
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
|
||||
@@ -21,17 +22,21 @@ export default defineEventHandler(async event => {
|
||||
|
||||
if (!project.customer_id) return [];
|
||||
|
||||
const invoices = await StripeService.getInvoices(project.customer_id);
|
||||
return await Redis.useCache({ key: `invoices:${project_id}`, exp: 10 }, async () => {
|
||||
|
||||
return invoices?.data.map(e => {
|
||||
const result: InvoiceData = {
|
||||
link: e.invoice_pdf || '',
|
||||
id: e.number || '',
|
||||
date: e.created * 1000,
|
||||
status: e.status || 'NO_STATUS',
|
||||
cost: e.amount_due
|
||||
}
|
||||
return result;
|
||||
})
|
||||
const invoices = await StripeService.getInvoices(project.customer_id);
|
||||
|
||||
return invoices?.data.map(e => {
|
||||
const result: InvoiceData = {
|
||||
link: e.invoice_pdf || '',
|
||||
id: e.number || '',
|
||||
date: e.created * 1000,
|
||||
status: e.status || 'NO_STATUS',
|
||||
cost: e.amount_due
|
||||
}
|
||||
return result;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -2,69 +2,187 @@
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
import type Event from 'stripe';
|
||||
import { ProjectModel } from '@schema/ProjectSchema';
|
||||
import { PREMIUM_LIMITS, getPlanFromPremiumTag, getPlanTagFromStripePrice } from '@data/PREMIUM_LIMITS';
|
||||
import { PREMIUM_PLAN, getPlanFromPrice } from '@data/PREMIUM';
|
||||
import { ProjectCountModel } from '@schema/ProjectsCounts';
|
||||
import { ProjectLimitModel } from '@schema/ProjectsLimits';
|
||||
import { UserModel } from '@schema/UserSchema';
|
||||
|
||||
async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||
|
||||
if (event.data.object.status === 'paid') {
|
||||
// if (event.data.object.status === 'paid') {
|
||||
|
||||
const pid = event.data.object.subscription_details?.metadata?.pid;
|
||||
// const data = event.data.object;
|
||||
|
||||
const project = await ProjectModel.findById(pid);
|
||||
if (!project) return { error: 'Project not found' }
|
||||
// const pid = data.subscription_details?.metadata?.pid;
|
||||
// if (!pid) return { error: 'ProjectId not found' }
|
||||
|
||||
const subscriptionId = event.data.object.subscription;
|
||||
if (!subscriptionId) return { error: 'SubscriptionId not found' }
|
||||
// const project = await ProjectModel.findById(pid);
|
||||
// if (!project) return { error: 'Project not found' }
|
||||
|
||||
const price = event.data.object.lines.data[0].plan?.id;
|
||||
if (!price) return { error: 'Price not found' }
|
||||
// const price = data.lines.data[0].plan?.id;
|
||||
// if (!price) return { error: 'Price not found' }
|
||||
|
||||
const premiumTag = getPlanTagFromStripePrice(price);
|
||||
if (!premiumTag) return { error: 'Premium tag not found' }
|
||||
// const PLAN = getPlanFromPrice(price);
|
||||
// if (!PLAN) return { error: 'Plan not found' }
|
||||
|
||||
const plan = getPlanFromPremiumTag(premiumTag);
|
||||
if (!plan) return { error: 'Plan not found' }
|
||||
// await ProjectModel.updateOne({ _id: pid }, {
|
||||
// premium: true,
|
||||
// customer_id: data.customer,
|
||||
// premium_type: PLAN.ID,
|
||||
// premium_expire_at: data.lines.data[0].period.end * 1000
|
||||
// });
|
||||
|
||||
await ProjectModel.updateOne({ _id: pid }, {
|
||||
premium: true,
|
||||
customer_id: event.data.object.customer,
|
||||
premium_type: plan.id,
|
||||
premium_expire_at: event.data.object.lines.data[0].period.end * 1000
|
||||
});
|
||||
// await ProjectCountModel.create({
|
||||
// project_id: project._id,
|
||||
// events: 0,
|
||||
// visits: 0,
|
||||
// ai_messages: 0,
|
||||
// limit: PLAN.COUNT_LIMIT,
|
||||
// ai_limit: PLAN.AI_MESSAGE_LIMIT,
|
||||
// billing_start_at: event.data.object.lines.data[0].period.start * 1000,
|
||||
// billing_expire_at: event.data.object.lines.data[0].period.end * 1000,
|
||||
// });
|
||||
|
||||
const limits = PREMIUM_LIMITS[premiumTag];
|
||||
|
||||
await ProjectCountModel.create({
|
||||
project_id: project._id,
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
limit: limits.COUNT_LIMIT,
|
||||
ai_limit: limits.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: event.data.object.lines.data[0].period.start * 1000,
|
||||
billing_expire_at: event.data.object.lines.data[0].period.end * 1000,
|
||||
});
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
// return { ok: true }
|
||||
// }
|
||||
|
||||
return { received: true }
|
||||
}
|
||||
|
||||
async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) {
|
||||
return { received: true }
|
||||
|
||||
const project = await ProjectModel.findOne({ customer_id: event.data.object.customer });
|
||||
if (!project) return { error: 'Project not found' }
|
||||
|
||||
const price = event.data.object.items.data[0].price.id;
|
||||
if (!price) return { error: 'Price not found' }
|
||||
|
||||
const PLAN = getPlanFromPrice(price);
|
||||
if (!PLAN) return { error: 'Plan not found' }
|
||||
|
||||
|
||||
if (project.subscription_id != event.data.object.id) {
|
||||
await StripeService.deleteSubscription(project.subscription_id);
|
||||
}
|
||||
|
||||
project.premium = PLAN.ID != 0;
|
||||
project.premium_type = PLAN.ID;
|
||||
project.subscription_id = event.data.object.id;
|
||||
project.premium_expire_at = new Date(event.data.object.current_period_end * 1000);
|
||||
|
||||
await Promise.all([
|
||||
|
||||
project.save(),
|
||||
|
||||
ProjectLimitModel.updateOne({ project_id: project._id }, {
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
limit: PLAN.COUNT_LIMIT,
|
||||
ai_limit: PLAN.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: event.data.object.current_period_start * 1000,
|
||||
billing_expire_at: event.data.object.current_period_end * 1000,
|
||||
}, { upsert: true })
|
||||
|
||||
]);
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
async function onSubscriptionDeleted(event: Event.CustomerSubscriptionDeletedEvent) {
|
||||
return { received: true }
|
||||
|
||||
const project = await ProjectModel.findOne({
|
||||
customer_id: event.data.object.customer,
|
||||
subscription_id: event.data.object.id
|
||||
});
|
||||
|
||||
if (!project) return { error: 'Project not found' }
|
||||
|
||||
const PLAN = PREMIUM_PLAN['FREE'];
|
||||
|
||||
const targetCustomer = await StripeService.getCustomer(project.customer_id);
|
||||
|
||||
let customer: Event.Customer;
|
||||
|
||||
if (!targetCustomer.deleted) {
|
||||
customer = targetCustomer;
|
||||
} else {
|
||||
const user = await UserModel.findById(project._id, { email: 1 });
|
||||
if (!user) return { error: 'User not found' }
|
||||
const newCustomer = await StripeService.createCustomer(user.email);
|
||||
customer = newCustomer;
|
||||
}
|
||||
|
||||
const freeSubscription = await StripeService.createFreeSubscription(customer.id);
|
||||
|
||||
|
||||
project.premium = false;
|
||||
project.premium_type = PLAN.ID;
|
||||
project.subscription_id = freeSubscription.id;
|
||||
project.premium_expire_at = new Date(freeSubscription.current_period_end * 1000);
|
||||
|
||||
|
||||
await Promise.all([
|
||||
|
||||
project.save(),
|
||||
|
||||
ProjectLimitModel.updateOne({ project_id: project._id }, {
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
limit: PLAN.COUNT_LIMIT,
|
||||
ai_limit: PLAN.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: event.data.object.current_period_start * 1000,
|
||||
billing_expire_at: event.data.object.current_period_end * 1000,
|
||||
}, { upsert: true })
|
||||
|
||||
]);
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
async function onSubscriptionUpdated(event: Event.CustomerSubscriptionUpdatedEvent) {
|
||||
return { received: true }
|
||||
|
||||
const project = await ProjectModel.findOne({
|
||||
customer_id: event.data.object.customer,
|
||||
subscription_id: event.data.object.id
|
||||
});
|
||||
|
||||
if (!project) return { error: 'Project not found' }
|
||||
|
||||
const price = event.data.object.items.data[0].price.id;
|
||||
if (!price) return { error: 'Price not found' }
|
||||
|
||||
const PLAN = getPlanFromPrice(price);
|
||||
if (!PLAN) return { error: 'Plan not found' }
|
||||
|
||||
project.premium = PLAN.ID != 0;
|
||||
project.premium_type = PLAN.ID;
|
||||
project.subscription_id = event.data.object.id;
|
||||
project.premium_expire_at = new Date(event.data.object.current_period_end * 1000);
|
||||
|
||||
await Promise.all([
|
||||
|
||||
project.save(),
|
||||
|
||||
ProjectLimitModel.updateOne({ project_id: project._id }, {
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
limit: PLAN.COUNT_LIMIT,
|
||||
ai_limit: PLAN.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: event.data.object.current_period_start * 1000,
|
||||
billing_expire_at: event.data.object.current_period_end * 1000,
|
||||
}, { upsert: true })
|
||||
|
||||
]);
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const body = await readRawBody(event);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -15,9 +16,23 @@ export default defineEventHandler(async event => {
|
||||
const existingUserProjects = await ProjectModel.countDocuments({ owner: userData.id });
|
||||
if (existingUserProjects == 3) return setResponseStatus(event, 400, 'Already have 3 projects');
|
||||
|
||||
const newProject = new ProjectModel({ owner: userData.id, name: newProjectName });
|
||||
const saved = await newProject.save();
|
||||
const customer = await StripeService.createCustomer(userData.user.email);
|
||||
if (!customer) return setResponseStatus(event, 400, 'Error creating customer');
|
||||
|
||||
return saved.toJSON() as TProject;
|
||||
const subscription = await StripeService.createFreeSubscription(customer.id);
|
||||
if (!subscription) return setResponseStatus(event, 400, 'Error creating subscription');
|
||||
|
||||
const project = await ProjectModel.create({
|
||||
owner: userData.id,
|
||||
name: newProjectName,
|
||||
premium: false,
|
||||
premium_type: 0,
|
||||
customer_id: customer.id,
|
||||
subscription_id: subscription.id,
|
||||
premium_expire_at: subscription.current_period_end * 1000
|
||||
});
|
||||
|
||||
|
||||
return project.toJSON() as TProject;
|
||||
|
||||
});
|
||||
@@ -1,4 +1,7 @@
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { ProjectCountModel } from "@schema/ProjectsCounts";
|
||||
import { ProjectLimitModel } from "@schema/ProjectsLimits";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -9,10 +12,29 @@ export default defineEventHandler(async event => {
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged');
|
||||
|
||||
const project = await ProjectModel.findById(projectId);
|
||||
if (!project) return setResponseStatus(event, 400, 'Project not exist');
|
||||
|
||||
const projects = await ProjectModel.countDocuments({ owner: userData.id });
|
||||
if (projects == 1) return setResponseStatus(event, 400, 'Cannot delete last project');
|
||||
|
||||
const deletation = await ProjectModel.deleteOne({ owner: userData.id, _id: projectId });
|
||||
return { ok: deletation.acknowledged };
|
||||
if (project.premium === true) return setResponseStatus(event, 400, 'Cannot delete premium project');
|
||||
|
||||
await StripeService.deleteCustomer(project.customer_id);
|
||||
|
||||
const countDeletation = await ProjectCountModel.deleteOne({ owner: userData.id, _id: projectId });
|
||||
const limitdeletation = await ProjectLimitModel.deleteOne({ owner: userData.id, _id: projectId });
|
||||
const projectDeletation = await ProjectModel.deleteOne({ owner: userData.id, _id: projectId });
|
||||
|
||||
const ok = countDeletation.acknowledged && limitdeletation.acknowledged && projectDeletation.acknowledged
|
||||
|
||||
return {
|
||||
ok,
|
||||
data: [
|
||||
countDeletation.acknowledged,
|
||||
limitdeletation.acknowledged,
|
||||
projectDeletation.acknowledged
|
||||
]
|
||||
};
|
||||
|
||||
});
|
||||
@@ -1,8 +1,7 @@
|
||||
import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
import { ProjectCountModel } from "@schema/ProjectsCounts";
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { ProjectLimitModel } from "@schema/ProjectsLimits";
|
||||
import { UserSettingsModel } from "@schema/UserSettings";
|
||||
|
||||
const { BROKER_UPDATE_EXPIRE_TIME_PATH } = useRuntimeConfig();
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -17,25 +16,20 @@ export default defineEventHandler(async event => {
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return setResponseStatus(event, 400, 'Project not found');
|
||||
|
||||
const subscription = await StripeService.getSubscription(project.subscription_id);
|
||||
|
||||
let projectCounts = await ProjectCountModel.findOne({ project_id }, {}, {
|
||||
sort: { billing_expire_at: -1 }
|
||||
});
|
||||
const projectLimits = await ProjectLimitModel.findOne({ project_id });
|
||||
if (!projectLimits) return setResponseStatus(event, 400, 'Project limits not found');
|
||||
|
||||
if (!projectCounts || Date.now() > new Date(projectCounts.billing_expire_at).getTime()) {
|
||||
await fetch(BROKER_UPDATE_EXPIRE_TIME_PATH + project._id.toString());
|
||||
projectCounts = await ProjectCountModel.findOne({ project_id }, {}, { sort: { billing_expire_at: -1 } });
|
||||
}
|
||||
|
||||
if (!projectCounts) return setResponseStatus(event, 400, 'Project counts not found');
|
||||
|
||||
const result = {
|
||||
premium: project.premium,
|
||||
premium_type: project.premium_type,
|
||||
billing_start_at: projectCounts.billing_start_at,
|
||||
billing_expire_at: projectCounts.billing_expire_at,
|
||||
limit: projectCounts.limit,
|
||||
count: projectCounts.events + projectCounts.visits,
|
||||
billing_start_at: projectLimits.billing_start_at,
|
||||
billing_expire_at: projectLimits.billing_expire_at,
|
||||
limit: projectLimits.limit,
|
||||
count: projectLimits.events + projectLimits.visits,
|
||||
subscription_status: subscription.status
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -5,7 +5,7 @@ import OpenAI from "openai";
|
||||
import { AiChatModel } from '@schema/ai/AiChatSchema';
|
||||
import { AI_EventsFunctions, AI_EventsTools } from '../api/ai/functions/AI_Events';
|
||||
import { ProjectCountModel } from '@schema/ProjectsCounts';
|
||||
import { getCurrentProjectCountId } from '@functions/UtilsProjectCounts';
|
||||
import { ProjectLimitModel } from '@schema/ProjectsLimits';
|
||||
|
||||
const { AI_ORG, AI_PROJECT, AI_KEY } = useRuntimeConfig();
|
||||
|
||||
@@ -135,9 +135,7 @@ export async function sendMessageOnChat(text: string, pid: string, initial_chat_
|
||||
|
||||
}
|
||||
|
||||
const currentCountId = await getCurrentProjectCountId(pid);
|
||||
if (!currentCountId) console.error('Project not exist');
|
||||
await ProjectCountModel.updateOne({ _id: currentCountId }, { $inc: { ai_messages: 1 } })
|
||||
await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { ai_messages: 1 } })
|
||||
|
||||
return responseMessage.content;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { getPlanFromTag } from '@data/PREMIUM';
|
||||
import Stripe from 'stripe';
|
||||
|
||||
class StripeService {
|
||||
@@ -10,11 +11,9 @@ class StripeService {
|
||||
this.webhookSecret = webhookSecret;
|
||||
this.stripe = new Stripe(this.privateKey);
|
||||
}
|
||||
|
||||
parseWebhook(body: any, sig: string) {
|
||||
if (!this.stripe) {
|
||||
console.error('Stripe not initialized')
|
||||
return;
|
||||
}
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
if (!this.webhookSecret) {
|
||||
console.error('Stripe not initialized')
|
||||
return;
|
||||
@@ -23,10 +22,7 @@ class StripeService {
|
||||
}
|
||||
|
||||
async cretePayment(price: string, success_url: string, pid: string, customer?: string) {
|
||||
if (!this.stripe) {
|
||||
console.error('Stripe not initialized')
|
||||
return;
|
||||
}
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
|
||||
const checkout = await this.stripe.checkout.sessions.create({
|
||||
payment_method_types: ['card'],
|
||||
@@ -44,11 +40,14 @@ class StripeService {
|
||||
return checkout;
|
||||
}
|
||||
|
||||
async deleteSubscription(subscriptionId: string) {
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
const subscription = await this.stripe.subscriptions.cancel(subscriptionId);
|
||||
return subscription;
|
||||
}
|
||||
|
||||
async getSubscription(subscriptionId: string) {
|
||||
if (!this.stripe) {
|
||||
console.error('Stripe not initialized')
|
||||
return;
|
||||
}
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
|
||||
return subscription;
|
||||
}
|
||||
@@ -57,6 +56,45 @@ class StripeService {
|
||||
const invoices = await this.stripe?.invoices.list({ customer: customer_id });
|
||||
return invoices;
|
||||
}
|
||||
|
||||
|
||||
async getCustomer(customer_id: string) {
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
const customer = await this.stripe.customers.retrieve(customer_id, { expand: [] })
|
||||
return customer;
|
||||
}
|
||||
|
||||
async createCustomer(email: string) {
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
const customer = await this.stripe.customers.create({ email });
|
||||
return customer;
|
||||
}
|
||||
|
||||
async deleteCustomer(customer_id: string) {
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
const { deleted } = await this.stripe.customers.del(customer_id);
|
||||
return deleted;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
async createFreeSubscription(customer_id: string) {
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
|
||||
const FREE_PLAN = getPlanFromTag('FREE');
|
||||
|
||||
const subscription = await this.stripe.subscriptions.create({
|
||||
customer: customer_id,
|
||||
items: [
|
||||
{ price: FREE_PLAN.PRICE, quantity: 1 }
|
||||
]
|
||||
});
|
||||
|
||||
return subscription;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const instance = new StripeService();
|
||||
|
||||
66
shared/data/PREMIUM.ts
Normal file
66
shared/data/PREMIUM.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
export type PREMIUM_TAG = typeof PREMIUM_TAGS[number];
|
||||
|
||||
export const PREMIUM_TAGS = [
|
||||
'FREE', 'PLAN_1', 'PLAN_2', 'PLAN_3', 'PLAN_99'
|
||||
] as const;
|
||||
|
||||
|
||||
export type PREMIUM_DATA = {
|
||||
COUNT_LIMIT: number,
|
||||
AI_MESSAGE_LIMIT: number,
|
||||
PRICE: string,
|
||||
ID: number
|
||||
}
|
||||
|
||||
export const PREMIUM_PLAN: Record<PREMIUM_TAG, PREMIUM_DATA> = {
|
||||
FREE: {
|
||||
ID: 0,
|
||||
COUNT_LIMIT: 3_000,
|
||||
AI_MESSAGE_LIMIT: 10,
|
||||
PRICE: 'price_1PNbHYB2lPUiVs9VZP32xglF'
|
||||
},
|
||||
PLAN_1: {
|
||||
ID: 1,
|
||||
COUNT_LIMIT: 150_000,
|
||||
AI_MESSAGE_LIMIT: 100,
|
||||
PRICE: 'price_1PNZjVB2lPUiVs9VrsTbJL04'
|
||||
},
|
||||
PLAN_2: {
|
||||
ID: 2,
|
||||
COUNT_LIMIT: 500_000,
|
||||
AI_MESSAGE_LIMIT: 5_000,
|
||||
PRICE: ''
|
||||
},
|
||||
PLAN_3: {
|
||||
ID: 3,
|
||||
COUNT_LIMIT: 2_000_000,
|
||||
AI_MESSAGE_LIMIT: 10_000,
|
||||
PRICE: ''
|
||||
},
|
||||
PLAN_99: {
|
||||
ID: 99,
|
||||
COUNT_LIMIT: 10_000_000,
|
||||
AI_MESSAGE_LIMIT: 100_000,
|
||||
PRICE: ''
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function getPlanFromTag(tag: PREMIUM_TAG) {
|
||||
return PREMIUM_PLAN[tag];
|
||||
}
|
||||
|
||||
export function getPlanFromId(id: number) {
|
||||
for (const tag of PREMIUM_TAGS) {
|
||||
const plan = getPlanFromTag(tag);
|
||||
if (plan.ID === id) return plan;
|
||||
}
|
||||
}
|
||||
|
||||
export function getPlanFromPrice(price: string) {
|
||||
for (const tag of PREMIUM_TAGS) {
|
||||
const plan = getPlanFromTag(tag);
|
||||
if (plan.PRICE === price) return plan;
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
|
||||
|
||||
export const PREMIUM_PLANS = [
|
||||
{ id: 0, tag: 'FREE', name: 'Free' },
|
||||
{ id: 1, tag: 'PLAN_1', name: 'Premium 1' },
|
||||
{ id: 2, tag: 'PLAN_2', name: 'Premium 2' },
|
||||
{ id: 3, tag: 'PLAN_3', name: 'Premium 3' },
|
||||
{ id: 99, tag: 'PLAN_99', name: 'Premium 99' },
|
||||
] as const;
|
||||
|
||||
export function getPlanFromPremiumType(premium_type?: number) {
|
||||
if (!premium_type) return PREMIUM_PLANS[0];
|
||||
const plan = PREMIUM_PLANS.find(e => e.id === premium_type);
|
||||
if (!plan) return PREMIUM_PLANS[0];
|
||||
return plan;
|
||||
}
|
||||
|
||||
export function getPlanFromPremiumTag(tag: PREMIUM_PLAN_TAG) {
|
||||
const plan = PREMIUM_PLANS.find(e => e.tag === tag);
|
||||
return plan;
|
||||
}
|
||||
|
||||
export type PREMIUM_PLAN_TAG = typeof PREMIUM_PLANS[number]['tag'];
|
||||
|
||||
export type PROJECT_LIMIT = {
|
||||
COUNT_LIMIT: number,
|
||||
AI_MESSAGE_LIMIT: number,
|
||||
}
|
||||
|
||||
export const PREMIUM_LIMITS: Record<PREMIUM_PLAN_TAG, PROJECT_LIMIT> = {
|
||||
FREE: {
|
||||
COUNT_LIMIT: 3_000,
|
||||
AI_MESSAGE_LIMIT: 10
|
||||
},
|
||||
PLAN_1: {
|
||||
COUNT_LIMIT: 150_000,
|
||||
AI_MESSAGE_LIMIT: 100
|
||||
},
|
||||
PLAN_2: {
|
||||
COUNT_LIMIT: 500_000,
|
||||
AI_MESSAGE_LIMIT: 5_000
|
||||
},
|
||||
PLAN_3: {
|
||||
COUNT_LIMIT: 2_000_000,
|
||||
AI_MESSAGE_LIMIT: 10_000
|
||||
},
|
||||
PLAN_99: {
|
||||
COUNT_LIMIT: 10_000_000,
|
||||
AI_MESSAGE_LIMIT: 100_000
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export type STRIPE_PLAN = {
|
||||
price: string
|
||||
}
|
||||
|
||||
export const STRIPE_PLANS: Record<PREMIUM_PLAN_TAG, STRIPE_PLAN> = {
|
||||
FREE: {
|
||||
price: 'price_1PNbHYB2lPUiVs9VZP32xglF'
|
||||
},
|
||||
PLAN_1: {
|
||||
price: 'price_1PNZjVB2lPUiVs9VrsTbJL04'
|
||||
},
|
||||
PLAN_2: {
|
||||
price: ''
|
||||
},
|
||||
PLAN_3: {
|
||||
price: ''
|
||||
},
|
||||
PLAN_99: {
|
||||
price: ''
|
||||
}
|
||||
}
|
||||
|
||||
export function getPlanTagFromStripePrice(price: string): PREMIUM_PLAN_TAG | undefined {
|
||||
for (const plan of PREMIUM_PLANS.map(e => e.tag)) {
|
||||
const stripePrice = STRIPE_PLANS[plan].price;
|
||||
if (stripePrice === price) return plan;
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import { ProjectCountModel } from '../schema/ProjectsCounts';
|
||||
import { ProjectModel } from '../schema/ProjectSchema';
|
||||
import { LimitNotifyModel } from '../schema/broker/LimitNotifySchema';
|
||||
import { PREMIUM_LIMITS, getPlanFromPremiumType } from '../data/PREMIUM_LIMITS';
|
||||
import { MONTH } from '../utilts/TIME';
|
||||
|
||||
|
||||
export async function getCurrentProjectCountId(project_id: string) {
|
||||
const projectCount = await ProjectCountModel.findOne({ project_id }, { _id: 1 }, { sort: { billing_expire_at: -1 } });
|
||||
return projectCount?._id.toString();
|
||||
}
|
||||
|
||||
export async function getAllLimitsFromProjectId(project_id: string) {
|
||||
const targetProject = await ProjectModel.findById(project_id, {
|
||||
premium: 1, premium_type: 1, premium_expire_at: 1
|
||||
});
|
||||
if (!targetProject) return PREMIUM_LIMITS.FREE;
|
||||
if (!targetProject.premium) return PREMIUM_LIMITS.FREE;
|
||||
const plan = getPlanFromPremiumType(targetProject.premium_type);
|
||||
return PREMIUM_LIMITS[plan.tag];
|
||||
}
|
||||
|
||||
export async function checkProjectCount(project_id: string) {
|
||||
|
||||
const targetProject = await ProjectModel.findById(project_id, {
|
||||
premium: 1, premium_type: 1, premium_expire_at: 1
|
||||
});
|
||||
|
||||
if (!targetProject) return;
|
||||
|
||||
if (new Date(targetProject.premium_expire_at).getTime() < Date.now()) {
|
||||
await ProjectModel.updateOne({ _id: project_id }, {
|
||||
premium: false,
|
||||
$unset: {
|
||||
premium_type: 1,
|
||||
premium_expire_at: 1
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const limits = await getAllLimitsFromProjectId(project_id);
|
||||
|
||||
const projectCounts = await ProjectCountModel.findOne({ project_id }, {}, { sort: { billing_expire_at: -1 } });
|
||||
|
||||
const billingExpireAt = projectCounts ? new Date(projectCounts.billing_expire_at).getTime() : -1;
|
||||
|
||||
if (projectCounts && Date.now() < billingExpireAt) {
|
||||
if (projectCounts.ai_limit) return projectCounts.toJSON();
|
||||
projectCounts.ai_limit = limits.AI_MESSAGE_LIMIT;
|
||||
const saved = await projectCounts.save();
|
||||
return saved.toJSON();
|
||||
}
|
||||
|
||||
const newProjectCounts = await ProjectCountModel.create({
|
||||
project_id,
|
||||
events: 0,
|
||||
visits: 0,
|
||||
limit: limits.COUNT_LIMIT,
|
||||
ai_messages: 0,
|
||||
ai_limit: limits.AI_MESSAGE_LIMIT,
|
||||
billing_start_at: projectCounts ? billingExpireAt : Date.now(),
|
||||
billing_expire_at: (projectCounts ? billingExpireAt : Date.now()) + MONTH
|
||||
});
|
||||
|
||||
await LimitNotifyModel.updateOne({ project_id }, { limit1: false, limit2: false, limit3: false });
|
||||
|
||||
return newProjectCounts.toJSON();
|
||||
|
||||
|
||||
}
|
||||
@@ -5,8 +5,9 @@ export type TProject = {
|
||||
owner: Schema.Types.ObjectId,
|
||||
name: string,
|
||||
premium: boolean,
|
||||
premium_type?: number,
|
||||
customer_id?: string,
|
||||
premium_type: number,
|
||||
customer_id: string,
|
||||
subscription_id: string,
|
||||
premium_expire_at: Date,
|
||||
created_at: Date
|
||||
}
|
||||
@@ -15,9 +16,10 @@ const ProjectSchema = new Schema<TProject>({
|
||||
owner: { type: Types.ObjectId, index: 1 },
|
||||
name: { type: String, required: true },
|
||||
premium: { type: Boolean, default: false },
|
||||
premium_type: { type: Number },
|
||||
customer_id: { type: String },
|
||||
premium_expire_at: { type: Date },
|
||||
premium_type: { type: Number, default: 0 },
|
||||
customer_id: { type: String, required: true },
|
||||
subscription_id: { type: String, required: true },
|
||||
premium_expire_at: { type: Date, required: true },
|
||||
created_at: { type: Date, default: () => Date.now() },
|
||||
})
|
||||
|
||||
|
||||
@@ -5,22 +5,12 @@ export type TProjectCount = {
|
||||
project_id: Schema.Types.ObjectId,
|
||||
events: number,
|
||||
visits: number,
|
||||
ai_messages: number,
|
||||
limit: number,
|
||||
ai_limit: number,
|
||||
billing_expire_at: Date,
|
||||
billing_start_at: Date,
|
||||
}
|
||||
|
||||
const ProjectCountSchema = new Schema<TProjectCount>({
|
||||
project_id: { type: Types.ObjectId, index: 1 },
|
||||
events: { type: Number, required: true, default: 0 },
|
||||
visits: { type: Number, required: true, default: 0 },
|
||||
ai_messages: { type: Number, required: true, default: 0 },
|
||||
limit: { type: Number, required: true },
|
||||
ai_limit: { type: Number, required: true },
|
||||
billing_start_at: { type: Date, required: true },
|
||||
billing_expire_at: { type: Date, required: true },
|
||||
});
|
||||
|
||||
export const ProjectCountModel = model<TProjectCount>('project_counts', ProjectCountSchema);
|
||||
26
shared/schema/ProjectsLimits.ts
Normal file
26
shared/schema/ProjectsLimits.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { model, Schema, Types } from 'mongoose';
|
||||
|
||||
export type TProjectLimit = {
|
||||
_id: Schema.Types.ObjectId,
|
||||
project_id: Schema.Types.ObjectId,
|
||||
events: number,
|
||||
visits: number,
|
||||
ai_messages: number,
|
||||
limit: number,
|
||||
ai_limit: number,
|
||||
billing_expire_at: Date,
|
||||
billing_start_at: Date,
|
||||
}
|
||||
|
||||
const ProjectLimitSchema = new Schema<TProjectLimit>({
|
||||
project_id: { type: Types.ObjectId, index: true, unique: true },
|
||||
events: { type: Number, required: true, default: 0 },
|
||||
visits: { type: Number, required: true, default: 0 },
|
||||
ai_messages: { type: Number, required: true, default: 0 },
|
||||
limit: { type: Number, required: true },
|
||||
ai_limit: { type: Number, required: true },
|
||||
billing_start_at: { type: Date, required: true },
|
||||
billing_expire_at: { type: Date, required: true },
|
||||
});
|
||||
|
||||
export const ProjectLimitModel = model<TProjectLimit>('project_limits', ProjectLimitSchema);
|
||||
Reference in New Issue
Block a user