mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
refactoring dashboard
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import { AuthContext } from "./middleware/01-authorization";
|
||||
import { ProjectModel } from "@schema/project/ProjectSchema";
|
||||
import { LITLYX_PROJECT_ID } from '@data/LITLYX'
|
||||
import { hasAccessToProject } from "./utils/hasAccessToProject";
|
||||
|
||||
export async function getUserProjectFromId(project_id: string, user: AuthContext | undefined, allowGuest: boolean = true) {
|
||||
if (!project_id) return;
|
||||
|
||||
if (project_id === LITLYX_PROJECT_ID) {
|
||||
if (project_id === "6643cd08a1854e3b81722ab5") {
|
||||
return await ProjectModel.findOne({ _id: project_id });
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { createUserJwt, readRegisterJwt } from '~/server/AuthManager';
|
||||
import { UserModel } from '@schema/UserSchema';
|
||||
import { PasswordModel } from '@schema/PasswordSchema';
|
||||
import EmailService from '@services/EmailService';
|
||||
// import EmailService from '@services/EmailService';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -14,7 +14,7 @@ export default defineEventHandler(async event => {
|
||||
try {
|
||||
await PasswordModel.create({ email: data.email, password: data.password })
|
||||
await UserModel.create({ email: data.email, given_name: '', name: 'EmailLogin', locale: '', picture: '', created_at: Date.now() });
|
||||
setImmediate(() => { EmailService.sendWelcomeEmail(data.email); });
|
||||
// setImmediate(() => { EmailService.sendWelcomeEmail(data.email); });
|
||||
const jwt = createUserJwt({ email: data.email, name: 'EmailLogin' });
|
||||
return sendRedirect(event,`https://dashboard.litlyx.com/jwt_login?jwt_login=${jwt}`);
|
||||
} catch (ex) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { OAuth2Client } from 'google-auth-library';
|
||||
import { createUserJwt } from '~/server/AuthManager';
|
||||
import { UserModel } from '@schema/UserSchema';
|
||||
import EmailService from '@services/EmailService';
|
||||
// import EmailService from '@services/EmailService';
|
||||
|
||||
const { GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig()
|
||||
|
||||
@@ -58,10 +58,10 @@ export default defineEventHandler(async event => {
|
||||
|
||||
const savedUser = await newUser.save();
|
||||
|
||||
setImmediate(() => {
|
||||
console.log('SENDING WELCOME EMAIL TO', payload.email);
|
||||
if (payload.email) EmailService.sendWelcomeEmail(payload.email);
|
||||
});
|
||||
// setImmediate(() => {
|
||||
// console.log('SENDING WELCOME EMAIL TO', payload.email);
|
||||
// if (payload.email) EmailService.sendWelcomeEmail(payload.email);
|
||||
// });
|
||||
|
||||
return { error: false, access_token: createUserJwt({ email: savedUser.email, name: savedUser.name }) }
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { createRegisterJwt, createUserJwt } from '~/server/AuthManager';
|
||||
import { UserModel } from '@schema/UserSchema';
|
||||
import { RegisterModel } from '@schema/RegisterSchema';
|
||||
import EmailService from '@services/EmailService';
|
||||
// import EmailService from '@services/EmailService';
|
||||
import crypto from 'crypto';
|
||||
|
||||
function canRegister(email: string, password: string) {
|
||||
@@ -33,9 +33,9 @@ export default defineEventHandler(async event => {
|
||||
|
||||
await RegisterModel.create({ email, password: hashedPassword });
|
||||
|
||||
setImmediate(() => {
|
||||
EmailService.sendConfirmEmail(email, `https://dashboard.litlyx.com/api/auth/confirm_email?register_code=${jwt}`);
|
||||
});
|
||||
// setImmediate(() => {
|
||||
// EmailService.sendConfirmEmail(email, `https://dashboard.litlyx.com/api/auth/confirm_email?register_code=${jwt}`);
|
||||
// });
|
||||
|
||||
return {
|
||||
error: false,
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { getRequestDataOld } from "~/server/utils/getRequestData";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestDataOld(event, { requireSchema: false });
|
||||
const data = await getRequestData(event, ['GUEST', 'RANGE', 'GUEST', 'DOMAIN']);
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, project_id, limit } = data;
|
||||
const { pid, from, to, project_id, limit, domain } = data;
|
||||
|
||||
const cacheKey = `browsers:${pid}:${limit}:${from}:${to}`;
|
||||
const cacheKey = `browsers:${pid}:${limit}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
@@ -19,7 +18,8 @@ export default defineEventHandler(async event => {
|
||||
{
|
||||
$match: {
|
||||
project_id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
||||
website: domain
|
||||
}
|
||||
},
|
||||
{ $group: { _id: "$browser", count: { $sum: 1, } } },
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { getRequestDataOld } from "~/server/utils/getRequestData";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestDataOld(event, { requireSchema: false });
|
||||
const data = await getRequestData(event, ['GUEST', 'RANGE', 'GUEST', 'DOMAIN']);
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, project_id, limit } = data;
|
||||
const { pid, from, to, project_id, limit, domain } = data;
|
||||
|
||||
const cacheKey = `oss:${pid}:${limit}:${from}:${to}`;
|
||||
const cacheKey = `oss:${pid}:${limit}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
@@ -19,7 +18,8 @@ export default defineEventHandler(async event => {
|
||||
{
|
||||
$match: {
|
||||
project_id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
||||
website: domain
|
||||
}
|
||||
},
|
||||
{ $group: { _id: "$os", count: { $sum: 1, } } },
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { getRequestDataOld } from "~/server/utils/getRequestData";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestDataOld(event, { requireSchema: false });
|
||||
const data = await getRequestData(event, ['GUEST', 'RANGE', 'GUEST', 'DOMAIN']);
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, project_id, limit } = data;
|
||||
const { pid, from, to, project_id, limit, domain } = data;
|
||||
|
||||
const cacheKey = `websites:${pid}:${limit}:${from}:${to}`;
|
||||
const cacheKey = `pages:${pid}:${limit}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
@@ -19,10 +18,11 @@ export default defineEventHandler(async event => {
|
||||
{
|
||||
$match: {
|
||||
project_id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
||||
}
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) },
|
||||
website: domain
|
||||
},
|
||||
},
|
||||
{ $group: { _id: "$website", count: { $sum: 1, } } },
|
||||
{ $group: { _id: "$page", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: limit }
|
||||
]);
|
||||
@@ -1,37 +0,0 @@
|
||||
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { getRequestDataOld } from "~/server/utils/getRequestData";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestDataOld(event, { requireSchema: false });
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, project_id, limit } = data;
|
||||
|
||||
const websiteName = getHeader(event, 'x-website-name');
|
||||
|
||||
const cacheKey = `websites_pages:${websiteName}:${pid}:${limit}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
|
||||
const result = await VisitModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id,
|
||||
created_at: { $gte: new Date(from), $lte: new Date(to) }
|
||||
},
|
||||
},
|
||||
{ $match: { website: websiteName, }, },
|
||||
{ $group: { _id: "$page", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: limit }
|
||||
]);
|
||||
|
||||
return result as { _id: string, count: number }[];
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,5 +1,4 @@
|
||||
import { getPlanFromId } from "@data/PREMIUM";
|
||||
import { PREMIUM_PLAN } from "../../../../shared/data/PREMIUM";
|
||||
import { getPlanFromId, PREMIUM_PLAN } from "@data/PREMIUM";
|
||||
import { canTryAppsumoCode, checkAppsumoCode, useAppsumoCode, useTryAppsumoCode } from "~/server/services/AppsumoService";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import type Event from 'stripe';
|
||||
import { ProjectModel } from '@schema/project/ProjectSchema';
|
||||
import { PREMIUM_DATA, PREMIUM_PLAN, getPlanFromId, getPlanFromPrice, getPlanFromTag } from '@data/PREMIUM';
|
||||
import { ProjectLimitModel } from '@schema/project/ProjectsLimits';
|
||||
import EmailService from '@services/EmailService'
|
||||
// import EmailService from '@services/EmailService'
|
||||
import { UserModel } from '@schema/UserSchema';
|
||||
|
||||
|
||||
@@ -93,9 +93,9 @@ async function onPaymentOnetimeSuccess(event: Event.PaymentIntentSucceededEvent)
|
||||
const user = await UserModel.findOne({ _id: project.owner });
|
||||
if (!user) return { ok: false, error: 'USER NOT EXIST FOR PROJECT' + project.id }
|
||||
|
||||
setTimeout(() => {
|
||||
EmailService.sendPurchaseEmail(user.email, project.name);
|
||||
}, 1);
|
||||
// setTimeout(() => {
|
||||
// EmailService.sendPurchaseEmail(user.email, project.name);
|
||||
// }, 1);
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
@@ -140,10 +140,10 @@ async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
||||
const user = await UserModel.findOne({ _id: project.owner });
|
||||
if (!user) return { ok: false, error: 'USER NOT EXIST FOR PROJECT' + project.id }
|
||||
|
||||
setTimeout(() => {
|
||||
if (PLAN.ID == 0) return;
|
||||
if (isNewSubscription) EmailService.sendPurchaseEmail(user.email, project.name);
|
||||
}, 1);
|
||||
// setTimeout(() => {
|
||||
// if (PLAN.ID == 0) return;
|
||||
// if (isNewSubscription) EmailService.sendPurchaseEmail(user.email, project.name);
|
||||
// }, 1);
|
||||
|
||||
|
||||
return { ok: true };
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
|
||||
import { executeTimelineAggregation } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestDataOld(event, { requireSchema: false, requireSlice: true });
|
||||
const data = await getRequestData(event, ['SLICE', 'GUEST', 'DOMAIN', 'RANGE', 'OFFSET']);
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, slice, project_id, timeOffset } = data;
|
||||
const { pid, from, to, slice, project_id, timeOffset, domain } = data;
|
||||
|
||||
const cacheKey = `timeline:events:${pid}:${slice}:${from}:${to}`;
|
||||
const cacheKey = `timeline:events:${pid}:${slice}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: EventModel,
|
||||
from, to, slice, timeOffset
|
||||
from, to, slice, timeOffset, domain, debug: true
|
||||
});
|
||||
return timelineData;
|
||||
});
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import { SessionModel } from "@schema/metrics/SessionSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
|
||||
import { executeTimelineAggregation } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestDataOld(event, { requireSchema: false, requireSlice: true });
|
||||
const data = await getRequestData(event, ['SLICE', 'GUEST', 'DOMAIN', 'RANGE', 'OFFSET']);
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, slice, project_id, timeOffset } = data;
|
||||
const { pid, from, to, slice, project_id, timeOffset, domain } = data;
|
||||
|
||||
const cacheKey = `timeline:sessions:${pid}:${slice}:${from}:${to}`;
|
||||
const cacheKey = `timeline:sessions:${pid}:${slice}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: SessionModel,
|
||||
from, to, slice, timeOffset
|
||||
from, to, slice, timeOffset, domain
|
||||
});
|
||||
return timelineData;
|
||||
});
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import { executeTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
|
||||
import { executeTimelineAggregation } from "~/server/services/TimelineService";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const data = await getRequestDataOld(event, { requireSchema: false, requireSlice: true });
|
||||
const data = await getRequestData(event, ['SLICE', 'GUEST', 'DOMAIN', 'RANGE', 'OFFSET']);
|
||||
if (!data) return;
|
||||
|
||||
const { pid, from, to, slice, project_id, timeOffset } = data;
|
||||
const { pid, from, to, slice, project_id, timeOffset, domain } = data;
|
||||
|
||||
const cacheKey = `timeline:visits:${pid}:${slice}:${from}:${to}`;
|
||||
const cacheKey = `timeline:visits:${pid}:${slice}:${from}:${to}:${domain}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async () => {
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: project_id,
|
||||
model: VisitModel,
|
||||
from, to, slice, timeOffset
|
||||
from, to, slice, timeOffset, domain
|
||||
});
|
||||
return timelineData;
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
import crypto from 'crypto';
|
||||
import { PasswordModel } from '@schema/PasswordSchema';
|
||||
import EmailService from '@services/EmailService'
|
||||
// import EmailService from '@services/EmailService'
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -19,7 +19,7 @@ export default defineEventHandler(async event => {
|
||||
target.password = hashedPassword;
|
||||
await target.save();
|
||||
|
||||
await EmailService.sendResetPasswordEmail(email, newPass);
|
||||
// await EmailService.sendResetPasswordEmail(email, newPass);
|
||||
|
||||
return { error: false, message: 'Password changed' }
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import mongoose from "mongoose";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
import EmailService from '@services/EmailService';
|
||||
// import EmailService from '@services/EmailService';
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
import { logger } from "./Logger";
|
||||
|
||||
@@ -14,10 +14,10 @@ export default async () => {
|
||||
|
||||
logger.info('[SERVER] Initializing');
|
||||
|
||||
if (config.EMAIL_SERVICE) {
|
||||
EmailService.init(config.BREVO_API_KEY);
|
||||
logger.info('[EMAIL] Initialized');
|
||||
}
|
||||
// if (config.EMAIL_SERVICE) {
|
||||
// EmailService.init(config.BREVO_API_KEY);
|
||||
// logger.info('[EMAIL] Initialized');
|
||||
// }
|
||||
|
||||
|
||||
if (config.STRIPE_SECRET) {
|
||||
|
||||
@@ -11,14 +11,16 @@ export type TimelineAggregationOptions = {
|
||||
to: string | number,
|
||||
slice: Slice,
|
||||
timeOffset?: number,
|
||||
debug?: boolean
|
||||
debug?: boolean,
|
||||
domain?: string
|
||||
}
|
||||
|
||||
export type AdvancedTimelineAggregationOptions = TimelineAggregationOptions & {
|
||||
customMatch?: Record<string, any>,
|
||||
customGroup?: Record<string, any>,
|
||||
customProjection?: Record<string, any>,
|
||||
customIdGroup?: Record<string, any>
|
||||
customIdGroup?: Record<string, any>,
|
||||
customAfterMatch?: Record<string, any>
|
||||
}
|
||||
|
||||
export async function executeAdvancedTimelineAggregation<T = {}>(options: AdvancedTimelineAggregationOptions) {
|
||||
@@ -36,6 +38,9 @@ export async function executeAdvancedTimelineAggregation<T = {}>(options: Advanc
|
||||
|
||||
const timeOffset = options.timeOffset || 0;
|
||||
|
||||
const domainMatch: any = {}
|
||||
if (options.domain) domainMatch.website = options.domain
|
||||
|
||||
const aggregation = [
|
||||
{
|
||||
$match: {
|
||||
@@ -44,6 +49,7 @@ export async function executeAdvancedTimelineAggregation<T = {}>(options: Advanc
|
||||
$gte: new Date(options.from),
|
||||
$lte: new Date(options.to)
|
||||
},
|
||||
...domainMatch,
|
||||
...options.customMatch
|
||||
}
|
||||
},
|
||||
@@ -94,7 +100,11 @@ export async function executeAdvancedTimelineAggregation<T = {}>(options: Advanc
|
||||
...options.customProjection
|
||||
}
|
||||
}
|
||||
] as any;
|
||||
] as any[];
|
||||
|
||||
|
||||
if (options.customAfterMatch) aggregation.splice(1, 0, options.customAfterMatch);
|
||||
|
||||
|
||||
if (options.debug === true) {
|
||||
console.log('---------- AGGREAGATION ----------')
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { AuthContext } from "../middleware/01-authorization";
|
||||
import type { EventHandlerRequest, H3Event } from 'h3'
|
||||
import { allowedModels, TModelName } from "../services/DataService";
|
||||
import { LITLYX_PROJECT_ID } from "@data/LITLYX";
|
||||
import { ProjectModel, TProject } from "@schema/project/ProjectSchema";
|
||||
import { Model, Types } from "mongoose";
|
||||
import { TeamMemberModel } from "@schema/TeamMemberSchema";
|
||||
@@ -171,7 +170,7 @@ export async function getRequestDataOld(event: H3Event<EventHandlerRequest>, opt
|
||||
if (!project) return setResponseStatus(event, 400, 'project not found');
|
||||
|
||||
|
||||
if (pid !== LITLYX_PROJECT_ID) {
|
||||
if (pid !== "6643cd08a1854e3b81722ab5") {
|
||||
const [hasAccess, role] = await hasAccessToProject(user.id, project);
|
||||
if (!hasAccess) return setResponseStatus(event, 400, 'no access to project');
|
||||
if (role === 'GUEST' && !allowGuests) return setResponseStatus(event, 403, 'only owner can access this');
|
||||
|
||||
Reference in New Issue
Block a user