ToolJet/server/src/services/folder_apps.service.ts
Ankush a07b8e3439
Fixes addition of application to folder more than once (#2961)
* fixed addition of application to folder more than once

* Update server/src/services/folder_apps.service.ts

Co-authored-by: Midhun G S <gsmithun4@gmail.com>

Co-authored-by: Midhun G S <gsmithun4@gmail.com>
2022-05-05 12:04:42 +05:30

37 lines
1.1 KiB
TypeScript

import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FolderApp } from '../../src/entities/folder_app.entity';
@Injectable()
export class FolderAppsService {
constructor(
@InjectRepository(FolderApp)
private folderAppsRepository: Repository<FolderApp>
) {}
async create(folderId: string, appId: string): Promise<FolderApp> {
const existingFolderApp = await this.folderAppsRepository.findOne({
where: { appId, folderId },
});
if (existingFolderApp) {
throw new BadRequestException('App has been already added to the folder');
}
const newFolderApp = this.folderAppsRepository.create({
folderId,
appId,
createdAt: new Date(),
updatedAt: new Date(),
});
const folderApp = await this.folderAppsRepository.save(newFolderApp);
return folderApp;
}
async remove(folderId: string, appId: string): Promise<void> {
await this.folderAppsRepository.delete({ folderId, appId });
}
}