mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 06:10:31 +00:00
feat: rename voucher to license.
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
import { Router, Request, Response } from 'express'
|
||||
import { check, oneOf, ValidationChain } from 'express-validator';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { Voucher, Plan } from '@/system/models';
|
||||
import { License, Plan } from '@/system/models';
|
||||
import BaseController from '@/http/controllers/BaseController';
|
||||
import VoucherService from '@/services/Payment/Voucher';
|
||||
import LicenseService from '@/services/Payment/License';
|
||||
import validateMiddleware from '@/http/middleware/validateMiddleware';
|
||||
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
||||
import { IVouchersFilter } from '@/interfaces';
|
||||
import { ILicensesFilter } from '@/interfaces';
|
||||
|
||||
@Service()
|
||||
export default class VouchersController extends BaseController {
|
||||
export default class LicensesController extends BaseController {
|
||||
@Inject()
|
||||
voucherService: VoucherService;
|
||||
licenseService: LicenseService;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
@@ -21,40 +21,40 @@ export default class VouchersController extends BaseController {
|
||||
|
||||
router.post(
|
||||
'/generate',
|
||||
this.generateVoucherSchema,
|
||||
this.generateLicenseSchema,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.validatePlanExistance.bind(this)),
|
||||
asyncMiddleware(this.generateVoucher.bind(this)),
|
||||
asyncMiddleware(this.generateLicense.bind(this)),
|
||||
);
|
||||
router.post(
|
||||
'/disable/:voucherId',
|
||||
'/disable/:licenseId',
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.validateVoucherExistance.bind(this)),
|
||||
asyncMiddleware(this.validateNotDisabledVoucher.bind(this)),
|
||||
asyncMiddleware(this.disableVoucher.bind(this)),
|
||||
asyncMiddleware(this.validateLicenseExistance.bind(this)),
|
||||
asyncMiddleware(this.validateNotDisabledLicense.bind(this)),
|
||||
asyncMiddleware(this.disableLicense.bind(this)),
|
||||
);
|
||||
router.post(
|
||||
'/send',
|
||||
this.sendVoucherSchemaValidation,
|
||||
this.sendLicenseSchemaValidation,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.sendVoucher.bind(this)),
|
||||
asyncMiddleware(this.sendLicense.bind(this)),
|
||||
);
|
||||
router.delete(
|
||||
'/:voucherId',
|
||||
asyncMiddleware(this.validateVoucherExistance.bind(this)),
|
||||
asyncMiddleware(this.deleteVoucher.bind(this)),
|
||||
'/:licenseId',
|
||||
asyncMiddleware(this.validateLicenseExistance.bind(this)),
|
||||
asyncMiddleware(this.deleteLicense.bind(this)),
|
||||
);
|
||||
router.get(
|
||||
'/',
|
||||
asyncMiddleware(this.listVouchers.bind(this)),
|
||||
asyncMiddleware(this.listLicenses.bind(this)),
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate voucher validation schema.
|
||||
* Generate license validation schema.
|
||||
*/
|
||||
get generateVoucherSchema(): ValidationChain[] {
|
||||
get generateLicenseSchema(): ValidationChain[] {
|
||||
return [
|
||||
check('loop').exists().isNumeric().toInt(),
|
||||
check('period').exists().isNumeric().toInt(),
|
||||
@@ -66,22 +66,22 @@ export default class VouchersController extends BaseController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific voucher validation schema.
|
||||
* Specific license validation schema.
|
||||
*/
|
||||
get specificVoucherSchema(): ValidationChain[] {
|
||||
get specificLicenseSchema(): ValidationChain[] {
|
||||
return [
|
||||
oneOf([
|
||||
check('voucher_id').exists().isNumeric().toInt(),
|
||||
check('license_id').exists().isNumeric().toInt(),
|
||||
], [
|
||||
check('voucher_code').exists().isNumeric().toInt(),
|
||||
check('license_code').exists().isNumeric().toInt(),
|
||||
])
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* Send voucher validation schema.
|
||||
* Send license validation schema.
|
||||
*/
|
||||
get sendVoucherSchemaValidation(): ValidationChain[] {
|
||||
get sendLicenseSchemaValidation(): ValidationChain[] {
|
||||
return [
|
||||
check('period').exists().isNumeric(),
|
||||
check('period_interval').exists().trim().escape(),
|
||||
@@ -113,60 +113,60 @@ export default class VouchersController extends BaseController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Valdiate the voucher existance on the storage.
|
||||
* Valdiate the license existance on the storage.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function}
|
||||
*/
|
||||
async validateVoucherExistance(req: Request, res: Response, next: Function) {
|
||||
async validateLicenseExistance(req: Request, res: Response, next: Function) {
|
||||
const body = this.matchedBodyData(req);
|
||||
|
||||
const voucherId = body.voucherId || req.params.voucherId;
|
||||
const foundVoucher = await Voucher.query().findById(voucherId);
|
||||
const licenseId = body.licenseId || req.params.licenseId;
|
||||
const foundLicense = await License.query().findById(licenseId);
|
||||
|
||||
if (!foundVoucher) {
|
||||
if (!foundLicense) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VOUCHER.NOT.FOUND', code: 200 }],
|
||||
errors: [{ type: 'LICENSE.NOT.FOUND', code: 200 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates whether the voucher id is disabled.
|
||||
* Validates whether the license id is disabled.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
async validateNotDisabledVoucher(req: Request, res: Response, next: Function) {
|
||||
const voucherId = req.params.voucherId || req.query.voucherId;
|
||||
const foundVoucher = await Voucher.query().findById(voucherId);
|
||||
async validateNotDisabledLicense(req: Request, res: Response, next: Function) {
|
||||
const licenseId = req.params.licenseId || req.query.licenseId;
|
||||
const foundLicense = await License.query().findById(licenseId);
|
||||
|
||||
if (foundVoucher.disabled) {
|
||||
if (foundLicense.disabled) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VOUCHER.ALREADY.DISABLED', code: 200 }],
|
||||
errors: [{ type: 'LICENSE.ALREADY.DISABLED', code: 200 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate vouchers codes with given period in bulk.
|
||||
* Generate licenses codes with given period in bulk.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async generateVoucher(req: Request, res: Response, next: Function) {
|
||||
async generateLicense(req: Request, res: Response, next: Function) {
|
||||
const { loop = 10, period, periodInterval, planId } = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
await this.voucherService.generateVouchers(
|
||||
await this.licenseService.generateLicenses(
|
||||
loop, period, periodInterval, planId,
|
||||
);
|
||||
return res.status(200).send({
|
||||
code: 100,
|
||||
type: 'VOUCHERES.GENERATED.SUCCESSFULLY',
|
||||
message: 'The vouchers have been generated successfully.'
|
||||
type: 'LICENSEES.GENERATED.SUCCESSFULLY',
|
||||
message: 'The licenses have been generated successfully.'
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
@@ -175,84 +175,84 @@ export default class VouchersController extends BaseController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the given voucher on the storage.
|
||||
* Disable the given license on the storage.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async disableVoucher(req: Request, res: Response) {
|
||||
const { voucherId } = req.params;
|
||||
async disableLicense(req: Request, res: Response) {
|
||||
const { licenseId } = req.params;
|
||||
|
||||
await this.voucherService.disableVoucher(voucherId);
|
||||
await this.licenseService.disableLicense(licenseId);
|
||||
|
||||
return res.status(200).send({ voucher_id: voucherId });
|
||||
return res.status(200).send({ license_id: licenseId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given voucher code on the storage.
|
||||
* Deletes the given license code on the storage.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async deleteVoucher(req: Request, res: Response) {
|
||||
const { voucherId } = req.params;
|
||||
async deleteLicense(req: Request, res: Response) {
|
||||
const { licenseId } = req.params;
|
||||
|
||||
await this.voucherService.deleteVoucher(voucherId);
|
||||
await this.licenseService.deleteLicense(licenseId);
|
||||
|
||||
return res.status(200).send({ voucher_id: voucherId });
|
||||
return res.status(200).send({ license_id: licenseId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Send voucher code in the given period to the customer via email or phone number
|
||||
* Send license code in the given period to the customer via email or phone number
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async sendVoucher(req: Request, res: Response) {
|
||||
async sendLicense(req: Request, res: Response) {
|
||||
const { phoneNumber, email, period, periodInterval, planId } = this.matchedBodyData(req);
|
||||
|
||||
const voucher = await Voucher.query()
|
||||
.modify('filterActiveVoucher')
|
||||
.where('voucher_period', period)
|
||||
const license = await License.query()
|
||||
.modify('filterActiveLicense')
|
||||
.where('license_period', period)
|
||||
.where('period_interval', periodInterval)
|
||||
.where('plan_id', planId)
|
||||
.first();
|
||||
|
||||
if (!voucher) {
|
||||
if (!license) {
|
||||
return res.status(400).send({
|
||||
status: 110,
|
||||
message: 'There is no vouchers availiable right now with the given period and plan.',
|
||||
code: 'NO.AVALIABLE.VOUCHER.CODE',
|
||||
message: 'There is no licenses availiable right now with the given period and plan.',
|
||||
code: 'NO.AVALIABLE.LICENSE.CODE',
|
||||
});
|
||||
}
|
||||
await this.voucherService.sendVoucherToCustomer(
|
||||
voucher.voucherCode, phoneNumber, email,
|
||||
await this.licenseService.sendLicenseToCustomer(
|
||||
license.licenseCode, phoneNumber, email,
|
||||
);
|
||||
return res.status(200).send({
|
||||
status: 100,
|
||||
code: 'VOUCHER.CODE.SENT',
|
||||
message: 'The voucher has been sent to the given customer.',
|
||||
code: 'LICENSE.CODE.SENT',
|
||||
message: 'The license has been sent to the given customer.',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Listing vouchers.
|
||||
* Listing licenses.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async listVouchers(req: Request, res: Response) {
|
||||
const filter: IVouchersFilter = {
|
||||
async listLicenses(req: Request, res: Response) {
|
||||
const filter: ILicensesFilter = {
|
||||
disabled: false,
|
||||
used: false,
|
||||
sent: false,
|
||||
active: false,
|
||||
...req.query,
|
||||
};
|
||||
const vouchers = await Voucher.query()
|
||||
const licenses = await License.query()
|
||||
.onBuild((builder) => {
|
||||
builder.modify('filter', filter);
|
||||
builder.orderBy('createdAt', 'ASC');
|
||||
});
|
||||
return res.status(200).send({ vouchers });
|
||||
return res.status(200).send({ licenses });
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { check, param, query, ValidationSchema } from 'express-validator';
|
||||
import { Voucher, Plan } from '@/system/models';
|
||||
import { License, Plan } from '@/system/models';
|
||||
import validateMiddleware from '@/http/middleware/validateMiddleware';
|
||||
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
||||
import PaymentMethodController from '@/http/controllers/Subscription/PaymentMethod';
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from '@/exceptions';
|
||||
|
||||
@Service()
|
||||
export default class PaymentViaVoucherController extends PaymentMethodController {
|
||||
export default class PaymentViaLicenseController extends PaymentMethodController {
|
||||
@Inject('logger')
|
||||
logger: any;
|
||||
|
||||
@@ -22,88 +22,88 @@ export default class PaymentViaVoucherController extends PaymentMethodController
|
||||
|
||||
router.post(
|
||||
'/payment',
|
||||
this.paymentViaVoucherSchema,
|
||||
this.paymentViaLicenseSchema,
|
||||
validateMiddleware,
|
||||
asyncMiddleware(this.validateVoucherCodeExistance.bind(this)),
|
||||
asyncMiddleware(this.validateLicenseCodeExistance.bind(this)),
|
||||
asyncMiddleware(this.validatePlanSlugExistance.bind(this)),
|
||||
asyncMiddleware(this.validateVoucherAndPlan.bind(this)),
|
||||
asyncMiddleware(this.paymentViaVoucher.bind(this)),
|
||||
asyncMiddleware(this.validateLicenseAndPlan.bind(this)),
|
||||
asyncMiddleware(this.paymentViaLicense.bind(this)),
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Payment via voucher validation schema.
|
||||
* Payment via license validation schema.
|
||||
*/
|
||||
get paymentViaVoucherSchema() {
|
||||
get paymentViaLicenseSchema() {
|
||||
return [
|
||||
check('plan_slug').exists().trim().escape(),
|
||||
check('voucher_code').exists().trim().escape(),
|
||||
check('license_code').exists().trim().escape(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the given voucher code exists on the storage.
|
||||
* Validate the given license code exists on the storage.
|
||||
* @async
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async validateVoucherCodeExistance(req: Request, res: Response, next: Function) {
|
||||
const { voucherCode } = this.matchedBodyData(req);
|
||||
this.logger.info('[voucher_payment] trying to validate voucher code.', { voucherCode });
|
||||
async validateLicenseCodeExistance(req: Request, res: Response, next: Function) {
|
||||
const { licenseCode } = this.matchedBodyData(req);
|
||||
this.logger.info('[license_payment] trying to validate license code.', { licenseCode });
|
||||
|
||||
const foundVoucher = await Voucher.query()
|
||||
.modify('filterActiveVoucher')
|
||||
.where('voucher_code', voucherCode)
|
||||
const foundLicense = await License.query()
|
||||
.modify('filterActiveLicense')
|
||||
.where('license_code', licenseCode)
|
||||
.first();
|
||||
|
||||
if (!foundVoucher) {
|
||||
if (!foundLicense) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VOUCHER.CODE.IS.INVALID', code: 120 }],
|
||||
errors: [{ type: 'LICENSE.CODE.IS.INVALID', code: 120 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the voucher period and plan period.
|
||||
* Validate the license period and plan period.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {Function} next
|
||||
*/
|
||||
async validateVoucherAndPlan(req: Request, res: Response, next: Function) {
|
||||
const { planSlug, voucherCode } = this.matchedBodyData(req);
|
||||
this.logger.info('[voucher_payment] trying to validate voucher with the plan.', { voucherCode });
|
||||
async validateLicenseAndPlan(req: Request, res: Response, next: Function) {
|
||||
const { planSlug, licenseCode } = this.matchedBodyData(req);
|
||||
this.logger.info('[license_payment] trying to validate license with the plan.', { licenseCode });
|
||||
|
||||
const voucher = await Voucher.query().findOne('voucher_code', voucherCode);
|
||||
const license = await License.query().findOne('license_code', licenseCode);
|
||||
const plan = await Plan.query().findOne('slug', planSlug);
|
||||
|
||||
if (voucher.planId !== plan.id) {
|
||||
if (license.planId !== plan.id) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VOUCHER.NOT.FOR.GIVEN.PLAN' }],
|
||||
errors: [{ type: 'LICENSE.NOT.FOR.GIVEN.PLAN' }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the subscription payment via voucher code.
|
||||
* Handle the subscription payment via license code.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @return {Response}
|
||||
*/
|
||||
async paymentViaVoucher(req: Request, res: Response, next: Function) {
|
||||
const { planSlug, voucherCode } = this.matchedBodyData(req);
|
||||
async paymentViaLicense(req: Request, res: Response, next: Function) {
|
||||
const { planSlug, licenseCode } = this.matchedBodyData(req);
|
||||
const { tenant } = req;
|
||||
|
||||
try {
|
||||
await this.subscriptionService
|
||||
.subscriptionViaVoucher(tenant.id, planSlug, voucherCode);
|
||||
.subscriptionViaLicense(tenant.id, planSlug, licenseCode);
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'PAYMENT.SUCCESSFULLY.MADE',
|
||||
message: 'Payment via voucher has been made successfully.',
|
||||
message: 'Payment via license has been made successfully.',
|
||||
});
|
||||
} catch (exception) {
|
||||
const errorReasons = [];
|
||||
@@ -3,7 +3,7 @@ import { Container, Service } from 'typedi';
|
||||
import JWTAuth from '@/http/middleware/jwtAuth';
|
||||
import TenancyMiddleware from '@/http/middleware/TenancyMiddleware';
|
||||
import AttachCurrentTenantUser from '@/http/middleware/AttachCurrentTenantUser';
|
||||
import PaymentViaVoucherController from '@/http/controllers/Subscription/PaymentViaVoucher';
|
||||
import PaymentViaLicenseController from '@/http/controllers/Subscription/PaymentViaLicense';
|
||||
|
||||
@Service()
|
||||
export default class SubscriptionController {
|
||||
@@ -17,7 +17,7 @@ export default class SubscriptionController {
|
||||
router.use(AttachCurrentTenantUser);
|
||||
router.use(TenancyMiddleware);
|
||||
|
||||
router.use('/voucher', Container.get(PaymentViaVoucherController).router());
|
||||
router.use('/license', Container.get(PaymentViaLicenseController).router());
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ import Media from '@/http/controllers/Media';
|
||||
import Ping from '@/http/controllers/Ping';
|
||||
import Agendash from '@/http/controllers/Agendash';
|
||||
import Subscription from '@/http/controllers/Subscription';
|
||||
import VouchersController from '@/http/controllers/Subscription/Vouchers';
|
||||
import LicensesController from '@/http/controllers/Subscription/Licenses';
|
||||
|
||||
export default () => {
|
||||
const app = Router();
|
||||
@@ -45,7 +45,7 @@ export default () => {
|
||||
app.use('/auth', Container.get(Authentication).router());
|
||||
app.use('/invite', Container.get(InviteUsers).nonAuthRouter());
|
||||
app.use('/organization', Container.get(Organization).router());
|
||||
app.use('/vouchers', Container.get(VouchersController).router());
|
||||
app.use('/licenses', Container.get(LicensesController).router());
|
||||
app.use('/subscription', Container.get(Subscription).router());
|
||||
app.use('/ping', Container.get(Ping).router());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user