mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
update payment system
This commit is contained in:
@@ -2,77 +2,56 @@ import { ProjectModel } from "./shared/schema/project/ProjectSchema";
|
||||
import { UserModel } from "./shared/schema/UserSchema";
|
||||
import { LimitNotifyModel } from "./shared/schema/broker/LimitNotifySchema";
|
||||
import { EmailService } from './shared/services/EmailService';
|
||||
import { TProjectLimit } from "./shared/schema/project/ProjectsLimits";
|
||||
import { EmailServiceHelper } from "./EmailServiceHelper";
|
||||
import { TUserLimit } from "./shared/schema/UserLimitSchema";
|
||||
|
||||
|
||||
export async function checkLimitsForEmail(projectCounts: TProjectLimit) {
|
||||
export async function checkLimitsForEmail(projectCounts: TUserLimit) {
|
||||
|
||||
const project_id = projectCounts.project_id;
|
||||
const hasNotifyEntry = await LimitNotifyModel.findOne({ project_id });
|
||||
const user_id = projectCounts.user_id;
|
||||
|
||||
const hasNotifyEntry = await LimitNotifyModel.findOne({ user_id });
|
||||
if (!hasNotifyEntry) {
|
||||
await LimitNotifyModel.create({ project_id, limit1: false, limit2: false, limit3: false })
|
||||
await LimitNotifyModel.create({ user_id, limit1: false, limit2: false, limit3: false })
|
||||
}
|
||||
|
||||
const owner = await UserModel.findById(project.owner);
|
||||
if (!owner) return;
|
||||
|
||||
const userName = owner.given_name || owner.name || 'no_name';
|
||||
|
||||
if ((projectCounts.visits + projectCounts.events) >= (projectCounts.limit)) {
|
||||
|
||||
if (hasNotifyEntry.limit3 === true) return;
|
||||
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return;
|
||||
|
||||
const owner = await UserModel.findById(project.owner);
|
||||
if (!owner) return;
|
||||
|
||||
setImmediate(() => {
|
||||
const emailData = EmailService.getEmailServerInfo('limit_max', {
|
||||
target: owner.email,
|
||||
projectName: project.name
|
||||
});
|
||||
const emailData = EmailService.getEmailServerInfo('limit_max', { target: owner.email, userName });
|
||||
EmailServiceHelper.sendEmail(emailData);
|
||||
});
|
||||
|
||||
await LimitNotifyModel.updateOne({ project_id: projectCounts.project_id }, { limit1: true, limit2: true, limit3: true });
|
||||
await LimitNotifyModel.updateOne({ user_id }, { limit1: true, limit2: true, limit3: true });
|
||||
|
||||
} else if ((projectCounts.visits + projectCounts.events) >= (projectCounts.limit * 0.9)) {
|
||||
|
||||
if (hasNotifyEntry.limit2 === true) return;
|
||||
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return;
|
||||
|
||||
const owner = await UserModel.findById(project.owner);
|
||||
if (!owner) return;
|
||||
|
||||
setImmediate(() => {
|
||||
const emailData = EmailService.getEmailServerInfo('limit_90', {
|
||||
target: owner.email,
|
||||
projectName: project.name
|
||||
});
|
||||
const emailData = EmailService.getEmailServerInfo('limit_90', { target: owner.email, userName });
|
||||
EmailServiceHelper.sendEmail(emailData);
|
||||
});
|
||||
|
||||
await LimitNotifyModel.updateOne({ project_id: projectCounts.project_id }, { limit1: true, limit2: true, limit3: false });
|
||||
await LimitNotifyModel.updateOne({ user_id }, { limit1: true, limit2: true, limit3: false });
|
||||
|
||||
} else if ((projectCounts.visits + projectCounts.events) >= (projectCounts.limit * 0.5)) {
|
||||
|
||||
if (hasNotifyEntry.limit1 === true) return;
|
||||
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return;
|
||||
|
||||
const owner = await UserModel.findById(project.owner);
|
||||
if (!owner) return;
|
||||
|
||||
setImmediate(() => {
|
||||
const emailData = EmailService.getEmailServerInfo('limit_50', {
|
||||
target: owner.email,
|
||||
projectName: project.name
|
||||
});
|
||||
const emailData = EmailService.getEmailServerInfo('limit_50', { target: owner.email, userName });
|
||||
EmailServiceHelper.sendEmail(emailData);
|
||||
});
|
||||
|
||||
await LimitNotifyModel.updateOne({ project_id: projectCounts.project_id }, { limit1: true, limit2: false, limit3: false });
|
||||
|
||||
await LimitNotifyModel.updateOne({ user_id }, { limit1: true, limit2: false, limit3: false });
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
|
||||
|
||||
import { ProjectLimitModel } from './shared/schema/project/ProjectsLimits';
|
||||
import { MAX_LOG_LIMIT_PERCENT } from './shared/data/broker/Limits';
|
||||
import { checkLimitsForEmail } from './EmailController';
|
||||
import { UserLimitModel } from './shared/schema/UserLimitSchema';
|
||||
|
||||
export async function checkLimits(project_id: string) {
|
||||
const projectLimits = await ProjectLimitModel.findOne({ project_id });
|
||||
if (!projectLimits) return false;
|
||||
const TOTAL_COUNT = projectLimits.events + projectLimits.visits;
|
||||
const COUNT_LIMIT = projectLimits.limit;
|
||||
export async function checkLimits(user_id: string) {
|
||||
const userLimits = await UserLimitModel.findOne({ user_id });
|
||||
if (!userLimits) return false;
|
||||
const TOTAL_COUNT = userLimits.events + userLimits.visits;
|
||||
const COUNT_LIMIT = userLimits.limit;
|
||||
if ((TOTAL_COUNT) > COUNT_LIMIT * MAX_LOG_LIMIT_PERCENT) return false;
|
||||
await checkLimitsForEmail(projectLimits);
|
||||
await checkLimitsForEmail(userLimits);
|
||||
return true;
|
||||
}
|
||||
@@ -11,10 +11,9 @@ import { UAParser } from 'ua-parser-js';
|
||||
import { checkLimits } from './LimitChecker';
|
||||
import express from 'express';
|
||||
|
||||
import { ProjectLimitModel } from './shared/schema/project/ProjectsLimits';
|
||||
import { ProjectCountModel } from './shared/schema/project/ProjectsCounts';
|
||||
import { metricsRouter } from './Metrics';
|
||||
|
||||
import { UserLimitModel } from './shared/schema/UserLimitSchema';
|
||||
|
||||
const app = express();
|
||||
|
||||
@@ -27,6 +26,12 @@ main();
|
||||
|
||||
const CONSUMER_NAME = `CONSUMER_${process.env.NODE_APP_INSTANCE || 'DEFAULT'}`
|
||||
|
||||
|
||||
async function getProjectOwner(pid: string) {
|
||||
const ownerData = await ProjectModel.findOne({ _id: pid }, { owner: 1 });
|
||||
return ownerData.owner;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
||||
await RedisStreamService.connect();
|
||||
@@ -51,18 +56,18 @@ async function processStreamEntry(data: Record<string, string>) {
|
||||
|
||||
const { pid, sessionHash } = data;
|
||||
|
||||
const project = await ProjectModel.exists({ _id: pid });
|
||||
if (!project) return;
|
||||
const owner = await getProjectOwner(pid);
|
||||
if (!owner) return;
|
||||
|
||||
const canLog = await checkLimits(pid);
|
||||
const canLog = await checkLimits(owner.toString());
|
||||
if (!canLog) return;
|
||||
|
||||
if (eventType === 'event') {
|
||||
await process_event(data, sessionHash);
|
||||
await process_event(data, sessionHash, owner.toString());
|
||||
} else if (eventType === 'keep_alive') {
|
||||
await process_keep_alive(data, sessionHash);
|
||||
await process_keep_alive(data, sessionHash, owner.toString());
|
||||
} else if (eventType === 'visit') {
|
||||
await process_visit(data, sessionHash);
|
||||
await process_visit(data, sessionHash, owner.toString());
|
||||
}
|
||||
|
||||
} catch (ex: any) {
|
||||
@@ -75,7 +80,7 @@ async function processStreamEntry(data: Record<string, string>) {
|
||||
|
||||
}
|
||||
|
||||
async function process_visit(data: Record<string, string>, sessionHash: string) {
|
||||
async function process_visit(data: Record<string, string>, sessionHash: string, user_id: string) {
|
||||
|
||||
const { pid, ip, website, page, referrer, userAgent, flowHash, timestamp } = data;
|
||||
|
||||
@@ -105,12 +110,12 @@ async function process_visit(data: Record<string, string>, sessionHash: string)
|
||||
created_at: new Date(parseInt(timestamp))
|
||||
}),
|
||||
ProjectCountModel.updateOne({ project_id: pid }, { $inc: { 'visits': 1 } }, { upsert: true }),
|
||||
ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { 'visits': 1 } })
|
||||
UserLimitModel.updateOne({ user_id }, { $inc: { 'visits': 1 } })
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
async function process_keep_alive(data: Record<string, string>, sessionHash: string) {
|
||||
async function process_keep_alive(data: Record<string, string>, sessionHash: string, user_id: string) {
|
||||
|
||||
const { pid, instant, flowHash, timestamp, website } = data;
|
||||
|
||||
@@ -137,7 +142,7 @@ async function process_keep_alive(data: Record<string, string>, sessionHash: str
|
||||
|
||||
}
|
||||
|
||||
async function process_event(data: Record<string, string>, sessionHash: string) {
|
||||
async function process_event(data: Record<string, string>, sessionHash: string, user_id: string) {
|
||||
|
||||
const { name, metadata, pid, flowHash, timestamp, website } = data;
|
||||
|
||||
@@ -155,7 +160,7 @@ async function process_event(data: Record<string, string>, sessionHash: string)
|
||||
created_at: new Date(parseInt(timestamp))
|
||||
}),
|
||||
ProjectCountModel.updateOne({ project_id: pid }, { $inc: { 'events': 1 } }, { upsert: true }),
|
||||
ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { 'events': 1 } })
|
||||
UserLimitModel.updateOne({ user_id }, { $inc: { 'events': 1 } })
|
||||
]);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user