forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.ts
More file actions
108 lines (98 loc) · 3.75 KB
/
service.ts
File metadata and controls
108 lines (98 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { BadRequestException, Injectable } from '@nestjs/common';
import { FolderApp } from '../../entities/folder_app.entity';
import { dbTransactionWrap } from '@helpers/database.helper';
import { EntityManager } from 'typeorm';
import { decamelizeKeys } from 'humps';
import { FoldersUtilService } from '@modules/folders/util.service';
import { FolderAppsUtilService } from './util.service';
import { IFolderAppsService } from './interfaces/IService';
import { MODULES } from '@modules/app/constants/modules';
import { AbilityService } from '@modules/ability/interfaces/IService';
import { User } from '@entities/user.entity';
import { USER_ROLE } from '@modules/group-permissions/constants';
import { APP_TYPES } from '@modules/apps/constants';
@Injectable()
export class FolderAppsService implements IFolderAppsService {
constructor(
protected abilityService: AbilityService,
protected foldersUtilService: FoldersUtilService,
protected folderAppsUtilService: FolderAppsUtilService
) {}
async create(folderId: string, appId: string): Promise<FolderApp> {
return dbTransactionWrap(async (manager: EntityManager) => {
const existingFolderApp = await manager.findOne(FolderApp, {
where: { appId, folderId },
});
if (existingFolderApp) {
throw new BadRequestException('App has already been added to the folder');
}
// TODO: check if folder under user.organizationId and user has edit permission on app
const newFolderApp = manager.create(FolderApp, {
folderId,
appId,
createdAt: new Date(),
updatedAt: new Date(),
});
const folderApp = await manager.save(FolderApp, newFolderApp);
return folderApp;
});
}
async remove(folderId: string, appId: string): Promise<void> {
return dbTransactionWrap(async (manager: EntityManager) => {
// TODO: folder under user.organizationId
return await manager.delete(FolderApp, { folderId, appId });
});
}
private getResourceTypefromAppType(type: APP_TYPES) {
switch (type) {
case APP_TYPES.FRONT_END:
return MODULES.APP;
case APP_TYPES.WORKFLOW:
return MODULES.WORKFLOWS;
case APP_TYPES.MODULE:
return MODULES.MODULES;
default:
throw new BadRequestException('Invalid resource type');
}
}
async getFolders(user: User, query) {
return dbTransactionWrap(async (manager: EntityManager) => {
const type = query.type;
const searchKey = query.searchKey;
const resourceType = this.getResourceTypefromAppType(type as APP_TYPES);
const userAppPermissions = (
await this.abilityService.resourceActionsPermission(user, {
resources: [{ resource: resourceType }],
organizationId: user.organizationId,
})
)?.[resourceType];
const allFolderList = await this.foldersUtilService.allFolders(user, manager, type);
if (allFolderList.length === 0) {
return { folders: [] };
}
const folders = await this.folderAppsUtilService.allFoldersWithAppCount(
user,
userAppPermissions,
manager,
type,
searchKey
);
allFolderList.forEach((folder, index) => {
const currentFolder = folders.find((f) => f.id === folder.id);
if (currentFolder) {
allFolderList[index].folderApps = [...(currentFolder?.folderApps || [])];
allFolderList[index].generateCount();
} else {
allFolderList[index].folderApps = [];
allFolderList[index].generateCount();
}
});
return decamelizeKeys({
folders:
user.roleGroup === USER_ROLE.END_USER
? allFolderList.filter((folder) => folder.folderApps.length > 0)
: allFolderList,
});
});
}
}