Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: add x-postiz-include-deleted header for the debug endpoints

Enno Gelhaus9485ca502d97
diff --git a/apps/backend/src/main.ts b/apps/backend/src/main.ts
index 31228ab82..b60dc7d8e 100644
--- a/apps/backend/src/main.ts
+++ b/apps/backend/src/main.ts
@@ -32,6 +32,7 @@ async function start() {
'showorg',
'impersonate',
'x-postiz-org',
+ 'x-postiz-include-deleted',
'x-copilotkit-runtime-client-gql-version',
],
exposedHeaders: [
diff --git a/apps/backend/src/public-api/routes/v1/public.integrations.controller.ts b/apps/backend/src/public-api/routes/v1/public.integrations.controller.ts
index b99e31fd9..5275ea421 100644
--- a/apps/backend/src/public-api/routes/v1/public.integrations.controller.ts
+++ b/apps/backend/src/public-api/routes/v1/public.integrations.controller.ts
@@ -16,6 +16,7 @@ import {
import { streamUploadOptions } from '@gitroom/nestjs-libraries/upload/multer.stream.engine';
import { ApiTags } from '@nestjs/swagger';
import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request';
+import { GetIncludeDeletedFromRequest } from '@gitroom/nestjs-libraries/user/include.deleted.from.request';
import { Organization } from '@prisma/client';
import { IntegrationService } from '@gitroom/nestjs-libraries/database/prisma/integrations/integration.service';
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';
@@ -354,15 +355,19 @@ export class PublicIntegrationsController {
@Get('/debug/channels')
@UseGuards(SuperAdminGuard)
- async getChannelHealth(@GetOrgFromRequest() org: Organization) {
+ async getChannelHealth(
+ @GetOrgFromRequest() org: Organization,
+ @GetIncludeDeletedFromRequest() includeDeleted: boolean
+ ) {
Sentry.metrics.count('public_api-request', 1);
- return this._integrationService.getChannelHealth(org.id);
+ return this._integrationService.getChannelHealth(org.id, includeDeleted);
}
@Get('/debug/activity')
@UseGuards(SuperAdminGuard)
async getOrgActivity(
@GetOrgFromRequest() org: Organization,
+ @GetIncludeDeletedFromRequest() includeDeleted: boolean,
@Query() query: GetOrgActivityDto
) {
Sentry.metrics.count('public_api-request', 1);
@@ -374,6 +379,7 @@ export class PublicIntegrationsController {
organizationId: org.id,
from: from.startOf('day').toDate(),
to: to.endOf('day').toDate(),
+ includeDeleted,
});
}
diff --git a/apps/backend/src/services/auth/public.auth.middleware.ts b/apps/backend/src/services/auth/public.auth.middleware.ts
index 96f2b11de..e93d2f98d 100644
--- a/apps/backend/src/services/auth/public.auth.middleware.ts
+++ b/apps/backend/src/services/auth/public.auth.middleware.ts
@@ -64,6 +64,9 @@ export class PublicAuthMiddleware implements NestMiddleware {
this.setOrg(req, org);
// @ts-ignore
req.authOrgId = org.id;
+ // @ts-ignore
+ req.includeDeleted =
+ (req.headers['x-postiz-include-deleted'] as string)?.trim() === 'true';
const overrideOrgId = (req.headers['x-postiz-org'] as string)?.trim();
diff --git a/libraries/nestjs-libraries/src/database/prisma/admin-stats/admin-stats.repository.ts b/libraries/nestjs-libraries/src/database/prisma/admin-stats/admin-stats.repository.ts
index cd1544951..fded269d2 100644
--- a/libraries/nestjs-libraries/src/database/prisma/admin-stats/admin-stats.repository.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/admin-stats/admin-stats.repository.ts
@@ -7,12 +7,14 @@ export interface StatsParams {
to: Date;
unknownOnly?: boolean;
organizationId?: string;
+ includeDeleted?: boolean;
}
export interface OrgActivityParams {
from: Date;
to: Date;
organizationId: string;
+ includeDeleted?: boolean;
}
// Unknown errors are stored as the serialized error payload, e.g.
@@ -103,7 +105,7 @@ export class AdminStatsRepository {
const where: Prisma.PostWhereInput = {
state: 'PUBLISHED',
parentPostId: null,
- deletedAt: null,
+ ...(params.includeDeleted ? {} : { deletedAt: null }),
publishDate: { gte: params.from, lte: params.to },
...(params.organizationId
? { organizationId: params.organizationId }
@@ -283,7 +285,7 @@ export class AdminStatsRepository {
private async connectedStats(params: StatsParams) {
const where: Prisma.IntegrationWhereInput = {
- deletedAt: null,
+ ...(params.includeDeleted ? {} : { deletedAt: null }),
createdAt: { gte: params.from, lte: params.to },
...(params.organizationId
? { organizationId: params.organizationId }
@@ -316,7 +318,7 @@ export class AdminStatsRepository {
where: {
organizationId: params.organizationId,
parentPostId: null,
- deletedAt: null,
+ ...(params.includeDeleted ? {} : { deletedAt: null }),
publishDate: { gte: params.from, lte: params.to },
},
_count: { _all: true },
@@ -327,10 +329,13 @@ export class AdminStatsRepository {
.sort((a, b) => b.count - a.count || a.state.localeCompare(b.state));
}
- private async currentChannelStats(organizationId: string) {
+ private async currentChannelStats(
+ organizationId: string,
+ includeDeleted?: boolean
+ ) {
const where: Prisma.IntegrationWhereInput = {
organizationId,
- deletedAt: null,
+ ...(includeDeleted ? {} : { deletedAt: null }),
};
const [total, grouped] = await Promise.all([
@@ -353,9 +358,16 @@ export class AdminStatsRepository {
};
}
- private async activityRange(organizationId: string) {
+ private async activityRange(
+ organizationId: string,
+ includeDeleted?: boolean
+ ) {
const { _min, _max } = await this._post.model.post.aggregate({
- where: { organizationId, state: 'PUBLISHED', deletedAt: null },
+ where: {
+ organizationId,
+ state: 'PUBLISHED',
+ ...(includeDeleted ? {} : { deletedAt: null }),
+ },
_min: { publishDate: true },
_max: { publishDate: true },
});
@@ -374,9 +386,9 @@ export class AdminStatsRepository {
this.errorStats(params),
this.postStats(params),
this.connectedStats(params),
- this.currentChannelStats(params.organizationId),
+ this.currentChannelStats(params.organizationId, params.includeDeleted),
this.postStateStats(params),
- this.activityRange(params.organizationId),
+ this.activityRange(params.organizationId, params.includeDeleted),
]);
return {
diff --git a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts
index 8b2ba3536..37274a6f3 100644
--- a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts
@@ -564,7 +564,7 @@ export class IntegrationRepository {
org: string,
state: 'PUBLISHED' | 'ERROR',
field: 'publishDate' | 'updatedAt',
- topLevelOnly: boolean,
+ options: { topLevelOnly: boolean; includeDeleted: boolean },
groups: { integrationId: string; date: Date | null }[]
) {
const matches = groups.filter((group) => group.date);
@@ -577,8 +577,8 @@ export class IntegrationRepository {
where: {
organizationId: org,
state,
- deletedAt: null,
- ...(topLevelOnly ? { parentPostId: null } : {}),
+ ...(options.includeDeleted ? {} : { deletedAt: null }),
+ ...(options.topLevelOnly ? { parentPostId: null } : {}),
OR: matches.map(
(group) =>
({
@@ -613,7 +613,7 @@ export class IntegrationRepository {
return byIntegration;
}
- async getChannelHealth(org: string) {
+ async getChannelHealth(org: string, includeDeleted = false) {
const [integrations, lastPublished, lastErrored] = await Promise.all([
this._integration.model.integration.findMany({
where: {
@@ -648,7 +648,7 @@ export class IntegrationRepository {
where: {
organizationId: org,
state: 'PUBLISHED',
- deletedAt: null,
+ ...(includeDeleted ? {} : { deletedAt: null }),
parentPostId: null,
},
_max: { publishDate: true },
@@ -658,7 +658,7 @@ export class IntegrationRepository {
where: {
organizationId: org,
state: 'ERROR',
- deletedAt: null,
+ ...(includeDeleted ? {} : { deletedAt: null }),
},
_max: { updatedAt: true },
}),
@@ -669,7 +669,7 @@ export class IntegrationRepository {
org,
'PUBLISHED',
'publishDate',
- true,
+ { topLevelOnly: true, includeDeleted },
lastPublished.map((group) => ({
integrationId: group.integrationId,
date: group._max.publishDate,
@@ -679,7 +679,7 @@ export class IntegrationRepository {
org,
'ERROR',
'updatedAt',
- false,
+ { topLevelOnly: false, includeDeleted },
lastErrored.map((group) => ({
integrationId: group.integrationId,
date: group._max.updatedAt,
diff --git a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts
index a0f4fe7a4..6a226355b 100644
--- a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts
@@ -154,8 +154,8 @@ export class IntegrationService {
return this._integrationRepository.getIntegrationsList(org);
}
- getChannelHealth(org: string) {
- return this._integrationRepository.getChannelHealth(org);
+ getChannelHealth(org: string, includeDeleted = false) {
+ return this._integrationRepository.getChannelHealth(org, includeDeleted);
}
getIntegrationForOrder(id: string, order: string, user: string, org: string) {
diff --git a/libraries/nestjs-libraries/src/user/include.deleted.from.request.ts b/libraries/nestjs-libraries/src/user/include.deleted.from.request.ts
new file mode 100644
index 000000000..904c4219f
--- /dev/null
+++ b/libraries/nestjs-libraries/src/user/include.deleted.from.request.ts
@@ -0,0 +1,8 @@
+import { createParamDecorator, ExecutionContext } from '@nestjs/common';
+
+export const GetIncludeDeletedFromRequest = createParamDecorator(
+ (data: unknown, ctx: ExecutionContext) => {
+ const request = ctx.switchToHttp().getRequest();
+ return !!request.includeDeleted;
+ }
+);