mirror of
https://github.com/Litlyx/litlyx
synced 2026-02-05 07:02:19 +01:00
add pricing
This commit is contained in:
@@ -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;
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user