mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-11 00:08:37 +01:00
fix dashboard premium tables
This commit is contained in:
@@ -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) {
|
||||
|
||||
await PremiumModel.updateOne({ _id: user_id }, {
|
||||
await PremiumModel.updateOne({ user_id }, {
|
||||
premium_type: plan.ID,
|
||||
subscription_id,
|
||||
expire_at: current_period_end * 1000
|
||||
}, { upsert: true });
|
||||
|
||||
await UserLimitModel.updateOne({ _id: user_id }, {
|
||||
await UserLimitModel.updateOne({ user_id }, {
|
||||
events: 0,
|
||||
visits: 0,
|
||||
ai_messages: 0,
|
||||
@@ -31,9 +31,9 @@ async function addSubscriptionToUser(user_id: string, plan: PLAN_DATA, subscript
|
||||
|
||||
export async function onPaymentFailed(event: Event.InvoicePaymentFailedEvent) {
|
||||
|
||||
|
||||
|
||||
if (event.data.object.attempt_count == 0) return { received: true, warn: 'attempt_count = 0' }
|
||||
|
||||
|
||||
//TODO: Send emails
|
||||
|
||||
const customer_id = event.data.object.customer as string;
|
||||
@@ -70,7 +70,11 @@ export async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||
const databaseSubscription = premiumData.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);
|
||||
|
||||
@@ -4,6 +4,7 @@ import { getPlanFromId } from '../shared/data/PLANS';
|
||||
import StripeService from '../services/StripeService';
|
||||
import { sendJson } from '../Utils';
|
||||
import { PremiumModel } from '../shared/schema/PremiumSchema';
|
||||
import { Types } from 'mongoose';
|
||||
|
||||
export const paymentRouter = Router();
|
||||
|
||||
@@ -20,7 +21,7 @@ paymentRouter.post('/create', json(), async (req, res) => {
|
||||
const plan = getPlanFromId(createPaymentData.plan_id);
|
||||
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.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 StripeService from '../services/StripeService';
|
||||
|
||||
@@ -8,7 +8,7 @@ import * as WebhookController from '../controllers/WebhookController'
|
||||
export const webhookRouter = Router();
|
||||
|
||||
|
||||
webhookRouter.get('/', json(), async (req, res) => {
|
||||
webhookRouter.post('/', raw({ type: 'application/json' }), async (req, res) => {
|
||||
try {
|
||||
|
||||
const signature = req.header('stripe-signature');
|
||||
@@ -26,7 +26,7 @@ webhookRouter.get('/', json(), async (req, res) => {
|
||||
const response = await WebhookController.onPaymentFailed(eventData);
|
||||
return sendJson(res, 200, response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} catch (ex) {
|
||||
res.status(500).json({ error: ex.message });
|
||||
|
||||
Reference in New Issue
Block a user