feat: retrieve organization subscriptions list api.

feat: subscriptions reducers.
This commit is contained in:
Ahmed Bouhuolia
2020-10-13 21:46:32 +02:00
parent d71845a4c4
commit 8b97673100
23 changed files with 289 additions and 55 deletions

View File

@@ -1,12 +1,17 @@
import { Router } from 'express'
import { Container, Service } from 'typedi';
import { Router, Request, Response, NextFunction } from 'express'
import { Container, Service, Inject } from 'typedi';
import JWTAuth from 'api/middleware/jwtAuth';
import TenancyMiddleware from 'api/middleware/TenancyMiddleware';
import AttachCurrentTenantUser from 'api/middleware/AttachCurrentTenantUser';
import PaymentViaLicenseController from 'api/controllers/Subscription/PaymentViaLicense';
import SubscriptionService from 'services/Subscription/SubscriptionService';
import asyncMiddleware from 'api/middleware/asyncMiddleware';
@Service()
export default class SubscriptionController {
export default class SubscriptionController {
@Inject()
subscriptionService: SubscriptionService;
/**
* Router constructor.
*/
@@ -19,6 +24,26 @@ export default class SubscriptionController {
router.use('/license', Container.get(PaymentViaLicenseController).router());
router.get('/',
asyncMiddleware(this.getSubscriptions.bind(this))
);
return router;
}
/**
* Retrieve all subscriptions of the authenticated user's tenant.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async getSubscriptions(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
try {
const subscriptions = await this.subscriptionService.getSubscriptions(tenantId);
return res.status(200).send({ subscriptions });
} catch (error) {
next(error);
}
}
}

View File

@@ -25,6 +25,10 @@ export default {
type: 'string',
// config: true,
},
{
key: 'financial_date_start',
type: 'string',
},
{
key: 'language',
type: 'string',

View File

@@ -100,7 +100,7 @@ export default class OrganizationService {
this.logger.info('[organization] trying to list all organizations.', { user });
const { tenantRepository } = this.sysRepositories;
const tenant = await tenantRepository.getByIdWithSubscriptions(user.tenantId);
const tenant = await tenantRepository.getById(user.tenantId);
return [tenant];
}

View File

@@ -1,5 +1,5 @@
import { Service, Inject } from 'typedi';
import { Plan, Tenant } from 'system/models';
import { Plan, PlanSubscription } from 'system/models';
import Subscription from 'services/Subscription/Subscription';
import LicensePaymentMethod from 'services/Payment/LicensePaymentMethod';
import PaymentContext from 'services/Payment';
@@ -29,7 +29,7 @@ export default class SubscriptionService {
* @param {string} licenseCode
* @return {Promise}
*/
async subscriptionViaLicense(
public async subscriptionViaLicense(
tenantId: number,
planSlug: string,
paymentModel?: ILicensePaymentModel,
@@ -53,4 +53,15 @@ export default class SubscriptionService {
tenantId, paymentModel
});
}
/**
* Retrieve all subscription of the given tenant.
* @param {number} tenantId
*/
public async getSubscriptions(tenantId: number) {
this.logger.info('[subscription] trying to get tenant subscriptions.', { tenantId });
const subscriptions = await PlanSubscription.query().where('tenant_id', tenantId);
return subscriptions;
}
}