Browse Source

refactor: add conditional service function logic

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5239/head
Pranav C 2 years ago
parent
commit
ac9596ded5
  1. 12
      packages/nocodb/src/lib/controllers/orgTokenController.ts
  2. 22
      packages/nocodb/src/lib/meta/api/ee/orgTokenApis.ts
  3. 17
      packages/nocodb/src/lib/meta/helpers/getHandler.ts
  4. 20
      packages/nocodb/src/lib/services/ee/orgTokenService.ts
  5. 3
      packages/nocodb/src/lib/services/index.ts

12
packages/nocodb/src/lib/controllers/orgTokenController.ts

@ -1,13 +1,15 @@
import { Request, Response, Router } from 'express';
import { metaApiMetrics } from '../meta/helpers/apiMetrics';
import getHandler from '../meta/helpers/getHandler';
import { getConditionalHandler } from '../meta/helpers/getHandler';
import ncMetaAclMw from '../meta/helpers/ncMetaAclMw';
import { apiTokenListEE } from '../meta/api/ee/orgTokenApis';
import { orgTokenService } from '../services';
import { orgTokenService, orgTokenServiceEE } from '../services';
async function apiTokenList(req, res) {
res.json(
await orgTokenService.apiTokenList({
await getConditionalHandler(
orgTokenService.apiTokenList,
orgTokenServiceEE.apiTokenListEE
)({
query: req.query,
user: req['user'],
})
@ -37,7 +39,7 @@ const router = Router({ mergeParams: true });
router.get(
'/api/v1/tokens',
metaApiMetrics,
ncMetaAclMw(getHandler(apiTokenList, apiTokenListEE), 'apiTokenList', {
ncMetaAclMw(apiTokenList, 'apiTokenList', {
// allowedRoles: [OrgUserRoles.SUPER],
blockApiTokenAccess: true,
})

22
packages/nocodb/src/lib/meta/api/ee/orgTokenApis.ts

@ -1,22 +0,0 @@
import { OrgUserRoles } from 'nocodb-sdk';
import ApiToken from '../../../models/ApiToken';
import { PagedResponseImpl } from '../../helpers/PagedResponse';
export async function apiTokenListEE(req, res) {
let fk_user_id = req.user.id;
// if super admin get all tokens
if (req.user.roles.includes(OrgUserRoles.SUPER_ADMIN)) {
fk_user_id = undefined;
}
res.json(
new PagedResponseImpl(
await ApiToken.listWithCreatedBy({ ...req.query, fk_user_id }),
{
...req.query,
count: await ApiToken.count({}),
}
)
);
}

17
packages/nocodb/src/lib/meta/helpers/getHandler.ts

@ -12,3 +12,20 @@ export default function getHandler(
return eeHandler(...args);
};
}
export function getConditionalHandler<
T extends (...args: any[]) => any,
U extends (...args: any[]) => any
>(
defaultHandler: T,
eeHandler: U
): (
...args: Parameters<T> | Parameters<U>
) => Promise<ReturnType<T> | ReturnType<U>> {
return async (...args: Parameters<T> | Parameters<U>) => {
if (Noco.isEE()) {
return defaultHandler(...args);
}
return eeHandler(...args);
};
}

20
packages/nocodb/src/lib/services/ee/orgTokenService.ts

@ -0,0 +1,20 @@
import { OrgUserRoles, UserType } from 'nocodb-sdk';
import { PagedResponseImpl } from '../../meta/helpers/PagedResponse';
import { ApiToken } from '../../models';
export async function apiTokenListEE(param: { user: UserType; query: any }) {
let fk_user_id = param.user.id;
// if super admin get all tokens
if (param.user.roles.includes(OrgUserRoles.SUPER_ADMIN)) {
fk_user_id = undefined;
}
return new PagedResponseImpl(
await ApiToken.listWithCreatedBy({ ...param.query, fk_user_id }),
{
...(param.query || {}),
count: await ApiToken.count({}),
}
);
}

3
packages/nocodb/src/lib/services/index.ts

@ -21,7 +21,6 @@ export * as mapViewService from './mapViewService';
export * as modelVisibilityService from './modelVisibilityService';
export * as sharedBaseService from './sharedBaseService';
export * as orgUserService from './orgUserService';
export * as orgTokenService from './orgTokenService';
export * as orgLicenseService from './orgLicenseService';
export * as projectUserService from './projectUserService';
export * as attachmentService from './attachmentService';
@ -34,3 +33,5 @@ export * as swaggerService from './swaggerService';
export * as userService from './userService';
export * as syncService from './syncService';
export * from './public';
export * as orgTokenService from './orgTokenService';
export * as orgTokenServiceEE from './ee/orgTokenService';

Loading…
Cancel
Save