fix: mark payment license as used after usage.

fix: fix system models with createdAt and updatedAt fields.
fix: reset password token expiration time.
This commit is contained in:
Ahmed Bouhuolia
2020-09-07 17:13:37 +02:00
parent d3870974c0
commit ffb0499280
24 changed files with 191 additions and 94 deletions

View File

@@ -1,7 +1,8 @@
import { Inject } from 'typedi';
import { Inject, Service } from 'typedi';
import { Tenant, Plan } from '@/system/models';
import { IPaymentContext } from '@/interfaces';
import { NotAllowedChangeSubscriptionPlan } from '@/exceptions';
import { NoPaymentModelWithPricedPlan } from '@/exceptions';
export default class Subscription<PaymentModel> {
paymentContext: IPaymentContext|null;
@@ -19,7 +20,7 @@ export default class Subscription<PaymentModel> {
/**
* Subscripe to the given plan.
* @param {Plan} plan
* @param {Plan} plan
* @throws {NotAllowedChangeSubscriptionPlan}
*/
async subscribe(
@@ -28,8 +29,11 @@ export default class Subscription<PaymentModel> {
paymentModel?: PaymentModel,
subscriptionSlug: string = 'main',
) {
if (plan.price < 0) {
await this.paymentContext.makePayment(paymentModel);
this.validateIfPlanHasPriceNoPayment(plan, paymentModel);
// @todo
if (plan.price > 0) {
await this.paymentContext.makePayment(paymentModel, plan);
}
const subscription = await tenant.$relatedQuery('subscriptions')
.modify('subscriptionBySlug', subscriptionSlug)
@@ -39,8 +43,7 @@ export default class Subscription<PaymentModel> {
if (subscription && subscription.active()) {
throw new NotAllowedChangeSubscriptionPlan;
// In case there is already subscription associated to the given tenant.
// renew it.
// In case there is already subscription associated to the given tenant renew it.
} else if(subscription && subscription.inactive()) {
await subscription.renew(plan);
@@ -49,4 +52,15 @@ export default class Subscription<PaymentModel> {
await tenant.newSubscription(subscriptionSlug, plan);
}
}
/**
* Throw error in plan has price and no payment model.
* @param {Plan} plan -
* @param {PaymentModel} paymentModel - payment input.
*/
validateIfPlanHasPriceNoPayment(plan: Plan, paymentModel: PaymentMode) {
if (plan.price > 0 && !paymentModel) {
throw new NoPaymentModelWithPricedPlan();
}
}
}

View File

@@ -1,10 +1,11 @@
import { Service, Inject } from 'typedi';
import { Plan, Tenant, License } from '@/system/models';
import { Plan, Tenant } from '@/system/models';
import Subscription from '@/services/Subscription/Subscription';
import VocuherPaymentMethod from '@/services/Payment/LicensePaymentMethod';
import LicensePaymentMethod from '@/services/Payment/LicensePaymentMethod';
import PaymentContext from '@/services/Payment';
import SubscriptionSMSMessages from '@/services/Subscription/SMSMessages';
import SubscriptionMailMessages from '@/services/Subscription/MailMessages';
import { ILicensePaymentModel } from '@/interfaces';
@Service()
export default class SubscriptionService {
@@ -14,31 +15,37 @@ export default class SubscriptionService {
@Inject()
mailMessages: SubscriptionMailMessages;
@Inject('logger')
logger: any;
/**
* Handles the payment process via license code and than subscribe to
* the given tenant.
*
* @param {number} tenantId
* @param {String} planSlug
* @param {string} licenseCode
*
* @return {Promise}
*/
async subscriptionViaLicense(
tenantId: number,
planSlug: string,
licenseCode: string,
paymentModel?: ILicensePaymentModel,
subscriptionSlug: string = 'main',
) {
this.logger.info('[subscription_via_license] try to subscribe via given license.', {
tenantId, paymentModel
});
const plan = await Plan.query().findOne('slug', planSlug);
const tenant = await Tenant.query().findById(tenantId);
const licenseModel = await License.query().findOne('license_code', licenseCode);
const paymentViaLicense = new VocuherPaymentMethod();
const paymentViaLicense = new LicensePaymentMethod();
const paymentContext = new PaymentContext(paymentViaLicense);
const subscription = new Subscription(paymentContext);
return subscription.subscribe(tenant, plan, licenseModel, subscriptionSlug);
await subscription.subscribe(tenant, plan, paymentModel, subscriptionSlug);
this.logger.info('[subscription_via_license] payment via license done successfully.', {
tenantId, paymentModel
});
}
}