feat: rename voucher to license.

This commit is contained in:
Ahmed Bouhuolia
2020-09-05 22:58:36 +02:00
parent 750377ba86
commit df0cf287ff
24 changed files with 347 additions and 361 deletions

View File

@@ -0,0 +1,104 @@
import { Service, Container, Inject } from 'typedi';
import cryptoRandomString from 'crypto-random-string';
import { times } from 'lodash';
import { License } from "@/system/models";
import { ILicense } from '@/interfaces';
import LicenseMailMessages from '@/services/Payment/LicenseMailMessages';
import LicenseSMSMessages from '@/services/Payment/LicenseSMSMessages';
@Service()
export default class LicenseService {
@Inject()
smsMessages: LicenseSMSMessages;
@Inject()
mailMessages: LicenseMailMessages;
/**
* Generates the license code in the given period.
* @param {number} licensePeriod
* @return {Promise<ILicense>}
*/
async generateLicense(
licensePeriod: number,
periodInterval: string = 'days',
planId: number,
): ILicense {
let licenseCode: string;
let repeat: boolean = true;
console.log(License);
while(repeat) {
licenseCode = cryptoRandomString({ length: 10, type: 'numeric' });
const foundLicenses = await License.query().where('license_code', licenseCode);
if (foundLicenses.length === 0) {
repeat = false;
}
}
return License.query().insert({
licenseCode, licensePeriod, periodInterval, planId,
});
}
/**
*
* @param {number} loop
* @param {number} licensePeriod
* @param {string} periodInterval
* @param {number} planId
*/
async generateLicenses(
loop = 1,
licensePeriod: numner,
periodInterval: string = 'days',
planId: number,
) {
const asyncOpers: Promise<any>[] = [];
times(loop, () => {
const generateOper = this.generateLicense(licensePeriod, periodInterval, planId);
asyncOpers.push(generateOper);
});
return Promise.all(asyncOpers);
}
/**
* Disables the given license id on the storage.
* @param {number} licenseId
* @return {Promise}
*/
async disableLicense(licenseId: number) {
return License.markLicenseAsDisabled(licenseId, 'id');
}
/**
* Deletes the given license id from the storage.
* @param licenseId
*/
async deleteLicense(licenseId: number) {
return License.query().where('id', licenseId).delete();
}
/**
* Sends license code to the given customer via SMS or mail message.
* @param {string} licenseCode - License code
* @param {string} phoneNumber - Phone number
* @param {string} email - Email address.
*/
async sendLicenseToCustomer(licenseCode: string, phoneNumber: string, email: string) {
const agenda = Container.get('agenda');
// Mark the license as used.
await License.markLicenseAsSent(licenseCode);
if (email) {
await agenda.schedule('1 second', 'send-license-via-email', { licenseCode, email });
}
if (phoneNumber) {
await agenda.schedule('1 second', 'send-license-via-phone', { licenseCode, phoneNumber });
}
}
}

View File

@@ -5,22 +5,22 @@ import { Container } from 'typedi';
export default class SubscriptionMailMessages {
/**
* Send voucher code to the given mail address.
* @param {string} voucherCode
* Send license code to the given mail address.
* @param {string} licenseCode
* @param {email} email
*/
public async sendMailVoucher(voucherCode: string, email: string) {
public async sendMailLicense(licenseCode: string, email: string) {
const Logger = Container.get('logger');
const Mail = Container.get('mail');
const filePath = path.join(global.rootPath, 'views/mail/VoucherReceive.html');
const filePath = path.join(global.rootPath, 'views/mail/LicenseReceive.html');
const template = fs.readFileSync(filePath, 'utf8');
const rendered = Mustache.render(template, { voucherCode });
const rendered = Mustache.render(template, { licenseCode });
const mailOptions = {
to: email,
from: `${process.env.MAIL_FROM_NAME} ${process.env.MAIL_FROM_ADDRESS}`,
subject: 'Bigcapital Voucher',
subject: 'Bigcapital License',
html: rendered,
};
return new Promise((resolve, reject) => {

View File

@@ -0,0 +1,14 @@
import { License } from "@/system/models";
import PaymentMethod from '@/services/Payment/PaymentMethod';
import { IPaymentMethod, ILicensePaymentModel } from '@/interfaces';
export default class VocuherPaymentMethod extends PaymentMethod implements IPaymentMethod {
/**
* Payment subscription of organization via license code.
* @param {ILicensePaymentModel}
*/
async payment(licensePaymentModel: ILicensePaymentModel) {
// Mark the license code as used.
return License.markLicenseAsUsed(licensePaymentModel.licenseCode);
}
}

View File

@@ -6,12 +6,12 @@ export default class SubscriptionSMSMessages {
smsClient: SMSClient;
/**
* Sends voucher code to the given phone number via SMS message.
* Sends license code to the given phone number via SMS message.
* @param {string} phoneNumber
* @param {string} voucherCode
* @param {string} licenseCode
*/
public async sendVoucherSMSMessage(phoneNumber: string, voucherCode: string) {
const message: string = `Your voucher card number: ${voucherCode}.`;
public async sendLicenseSMSMessage(phoneNumber: string, licenseCode: string) {
const message: string = `Your license card number: ${licenseCode}.`;
return this.smsClient.sendMessage(phoneNumber, message);
}
}

View File

@@ -1,104 +0,0 @@
import { Service, Container, Inject } from 'typedi';
import cryptoRandomString from 'crypto-random-string';
import { times } from 'lodash';
import { Voucher } from "@/system/models";
import { IVoucher } from '@/interfaces';
import VoucherMailMessages from '@/services/Payment/VoucherMailMessages';
import VoucherSMSMessages from '@/services/Payment/VoucherSMSMessages';
@Service()
export default class VoucherService {
@Inject()
smsMessages: VoucherSMSMessages;
@Inject()
mailMessages: VoucherMailMessages;
/**
* Generates the voucher code in the given period.
* @param {number} voucherPeriod
* @return {Promise<IVoucher>}
*/
async generateVoucher(
voucherPeriod: number,
periodInterval: string = 'days',
planId: number,
): IVoucher {
let voucherCode: string;
let repeat: boolean = true;
console.log(Voucher);
while(repeat) {
voucherCode = cryptoRandomString({ length: 10, type: 'numeric' });
const foundVouchers = await Voucher.query().where('voucher_code', voucherCode);
if (foundVouchers.length === 0) {
repeat = false;
}
}
return Voucher.query().insert({
voucherCode, voucherPeriod, periodInterval, planId,
});
}
/**
*
* @param {number} loop
* @param {number} voucherPeriod
* @param {string} periodInterval
* @param {number} planId
*/
async generateVouchers(
loop = 1,
voucherPeriod: numner,
periodInterval: string = 'days',
planId: number,
) {
const asyncOpers: Promise<any>[] = [];
times(loop, () => {
const generateOper = this.generateVoucher(voucherPeriod, periodInterval, planId);
asyncOpers.push(generateOper);
});
return Promise.all(asyncOpers);
}
/**
* Disables the given voucher id on the storage.
* @param {number} voucherId
* @return {Promise}
*/
async disableVoucher(voucherId: number) {
return Voucher.markVoucherAsDisabled(voucherId, 'id');
}
/**
* Deletes the given voucher id from the storage.
* @param voucherId
*/
async deleteVoucher(voucherId: number) {
return Voucher.query().where('id', voucherId).delete();
}
/**
* Sends voucher code to the given customer via SMS or mail message.
* @param {string} voucherCode - Voucher code
* @param {string} phoneNumber - Phone number
* @param {string} email - Email address.
*/
async sendVoucherToCustomer(voucherCode: string, phoneNumber: string, email: string) {
const agenda = Container.get('agenda');
// Mark the voucher as used.
await Voucher.markVoucherAsSent(voucherCode);
if (email) {
await agenda.schedule('1 second', 'send-voucher-via-email', { voucherCode, email });
}
if (phoneNumber) {
await agenda.schedule('1 second', 'send-voucher-via-phone', { voucherCode, phoneNumber });
}
}
}

View File

@@ -1,14 +0,0 @@
import { Voucher } from "@/system/models";
import PaymentMethod from '@/services/Payment/PaymentMethod';
import { IPaymentMethod, IVoucherPaymentModel } from '@/interfaces';
export default class VocuherPaymentMethod extends PaymentMethod implements IPaymentMethod {
/**
* Payment subscription of organization via voucher code.
* @param {IVoucherPaymentModel}
*/
async payment(voucherPaymentModel: IVoucherPaymentModel) {
// Mark the voucher code as used.
return Voucher.markVoucherAsUsed(voucherPaymentModel.voucherCode);
}
}

View File

@@ -1,7 +1,7 @@
import { Service, Inject } from 'typedi';
import { Plan, Tenant, Voucher } from '@/system/models';
import { Plan, Tenant, License } from '@/system/models';
import Subscription from '@/services/Subscription/Subscription';
import VocuherPaymentMethod from '@/services/Payment/VoucherPaymentMethod';
import VocuherPaymentMethod from '@/services/Payment/LicensePaymentMethod';
import PaymentContext from '@/services/Payment';
import SubscriptionSMSMessages from '@/services/Subscription/SMSMessages';
import SubscriptionMailMessages from '@/services/Subscription/MailMessages';
@@ -15,30 +15,30 @@ export default class SubscriptionService {
mailMessages: SubscriptionMailMessages;
/**
* Handles the payment process via voucher code and than subscribe to
* Handles the payment process via license code and than subscribe to
* the given tenant.
*
* @param {number} tenantId
* @param {String} planSlug
* @param {string} voucherCode
* @param {string} licenseCode
*
* @return {Promise}
*/
async subscriptionViaVoucher(
async subscriptionViaLicense(
tenantId: number,
planSlug: string,
voucherCode: string,
licenseCode: string,
subscriptionSlug: string = 'main',
) {
const plan = await Plan.query().findOne('slug', planSlug);
const tenant = await Tenant.query().findById(tenantId);
const voucherModel = await Voucher.query().findOne('voucher_code', voucherCode);
const licenseModel = await License.query().findOne('license_code', licenseCode);
const paymentViaVoucher = new VocuherPaymentMethod();
const paymentContext = new PaymentContext(paymentViaVoucher);
const paymentViaLicense = new VocuherPaymentMethod();
const paymentContext = new PaymentContext(paymentViaLicense);
const subscription = new Subscription(paymentContext);
return subscription.subscribe(tenant, plan, voucherModel, subscriptionSlug);
return subscription.subscribe(tenant, plan, licenseModel, subscriptionSlug);
}
}