Skip to content

Commit 23716fa

Browse files
committed
added initial discovery items route
1 parent 2540476 commit 23716fa

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

src/constants/routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ export const ENROLMENTS = '/enrolments';
2525
export const MODULES = '/modules';
2626
export const UNITS = '/units';
2727
export const COMMENTS = '/comments';
28+
export const DISCOVERY_ITEMS = '/discovery-items';

src/presenter/express/api/v1/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
AUTH,
44
CATEGORIES,
55
COURSES,
6+
DISCOVERY_ITEMS,
67
ENROLMENTS,
78
// MODULES,
89
// UNITS
@@ -16,6 +17,7 @@ import authFactory from './routes/auth/factory';
1617
import categoriesFactory from './routes/categories/factory';
1718
import coursesFactory from './routes/courses/factory';
1819
import enrolmentsFactory from './routes/enrolments/factory';
20+
import getDiscoveryItems from './routes/getDiscoveryItems';
1921
import permissionsFactory from './routes/permissions/factory';
2022
import rolesFactory from './routes/roles/factory';
2123
import usersFactory from './routes/users/factory';
@@ -38,6 +40,7 @@ const apiV1 = (config: Config): Router => {
3840
router.use(CATEGORIES, categoriesFactory(config));
3941
router.use(COURSES, coursesFactory(config));
4042
router.use(ENROLMENTS, enrolmentsFactory(config));
43+
router.use(DISCOVERY_ITEMS, getDiscoveryItems(config));
4144
// router.use(MODULES, modulesFactory(config));
4245
// router.use(UNITS, unitsFactory(config));
4346
// TODO: add route for uploading /avatar
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sendResponse from '@js-items/express/dist/utils/sendResponse';
2+
import { toSnake } from 'convert-keys';
3+
import { OK } from 'http-status-codes';
4+
import Config from '../../../../presenterFactory/Config';
5+
import catchErrors from '../../../../utils/errors/catchErrors';
6+
7+
const getDiscoveryItems = (config: Config) =>
8+
catchErrors(config, async (req, res) => {
9+
10+
const response = await config.service.getDiscoveryItems({});
11+
12+
sendResponse({
13+
body: toSnake(response),
14+
req,
15+
res,
16+
status: OK,
17+
});
18+
});
19+
20+
export default getDiscoveryItems;

src/service/factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import migrationsServiceFactory from '@js-migrations/core/dist/factory';
22
import FactoryConfig from './FactoryConfig';
33
import authFactory from './functions/auth/factory';
4+
import getDiscoveryItems from './functions/getDiscoveryItems';
45
import hasPermission from './functions/hasPermission';
56
import assignRolePermission from './functions/roles/assignRolePermission';
67
import revokeRolePermission from './functions/roles/revokeRolePermission';
@@ -15,6 +16,7 @@ export default (config: FactoryConfig) => ({
1516
closeDbConnection: config.repo.closeDbConnection,
1617
courses: config.repo.courses,
1718
enrolments: config.repo.enrolments,
19+
getDiscoveryItems: getDiscoveryItems(config),
1820
hasPermission: hasPermission(config),
1921
migrations: migrationsServiceFactory({
2022
repo: config.repo.migrations,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import _pluck from 'ramda/src/pluck';
2+
import Config from '../../FactoryConfig';
3+
4+
// tslint:disable-next-line:no-empty-interface
5+
export interface Options {}
6+
7+
// tslint:disable-next-line:arrow-return-shorthand
8+
export default (_config: Config) => async (_options: Options) => {
9+
// TODO: return items for homepage basing on query param
10+
// const { items: userRoles } = await repo.userRole.getItems({
11+
// filter: {
12+
// userId: user.id,
13+
// },
14+
// });
15+
16+
// if (userRoles.length === 0) {
17+
// throw new ForbiddenError();
18+
// }
19+
20+
// const rolesIds = _pluck('roleId', userRoles);
21+
22+
// const { items: rolePermissions } = await repo.rolePermission.getItems({
23+
// filter: {
24+
// roleId: {
25+
// $in: rolesIds,
26+
// },
27+
// },
28+
// pagination: {
29+
// // high number to get all permissions
30+
// limit: 1000
31+
// }
32+
// });
33+
34+
// if (rolePermissions.length === 0) {
35+
// throw new ForbiddenError();
36+
// }
37+
38+
// const permissionsIds = _pluck('permissionId', rolePermissions);
39+
40+
// const { count } = await repo.countPermissions({
41+
// method: req.method,
42+
// permissionsIds,
43+
// url: req.originalUrl,
44+
// });
45+
46+
// if (count === 0) {
47+
// throw new ForbiddenError();
48+
// }
49+
50+
return Promise.resolve({});
51+
};

0 commit comments

Comments
 (0)