fix delete account

This commit is contained in:
Emily
2024-06-06 15:08:55 +02:00
parent f8a3a5ce68
commit c15e5d966e
8 changed files with 97 additions and 52 deletions

View File

@@ -1,5 +1,7 @@
import { ProjectCountModel } from "@schema/ProjectsCounts";
import { EventModel } from "@schema/metrics/EventSchema";
import { VisitModel } from "@schema/metrics/VisitSchema";
export default defineEventHandler(async event => {
const userData = getRequestUser(event);
@@ -9,7 +11,10 @@ export default defineEventHandler(async event => {
const { project_id } = getQuery(event);
if (!project_id) return setResponseStatus(event, 400, 'ProjectId is required');
await ProjectCountModel.updateOne({ project_id, events: 0, visits: 0 }, {}, { upsert: true });
const events = await EventModel.countDocuments({ project_id });
const visits = await VisitModel.countDocuments({ project_id });
await ProjectCountModel.updateOne({ project_id, events, visits }, {}, { upsert: true });
return { ok: true };
});

View File

@@ -1,18 +1,21 @@
import { ProjectModel } from "@schema/ProjectSchema";
import { ProjectCountModel } from "@schema/ProjectsCounts";
import { ProjectLimitModel } from "@schema/ProjectsLimits";
import { SessionModel } from "@schema/metrics/SessionSchema";
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
import StripeService from '~/server/services/StripeService';
import { AiChatModel } from "@schema/ai/AiChatSchema";
export default defineEventHandler(async event => {
const body = await readBody(event);
const projectId = body.project_id;
const project_id = body.project_id;
const userData = getRequestUser(event);
if (!userData?.logged) return setResponseStatus(event, 400, 'NotLogged');
const project = await ProjectModel.findById(projectId);
const project = await ProjectModel.findById(project_id);
if (!project) return setResponseStatus(event, 400, 'Project not exist');
const projects = await ProjectModel.countDocuments({ owner: userData.id });
@@ -22,18 +25,25 @@ export default defineEventHandler(async event => {
await StripeService.deleteCustomer(project.customer_id);
const countDeletation = await ProjectCountModel.deleteOne({ owner: userData.id, _id: projectId });
const limitdeletation = await ProjectLimitModel.deleteOne({ owner: userData.id, _id: projectId });
const projectDeletation = await ProjectModel.deleteOne({ owner: userData.id, _id: projectId });
const projectDeletation = await ProjectModel.deleteOne({ _id: project_id });
const countDeletation = await ProjectCountModel.deleteMany({ project_id });
const limitdeletation = await ProjectLimitModel.deleteMany({ project_id });
const sessionsDeletation = await SessionModel.deleteMany({ project_id });
const notifiesDeletation = await LimitNotifyModel.deleteMany({ project_id });
const aiChatsDeletation = await AiChatModel.deleteMany({ project_id });
const ok = countDeletation.acknowledged && limitdeletation.acknowledged &&
projectDeletation.acknowledged && sessionsDeletation.acknowledged && notifiesDeletation.acknowledged && aiChatsDeletation.acknowledged
const ok = countDeletation.acknowledged && limitdeletation.acknowledged && projectDeletation.acknowledged
return {
ok,
data: [
countDeletation.acknowledged,
limitdeletation.acknowledged,
projectDeletation.acknowledged
projectDeletation.acknowledged,
sessionsDeletation.acknowledged,
notifiesDeletation.acknowledged,
aiChatsDeletation.acknowledged
]
};

View File

@@ -0,0 +1,38 @@
import { ProjectModel } from "@schema/ProjectSchema";
import { ProjectCountModel } from "@schema/ProjectsCounts";
import { ProjectLimitModel } from "@schema/ProjectsLimits";
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";
export default defineEventHandler(async event => {
const userData = getRequestUser(event);
if (!userData?.logged) return;
const projects = await ProjectModel.find({ owner: userData.id });
const premiumProjects = projects.filter(e => { return e.premium && e.premium_type != 0 }).length;
if (premiumProjects > 0) return setResponseStatus(event, 400, 'Cannot delete an account with a premium project');
for (const project of projects) {
const project_id = project._id;
await StripeService.deleteCustomer(project.customer_id);
const projectDeletation = await ProjectModel.deleteOne({ _id: project_id });
const userSettingsDeletation = await UserSettingsModel.deleteOne({ project_id });
const countDeletation = await ProjectCountModel.deleteMany({ project_id });
const limitdeletation = await ProjectLimitModel.deleteMany({ project_id });
const sessionsDeletation = await SessionModel.deleteMany({ project_id });
const notifiesDeletation = await LimitNotifyModel.deleteMany({ project_id });
const aiChatsDeletation = await AiChatModel.deleteMany({ project_id });
}
return { ok: true };
});