feat(subscriptions): added polar as payment provider for subscriptions
* feature(dashboard): add polar / subscription * wip(payments): manage subscription * wip(payments): add free product, faq and some other improvements * fix(root): change node to bundler in tsconfig * wip(payments): display current subscription * feat(dashboard): schedule project for deletion * wip(payments): support custom products/subscriptions * wip(payments): fix polar scripts * wip(payments): add json package to dockerfiles
This commit is contained in:
committed by
GitHub
parent
86bf9dd064
commit
168ebc3430
@@ -2,8 +2,79 @@ import type { Job } from 'bullmq';
|
||||
|
||||
import type { SessionsQueuePayload } from '@openpanel/queue';
|
||||
|
||||
import { logger } from '@/utils/logger';
|
||||
import {
|
||||
db,
|
||||
getOrganizationBillingEventsCount,
|
||||
getOrganizationByProjectIdCached,
|
||||
getProjectEventsCount,
|
||||
} from '@openpanel/db';
|
||||
import { cacheable } from '@openpanel/redis';
|
||||
import { createSessionEnd } from './events.create-session-end';
|
||||
|
||||
export async function sessionsJob(job: Job<SessionsQueuePayload>) {
|
||||
return await createSessionEnd(job);
|
||||
const res = await createSessionEnd(job);
|
||||
try {
|
||||
await updateEventsCount(job.data.payload.projectId);
|
||||
} catch (e) {
|
||||
logger.error('Failed to update events count', e);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
const updateEventsCount = cacheable(async function updateEventsCount(
|
||||
projectId: string,
|
||||
) {
|
||||
const organization = await db.organization.findFirst({
|
||||
where: {
|
||||
projects: {
|
||||
some: {
|
||||
id: projectId,
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
projects: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!organization) {
|
||||
return;
|
||||
}
|
||||
|
||||
const organizationEventsCount =
|
||||
await getOrganizationBillingEventsCount(organization);
|
||||
const projectEventsCount = await getProjectEventsCount(projectId);
|
||||
|
||||
if (projectEventsCount) {
|
||||
await db.project.update({
|
||||
where: {
|
||||
id: projectId,
|
||||
},
|
||||
data: {
|
||||
eventsCount: projectEventsCount,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (organizationEventsCount) {
|
||||
await db.organization.update({
|
||||
where: {
|
||||
id: organization.id,
|
||||
},
|
||||
data: {
|
||||
subscriptionPeriodEventsCount: organizationEventsCount,
|
||||
subscriptionPeriodEventsCountExceededAt:
|
||||
organizationEventsCount >
|
||||
organization.subscriptionPeriodEventsLimit &&
|
||||
!organization.subscriptionPeriodEventsCountExceededAt
|
||||
? new Date()
|
||||
: organizationEventsCount <=
|
||||
organization.subscriptionPeriodEventsLimit
|
||||
? null
|
||||
: organization.subscriptionPeriodEventsCountExceededAt,
|
||||
},
|
||||
});
|
||||
await getOrganizationByProjectIdCached.clear(projectId);
|
||||
}
|
||||
}, 60 * 60);
|
||||
|
||||
Reference in New Issue
Block a user