Services rewrite

This commit is contained in:
Emily
2024-09-18 17:43:04 +02:00
parent fa5a37ece2
commit 0be3dbecbf
40 changed files with 488146 additions and 125 deletions

View File

@@ -77,7 +77,7 @@ const chartData = ref<ChartData<'doughnut'>>({
const { doughnutChartProps, doughnutChartRef } = useDoughnutChart({ chartData: chartData, options: chartOptions });
const activeProject = useActiveProject();
const activeProjectId = useActiveProjectId();
const { safeSnapshotDates } = useSnapshot();
@@ -102,12 +102,14 @@ const headers = computed(() => {
return {
'x-from': safeSnapshotDates.value.from,
'x-to': safeSnapshotDates.value.to,
Authorization: authorizationHeaderComputed.value,
limit: "10"
'Authorization': authorizationHeaderComputed.value,
'x-schema': 'events',
'x-limit': "10",
'x-pid': activeProjectId.data.value || ''
}
});
const eventsData = useFetch(`/api/metrics/${activeProject.value?._id}/data/events`, {
const eventsData = useFetch(`/api/data/query`, {
method: 'POST', headers, lazy: true, immediate: false, transform: transformResponse
});

View File

@@ -8,8 +8,11 @@ const activeProject = useActiveProject();
definePageMeta({ layout: 'dashboard' });
const { data: planData, refresh: planRefresh, pending: planPending } = useFetch('/api/project/plan', {
...signHeaders(),
lazy: true
...signHeaders(), lazy: true
});
const { data: customerAddress, refresh: refreshCustomerAddress } = useFetch(`/api/pay/${activeProject.value?._id.toString()}/customer_info`, {
...signHeaders(), lazy: true
});
const percent = computed(() => {
@@ -43,8 +46,7 @@ const prettyExpireDate = computed(() => {
const { data: invoices, refresh: invoicesRefresh, pending: invoicesPending } = useFetch(`/api/pay/${activeProject.value?._id.toString()}/invoices`, {
...signHeaders(),
lazy: true
...signHeaders(), lazy: true
})
function openInvoice(link: string) {
@@ -65,25 +67,50 @@ function getPremiumPrice(type: number) {
return (PLAN.COST / 100).toFixed(2).replace('.', ',')
}
watch(activeProject, () => {
invoicesRefresh();
planRefresh();
})
const entries: SettingsTemplateEntry[] = [
// { id: 'info', title: 'Billing informations', text: 'Manage billing informations for this project' },
{ id: 'info', title: 'Billing informations', text: 'Manage billing informations for this project' },
{ id: 'plan', title: 'Current plan', text: 'Manage current plat for this project' },
{ id: 'usage', title: 'Usage', text: 'Show usage of current project' },
{ id: 'invoices', title: 'Invoices', text: 'Manage invoices of current project' },
]
watch(customerAddress, () => {
console.log('UPDATE',customerAddress.value)
if (!customerAddress.value) return;
currentBillingInfo.value = customerAddress.value;
});
const currentBillingInfo = ref<any>({
address: ''
line1: '',
line2: '',
city: '',
country: '',
postal_code: '',
state: ''
});
const { createAlert } = useAlert()
async function saveBillingInfo() {
try {
const res = await $fetch(`/api/pay/${activeProject.value?._id.toString()}/update_customer`, {
method: 'POST',
...signHeaders({
'Content-Type': 'application/json'
}),
body: JSON.stringify(currentBillingInfo.value)
});
createAlert('Customer updated','Customer updated successfully', 'far fa-check', 5000);
} catch(ex) {
createAlert('Error updating customer','An error occurred while updating the customer', 'far fa-error', 8000);
}
}
const { visible } = usePricingDrawer();
</script>
@@ -97,6 +124,35 @@ const { visible } = usePricingDrawer();
</div>
<SettingsTemplate v-if="!invoicesPending && !planPending" :entries="entries">
<template #info>
<div>
<div class="flex flex-col gap-2">
<LyxUiInput class="px-2 py-1" placeholder="Address line 1" v-model="currentBillingInfo.line1">
</LyxUiInput>
<LyxUiInput class="px-2 py-1" placeholder="Address line 2" v-model="currentBillingInfo.line2">
</LyxUiInput>
<div class="flex gap-2 w-full">
<LyxUiInput class="px-2 py-1 w-full" placeholder="Country"
v-model="currentBillingInfo.country">
</LyxUiInput>
<LyxUiInput class="px-2 py-1 w-full" placeholder="Postal code"
v-model="currentBillingInfo.postal_code">
</LyxUiInput>
</div>
<div class="flex gap-2 w-full">
<LyxUiInput class="px-2 py-1 w-full" placeholder="City" v-model="currentBillingInfo.city">
</LyxUiInput>
<LyxUiInput class="px-2 py-1 w-full" placeholder="State" v-model="currentBillingInfo.state">
</LyxUiInput>
</div>
</div>
<div class="mt-5 flex justify-end">
<LyxUiButton type="primary" @click="saveBillingInfo">
Save
</LyxUiButton>
</div>
</div>
</template>
<template #plan>
<LyxUiCard v-if="planData" class="flex flex-col w-full">
<div class="flex flex-col gap-6 px-8 grow">

View File

@@ -3,6 +3,8 @@
definePageMeta({ layout: 'dashboard' });
const activeProject = useActiveProject();
const items = [
{ label: 'General', slot: 'general' },
{ label: 'Members', slot: 'members' },
@@ -18,32 +20,18 @@ const items = [
<CustomTab :items="items" class="mt-8">
<template #general>
<SettingsGeneral></SettingsGeneral>
<SettingsGeneral :key="activeProject?._id.toString()"></SettingsGeneral>
</template>
<template #members>
<SettingsMembers></SettingsMembers>
<SettingsMembers :key="activeProject?._id.toString()"></SettingsMembers>
</template>
<template #billing>
<SettingsBilling></SettingsBilling>
<SettingsBilling :key="activeProject?._id.toString()"></SettingsBilling>
</template>
<template #account>
<SettingsAccount></SettingsAccount>
<SettingsAccount :key="activeProject?._id.toString()"></SettingsAccount>
</template>
</CustomTab>
<!-- <UTabs :items="items" class="mt-8">
<template #general>
<SettingsGeneral></SettingsGeneral>
</template>
<template #members>
<SettingsMembers></SettingsMembers>
</template>
<template #billing>
<SettingsBilling></SettingsBilling>
</template>
<template #account>
<SettingsAccount></SettingsAccount>
</template>
</UTabs> -->
</div>
</template>

View File

@@ -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;
}

View 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;
});
});

View File

@@ -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;
});

View File

@@ -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,

View 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;
});

View 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 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 }
});

View File

@@ -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;
}
}

View File

@@ -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');

View File

@@ -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;

View File

@@ -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'];
}