fix dashboard premium tables

This commit is contained in:
Emily
2025-04-05 16:32:38 +02:00
parent 70c15238a0
commit 10d4a9f1bc
24 changed files with 341 additions and 370 deletions

View File

@@ -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);

View File

@@ -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' });

View File

@@ -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 });