mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +01:00
Services rewrite
This commit is contained in:
@@ -4,19 +4,22 @@ 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 == LITLYX_PROJECT_ID) {
|
||||
const project = await ProjectModel.findOne({ _id: project_id });
|
||||
return project;
|
||||
} else {
|
||||
if (!user?.logged) return;
|
||||
if (!project_id) return;
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return;
|
||||
const [hasAccess, role] = await hasAccessToProject(user.id, project_id, project);
|
||||
if (!hasAccess) return;
|
||||
if (role === 'GUEST' && !allowGuest) return false;
|
||||
return project;
|
||||
if (!project_id) return;
|
||||
|
||||
if (project_id === LITLYX_PROJECT_ID) {
|
||||
return await ProjectModel.findOne({ _id: project_id });
|
||||
}
|
||||
|
||||
if (!user || !user.logged) return;
|
||||
|
||||
const project = await ProjectModel.findById(project_id);
|
||||
if (!project) return;
|
||||
|
||||
const [hasAccess, role] = await hasAccessToProject(user.id, project_id, project);
|
||||
if (!hasAccess) return;
|
||||
|
||||
if (role === 'GUEST' && !allowGuest) return false;
|
||||
|
||||
return project;
|
||||
|
||||
}
|
||||
65
dashboard/server/api/data/query.ts
Normal file
65
dashboard/server/api/data/query.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { Redis } from "~/server/services/CacheService";
|
||||
|
||||
import type { Model } from "mongoose";
|
||||
|
||||
|
||||
const allowedModels: Record<string, { model: Model<any>, field: string }> = {
|
||||
'events': {
|
||||
model: EventModel,
|
||||
field: 'name'
|
||||
}
|
||||
}
|
||||
|
||||
type TModelName = keyof typeof allowedModels;
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const project_id = getHeader(event, 'x-pid');
|
||||
if (!project_id) return;
|
||||
|
||||
const user = getRequestUser(event);
|
||||
const project = await getUserProjectFromId(project_id, user);
|
||||
if (!project) return;
|
||||
|
||||
const from = getRequestHeader(event, 'x-from');
|
||||
const to = getRequestHeader(event, 'x-to');
|
||||
|
||||
if (!from || !to) return setResponseStatus(event, 400, 'x-from and x-to are required');
|
||||
|
||||
const schemaName = getRequestHeader(event, 'x-schema');
|
||||
if (!schemaName) return setResponseStatus(event, 400, 'x-schema is required');
|
||||
|
||||
if (!Object.keys(allowedModels).includes(schemaName)) return setResponseStatus(event, 400, 'x-schema value is not valid');
|
||||
|
||||
const limitHeader = getRequestHeader(event, 'x-query-limit');
|
||||
const limitNumber = parseInt(limitHeader || '10');
|
||||
const limit = isNaN(limitNumber) ? 10 : limitNumber;
|
||||
|
||||
const cacheKey = `${schemaName}:${project_id}:${from}:${to}`;
|
||||
const cacheExp = 60;
|
||||
|
||||
return await Redis.useCacheV2(cacheKey, cacheExp, async (noStore, updateExp) => {
|
||||
|
||||
const { model } = allowedModels[schemaName as TModelName];
|
||||
|
||||
const result = await model.aggregate([
|
||||
{
|
||||
$match: {
|
||||
project_id: project._id,
|
||||
created_at: {
|
||||
$gte: new Date(from),
|
||||
$lte: new Date(to)
|
||||
}
|
||||
}
|
||||
},
|
||||
{ $group: { _id: "$name", count: { $sum: 1, } } },
|
||||
{ $sort: { count: -1 } },
|
||||
{ $limit: limit }
|
||||
]);
|
||||
|
||||
return result;
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -2,7 +2,8 @@ import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { getTimeline } from "./generic";
|
||||
import { Redis, TIMELINE_EXPIRE_TIME } from "~/server/services/CacheService";
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import { executeAdvancedTimelineAggregation } from "~/server/services/TimelineService";
|
||||
import { executeAdvancedTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
|
||||
import DateService from '@services/DateService';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const project_id = getRequestProjectId(event);
|
||||
@@ -29,6 +30,9 @@ export default defineEventHandler(async event => {
|
||||
customIdGroup: { name: '$name' },
|
||||
})
|
||||
|
||||
// const filledDates = DateService.createBetweenDates(from, to, slice);
|
||||
// const merged = DateService.mergeFilledDates(filledDates.dates, timelineStackedEvents, '_id', slice, { count: 0, name: '' });
|
||||
|
||||
return timelineStackedEvents;
|
||||
});
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ export default defineEventHandler(async event => {
|
||||
return setResponseStatus(event, 400, 'Plan not exist');
|
||||
}
|
||||
|
||||
const checkout = await StripeService.cretePayment(
|
||||
const checkout = await StripeService.createPayment(
|
||||
StripeService.testMode ? PLAN.PRICE_TEST : PLAN.PRICE,
|
||||
'https://dashboard.litlyx.com/payment_ok',
|
||||
project_id,
|
||||
|
||||
21
dashboard/server/api/pay/[project_id]/customer_info.ts
Normal file
21
dashboard/server/api/pay/[project_id]/customer_info.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const project_id = getRequestProjectId(event);
|
||||
if (!project_id) return;
|
||||
|
||||
const user = getRequestUser(event);
|
||||
const project = await getUserProjectFromId(project_id, user, false);
|
||||
if (!project) return;
|
||||
|
||||
if (!project.customer_id) return;
|
||||
|
||||
const customer = await StripeService.getCustomer(project.customer_id);
|
||||
if (customer?.deleted) return;
|
||||
|
||||
return customer?.address;
|
||||
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
import StripeService from '~/server/services/StripeService';
|
||||
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const project_id = getRequestProjectId(event);
|
||||
if (!project_id) return setResponseStatus(event, 400, 'Cannot get project_id');
|
||||
|
||||
const user = getRequestUser(event);
|
||||
const project = await getUserProjectFromId(project_id, user, false);
|
||||
if (!project) return setResponseStatus(event, 400, 'Cannot get user from project_id');
|
||||
|
||||
if (!project.customer_id) return setResponseStatus(event, 400, 'Project has no customer_id');
|
||||
|
||||
const body = await readBody(event);
|
||||
const res = await StripeService.setCustomerInfo(project.customer_id, body);
|
||||
|
||||
return { ok: true, data: res }
|
||||
|
||||
});
|
||||
@@ -13,6 +13,8 @@ export const EVENT_NAMES_EXPIRE_TIME = 60;
|
||||
|
||||
export const EVENT_METADATA_FIELDS_EXPIRE_TIME = 30;
|
||||
|
||||
type UseCacheV2Callback<T> = (noStore: () => void, updateExp: (value: number) => void) => Promise<T>
|
||||
|
||||
|
||||
export class Redis {
|
||||
|
||||
@@ -65,4 +67,17 @@ export class Redis {
|
||||
return result;
|
||||
}
|
||||
|
||||
static async useCacheV2<T extends any>(key: string, exp: number, callback: UseCacheV2Callback<T>) {
|
||||
const cached = await this.get<T>(key);
|
||||
if (cached) return cached;
|
||||
let expireValue = exp;
|
||||
let shouldStore = true;
|
||||
const noStore = () => shouldStore = false;
|
||||
const updateExp = (newExp: number) => expireValue = newExp;
|
||||
const result = await callback(noStore, updateExp);
|
||||
if (!shouldStore) return result;
|
||||
await this.set(key, result, expireValue);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -56,7 +56,7 @@ class StripeService {
|
||||
return checkout;
|
||||
}
|
||||
|
||||
async cretePayment(price: string, success_url: string, pid: string, customer?: string) {
|
||||
async createPayment(price: string, success_url: string, pid: string, customer?: string) {
|
||||
if (this.disabledMode) return;
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
|
||||
@@ -126,6 +126,22 @@ class StripeService {
|
||||
return customer;
|
||||
}
|
||||
|
||||
async setCustomerInfo(customer_id: string, address: { line1: string, line2: string, city: string, country: string, postal_code: string, state: string }) {
|
||||
if (this.disabledMode) return;
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
const customer = await this.stripe.customers.update(customer_id, {
|
||||
address: {
|
||||
line1: address.line1,
|
||||
line2: address.line2,
|
||||
city: address.city,
|
||||
country: address.country,
|
||||
postal_code: address.postal_code,
|
||||
state: address.state
|
||||
}
|
||||
})
|
||||
return customer.id;
|
||||
}
|
||||
|
||||
async deleteCustomer(customer_id: string) {
|
||||
if (this.disabledMode) return;
|
||||
if (!this.stripe) throw Error('Stripe not initialized');
|
||||
|
||||
@@ -46,7 +46,7 @@ export async function executeAdvancedTimelineAggregation<T = {}>(options: Advanc
|
||||
console.log(JSON.stringify(aggregation, null, 2));
|
||||
}
|
||||
|
||||
const timeline: { _id: string, count: number & T }[] = await options.model.aggregate(aggregation);
|
||||
const timeline: ({ _id: string, count: number } & T)[] = await options.model.aggregate(aggregation);
|
||||
|
||||
return timeline;
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ import { ProjectModel, TProject } from "@schema/ProjectSchema";
|
||||
import { TeamMemberModel } from "@schema/TeamMemberSchema";
|
||||
|
||||
export async function hasAccessToProject(user_id: string, project_id: string, project?: TProject) {
|
||||
const targetProject = project || await ProjectModel.findById(project_id, { owner: true });
|
||||
const targetProject = project ?? await ProjectModel.findById(project_id, { owner: true });
|
||||
if (!targetProject) return [false, 'NONE'];
|
||||
if (targetProject.owner.toString() === user_id) return [true, 'OWNER'];
|
||||
const members = await TeamMemberModel.find({ project_id });
|
||||
if (members.map(e => e.user_id.toString()).includes(user_id)) return [true, 'GUEST'];
|
||||
const isGuest = await TeamMemberModel.exists({ project_id, user_id });
|
||||
if (isGuest) return [true, 'GUEST'];
|
||||
return [false, 'NONE'];
|
||||
}
|
||||
Reference in New Issue
Block a user