Skip to content

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

fix: exclude oauth apps from superadmin surfaces and bound the channel health query

Enno Gelhausb4da5597504a
diff --git a/apps/backend/src/services/auth/public.auth.middleware.ts b/apps/backend/src/services/auth/public.auth.middleware.ts
index 4436a80df..6c0b44301 100644
--- a/apps/backend/src/services/auth/public.auth.middleware.ts
+++ b/apps/backend/src/services/auth/public.auth.middleware.ts
@@ -29,8 +29,12 @@ export class PublicAuthMiddleware implements NestMiddleware {
}
try {
let org: Organization & { subscription?: unknown };
+ const isOAuthApp = auth.startsWith('pos_');
- if (auth.startsWith('pos_')) {
+ // @ts-ignore
+ req.isOAuthApp = isOAuthApp;
+
+ if (isOAuthApp) {
const authorization = await this._oauthService.getOrgByOAuthToken(auth);
if (!authorization) {
res
@@ -62,7 +66,10 @@ export class PublicAuthMiddleware implements NestMiddleware {
const overrideOrgId = (req.headers['x-postiz-org'] as string)?.trim();
if (overrideOrgId) {
- if (!(await this._organizationService.hasSuperAdminUser(org.id))) {
+ if (
+ isOAuthApp ||
+ !(await this._organizationService.hasSuperAdminUser(org.id))
+ ) {
res.status(HttpStatus.FORBIDDEN).json({ msg: 'Unauthorized' });
return;
}
diff --git a/apps/backend/src/services/auth/super.admin.guard.ts b/apps/backend/src/services/auth/super.admin.guard.ts
index 323d661e4..fb2a46409 100644
--- a/apps/backend/src/services/auth/super.admin.guard.ts
+++ b/apps/backend/src/services/auth/super.admin.guard.ts
@@ -16,10 +16,12 @@ export class SuperAdminGuard implements CanActivate {
const request: Request = context.switchToHttp().getRequest();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
- const { org }: { org: Organization } = request;
+ const { org, isOAuthApp }: { org: Organization; isOAuthApp?: boolean } =
+ request;
if (
!org ||
+ isOAuthApp ||
!(await this._organizationService.hasSuperAdminUser(org.id))
) {
throw new HttpException({ msg: 'Unauthorized' }, 403);
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 7dd598008..0916112b4 100644
--- a/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/integrations/integration.repository.ts
@@ -2,7 +2,7 @@ import { PrismaRepository } from '@gitroom/nestjs-libraries/database/prisma/pris
import { Injectable } from '@nestjs/common';
import { createHash } from 'crypto';
import dayjs from 'dayjs';
-import { Integration } from '@prisma/client';
+import { Integration, Prisma } from '@prisma/client';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import { IntegrationTimeDto } from '@gitroom/nestjs-libraries/dtos/integrations/integration.time.dto';
import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';
@@ -560,6 +560,42 @@ export class IntegrationRepository {
});
}
+ private async latestPostsFor(
+ org: string,
+ state: 'PUBLISHED' | 'ERROR',
+ field: 'publishDate' | 'updatedAt',
+ groups: { integrationId: string; date: Date | null }[]
+ ) {
+ const matches = groups.filter((group) => group.date);
+
+ if (!matches.length) {
+ return [];
+ }
+
+ return this._posts.model.post.findMany({
+ where: {
+ organizationId: org,
+ state,
+ deletedAt: null,
+ OR: matches.map(
+ (group) =>
+ ({
+ integrationId: group.integrationId,
+ [field]: group.date,
+ } as Prisma.PostWhereInput)
+ ),
+ },
+ select: {
+ id: true,
+ integrationId: true,
+ publishDate: true,
+ updatedAt: true,
+ releaseURL: true,
+ error: true,
+ },
+ });
+ }
+
async getChannelHealth(org: string) {
const [integrations, lastPublished, lastErrored] = await Promise.all([
this._integration.model.integration.findMany({
@@ -590,43 +626,52 @@ export class IntegrationRepository {
},
},
}),
- this._posts.model.post.findMany({
+ this._posts.model.post.groupBy({
+ by: ['integrationId'],
where: {
organizationId: org,
state: 'PUBLISHED',
deletedAt: null,
},
- orderBy: [{ integrationId: 'asc' }, { publishDate: 'desc' }],
- distinct: ['integrationId'],
- select: {
- id: true,
- integrationId: true,
- publishDate: true,
- releaseURL: true,
- },
+ _max: { publishDate: true },
}),
- this._posts.model.post.findMany({
+ this._posts.model.post.groupBy({
+ by: ['integrationId'],
where: {
organizationId: org,
state: 'ERROR',
deletedAt: null,
},
- orderBy: [{ integrationId: 'asc' }, { updatedAt: 'desc' }],
- distinct: ['integrationId'],
- select: {
- id: true,
- integrationId: true,
- updatedAt: true,
- error: true,
- },
+ _max: { updatedAt: true },
}),
]);
+ const [publishedPosts, erroredPosts] = await Promise.all([
+ this.latestPostsFor(
+ org,
+ 'PUBLISHED',
+ 'publishDate',
+ lastPublished.map((group) => ({
+ integrationId: group.integrationId,
+ date: group._max.publishDate,
+ }))
+ ),
+ this.latestPostsFor(
+ org,
+ 'ERROR',
+ 'updatedAt',
+ lastErrored.map((group) => ({
+ integrationId: group.integrationId,
+ date: group._max.updatedAt,
+ }))
+ ),
+ ]);
+
const publishedByIntegration = new Map(
- lastPublished.map((post) => [post.integrationId, post])
+ publishedPosts.map((post) => [post.integrationId, post])
);
const erroredByIntegration = new Map(
- lastErrored.map((post) => [post.integrationId, post])
+ erroredPosts.map((post) => [post.integrationId, post])
);
return integrations.map((integration) => {