postiz/apps/backend/src/public-api/routes/v1/public.integrations.control...

181 lines
4.9 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
HttpException,
Param,
Post,
Query,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.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';
import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';
import { MediaService } from '@gitroom/nestjs-libraries/database/prisma/media/media.service';
import { GetPostsDto } from '@gitroom/nestjs-libraries/dtos/posts/get.posts.dto';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permission.exception.class';
import { VideoDto } from '@gitroom/nestjs-libraries/dtos/videos/video.dto';
import { VideoFunctionDto } from '@gitroom/nestjs-libraries/dtos/videos/video.function.dto';
import { UploadDto } from '@gitroom/nestjs-libraries/dtos/media/upload.dto';
import axios from 'axios';
import { Readable } from 'stream';
@ApiTags('Public API')
@Controller('/public/v1')
export class PublicIntegrationsController {
private storage = UploadFactory.createStorage();
constructor(
private _integrationService: IntegrationService,
private _postsService: PostsService,
private _mediaService: MediaService
) {}
@Post('/upload')
@UseInterceptors(FileInterceptor('file'))
async uploadSimple(
@GetOrgFromRequest() org: Organization,
@UploadedFile('file') file: Express.Multer.File
) {
if (!file) {
throw new HttpException({ msg: 'No file provided' }, 400);
}
const getFile = await this.storage.uploadFile(file);
return this._mediaService.saveFile(
org.id,
getFile.originalname,
getFile.path
);
}
@Post('/upload-from-url')
async uploadFromUrl(
@GetOrgFromRequest() org: Organization,
@Body() body: UploadDto
) {
const response = await axios.get(body.url, {
responseType: 'arraybuffer',
});
const buffer = Buffer.from(response.data);
const getFile = await this.storage.uploadFile({
buffer,
mimetype: 'image/jpeg',
size: buffer.length,
path: '',
fieldname: '',
destination: '',
stream: new Readable(),
filename: '',
originalname: '',
encoding: '',
});
return this._mediaService.saveFile(
org.id,
getFile.originalname,
getFile.path
);
}
@Get('/find-slot/:id')
async findSlotIntegration(
@GetOrgFromRequest() org: Organization,
@Param('id') id?: string
) {
return { date: await this._postsService.findFreeDateTime(org.id, id) };
}
@Get('/posts')
async getPosts(
@GetOrgFromRequest() org: Organization,
@Query() query: GetPostsDto
) {
const posts = await this._postsService.getPosts(org.id, query);
return {
posts,
// comments,
};
}
@Post('/posts')
@CheckPolicies([AuthorizationActions.Create, Sections.POSTS_PER_MONTH])
async createPost(
@GetOrgFromRequest() org: Organization,
@Body() rawBody: any
) {
const body = await this._postsService.mapTypeToPost(
rawBody,
org.id,
rawBody.type === 'draft'
);
body.type = rawBody.type;
console.log(JSON.stringify(body, null, 2));
return this._postsService.createPost(org.id, body);
}
@Delete('/posts/:id')
async deletePost(
@GetOrgFromRequest() org: Organization,
@Param() body: { id: string }
) {
const getPostById = await this._postsService.getPost(org.id, body.id);
return this._postsService.deletePost(org.id, getPostById.group);
}
@Get('/is-connected')
async getActiveIntegrations(@GetOrgFromRequest() org: Organization) {
return { connected: true };
}
@Get('/integrations')
async listIntegration(@GetOrgFromRequest() org: Organization) {
return (await this._integrationService.getIntegrationsList(org.id)).map(
(org) => ({
id: org.id,
name: org.name,
identifier: org.providerIdentifier,
picture: org.picture,
disabled: org.disabled,
profile: org.profile,
customer: org.customer
? {
id: org.customer.id,
name: org.customer.name,
}
: undefined,
})
);
}
@Post('/generate-video')
generateVideo(
@GetOrgFromRequest() org: Organization,
@Body() body: VideoDto
) {
return this._mediaService.generateVideo(org, body);
}
@Post('/video/function')
videoFunction(@Body() body: VideoFunctionDto) {
return this._mediaService.videoFunction(
body.identifier,
body.functionName,
body.params
);
}
}