feat: register pages routes guards.

feat: retrieve all organizations details to authenticated user.
feat: redux organization reducers and actions.
This commit is contained in:
Ahmed Bouhuolia
2020-10-11 00:08:51 +02:00
parent 8622320eef
commit 507690fedf
22 changed files with 348 additions and 66 deletions

View File

@@ -1,5 +1,5 @@
import { Inject, Service } from 'typedi';
import { Router, Request, Response } from 'express';
import { Router, Request, Response, NextFunction } from 'express';
import asyncMiddleware from "api/middleware/asyncMiddleware";
import JWTAuth from 'api/middleware/jwtAuth';
import TenancyMiddleware from 'api/middleware/TenancyMiddleware';
@@ -27,12 +27,14 @@ export default class OrganizationController extends BaseController{
router.use(JWTAuth);
router.use(AttachCurrentTenantUser);
router.use(TenancyMiddleware);
router.use(SubscriptionMiddleware('main'));
// Should to seed organization tenant be configured.
router.use('/seed', SubscriptionMiddleware('main'));
router.use('/seed', SettingsMiddleware);
router.use('/seed', EnsureConfiguredMiddleware);
router.use('/build', SubscriptionMiddleware('main'));
router.post(
'/build',
asyncMiddleware(this.build.bind(this))
@@ -41,6 +43,10 @@ export default class OrganizationController extends BaseController{
'/seed',
asyncMiddleware(this.seed.bind(this)),
);
router.get(
'/all',
asyncMiddleware(this.allOrganizations.bind(this)),
);
return router;
}
@@ -116,4 +122,21 @@ export default class OrganizationController extends BaseController{
next(error);
}
}
/**
* Listing all organizations that assocaited to the authorized user.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async allOrganizations(req: Request, res: Response, next: NextFunction) {
const { user } = req;
try {
const organizations = await this.organizationService.listOrganizations(user);
return res.status(200).send({ organizations });
} catch (error) {
next(error);
}
}
}

View File

@@ -1,6 +1,6 @@
import { Service, Inject } from 'typedi';
import { ServiceError } from 'exceptions';
import { ITenant } from 'interfaces';
import { ISystemService, ISystemUser, ITenant } from 'interfaces';
import {
EventDispatcher,
EventDispatcherInterface,
@@ -38,7 +38,7 @@ export default class OrganizationService {
* @param {srting} organizationId
* @return {Promise<void>}
*/
async build(organizationId: string): Promise<void> {
public async build(organizationId: string): Promise<void> {
const tenant = await this.getTenantByOrgIdOrThrowError(organizationId);
this.throwIfTenantInitizalized(tenant);
@@ -69,7 +69,7 @@ export default class OrganizationService {
* @param {number} organizationId
* @return {Promise<void>}
*/
async seed(organizationId: string): Promise<void> {
public async seed(organizationId: string): Promise<void> {
const tenant = await this.getTenantByOrgIdOrThrowError(organizationId);
this.throwIfTenantSeeded(tenant);
@@ -91,6 +91,20 @@ export default class OrganizationService {
}
}
/**
* Listing all associated organizations to the given user.
* @param {ISystemUser} user -
* @return {Promise<void>}
*/
public async listOrganizations(user: ISystemUser): Promise<ITenant[]> {
this.logger.info('[organization] trying to list all organizations.', { user });
const { tenantRepository } = this.sysRepositories;
const tenant = await tenantRepository.getByIdWithSubscriptions(user.tenantId);
return [tenant];
}
/**
* Throws error in case the given tenant is undefined.
* @param {ITenant} tenant

View File

@@ -63,11 +63,23 @@ export default class TenantRepository extends SystemRepository {
/**
* Retrieve tenant details by the given tenant id.
* @param {string} tenantId
* @param {string} tenantId - Tenant id.
*/
getById(tenantId: number) {
return this.cache.get(`tenant.id.${tenantId}`, () => {
return Tenant.query().findById(tenantId);
});
}
/**
* Retrieve tenant details with associated subscriptions
* and plans by the given tenant id.
* @param {number} tenantId - Tenant id.
*/
getByIdWithSubscriptions(tenantId: number) {
return this.cache.get(`tenant.id.${tenantId}.subscriptions`, () => {
return Tenant.query().findById(tenantId)
.withGraphFetched('subscriptions.plan');
});
}
}