mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +01:00
fix dashboard premium tables
This commit is contained in:
@@ -7,11 +7,7 @@ function goToUpgrade() {
|
|||||||
showDrawer('PRICING');
|
showDrawer('PRICING');
|
||||||
}
|
}
|
||||||
|
|
||||||
const { project } = useProject()
|
const { isPremium } = useLoggedUser()
|
||||||
|
|
||||||
const isPremium = computed(() => {
|
|
||||||
return project.value?.premium ?? false;
|
|
||||||
});
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ const { showDrawer } = useDrawer();
|
|||||||
<div class="poppins"> Usage:</div>
|
<div class="poppins"> Usage:</div>
|
||||||
<div class="flex items-center gap-2 md:gap-4 flex-col pt-4 md:pt-0 md:flex-row">
|
<div class="flex items-center gap-2 md:gap-4 flex-col pt-4 md:pt-0 md:flex-row">
|
||||||
<div class="grow w-full md:w-auto">
|
<div class="grow w-full md:w-auto">
|
||||||
<UProgress :color="color" :min="0" :max="planData.limit" :value="planData.count">
|
<UProgress v-if="planData" :color="color" :min="0" :max="planData.limit" :value="planData.count">
|
||||||
</UProgress>
|
</UProgress>
|
||||||
</div>
|
</div>
|
||||||
<div class="poppins"> {{ percent }}</div>
|
<div class="poppins"> {{ percent }}</div>
|
||||||
|
|||||||
@@ -11,17 +11,18 @@ const isLogged = computed(() => {
|
|||||||
return loggedUser.value?.logged;
|
return loggedUser.value?.logged;
|
||||||
})
|
})
|
||||||
|
|
||||||
function getUserRoles() {
|
|
||||||
const isPremium = computed(() => {
|
|
||||||
if (!loggedUser.value?.logged) return false;
|
|
||||||
return loggedUser.value.user.roles.includes('PREMIUM');
|
|
||||||
});
|
|
||||||
const isAdmin = computed(() => {
|
const isAdmin = computed(() => {
|
||||||
if (!loggedUser.value?.logged) return false;
|
if (!loggedUser.value?.logged) return false;
|
||||||
return loggedUser.value.user.roles.includes('ADMIN');
|
return loggedUser.value.user.roles.includes('ADMIN');
|
||||||
});
|
});
|
||||||
|
|
||||||
return { isPremium, isAdmin }
|
const isPremium = computed(() => {
|
||||||
|
if (!loggedUser.value?.logged) return false;
|
||||||
|
return loggedUser.value.user.roles.includes('PREMIUM');
|
||||||
|
});
|
||||||
|
|
||||||
|
function getUserRoles() {
|
||||||
|
return { isAdmin, isPremium }
|
||||||
}
|
}
|
||||||
|
|
||||||
export const isAdminHidden = ref<boolean>(false);
|
export const isAdminHidden = ref<boolean>(false);
|
||||||
@@ -29,6 +30,8 @@ export const isAdminHidden = ref<boolean>(false);
|
|||||||
export function useLoggedUser() {
|
export function useLoggedUser() {
|
||||||
return {
|
return {
|
||||||
isLogged,
|
isLogged,
|
||||||
|
isPremium,
|
||||||
|
isAdmin,
|
||||||
user: loggedUser,
|
user: loggedUser,
|
||||||
userRoles: getUserRoles(),
|
userRoles: getUserRoles(),
|
||||||
setLoggedUser
|
setLoggedUser
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ definePageMeta({ layout: 'dashboard' });
|
|||||||
|
|
||||||
const { project } = useProject();
|
const { project } = useProject();
|
||||||
|
|
||||||
const isPremium = computed(() => (project.value?.premium_type || 0) > 0);
|
const { isPremium } = useLoggedUser();
|
||||||
|
|
||||||
const selfhosted = useSelfhosted();
|
const selfhosted = useSelfhosted();
|
||||||
const canDownload = computed(() => {
|
const canDownload = computed(() => {
|
||||||
if (selfhosted) return true;
|
if (selfhosted) return true;
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ definePageMeta({ layout: 'dashboard' });
|
|||||||
|
|
||||||
const { project } = useProject();
|
const { project } = useProject();
|
||||||
|
|
||||||
const isPremium = computed(() => (project.value?.premium_type || 0) > 0);
|
const { isPremium } = useLoggedUser();
|
||||||
|
|
||||||
const selfhosted = useSelfhosted();
|
const selfhosted = useSelfhosted();
|
||||||
const canDownload = computed(() => {
|
const canDownload = computed(() => {
|
||||||
if (selfhosted) return true;
|
if (selfhosted) return true;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
import { ProjectLimitModel } from "@schema/project/ProjectsLimits";
|
import { UserLimitModel } from "@schema/UserLimitSchema";
|
||||||
import { AIPlugin } from "../Plugin";
|
import { AIPlugin } from "../Plugin";
|
||||||
import { MAX_LOG_LIMIT_PERCENT } from "@data/broker/Limits";
|
import { MAX_LOG_LIMIT_PERCENT } from "@data/broker/Limits";
|
||||||
import { ProjectModel } from "@schema/project/ProjectSchema";
|
import { ProjectModel } from "@schema/project/ProjectSchema";
|
||||||
@@ -16,11 +16,11 @@ export class AiBilling extends AIPlugin<[
|
|||||||
super({
|
super({
|
||||||
|
|
||||||
'getInvoices': {
|
'getInvoices': {
|
||||||
handler: async (data: { project_id: string }) => {
|
handler: async (data: { user_id: string }) => {
|
||||||
|
|
||||||
const project = await ProjectModel.findOne({ _id: data.project_id });
|
const project = await ProjectModel.findOne({ user_id: data.user_id });
|
||||||
if (!project) return { error: 'Project not found' };
|
if (!project) return { error: 'Project not found' };
|
||||||
const invoices = await StripeService.getInvoices(project.customer_id);
|
const invoices = await StripeService.getInvoices(data.user_id);
|
||||||
if (!invoices) return [];
|
if (!invoices) return [];
|
||||||
|
|
||||||
return invoices?.data.map(e => {
|
return invoices?.data.map(e => {
|
||||||
@@ -45,46 +45,44 @@ export class AiBilling extends AIPlugin<[
|
|||||||
},
|
},
|
||||||
|
|
||||||
'getBillingInfo': {
|
'getBillingInfo': {
|
||||||
handler: async (data: { project_id: string }) => {
|
handler: async (data: { user_id: string }) => {
|
||||||
|
|
||||||
const project = await ProjectModel.findOne({ _id: data.project_id });
|
return { error: 'NOT IMPLEMENTED YET' }
|
||||||
if (!project) return { error: 'Project not found' };
|
// if (project.subscription_id === 'onetime') {
|
||||||
|
|
||||||
if (project.subscription_id === 'onetime') {
|
// const projectLimits = await ProjectLimitModel.findOne({ project_id: data.project_id });
|
||||||
|
// if (!projectLimits) return { error: 'Limits not found' }
|
||||||
|
|
||||||
const projectLimits = await ProjectLimitModel.findOne({ project_id: data.project_id });
|
// const result = {
|
||||||
if (!projectLimits) return { error: 'Limits not found' }
|
// premium: project.premium,
|
||||||
|
// premium_type: project.premium_type,
|
||||||
|
// billing_start_at: projectLimits.billing_start_at,
|
||||||
|
// billing_expire_at: projectLimits.billing_expire_at,
|
||||||
|
// limit: projectLimits.limit,
|
||||||
|
// count: projectLimits.events + projectLimits.visits,
|
||||||
|
// subscription_status: StripeService.isDisabled() ? 'Disabled mode' : ('One time payment')
|
||||||
|
// }
|
||||||
|
|
||||||
const result = {
|
// return result;
|
||||||
premium: project.premium,
|
// }
|
||||||
premium_type: project.premium_type,
|
|
||||||
billing_start_at: projectLimits.billing_start_at,
|
|
||||||
billing_expire_at: projectLimits.billing_expire_at,
|
|
||||||
limit: projectLimits.limit,
|
|
||||||
count: projectLimits.events + projectLimits.visits,
|
|
||||||
subscription_status: StripeService.isDisabled() ? 'Disabled mode' : ('One time payment')
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
// const subscription = await StripeService.getSubscription(project.subscription_id);
|
||||||
}
|
|
||||||
|
|
||||||
const subscription = await StripeService.getSubscription(project.subscription_id);
|
// const projectLimits = await ProjectLimitModel.findOne({ project_id: data.project_id });
|
||||||
|
// if (!projectLimits) return { error: 'Limits not found' }
|
||||||
const projectLimits = await ProjectLimitModel.findOne({ project_id: data.project_id });
|
|
||||||
if (!projectLimits) return { error: 'Limits not found' }
|
|
||||||
|
|
||||||
|
|
||||||
const result = {
|
// const result = {
|
||||||
premium: project.premium,
|
// premium: project.premium,
|
||||||
premium_type: project.premium_type,
|
// premium_type: project.premium_type,
|
||||||
billing_start_at: projectLimits.billing_start_at,
|
// billing_start_at: projectLimits.billing_start_at,
|
||||||
billing_expire_at: projectLimits.billing_expire_at,
|
// billing_expire_at: projectLimits.billing_expire_at,
|
||||||
limit: projectLimits.limit,
|
// limit: projectLimits.limit,
|
||||||
count: projectLimits.events + projectLimits.visits,
|
// count: projectLimits.events + projectLimits.visits,
|
||||||
subscription_status: StripeService.isDisabled() ? 'Disabled mode' : (subscription?.status ?? '?')
|
// subscription_status: StripeService.isDisabled() ? 'Disabled mode' : (subscription?.status ?? '?')
|
||||||
}
|
// }
|
||||||
|
|
||||||
return result;
|
// return result;
|
||||||
},
|
},
|
||||||
tool: {
|
tool: {
|
||||||
type: 'function',
|
type: 'function',
|
||||||
@@ -98,16 +96,17 @@ export class AiBilling extends AIPlugin<[
|
|||||||
|
|
||||||
'getLimits': {
|
'getLimits': {
|
||||||
handler: async (data: { project_id: string }) => {
|
handler: async (data: { project_id: string }) => {
|
||||||
const projectLimits = await ProjectLimitModel.findOne({ project_id: data.project_id });
|
return { error: 'NOT IMPLEMENTED YET' }
|
||||||
if (!projectLimits) return { error: 'Project limits not found' };
|
// const projectLimits = await ProjectLimitModel.findOne({ project_id: data.project_id });
|
||||||
const TOTAL_COUNT = projectLimits.events + projectLimits.visits;
|
// if (!projectLimits) return { error: 'Project limits not found' };
|
||||||
const COUNT_LIMIT = projectLimits.limit;
|
// const TOTAL_COUNT = projectLimits.events + projectLimits.visits;
|
||||||
return {
|
// const COUNT_LIMIT = projectLimits.limit;
|
||||||
total: TOTAL_COUNT,
|
// return {
|
||||||
limit: COUNT_LIMIT,
|
// total: TOTAL_COUNT,
|
||||||
limited: TOTAL_COUNT > COUNT_LIMIT * MAX_LOG_LIMIT_PERCENT,
|
// limit: COUNT_LIMIT,
|
||||||
percent: Math.round(100 / COUNT_LIMIT * TOTAL_COUNT)
|
// limited: TOTAL_COUNT > COUNT_LIMIT * MAX_LOG_LIMIT_PERCENT,
|
||||||
}
|
// percent: Math.round(100 / COUNT_LIMIT * TOTAL_COUNT)
|
||||||
|
// }
|
||||||
},
|
},
|
||||||
tool: {
|
tool: {
|
||||||
type: 'function',
|
type: 'function',
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { ProjectModel } from "@schema/project/ProjectSchema";
|
import { ProjectModel } from "@schema/project/ProjectSchema";
|
||||||
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
||||||
import { ProjectLimitModel } from "@schema/project/ProjectsLimits";
|
import { UserLimitModel } from "@schema/UserLimitSchema";
|
||||||
import { UserModel } from "@schema/UserSchema";
|
import { UserModel } from "@schema/UserSchema";
|
||||||
import StripeService from '~/server/services/StripeService';
|
import StripeService from '~/server/services/StripeService';
|
||||||
|
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
const userData = getRequestUser(event);
|
const userData = getRequestUser(event);
|
||||||
@@ -13,17 +14,19 @@ export default defineEventHandler(async event => {
|
|||||||
if (!project_id) return setResponseStatus(event, 400, 'ProjectId is required');
|
if (!project_id) return setResponseStatus(event, 400, 'ProjectId is required');
|
||||||
|
|
||||||
const project = await ProjectModel.findById(project_id);
|
const project = await ProjectModel.findById(project_id);
|
||||||
const limits = await ProjectLimitModel.findOne({ project_id });
|
const limits = await UserLimitModel.findOne({ user_id: userData.id });
|
||||||
const counts = await ProjectCountModel.findOne({ project_id });
|
const counts = await ProjectCountModel.findOne({ project_id });
|
||||||
const user = await UserModel.findOne({ project_id });
|
const user = await UserModel.findOne({ project_id });
|
||||||
|
|
||||||
|
const premium = await PremiumModel.findOne({ user_id: userData.id });
|
||||||
|
|
||||||
const subscription =
|
const subscription =
|
||||||
project?.subscription_id ?
|
premium?.subscription_id ?
|
||||||
await StripeService.getSubscription(project.subscription_id) : 'NONE';
|
await StripeService.getSubscription(premium.subscription_id) : 'NONE';
|
||||||
|
|
||||||
const customer =
|
const customer =
|
||||||
project?.customer_id ?
|
premium?.customer_id ?
|
||||||
await StripeService.getCustomer(project.customer_id) : 'NONE';
|
await StripeService.getCustomer(premium.customer_id) : 'NONE';
|
||||||
|
|
||||||
return { project, limits, counts, user, subscription, customer }
|
return { project, limits, counts, user, subscription, customer }
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { AiChatModel } from "@schema/ai/AiChatSchema";
|
|||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
const data = await getRequestDataOld(event);
|
const data = await getRequestData(event, [], ['AI']);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { project_id } = data;
|
const { project_id } = data;
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { ProjectLimitModel } from "@schema/project/ProjectsLimits";
|
|
||||||
|
|
||||||
export async function getAiChatRemainings(project_id: string) {
|
import { UserLimitModel } from "@schema/UserLimitSchema";
|
||||||
const limits = await ProjectLimitModel.findOne({ project_id })
|
|
||||||
|
export async function getAiChatRemainings(user_id: string) {
|
||||||
|
const limits = await UserLimitModel.findOne({ user_id })
|
||||||
if (!limits) return 0;
|
if (!limits) return 0;
|
||||||
const chatsRemaining = limits.ai_limit - limits.ai_messages;
|
const chatsRemaining = limits.ai_limit - limits.ai_messages;
|
||||||
|
|
||||||
if (isNaN(chatsRemaining)) return 0;
|
if (isNaN(chatsRemaining)) return 0;
|
||||||
return chatsRemaining;
|
return chatsRemaining;
|
||||||
}
|
}
|
||||||
@@ -13,8 +13,8 @@ export default defineEventHandler(async event => {
|
|||||||
const data = await getRequestData(event, [], ['AI']);
|
const data = await getRequestData(event, [], ['AI']);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { pid } = data;
|
const { pid, user } = data;
|
||||||
|
|
||||||
const chatsRemaining = await getAiChatRemainings(pid);
|
const chatsRemaining = await getAiChatRemainings(user.id);
|
||||||
return chatsRemaining;
|
return chatsRemaining;
|
||||||
});
|
});
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { sendMessageOnChat, updateChatStatus } from "~/server/services/AiService";
|
import { sendMessageOnChat, updateChatStatus } from "~/server/services/AiService";
|
||||||
import { getAiChatRemainings } from "./chats_remaining";
|
import { getAiChatRemainings } from "./chats_remaining";
|
||||||
import { ProjectLimitModel } from "@schema/project/ProjectsLimits";
|
import { UserLimitModel } from "@schema/UserLimitSchema";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -8,16 +8,15 @@ export default defineEventHandler(async event => {
|
|||||||
const data = await getRequestData(event, [], ['AI']);
|
const data = await getRequestData(event, [], ['AI']);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { pid } = data;
|
const { pid, user } = data;
|
||||||
|
|
||||||
const { text, chat_id, timeOffset } = await readBody(event);
|
const { text, chat_id, timeOffset } = await readBody(event);
|
||||||
if (!text) return setResponseStatus(event, 400, 'text parameter missing');
|
if (!text) return setResponseStatus(event, 400, 'text parameter missing');
|
||||||
|
|
||||||
const chatsRemaining = await getAiChatRemainings(pid);
|
const chatsRemaining = await getAiChatRemainings(user.id);
|
||||||
if (chatsRemaining <= 0) return setResponseStatus(event, 400, 'CHAT_LIMIT_REACHED');
|
if (chatsRemaining <= 0) return setResponseStatus(event, 400, 'CHAT_LIMIT_REACHED');
|
||||||
|
|
||||||
|
await UserLimitModel.updateOne({ user_id: user.id }, { $inc: { ai_messages: 1 } });
|
||||||
await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { ai_messages: 1 } });
|
|
||||||
|
|
||||||
const currentStatus: string[] = [];
|
const currentStatus: string[] = [];
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
|
|
||||||
import StripeService from '~/server/services/StripeService';
|
import StripeService from '~/server/services/StripeService';
|
||||||
|
import { PremiumModel } from '~/shared/schema/PremiumSchema';
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
const data = await getRequestData(event, []);
|
const data = await getRequestData(event, []);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { project } = data;
|
const premium = await PremiumModel.findOne({ user_id: data.user.id })
|
||||||
|
if (!premium) return;
|
||||||
|
|
||||||
const customer = await StripeService.getCustomer(project.customer_id);
|
const customer = await StripeService.getCustomer(premium.customer_id);
|
||||||
if (customer?.deleted) return;
|
if (customer?.deleted) return;
|
||||||
|
|
||||||
return customer?.address;
|
return customer?.address;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||||
import { Redis } from "~/server/services/CacheService";
|
import { Redis } from "~/server/services/CacheService";
|
||||||
import StripeService from '~/server/services/StripeService';
|
import StripeService from '~/server/services/StripeService';
|
||||||
|
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||||
|
|
||||||
|
|
||||||
export type InvoiceData = {
|
export type InvoiceData = {
|
||||||
@@ -15,13 +16,13 @@ export default defineEventHandler(async event => {
|
|||||||
const data = await getRequestData(event, []);
|
const data = await getRequestData(event, []);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { project, pid } = data;
|
|
||||||
|
|
||||||
if (!project.customer_id) return [];
|
return await Redis.useCache({ key: `invoices:${data.user.id}`, exp: 10 }, async () => {
|
||||||
|
|
||||||
return await Redis.useCache({ key: `invoices:${pid}`, exp: 10 }, async () => {
|
const premium = await PremiumModel.findOne({ user_id: data.user.id });
|
||||||
|
if (!premium) return [];
|
||||||
|
|
||||||
const invoices = await StripeService.getInvoices(project.customer_id);
|
const invoices = await StripeService.getInvoices(premium.customer_id);
|
||||||
if (!invoices) return [];
|
if (!invoices) return [];
|
||||||
|
|
||||||
return invoices?.data.map(e => {
|
return invoices?.data.map(e => {
|
||||||
|
|||||||
@@ -1,268 +1,270 @@
|
|||||||
|
|
||||||
import StripeService from '~/server/services/StripeService';
|
// import StripeService from '~/server/services/StripeService';
|
||||||
import type Event from 'stripe';
|
// import type Event from 'stripe';
|
||||||
import { ProjectModel } from '@schema/project/ProjectSchema';
|
// import { ProjectModel } from '@schema/project/ProjectSchema';
|
||||||
import { PREMIUM_DATA, PREMIUM_PLAN, getPlanFromId, getPlanFromPrice, getPlanFromTag } from '@data/PREMIUM';
|
// import { PREMIUM_DATA, PREMIUM_PLAN, getPlanFromId, getPlanFromPrice, getPlanFromTag } from '@data/PREMIUM';
|
||||||
import { ProjectLimitModel } from '@schema/project/ProjectsLimits';
|
// import { ProjectLimitModel } from '@schema/project/ProjectsLimits';
|
||||||
import { EmailService } from '@services/EmailService'
|
// import { EmailService } from '@services/EmailService'
|
||||||
import { UserModel } from '@schema/UserSchema';
|
// import { UserModel } from '@schema/UserSchema';
|
||||||
import { EmailServiceHelper } from '~/server/services/EmailServiceHelper';
|
// import { EmailServiceHelper } from '~/server/services/EmailServiceHelper';
|
||||||
|
|
||||||
|
|
||||||
async function addSubscriptionToProject(project_id: string, plan: PREMIUM_DATA, subscription_id: string, current_period_start: number, current_period_end: number) {
|
// async function addSubscriptionToProject(project_id: string, plan: PREMIUM_DATA, subscription_id: string, current_period_start: number, current_period_end: number) {
|
||||||
|
|
||||||
await ProjectModel.updateOne({ _id: project_id }, {
|
// await ProjectModel.updateOne({ _id: project_id }, {
|
||||||
premium: plan.ID != 0,
|
// premium: plan.ID != 0,
|
||||||
premium_type: plan.ID,
|
// premium_type: plan.ID,
|
||||||
subscription_id,
|
// subscription_id,
|
||||||
premium_expire_at: current_period_end * 1000
|
// premium_expire_at: current_period_end * 1000
|
||||||
});
|
// });
|
||||||
|
|
||||||
await ProjectLimitModel.updateOne({ project_id }, {
|
// await ProjectLimitModel.updateOne({ project_id }, {
|
||||||
events: 0,
|
// events: 0,
|
||||||
visits: 0,
|
// visits: 0,
|
||||||
ai_messages: 0,
|
// ai_messages: 0,
|
||||||
limit: plan.COUNT_LIMIT,
|
// limit: plan.COUNT_LIMIT,
|
||||||
ai_limit: plan.AI_MESSAGE_LIMIT,
|
// ai_limit: plan.AI_MESSAGE_LIMIT,
|
||||||
billing_start_at: current_period_start * 1000,
|
// billing_start_at: current_period_start * 1000,
|
||||||
billing_expire_at: current_period_end * 1000,
|
// billing_expire_at: current_period_end * 1000,
|
||||||
}, { upsert: true })
|
// }, { upsert: true })
|
||||||
|
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async function onPaymentFailed(event: Event.InvoicePaymentFailedEvent) {
|
// async function onPaymentFailed(event: Event.InvoicePaymentFailedEvent) {
|
||||||
|
|
||||||
//TODO: Send emails
|
// //TODO: Send emails
|
||||||
|
|
||||||
if (event.data.object.attempt_count > 1) {
|
// if (event.data.object.attempt_count > 1) {
|
||||||
const customer_id = event.data.object.customer as string;
|
// const customer_id = event.data.object.customer as string;
|
||||||
const project = await ProjectModel.findOne({ customer_id });
|
// const project = await ProjectModel.findOne({ customer_id });
|
||||||
if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
|
||||||
|
|
||||||
const allSubscriptions = await StripeService.getAllSubscriptions(customer_id);
|
|
||||||
if (!allSubscriptions) return;
|
|
||||||
|
|
||||||
for (const subscription of allSubscriptions.data) {
|
|
||||||
await StripeService.deleteSubscription(subscription.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
const freeSub = await StripeService.createFreeSubscription(customer_id);
|
|
||||||
if (!freeSub) return;
|
|
||||||
|
|
||||||
await addSubscriptionToProject(
|
|
||||||
project._id.toString(),
|
|
||||||
getPlanFromTag('FREE'),
|
|
||||||
freeSub.id,
|
|
||||||
freeSub.current_period_start,
|
|
||||||
freeSub.current_period_end
|
|
||||||
)
|
|
||||||
|
|
||||||
return { ok: true };
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async function onPaymentOnetimeSuccess(event: Event.PaymentIntentSucceededEvent) {
|
|
||||||
const customer_id = event.data.object.customer as string;
|
|
||||||
const project = await ProjectModel.findOne({ customer_id });
|
|
||||||
if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
|
||||||
|
|
||||||
if (event.data.object.status === 'succeeded') {
|
|
||||||
|
|
||||||
const PLAN = getPlanFromPrice(event.data.object.metadata.price, StripeService.testMode || false);
|
|
||||||
if (!PLAN) return { error: 'Plan not found' }
|
|
||||||
const dummyPlan = PLAN.ID + 3000;
|
|
||||||
|
|
||||||
const subscription = await StripeService.createOneTimeSubscriptionDummy(customer_id, dummyPlan);
|
|
||||||
if (!subscription) return { error: 'Error creating subscription' }
|
|
||||||
|
|
||||||
const allSubscriptions = await StripeService.getAllSubscriptions(customer_id);
|
|
||||||
if (!allSubscriptions) return;
|
|
||||||
for (const subscription of allSubscriptions.data) {
|
|
||||||
if (subscription.id === subscription.id) continue;
|
|
||||||
await StripeService.deleteSubscription(subscription.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
await addSubscriptionToProject(project._id.toString(), PLAN, subscription.id, subscription.current_period_start, subscription.current_period_end)
|
|
||||||
|
|
||||||
|
|
||||||
const user = await UserModel.findOne({ _id: project.owner });
|
|
||||||
if (!user) return { ok: false, error: 'USER NOT EXIST FOR PROJECT' + project.id }
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
const emailData = EmailService.getEmailServerInfo('purchase', { target: user.email, projectName: project.name });
|
|
||||||
EmailServiceHelper.sendEmail(emailData);
|
|
||||||
}, 1);
|
|
||||||
|
|
||||||
return { ok: true };
|
|
||||||
}
|
|
||||||
|
|
||||||
return { received: true, warn: 'object status not succeeded' }
|
|
||||||
}
|
|
||||||
|
|
||||||
async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
|
||||||
|
|
||||||
const customer_id = event.data.object.customer as string;
|
|
||||||
const project = await ProjectModel.findOne({ customer_id });
|
|
||||||
if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
|
||||||
|
|
||||||
|
|
||||||
if (event.data.object.status === 'paid') {
|
|
||||||
|
|
||||||
const subscription_id = event.data.object.subscription as string;
|
|
||||||
|
|
||||||
const isNewSubscription = project.subscription_id != subscription_id;
|
|
||||||
|
|
||||||
const allSubscriptions = await StripeService.getAllSubscriptions(customer_id);
|
|
||||||
if (!allSubscriptions) return;
|
|
||||||
|
|
||||||
const currentSubscription = allSubscriptions.data.find(e => e.id === subscription_id);
|
|
||||||
if (!currentSubscription) return { error: 'SUBSCRIPTION NOT EXIST' }
|
|
||||||
|
|
||||||
if (currentSubscription.status !== 'active') return { error: 'SUBSCRIPTION NOT ACTIVE' }
|
|
||||||
|
|
||||||
for (const subscription of allSubscriptions.data) {
|
|
||||||
if (subscription.id === subscription_id) continue;
|
|
||||||
await StripeService.deleteSubscription(subscription.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
const price = currentSubscription.items.data[0].price.id;
|
|
||||||
if (!price) return { error: 'Price not found' }
|
|
||||||
|
|
||||||
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
|
||||||
if (!PLAN) return { error: `Plan not found. Price: ${price}. TestMode: ${StripeService.testMode}` }
|
|
||||||
|
|
||||||
await addSubscriptionToProject(project._id.toString(), PLAN, subscription_id, currentSubscription.current_period_start, currentSubscription.current_period_end)
|
|
||||||
|
|
||||||
const user = await UserModel.findOne({ _id: project.owner });
|
|
||||||
if (!user) return { ok: false, error: 'USER NOT EXIST FOR PROJECT' + project.id }
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
if (PLAN.ID == 0) return;
|
|
||||||
const emailData = EmailService.getEmailServerInfo('purchase', { target: user.email, projectName: project.name });
|
|
||||||
EmailServiceHelper.sendEmail(emailData);
|
|
||||||
}, 1);
|
|
||||||
|
|
||||||
|
|
||||||
return { ok: true };
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
return { received: true, warn: 'payment status not paid' }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) {
|
|
||||||
|
|
||||||
// const project = await ProjectModel.findOne({ customer_id: event.data.object.customer });
|
|
||||||
// if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
// if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
||||||
|
|
||||||
// const price = event.data.object.items.data[0].price.id;
|
// const allSubscriptions = await StripeService.getAllSubscriptions(customer_id);
|
||||||
|
// if (!allSubscriptions) return;
|
||||||
|
|
||||||
|
// for (const subscription of allSubscriptions.data) {
|
||||||
|
// await StripeService.deleteSubscription(subscription.id);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const freeSub = await StripeService.createFreeSubscription(customer_id);
|
||||||
|
// if (!freeSub) return;
|
||||||
|
|
||||||
|
// await addSubscriptionToProject(
|
||||||
|
// project._id.toString(),
|
||||||
|
// getPlanFromTag('FREE'),
|
||||||
|
// freeSub.id,
|
||||||
|
// freeSub.current_period_start,
|
||||||
|
// freeSub.current_period_end
|
||||||
|
// )
|
||||||
|
|
||||||
|
// return { ok: true };
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// async function onPaymentOnetimeSuccess(event: Event.PaymentIntentSucceededEvent) {
|
||||||
|
// const customer_id = event.data.object.customer as string;
|
||||||
|
// const project = await ProjectModel.findOne({ customer_id });
|
||||||
|
// if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
||||||
|
|
||||||
|
// if (event.data.object.status === 'succeeded') {
|
||||||
|
|
||||||
|
// const PLAN = getPlanFromPrice(event.data.object.metadata.price, StripeService.testMode || false);
|
||||||
|
// if (!PLAN) return { error: 'Plan not found' }
|
||||||
|
// const dummyPlan = PLAN.ID + 3000;
|
||||||
|
|
||||||
|
// const subscription = await StripeService.createOneTimeSubscriptionDummy(customer_id, dummyPlan);
|
||||||
|
// if (!subscription) return { error: 'Error creating subscription' }
|
||||||
|
|
||||||
|
// const allSubscriptions = await StripeService.getAllSubscriptions(customer_id);
|
||||||
|
// if (!allSubscriptions) return;
|
||||||
|
// for (const subscription of allSubscriptions.data) {
|
||||||
|
// if (subscription.id === subscription.id) continue;
|
||||||
|
// await StripeService.deleteSubscription(subscription.id);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// await addSubscriptionToProject(project._id.toString(), PLAN, subscription.id, subscription.current_period_start, subscription.current_period_end)
|
||||||
|
|
||||||
|
|
||||||
|
// const user = await UserModel.findOne({ _id: project.owner });
|
||||||
|
// if (!user) return { ok: false, error: 'USER NOT EXIST FOR PROJECT' + project.id }
|
||||||
|
|
||||||
|
// setTimeout(() => {
|
||||||
|
// const emailData = EmailService.getEmailServerInfo('purchase', { target: user.email, projectName: project.name });
|
||||||
|
// EmailServiceHelper.sendEmail(emailData);
|
||||||
|
// }, 1);
|
||||||
|
|
||||||
|
// return { ok: true };
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return { received: true, warn: 'object status not succeeded' }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||||
|
|
||||||
|
// const customer_id = event.data.object.customer as string;
|
||||||
|
// const project = await ProjectModel.findOne({ customer_id });
|
||||||
|
// if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
||||||
|
|
||||||
|
|
||||||
|
// if (event.data.object.status === 'paid') {
|
||||||
|
|
||||||
|
// const subscription_id = event.data.object.subscription as string;
|
||||||
|
|
||||||
|
// const isNewSubscription = project.subscription_id != subscription_id;
|
||||||
|
|
||||||
|
// const allSubscriptions = await StripeService.getAllSubscriptions(customer_id);
|
||||||
|
// if (!allSubscriptions) return;
|
||||||
|
|
||||||
|
// const currentSubscription = allSubscriptions.data.find(e => e.id === subscription_id);
|
||||||
|
// if (!currentSubscription) return { error: 'SUBSCRIPTION NOT EXIST' }
|
||||||
|
|
||||||
|
// if (currentSubscription.status !== 'active') return { error: 'SUBSCRIPTION NOT ACTIVE' }
|
||||||
|
|
||||||
|
// for (const subscription of allSubscriptions.data) {
|
||||||
|
// if (subscription.id === subscription_id) continue;
|
||||||
|
// await StripeService.deleteSubscription(subscription.id);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const price = currentSubscription.items.data[0].price.id;
|
||||||
// if (!price) return { error: 'Price not found' }
|
// if (!price) return { error: 'Price not found' }
|
||||||
|
|
||||||
// const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
// const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
||||||
// if (!PLAN) return { error: 'Plan not found' }
|
// if (!PLAN) return { error: `Plan not found. Price: ${price}. TestMode: ${StripeService.testMode}` }
|
||||||
|
|
||||||
|
// await addSubscriptionToProject(project._id.toString(), PLAN, subscription_id, currentSubscription.current_period_start, currentSubscription.current_period_end)
|
||||||
|
|
||||||
|
// const user = await UserModel.findOne({ _id: project.owner });
|
||||||
|
// if (!user) return { ok: false, error: 'USER NOT EXIST FOR PROJECT' + project.id }
|
||||||
|
|
||||||
|
// setTimeout(() => {
|
||||||
|
// if (PLAN.ID == 0) return;
|
||||||
|
// const emailData = EmailService.getEmailServerInfo('purchase', { target: user.email, projectName: project.name });
|
||||||
|
// EmailServiceHelper.sendEmail(emailData);
|
||||||
|
// }, 1);
|
||||||
|
|
||||||
|
|
||||||
|
// return { ok: true };
|
||||||
|
|
||||||
|
|
||||||
// if (project.subscription_id != event.data.object.id) {
|
|
||||||
// try {
|
|
||||||
// await StripeService.deleteSubscription(project.subscription_id);
|
|
||||||
// } catch (ex) { }
|
|
||||||
// }
|
// }
|
||||||
|
// return { received: true, warn: 'payment status not paid' }
|
||||||
|
|
||||||
// if (event.data.object.status === 'active') {
|
|
||||||
// await addSubscriptionToProject(
|
|
||||||
// project._id.toString(),
|
|
||||||
// PLAN,
|
|
||||||
// event.data.object.id,
|
|
||||||
// event.data.object.current_period_start,
|
|
||||||
// event.data.object.current_period_end
|
|
||||||
// );
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return { ok: true }
|
// async function onSubscriptionCreated(event: Event.CustomerSubscriptionCreatedEvent) {
|
||||||
}
|
|
||||||
|
|
||||||
async function onSubscriptionDeleted(event: Event.CustomerSubscriptionDeletedEvent) {
|
// // const project = await ProjectModel.findOne({ customer_id: event.data.object.customer });
|
||||||
|
// // if (!project) return { error: 'CUSTOMER NOT EXIST' }
|
||||||
|
|
||||||
// const project = await ProjectModel.findOne({
|
// // const price = event.data.object.items.data[0].price.id;
|
||||||
// customer_id: event.data.object.customer,
|
// // if (!price) return { error: 'Price not found' }
|
||||||
// subscription_id: event.data.object.id
|
|
||||||
// });
|
|
||||||
|
|
||||||
// if (!project) return { error: 'PROJECT WITH SUBSCRIPTION NOT FOUND' }
|
// // const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
||||||
|
// // if (!PLAN) return { error: 'Plan not found' }
|
||||||
|
|
||||||
// const targetCustomer = await StripeService.getCustomer(project.customer_id);
|
// // if (project.subscription_id != event.data.object.id) {
|
||||||
|
// // try {
|
||||||
|
// // await StripeService.deleteSubscription(project.subscription_id);
|
||||||
|
// // } catch (ex) { }
|
||||||
|
// // }
|
||||||
|
|
||||||
// let customer: Event.Customer;
|
|
||||||
|
|
||||||
// if (!targetCustomer.deleted) {
|
// // if (event.data.object.status === 'active') {
|
||||||
// customer = targetCustomer;
|
// // await addSubscriptionToProject(
|
||||||
// } else {
|
// // project._id.toString(),
|
||||||
// const user = await UserModel.findById(project._id, { email: 1 });
|
// // PLAN,
|
||||||
// if (!user) return { error: 'User not found' }
|
// // event.data.object.id,
|
||||||
// const newCustomer = await StripeService.createCustomer(user.email);
|
// // event.data.object.current_period_start,
|
||||||
// customer = newCustomer;
|
// // event.data.object.current_period_end
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// return { ok: true }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// await StripeService.createFreeSubscription(customer.id);
|
// 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
|
||||||
|
// // });
|
||||||
|
|
||||||
async function onSubscriptionUpdated(event: Event.CustomerSubscriptionUpdatedEvent) {
|
// // if (!project) return { error: 'PROJECT WITH SUBSCRIPTION NOT FOUND' }
|
||||||
|
|
||||||
// const project = await ProjectModel.findOne({
|
// // const targetCustomer = await StripeService.getCustomer(project.customer_id);
|
||||||
// customer_id: event.data.object.customer,
|
|
||||||
// });
|
|
||||||
|
|
||||||
// if (!project) return { error: 'Project not found' }
|
// // let customer: Event.Customer;
|
||||||
|
|
||||||
// const price = event.data.object.items.data[0].price.id;
|
// // if (!targetCustomer.deleted) {
|
||||||
// if (!price) return { error: 'Price not found' }
|
// // 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 PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
// // await StripeService.createFreeSubscription(customer.id);
|
||||||
// if (!PLAN) return { error: 'Plan not found' }
|
|
||||||
|
|
||||||
// if (event.data.object.status === 'active') {
|
// return { received: true }
|
||||||
// await addSubscriptionToProject(
|
|
||||||
// project._id.toString(),
|
|
||||||
// PLAN,
|
|
||||||
// event.data.object.id,
|
|
||||||
// event.data.object.current_period_start,
|
|
||||||
// event.data.object.current_period_end
|
|
||||||
// );
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
return { ok: true }
|
// async function onSubscriptionUpdated(event: Event.CustomerSubscriptionUpdatedEvent) {
|
||||||
}
|
|
||||||
|
// // 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, StripeService.testMode || false);
|
||||||
|
// // if (!PLAN) return { error: 'Plan not found' }
|
||||||
|
|
||||||
|
// // if (event.data.object.status === 'active') {
|
||||||
|
// // await addSubscriptionToProject(
|
||||||
|
// // project._id.toString(),
|
||||||
|
// // PLAN,
|
||||||
|
// // event.data.object.id,
|
||||||
|
// // event.data.object.current_period_start,
|
||||||
|
// // event.data.object.current_period_end
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// return { ok: true }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
const body = await readRawBody(event);
|
|
||||||
const signature = getHeader(event, 'stripe-signature') || '';
|
|
||||||
|
|
||||||
const eventData = StripeService.parseWebhook(body, signature);
|
return { error: 'NOT IMPLEMENTED ANYMORE' }
|
||||||
if (!eventData) return;
|
// const body = await readRawBody(event);
|
||||||
|
// const signature = getHeader(event, 'stripe-signature') || '';
|
||||||
|
|
||||||
// console.log('WEBHOOK FIRED', eventData.type);
|
// const eventData = StripeService.parseWebhook(body, signature);
|
||||||
|
// if (!eventData) return;
|
||||||
|
|
||||||
if (eventData.type === 'invoice.paid') return await onPaymentSuccess(eventData);
|
// // console.log('WEBHOOK FIRED', eventData.type);
|
||||||
if (eventData.type === 'payment_intent.succeeded') return await onPaymentOnetimeSuccess(eventData);
|
|
||||||
if (eventData.type === 'invoice.payment_failed') return await onPaymentFailed(eventData);
|
|
||||||
if (eventData.type === 'customer.subscription.deleted') return await onSubscriptionDeleted(eventData);
|
|
||||||
if (eventData.type === 'customer.subscription.created') return await onSubscriptionCreated(eventData);
|
|
||||||
if (eventData.type === 'customer.subscription.updated') return await onSubscriptionUpdated(eventData);
|
|
||||||
|
|
||||||
return { received: true }
|
// if (eventData.type === 'invoice.paid') return await onPaymentSuccess(eventData);
|
||||||
|
// if (eventData.type === 'payment_intent.succeeded') return await onPaymentOnetimeSuccess(eventData);
|
||||||
|
// if (eventData.type === 'invoice.payment_failed') return await onPaymentFailed(eventData);
|
||||||
|
// if (eventData.type === 'customer.subscription.deleted') return await onSubscriptionDeleted(eventData);
|
||||||
|
// if (eventData.type === 'customer.subscription.created') return await onSubscriptionCreated(eventData);
|
||||||
|
// if (eventData.type === 'customer.subscription.updated') return await onSubscriptionUpdated(eventData);
|
||||||
|
|
||||||
|
// return { received: true }
|
||||||
});
|
});
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
import { ProjectModel, TProject } from "@schema/project/ProjectSchema";
|
import { ProjectModel, TProject } from "@schema/project/ProjectSchema";
|
||||||
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
||||||
import { ProjectLimitModel } from "@schema/project/ProjectsLimits";
|
|
||||||
import { UserSettingsModel } from "@schema/UserSettings";
|
|
||||||
import StripeService from '~/server/services/StripeService';
|
import StripeService from '~/server/services/StripeService';
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
@@ -41,16 +39,6 @@ export default defineEventHandler(async event => {
|
|||||||
sessions: 0
|
sessions: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
await ProjectLimitModel.updateOne({ project_id: project._id }, {
|
|
||||||
events: 0,
|
|
||||||
visits: 0,
|
|
||||||
ai_messages: 0,
|
|
||||||
limit: 10_000_000,
|
|
||||||
ai_limit: 1_000_000,
|
|
||||||
billing_start_at: Date.now(),
|
|
||||||
billing_expire_at: new Date(3000, 1, 1)
|
|
||||||
}, { upsert: true })
|
|
||||||
|
|
||||||
return project.toJSON() as TProject;
|
return project.toJSON() as TProject;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
import { ProjectModel } from "@schema/project/ProjectSchema";
|
import { ProjectModel } from "@schema/project/ProjectSchema";
|
||||||
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
||||||
import { ProjectLimitModel } from "@schema/project/ProjectsLimits";
|
|
||||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||||
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
|
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
|
||||||
import StripeService from '~/server/services/StripeService';
|
|
||||||
import { AiChatModel } from "@schema/ai/AiChatSchema";
|
import { AiChatModel } from "@schema/ai/AiChatSchema";
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
const data = await getRequestDataOld(event, { requireSchema: false, allowGuests: false, allowLitlyx: false });
|
const data = await getRequestData(event, [], []);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { project, user, project_id } = data;
|
const { project, user, project_id } = data;
|
||||||
@@ -16,15 +14,9 @@ export default defineEventHandler(async event => {
|
|||||||
const projects = await ProjectModel.countDocuments({ owner: user.id });
|
const projects = await ProjectModel.countDocuments({ owner: user.id });
|
||||||
if (projects == 1) return setResponseStatus(event, 400, 'Cannot delete last project');
|
if (projects == 1) return setResponseStatus(event, 400, 'Cannot delete last project');
|
||||||
|
|
||||||
if (project.premium === true) return setResponseStatus(event, 400, 'Cannot delete premium project');
|
|
||||||
|
|
||||||
if (project.customer_id) {
|
|
||||||
await StripeService.deleteCustomer(project.customer_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
const projectDeletation = ProjectModel.deleteOne({ _id: project_id });
|
const projectDeletation = ProjectModel.deleteOne({ _id: project_id });
|
||||||
const countDeletation = ProjectCountModel.deleteMany({ project_id });
|
const countDeletation = ProjectCountModel.deleteMany({ project_id });
|
||||||
const limitdeletation = ProjectLimitModel.deleteMany({ project_id });
|
|
||||||
const sessionsDeletation = SessionModel.deleteMany({ project_id });
|
const sessionsDeletation = SessionModel.deleteMany({ project_id });
|
||||||
const notifiesDeletation = LimitNotifyModel.deleteMany({ project_id });
|
const notifiesDeletation = LimitNotifyModel.deleteMany({ project_id });
|
||||||
const aiChatsDeletation = AiChatModel.deleteMany({ project_id });
|
const aiChatsDeletation = AiChatModel.deleteMany({ project_id });
|
||||||
@@ -32,7 +24,6 @@ export default defineEventHandler(async event => {
|
|||||||
const results = await Promise.all([
|
const results = await Promise.all([
|
||||||
projectDeletation,
|
projectDeletation,
|
||||||
countDeletation,
|
countDeletation,
|
||||||
limitdeletation,
|
|
||||||
sessionsDeletation,
|
sessionsDeletation,
|
||||||
notifiesDeletation,
|
notifiesDeletation,
|
||||||
aiChatsDeletation
|
aiChatsDeletation
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
|
|
||||||
import { ProjectLimitModel } from "@schema/project/ProjectsLimits";
|
import { UserLimitModel } from "@schema/UserLimitSchema";
|
||||||
import { MAX_LOG_LIMIT_PERCENT } from '@data/broker/Limits';
|
import { MAX_LOG_LIMIT_PERCENT } from '@data/broker/Limits';
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
|
const data = await getRequestData(event, [], []);
|
||||||
|
|
||||||
const data = await getRequestDataOld(event);
|
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { project_id } = data;
|
const { user } = data;
|
||||||
|
|
||||||
const projectLimits = await ProjectLimitModel.findOne({ project_id });
|
const projectLimits = await UserLimitModel.findOne({ user_id: user.id });
|
||||||
if (!projectLimits) return;
|
if (!projectLimits) return;
|
||||||
|
|
||||||
const TOTAL_COUNT = projectLimits.events + projectLimits.visits;
|
const TOTAL_COUNT = projectLimits.events + projectLimits.visits;
|
||||||
|
|||||||
@@ -1,44 +1,46 @@
|
|||||||
import { ProjectLimitModel } from "@schema/project/ProjectsLimits";
|
import { UserLimitModel } from "@schema/UserLimitSchema";
|
||||||
import StripeService from '~/server/services/StripeService';
|
import StripeService from '~/server/services/StripeService';
|
||||||
|
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
const data = await getRequestData(event, []);
|
const data = await getRequestData(event, [], ['OWNER']);
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { project, project_id } = data;
|
const premium = await PremiumModel.findOne({ user_id: data.user.id });
|
||||||
|
if (!premium) return;
|
||||||
|
|
||||||
if (project.subscription_id === 'onetime') {
|
if (premium.subscription_id === 'onetime') {
|
||||||
|
|
||||||
const projectLimits = await ProjectLimitModel.findOne({ project_id });
|
const userLimits = await UserLimitModel.findOne({ user_id: data.user.id });
|
||||||
if (!projectLimits) return setResponseStatus(event, 400, 'Project limits not found');
|
if (!userLimits) return setResponseStatus(event, 400, 'User limits not found');
|
||||||
|
|
||||||
const result = {
|
const result = {
|
||||||
premium: project.premium,
|
premium: premium.premium_type > 0,
|
||||||
premium_type: project.premium_type,
|
premium_type: premium.premium_type,
|
||||||
billing_start_at: projectLimits.billing_start_at,
|
billing_start_at: userLimits.billing_start_at,
|
||||||
billing_expire_at: projectLimits.billing_expire_at,
|
billing_expire_at: userLimits.billing_expire_at,
|
||||||
limit: projectLimits.limit,
|
limit: userLimits.limit,
|
||||||
count: projectLimits.events + projectLimits.visits,
|
count: userLimits.events + userLimits.visits,
|
||||||
subscription_status: StripeService.isDisabled() ? 'Disabled mode' : ('One time payment')
|
subscription_status: StripeService.isDisabled() ? 'Disabled mode' : ('One time payment')
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
const subscription = await StripeService.getSubscription(project.subscription_id);
|
const subscription = await StripeService.getSubscription(premium.subscription_id);
|
||||||
|
|
||||||
const projectLimits = await ProjectLimitModel.findOne({ project_id });
|
const userLimits = await UserLimitModel.findOne({ user_id: data.user.id });
|
||||||
if (!projectLimits) return setResponseStatus(event, 400, 'Project limits not found');
|
if (!userLimits) return setResponseStatus(event, 400, 'User limits not found');
|
||||||
|
|
||||||
|
|
||||||
const result = {
|
const result = {
|
||||||
premium: project.premium,
|
premium: premium.premium_type > 0,
|
||||||
premium_type: project.premium_type,
|
premium_type: premium.premium_type,
|
||||||
billing_start_at: projectLimits.billing_start_at,
|
billing_start_at: userLimits.billing_start_at,
|
||||||
billing_expire_at: projectLimits.billing_expire_at,
|
billing_expire_at: userLimits.billing_expire_at,
|
||||||
limit: projectLimits.limit,
|
limit: userLimits.limit,
|
||||||
count: projectLimits.events + projectLimits.visits,
|
count: userLimits.events + userLimits.visits,
|
||||||
subscription_status: StripeService.isDisabled() ? 'Disabled mode' : (subscription?.status ?? '?')
|
subscription_status: StripeService.isDisabled() ? 'Disabled mode' : (subscription?.status ?? '?')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
import { ProjectModel } from "@schema/project/ProjectSchema";
|
import { ProjectModel } from "@schema/project/ProjectSchema";
|
||||||
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
||||||
import { ProjectLimitModel } from "@schema/project/ProjectsLimits";
|
import { UserLimitModel } from "@schema/UserLimitSchema";
|
||||||
import { UserSettingsModel } from "@schema/UserSettings";
|
import { UserSettingsModel } from "@schema/UserSettings";
|
||||||
import { AiChatModel } from "@schema/ai/AiChatSchema";
|
import { AiChatModel } from "@schema/ai/AiChatSchema";
|
||||||
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
|
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
|
||||||
@@ -14,6 +14,7 @@ import { CountryBlacklistModel } from "~/shared/schema/shields/CountryBlacklistS
|
|||||||
import { BotTrafficOptionModel } from "~/shared/schema/shields/BotTrafficOptionSchema";
|
import { BotTrafficOptionModel } from "~/shared/schema/shields/BotTrafficOptionSchema";
|
||||||
import { TeamMemberModel } from "~/shared/schema/TeamMemberSchema";
|
import { TeamMemberModel } from "~/shared/schema/TeamMemberSchema";
|
||||||
import { PasswordModel } from "~/shared/schema/PasswordSchema";
|
import { PasswordModel } from "~/shared/schema/PasswordSchema";
|
||||||
|
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||||
|
|
||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
|
|
||||||
@@ -22,21 +23,25 @@ export default defineEventHandler(async event => {
|
|||||||
|
|
||||||
const projects = await ProjectModel.find({ owner: userData.id });
|
const projects = await ProjectModel.find({ owner: userData.id });
|
||||||
|
|
||||||
const premiumProjects = projects.filter(e => { return e.premium && e.premium_type != 0 }).length;
|
const premium = await PremiumModel.findOne({ user_id: userData.id });
|
||||||
if (premiumProjects > 0) return setResponseStatus(event, 400, 'Cannot delete an account with a premium project');
|
if (!premium) return;
|
||||||
|
|
||||||
|
if (premium.premium_type > 0) return setResponseStatus(event, 400, 'Cannot delete an account with a premium project');
|
||||||
|
|
||||||
const membersDeletation = await TeamMemberModel.deleteMany({ user_id: userData.id });
|
const membersDeletation = await TeamMemberModel.deleteMany({ user_id: userData.id });
|
||||||
const membersEmailDeletation = await TeamMemberModel.deleteMany({ email: userData.user.email });
|
const membersEmailDeletation = await TeamMemberModel.deleteMany({ email: userData.user.email });
|
||||||
|
|
||||||
const passwordDeletation = await PasswordModel.deleteMany({ user_id: userData.id });
|
const passwordDeletation = await PasswordModel.deleteMany({ user_id: userData.id });
|
||||||
|
|
||||||
|
const limitdeletation = await UserLimitModel.deleteMany({ user_id: userData.id });
|
||||||
|
|
||||||
|
await StripeService.deleteCustomer(premium.customer_id);
|
||||||
|
|
||||||
for (const project of projects) {
|
for (const project of projects) {
|
||||||
const project_id = project._id;
|
const project_id = project._id;
|
||||||
await StripeService.deleteCustomer(project.customer_id);
|
|
||||||
const projectDeletation = await ProjectModel.deleteOne({ _id: project_id });
|
const projectDeletation = await ProjectModel.deleteOne({ _id: project_id });
|
||||||
const userSettingsDeletation = await UserSettingsModel.deleteOne({ project_id });
|
const userSettingsDeletation = await UserSettingsModel.deleteOne({ project_id });
|
||||||
const countDeletation = await ProjectCountModel.deleteMany({ project_id });
|
const countDeletation = await ProjectCountModel.deleteMany({ project_id });
|
||||||
const limitdeletation = await ProjectLimitModel.deleteMany({ project_id });
|
|
||||||
const sessionsDeletation = await SessionModel.deleteMany({ project_id });
|
const sessionsDeletation = await SessionModel.deleteMany({ project_id });
|
||||||
const notifiesDeletation = await LimitNotifyModel.deleteMany({ project_id });
|
const notifiesDeletation = await LimitNotifyModel.deleteMany({ project_id });
|
||||||
const aiChatsDeletation = await AiChatModel.deleteMany({ project_id });
|
const aiChatsDeletation = await AiChatModel.deleteMany({ project_id });
|
||||||
|
|||||||
@@ -4,7 +4,5 @@ import { AuthContext } from "~/server/middleware/01-authorization";
|
|||||||
export default defineEventHandler(async event => {
|
export default defineEventHandler(async event => {
|
||||||
const userData: AuthContext = getRequestUser(event) as any;
|
const userData: AuthContext = getRequestUser(event) as any;
|
||||||
if (!userData.logged) return;
|
if (!userData.logged) return;
|
||||||
// const userSettings = await UserSettingsModel.findOne({ user_id: userData.id }, { max_projects: 1 });
|
|
||||||
// return userSettings?.max_projects || 3;
|
|
||||||
return 20;
|
return 20;
|
||||||
});
|
});
|
||||||
@@ -4,6 +4,7 @@ import { UserModel } from "@schema/UserSchema";
|
|||||||
import { ADMIN_EMAILS } from '@data/ADMINS';
|
import { ADMIN_EMAILS } from '@data/ADMINS';
|
||||||
|
|
||||||
import type { H3Event, EventHandlerRequest } from 'h3';
|
import type { H3Event, EventHandlerRequest } from 'h3';
|
||||||
|
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||||
|
|
||||||
export type AuthContextLogged = {
|
export type AuthContextLogged = {
|
||||||
id: string,
|
id: string,
|
||||||
@@ -43,13 +44,19 @@ async function authorizationMiddleware(event: H3Event<EventHandlerRequest>) {
|
|||||||
roles.push('ADMIN');
|
roles.push('ADMIN');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const premium = await PremiumModel.findOne({ user_id: user.id });
|
||||||
|
|
||||||
|
if ((premium?.premium_type || 0) > 0) {
|
||||||
|
roles.push('PREMIUM');
|
||||||
|
}
|
||||||
|
|
||||||
const authContext: AuthContext = {
|
const authContext: AuthContext = {
|
||||||
logged: true,
|
logged: true,
|
||||||
user: {
|
user: {
|
||||||
email: user.email,
|
email: user.email,
|
||||||
name: user.name,
|
name: user.name,
|
||||||
picture: user.picture || `https://robohash.org/${user.email}?set=set4`,
|
picture: user.picture || `https://robohash.org/${user.email}?set=set4`,
|
||||||
roles
|
roles,
|
||||||
},
|
},
|
||||||
id: user._id.toString()
|
id: user._id.toString()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import OpenAI from "openai";
|
import OpenAI from "openai";
|
||||||
import { AiChatModel } from '@schema/ai/AiChatSchema';
|
import { AiChatModel } from '@schema/ai/AiChatSchema';
|
||||||
import { ProjectLimitModel } from '@schema/project/ProjectsLimits';
|
|
||||||
|
|
||||||
import { AiEventsInstance } from '../ai/functions/AI_Events';
|
import { AiEventsInstance } from '../ai/functions/AI_Events';
|
||||||
import { AiVisitsInstance } from '../ai/functions/AI_Visits';
|
import { AiVisitsInstance } from '../ai/functions/AI_Visits';
|
||||||
@@ -218,34 +217,5 @@ export async function sendMessageOnChat(text: string, pid: string, time_offset:
|
|||||||
return { content: ex.message, charts: [] };
|
return { content: ex.message, charts: [] };
|
||||||
}
|
}
|
||||||
|
|
||||||
// let response = await openai.chat.completions.create({ model: OPENAI_MODEL, messages, n: 1, tools });
|
|
||||||
|
|
||||||
// const chartsData: string[][] = [];
|
|
||||||
|
|
||||||
// while ((response.choices[0].message.tool_calls?.length || 0) > 0) {
|
|
||||||
// await addMessageToChat(response.choices[0].message, chat_id);
|
|
||||||
// messages.push(response.choices[0].message);
|
|
||||||
// if (response.choices[0].message.tool_calls) {
|
|
||||||
|
|
||||||
// console.log('Tools to call', response.choices[0].message.tool_calls.length);
|
|
||||||
// chartsData.push(getChartsInMessage(response.choices[0].message));
|
|
||||||
|
|
||||||
// for (const toolCall of response.choices[0].message.tool_calls) {
|
|
||||||
// const functionName = toolCall.function.name;
|
|
||||||
// console.log('Calling tool function', functionName);
|
|
||||||
// const functionToCall = functions[functionName];
|
|
||||||
// const functionArgs = JSON.parse(toolCall.function.arguments);
|
|
||||||
// const functionResponse = await functionToCall({ project_id: pid, ...functionArgs });
|
|
||||||
// messages.push({ tool_call_id: toolCall.id, role: "tool", content: JSON.stringify(functionResponse) });
|
|
||||||
// await addMessageToChat({ tool_call_id: toolCall.id, role: "tool", content: JSON.stringify(functionResponse) }, chat_id);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// response = await openai.chat.completions.create({ model: OPENAI_MODEL, messages, n: 1, tools });
|
|
||||||
// }
|
|
||||||
// await addMessageToChat(response.choices[0].message, chat_id);
|
|
||||||
// await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { ai_messages: 1 } })
|
|
||||||
// return { content: response.choices[0].message.content, charts: chartsData.filter(e => e.length > 0).flat() };
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,13 +11,13 @@ import { EmailService } from '../shared/services/EmailService';
|
|||||||
|
|
||||||
async function addSubscriptionToUser(user_id: string, plan: PLAN_DATA, subscription_id: string, current_period_start: number, current_period_end: number) {
|
async function addSubscriptionToUser(user_id: string, plan: PLAN_DATA, subscription_id: string, current_period_start: number, current_period_end: number) {
|
||||||
|
|
||||||
await PremiumModel.updateOne({ _id: user_id }, {
|
await PremiumModel.updateOne({ user_id }, {
|
||||||
premium_type: plan.ID,
|
premium_type: plan.ID,
|
||||||
subscription_id,
|
subscription_id,
|
||||||
expire_at: current_period_end * 1000
|
expire_at: current_period_end * 1000
|
||||||
}, { upsert: true });
|
}, { upsert: true });
|
||||||
|
|
||||||
await UserLimitModel.updateOne({ _id: user_id }, {
|
await UserLimitModel.updateOne({ user_id }, {
|
||||||
events: 0,
|
events: 0,
|
||||||
visits: 0,
|
visits: 0,
|
||||||
ai_messages: 0,
|
ai_messages: 0,
|
||||||
@@ -70,7 +70,11 @@ export async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
|||||||
const databaseSubscription = premiumData.subscription_id;
|
const databaseSubscription = premiumData.subscription_id;
|
||||||
|
|
||||||
if (databaseSubscription != subscription_id) {
|
if (databaseSubscription != subscription_id) {
|
||||||
await StripeService.deleteSubscription(subscription_id);
|
try {
|
||||||
|
await StripeService.deleteSubscription(databaseSubscription);
|
||||||
|
} catch (ex) {
|
||||||
|
console.error(ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await addSubscriptionToUser(premiumData.user_id.toString(), plan, subscription_id, event.data.object.period_start, event.data.object.period_end);
|
await addSubscriptionToUser(premiumData.user_id.toString(), plan, subscription_id, event.data.object.period_start, event.data.object.period_end);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { getPlanFromId } from '../shared/data/PLANS';
|
|||||||
import StripeService from '../services/StripeService';
|
import StripeService from '../services/StripeService';
|
||||||
import { sendJson } from '../Utils';
|
import { sendJson } from '../Utils';
|
||||||
import { PremiumModel } from '../shared/schema/PremiumSchema';
|
import { PremiumModel } from '../shared/schema/PremiumSchema';
|
||||||
|
import { Types } from 'mongoose';
|
||||||
|
|
||||||
export const paymentRouter = Router();
|
export const paymentRouter = Router();
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ paymentRouter.post('/create', json(), async (req, res) => {
|
|||||||
const plan = getPlanFromId(createPaymentData.plan_id);
|
const plan = getPlanFromId(createPaymentData.plan_id);
|
||||||
if (!plan) return sendJson(res, 400, { error: 'plan not found' });
|
if (!plan) return sendJson(res, 400, { error: 'plan not found' });
|
||||||
|
|
||||||
const premiumData = await PremiumModel.findById(createPaymentData.user_id);
|
const premiumData = await PremiumModel.findOne({ user_id: createPaymentData.user_id });
|
||||||
if (!premiumData) return sendJson(res, 400, { error: 'user not found' });
|
if (!premiumData) return sendJson(res, 400, { error: 'user not found' });
|
||||||
if (!premiumData.customer_id) return sendJson(res, 400, { error: 'user have no customer_id' });
|
if (!premiumData.customer_id) return sendJson(res, 400, { error: 'user have no customer_id' });
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
import { json, Router } from 'express';
|
import { raw, Router } from 'express';
|
||||||
import { sendJson } from '../Utils';
|
import { sendJson } from '../Utils';
|
||||||
import StripeService from '../services/StripeService';
|
import StripeService from '../services/StripeService';
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ import * as WebhookController from '../controllers/WebhookController'
|
|||||||
export const webhookRouter = Router();
|
export const webhookRouter = Router();
|
||||||
|
|
||||||
|
|
||||||
webhookRouter.get('/', json(), async (req, res) => {
|
webhookRouter.post('/', raw({ type: 'application/json' }), async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const signature = req.header('stripe-signature');
|
const signature = req.header('stripe-signature');
|
||||||
|
|||||||
Reference in New Issue
Block a user