add pricing

This commit is contained in:
Emily
2024-06-05 15:40:51 +02:00
parent f7891a94cd
commit 854d6eb528
22 changed files with 435 additions and 294 deletions

View File

@@ -1,9 +1,9 @@
import { TProjectCount } from "@schema/ProjectsCounts";
import { ProjectModel } from "@schema/ProjectSchema";
import { UserModel } from "@schema/UserSchema";
import { LimitNotifyModel } from "@schema/broker/LimitNotifySchema";
import EmailService from '@services/EmailService';
import { requireEnv } from "../../shared/utilts/requireEnv";
import { TProjectLimit } from "@schema/ProjectsLimits";
EmailService.createTransport(
@@ -13,7 +13,7 @@ EmailService.createTransport(
requireEnv('EMAIL_PASS'),
);
export async function checkLimitsForEmail(projectCounts: TProjectCount) {
export async function checkLimitsForEmail(projectCounts: TProjectLimit) {
if ((projectCounts.visits + projectCounts.events) >= (projectCounts.limit / 2)) {
const notify = await LimitNotifyModel.findOne({ project_id: projectCounts._id });

View File

@@ -1,7 +1,6 @@
import { Router, json } from "express";
import { createSessionHash, getIPFromRequest } from "../../utils/Utils";
import { checkProjectCount } from "@functions/UtilsProjectCounts";
import { SessionModel } from "@schema/metrics/SessionSchema";
import { EVENT_LOG_LIMIT_PERCENT } from '@data/broker/Limits';
@@ -13,6 +12,8 @@ import { VisitModel } from "@schema/metrics/VisitSchema";
import { EventModel } from "@schema/metrics/EventSchema";
import { ProjectCountModel } from "@schema/ProjectsCounts";
import { checkLimitsForEmail } from "../../Controller";
import { ProjectLimitModel } from "@schema/ProjectsLimits";
import { ProjectModel } from "@schema/ProjectSchema";
const router = Router();
@@ -55,13 +56,21 @@ router.post('/metrics/push', json(jsonOptions), async (req, res) => {
const { pid } = req.body;
const projectCounts = await checkProjectCount(pid);
const projectExist = await ProjectModel.exists({ _id: pid });
if (!projectExist) return res.status(400).json({ error: 'Project not exist' });
const projectLimits = await ProjectLimitModel.findOne({ project_id: pid });
if (!projectLimits) return res.status(400).json({ error: 'No limits found' });
const TOTAL_COUNT = projectLimits.events + projectLimits.visits;
const COUNT_LIMIT = projectLimits.limit;
if ((TOTAL_COUNT * EVENT_LOG_LIMIT_PERCENT) > COUNT_LIMIT) {
return res.status(200).json({ error: 'Limit reached' });
};
await checkLimitsForEmail(projectLimits);
const TOTAL_COUNT = projectCounts.events + projectCounts.visits;
const LIMIT = projectCounts.limit;
if ((TOTAL_COUNT * EVENT_LOG_LIMIT_PERCENT) > LIMIT) return;
await checkLimitsForEmail(projectCounts);
const ip = getIPFromRequest(req);
@@ -113,7 +122,11 @@ router.post('/metrics/push', json(jsonOptions), async (req, res) => {
const fieldToInc = type === EventType.VISIT ? 'visits' : 'events';
await ProjectCountModel.updateOne({ _id: projectCounts._id }, { $inc: { [fieldToInc]: 1 } });
await ProjectCountModel.updateOne({ project_id: pid }, { $inc: { [fieldToInc]: 1 } }, { upsert: true });
await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { [fieldToInc]: 1 } });
return res.sendStatus(200);