mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
fix dashboard + payments
This commit is contained in:
@@ -2,7 +2,6 @@ import { ProjectModel } from "@schema/project/ProjectSchema";
|
||||
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
||||
import { UserLimitModel } from "@schema/UserLimitSchema";
|
||||
import { UserModel } from "@schema/UserSchema";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
@@ -20,14 +19,18 @@ export default defineEventHandler(async event => {
|
||||
|
||||
const premium = await PremiumModel.findOne({ user_id: userData.id });
|
||||
|
||||
const subscription =
|
||||
premium?.subscription_id ?
|
||||
await StripeService.getSubscription(premium.subscription_id) : 'NONE';
|
||||
// const subscription =
|
||||
// premium?.subscription_id ?
|
||||
// await StripeService.getSubscription(premium.subscription_id) : 'NONE';
|
||||
|
||||
const customer =
|
||||
premium?.customer_id ?
|
||||
await StripeService.getCustomer(premium.customer_id) : 'NONE';
|
||||
// const customer =
|
||||
// premium?.customer_id ?
|
||||
// await StripeService.getCustomer(premium.customer_id) : 'NONE';
|
||||
|
||||
return { project, limits, counts, user, subscription, customer }
|
||||
return {
|
||||
project, limits, counts, user,
|
||||
subscription: '',
|
||||
customer: ''
|
||||
}
|
||||
|
||||
});
|
||||
@@ -4,6 +4,7 @@ import { UserModel } from '@schema/UserSchema';
|
||||
import { PasswordModel } from '@schema/PasswordSchema';
|
||||
import { EmailService } from '@services/EmailService';
|
||||
import { EmailServiceHelper } from '~/server/services/EmailServiceHelper';
|
||||
import { PaymentServiceHelper } from '~/server/services/PaymentServiceHelper';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -13,15 +14,22 @@ export default defineEventHandler(async event => {
|
||||
if (!data) return setResponseStatus(event, 400, 'Error decoding register_code');
|
||||
|
||||
try {
|
||||
await PasswordModel.create({ email: data.email, password: data.password })
|
||||
await UserModel.create({ email: data.email, given_name: '', name: 'EmailLogin', locale: '', picture: '', created_at: Date.now() });
|
||||
await PasswordModel.updateOne({ email: data.email }, { password: data.password }, { upsert: true });
|
||||
|
||||
const user = await UserModel.create({ email: data.email, given_name: '', name: 'EmailLogin', locale: '', picture: '', created_at: Date.now() });
|
||||
|
||||
const [ok, error] = await PaymentServiceHelper.create_customer(user.id);
|
||||
if (!ok) throw error;
|
||||
|
||||
setImmediate(() => {
|
||||
const emailData = EmailService.getEmailServerInfo('welcome', { target: data.email });
|
||||
EmailServiceHelper.sendEmail(emailData);
|
||||
});
|
||||
|
||||
const jwt = createUserJwt({ email: data.email, name: 'EmailLogin' });
|
||||
return sendRedirect(event, `https://dashboard.litlyx.com/jwt_login?jwt_login=${jwt}`);
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
return setResponseStatus(event, 400, 'Error creating user');
|
||||
}
|
||||
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
import { getPlanFromId } from "@data/PREMIUM";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
// import StripeService from '~/server/services/StripeService';
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestDataOld(event, { requireSchema: false, allowGuests: false, allowLitlyx: false });
|
||||
if (!data) return;
|
||||
// const data = await getRequestDataOld(event, { requireSchema: false, allowGuests: false, allowLitlyx: false });
|
||||
// if (!data) return;
|
||||
|
||||
const { project, pid } = data;
|
||||
// const { project, pid } = data;
|
||||
|
||||
const body = await readBody(event);
|
||||
// const body = await readBody(event);
|
||||
|
||||
const { planId } = body;
|
||||
// const { planId } = body;
|
||||
|
||||
const PLAN = getPlanFromId(planId);
|
||||
// const PLAN = getPlanFromId(planId);
|
||||
|
||||
if (!PLAN) {
|
||||
console.error('PLAN', planId, 'NOT EXIST');
|
||||
return setResponseStatus(event, 400, 'Plan not exist');
|
||||
}
|
||||
// if (!PLAN) {
|
||||
// console.error('PLAN', planId, 'NOT EXIST');
|
||||
// return setResponseStatus(event, 400, 'Plan not exist');
|
||||
// }
|
||||
|
||||
const intent = await StripeService.createOnetimePayment(
|
||||
StripeService.testMode ? PLAN.PRICE_TEST : PLAN.PRICE,
|
||||
'https://dashboard.litlyx.com/payment_ok',
|
||||
pid,
|
||||
project.customer_id
|
||||
)
|
||||
// const intent = await StripeService.createOnetimePayment(
|
||||
// StripeService.testMode ? PLAN.PRICE_TEST : PLAN.PRICE,
|
||||
// 'https://dashboard.litlyx.com/payment_ok',
|
||||
// pid,
|
||||
// project.customer_id
|
||||
// )
|
||||
|
||||
if (!intent) {
|
||||
console.error('Cannot create Intent', { plan: PLAN });
|
||||
return setResponseStatus(event, 400, 'Cannot create intent');
|
||||
}
|
||||
// if (!intent) {
|
||||
// console.error('Cannot create Intent', { plan: PLAN });
|
||||
// return setResponseStatus(event, 400, 'Cannot create intent');
|
||||
// }
|
||||
|
||||
return intent.url;
|
||||
// return intent.url;
|
||||
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { getPlanFromId } from "@data/PREMIUM";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
// import StripeService from '~/server/services/StripeService';
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
@@ -9,29 +9,30 @@ export default defineEventHandler(async event => {
|
||||
|
||||
const { project, pid } = data;
|
||||
|
||||
const body = await readBody(event);
|
||||
// const body = await readBody(event);
|
||||
|
||||
const { planId } = body;
|
||||
// const { planId } = body;
|
||||
|
||||
const PLAN = getPlanFromId(planId);
|
||||
// const PLAN = getPlanFromId(planId);
|
||||
|
||||
if (!PLAN) {
|
||||
console.error('PLAN', planId, 'NOT EXIST');
|
||||
return setResponseStatus(event, 400, 'Plan not exist');
|
||||
}
|
||||
// if (!PLAN) {
|
||||
// console.error('PLAN', planId, 'NOT EXIST');
|
||||
// return setResponseStatus(event, 400, 'Plan not exist');
|
||||
// }
|
||||
|
||||
const checkout = await StripeService.createPayment(
|
||||
StripeService.testMode ? PLAN.PRICE_TEST : PLAN.PRICE,
|
||||
'https://dashboard.litlyx.com/payment_ok',
|
||||
pid,
|
||||
project.customer_id
|
||||
);
|
||||
// const checkout = await StripeService.createPayment(
|
||||
// StripeService.testMode ? PLAN.PRICE_TEST : PLAN.PRICE,
|
||||
// 'https://dashboard.litlyx.com/payment_ok',
|
||||
// pid,
|
||||
// project.customer_id
|
||||
// );
|
||||
|
||||
if (!checkout) {
|
||||
console.error('Cannot create payment', { plan: PLAN });
|
||||
return setResponseStatus(event, 400, 'Cannot create payment');
|
||||
}
|
||||
// if (!checkout) {
|
||||
// console.error('Cannot create payment', { plan: PLAN });
|
||||
// return setResponseStatus(event, 400, 'Cannot create payment');
|
||||
// }
|
||||
|
||||
return checkout.url;
|
||||
// return checkout.url;
|
||||
return '';
|
||||
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
import { PaymentServiceHelper } from '~/server/services/PaymentServiceHelper';
|
||||
import { PremiumModel } from '~/shared/schema/PremiumSchema';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
@@ -10,9 +10,9 @@ export default defineEventHandler(async event => {
|
||||
const premium = await PremiumModel.findOne({ user_id: data.user.id })
|
||||
if (!premium) return;
|
||||
|
||||
const customer = await StripeService.getCustomer(premium.customer_id);
|
||||
if (customer?.deleted) return;
|
||||
const [ok, customerInfoOrError] = await PaymentServiceHelper.customer_info(data.user.id);
|
||||
if (!ok) throw customerInfoOrError;
|
||||
|
||||
return customer?.address;
|
||||
return customerInfoOrError;
|
||||
|
||||
});
|
||||
@@ -1,9 +1,7 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
import { PaymentServiceHelper } from "~/server/services/PaymentServiceHelper";
|
||||
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||
|
||||
|
||||
export type InvoiceData = {
|
||||
date: number,
|
||||
cost: number,
|
||||
@@ -21,11 +19,14 @@ export default defineEventHandler(async event => {
|
||||
|
||||
const premium = await PremiumModel.findOne({ user_id: data.user.id });
|
||||
if (!premium) return [];
|
||||
|
||||
const invoices = await StripeService.getInvoices(premium.customer_id);
|
||||
if (!invoices) return [];
|
||||
|
||||
return invoices?.data.map(e => {
|
||||
const [ok, invoicesOrError] = await PaymentServiceHelper.invoices_list(data.user.id);
|
||||
if (!ok) {
|
||||
console.error(invoicesOrError);
|
||||
return [];
|
||||
}
|
||||
|
||||
return invoicesOrError.invoices.map(e => {
|
||||
const result: InvoiceData = {
|
||||
link: e.invoice_pdf || '',
|
||||
id: e.number || '',
|
||||
@@ -36,7 +37,6 @@ export default defineEventHandler(async event => {
|
||||
return result;
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { getPlanFromId, PREMIUM_PLAN } from "@data/PREMIUM";
|
||||
import { canTryAppsumoCode, checkAppsumoCode, useAppsumoCode, useTryAppsumoCode } from "~/server/services/AppsumoService";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
|
||||
function getPlanToActivate(current_plan_id: number) {
|
||||
if (current_plan_id === PREMIUM_PLAN.FREE.ID) {
|
||||
@@ -38,13 +38,13 @@ export default defineEventHandler(async event => {
|
||||
const valid = await checkAppsumoCode(code);
|
||||
if (!valid) return setResponseStatus(event, 400, 'Code not valid');
|
||||
|
||||
const currentPlan = getPlanFromId(project.premium_type);
|
||||
if (!currentPlan) return setResponseStatus(event, 400, 'Current plan not found');
|
||||
const planToActivate = getPlanToActivate(currentPlan.ID);
|
||||
if (!planToActivate) return setResponseStatus(event, 400, 'Cannot use code on current plan');
|
||||
// const currentPlan = getPlanFromId(project.premium_type);
|
||||
// if (!currentPlan) return setResponseStatus(event, 400, 'Current plan not found');
|
||||
// const planToActivate = getPlanToActivate(currentPlan.ID);
|
||||
// if (!planToActivate) return setResponseStatus(event, 400, 'Cannot use code on current plan');
|
||||
|
||||
await StripeService.createSubscription(project.customer_id, planToActivate.ID);
|
||||
// await StripeService.createSubscription(project.customer_id, planToActivate.ID);
|
||||
|
||||
await useAppsumoCode(pid, code);
|
||||
// await useAppsumoCode(pid, code);
|
||||
|
||||
});
|
||||
@@ -1,19 +1,17 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
import { PaymentServiceHelper } from '~/server/services/PaymentServiceHelper';
|
||||
import { PremiumModel } from '~/shared/schema/PremiumSchema';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestData(event, []);
|
||||
if (!data) return;
|
||||
|
||||
const { project } = data;
|
||||
|
||||
if (!project.customer_id) return setResponseStatus(event, 400, 'Project has no customer_id');
|
||||
const premium = await PremiumModel.findOne({ user_id: data.user.id })
|
||||
if (!premium) return;
|
||||
|
||||
const body = await readBody(event);
|
||||
const res = await StripeService.setCustomerInfo(project.customer_id, body);
|
||||
|
||||
return { ok: true, data: res }
|
||||
return await PaymentServiceHelper.update_customer_info(data.user.id, body);
|
||||
|
||||
|
||||
});
|
||||
@@ -1,6 +1,5 @@
|
||||
import { ProjectModel, TProject } from "@schema/project/ProjectSchema";
|
||||
import { ProjectCountModel } from "@schema/project/ProjectsCounts";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -19,57 +18,11 @@ export default defineEventHandler(async event => {
|
||||
const existingUserProjects = await ProjectModel.countDocuments({ owner: userData.id });
|
||||
if (existingUserProjects >= maxProjects) return setResponseStatus(event, 400, 'Already have max number of projects');
|
||||
|
||||
if (StripeService.isDisabled()) {
|
||||
const project = await ProjectModel.create({ owner: userData.id, name: newProjectName });
|
||||
|
||||
const project = await ProjectModel.create({
|
||||
owner: userData.id,
|
||||
name: newProjectName,
|
||||
premium: false,
|
||||
premium_type: 0,
|
||||
customer_id: 'DISABLED_MODE',
|
||||
subscription_id: "DISABLED_MODE",
|
||||
premium_expire_at: new Date(3000, 1, 1)
|
||||
});
|
||||
await ProjectCountModel.create({ project_id: project._id, events: 0, visits: 0, sessions: 0 });
|
||||
|
||||
|
||||
await ProjectCountModel.create({
|
||||
project_id: project._id,
|
||||
events: 0,
|
||||
visits: 0,
|
||||
sessions: 0
|
||||
});
|
||||
|
||||
return project.toJSON() as TProject;
|
||||
|
||||
} else {
|
||||
|
||||
const customer = await StripeService.createCustomer(userData.user.email);
|
||||
if (!customer) return setResponseStatus(event, 400, 'Error creating customer');
|
||||
|
||||
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
|
||||
});
|
||||
|
||||
|
||||
await ProjectCountModel.create({
|
||||
project_id: project._id,
|
||||
events: 0,
|
||||
visits: 0,
|
||||
sessions: 0
|
||||
});
|
||||
|
||||
return project.toJSON() as TProject;
|
||||
|
||||
}
|
||||
return project.toJSON() as TProject;
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { UserLimitModel } from "@schema/UserLimitSchema";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
import { PremiumModel } from "~/shared/schema/PremiumSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
@@ -22,14 +21,12 @@ export default defineEventHandler(async event => {
|
||||
billing_expire_at: userLimits.billing_expire_at,
|
||||
limit: userLimits.limit,
|
||||
count: userLimits.events + userLimits.visits,
|
||||
subscription_status: StripeService.isDisabled() ? 'Disabled mode' : ('One time payment')
|
||||
subscription_status: 'One time'
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const subscription = await StripeService.getSubscription(premium.subscription_id);
|
||||
|
||||
const userLimits = await UserLimitModel.findOne({ user_id: data.user.id });
|
||||
if (!userLimits) return setResponseStatus(event, 400, 'User limits not found');
|
||||
|
||||
@@ -41,7 +38,7 @@ export default defineEventHandler(async event => {
|
||||
billing_expire_at: userLimits.billing_expire_at,
|
||||
limit: userLimits.limit,
|
||||
count: userLimits.events + userLimits.visits,
|
||||
subscription_status: StripeService.isDisabled() ? 'Disabled mode' : (subscription?.status ?? '?')
|
||||
subscription_status: ''
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -6,7 +6,6 @@ import { UserSettingsModel } from "@schema/UserSettings";
|
||||
import { AiChatModel } from "@schema/ai/AiChatSchema";
|
||||
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
|
||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||
import StripeService from "~/server/services/StripeService";
|
||||
import { UserModel } from "@schema/UserSchema";
|
||||
import { AddressBlacklistModel } from "~/shared/schema/shields/AddressBlacklistSchema";
|
||||
import { DomainWhitelistModel } from "~/shared/schema/shields/DomainWhitelistSchema";
|
||||
@@ -36,7 +35,7 @@ export default defineEventHandler(async event => {
|
||||
const limitdeletation = await UserLimitModel.deleteMany({ user_id: userData.id });
|
||||
const notifiesDeletation = await LimitNotifyModel.deleteMany({ user_id: userData.id });
|
||||
|
||||
await StripeService.deleteCustomer(premium.customer_id);
|
||||
// await StripeService.deleteCustomer(premium.customer_id);
|
||||
|
||||
for (const project of projects) {
|
||||
const project_id = project._id;
|
||||
@@ -51,12 +50,11 @@ export default defineEventHandler(async event => {
|
||||
const botTrafficOptionsDeletation = await BotTrafficOptionModel.deleteMany({ project_id });
|
||||
const countryBlacklistDeletation = await CountryBlacklistModel.deleteMany({ project_id });
|
||||
const domainWhitelistDeletation = await DomainWhitelistModel.deleteMany({ project_id });
|
||||
|
||||
|
||||
const userDeletation = await UserModel.deleteOne({ _id: userData.id });
|
||||
|
||||
|
||||
}
|
||||
|
||||
const userDeletation = await UserModel.deleteOne({ _id: userData.id });
|
||||
|
||||
return { ok: true };
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user