feat: remove path alias.

feat: remove Webpack and depend on nodemon.
feat: refactoring expenses.
feat: optimize system users with caching.
feat: architecture tenant optimize.
This commit is contained in:
Ahmed Bouhuolia
2020-09-15 00:51:39 +02:00
parent ad00f140d1
commit a22c8395f3
293 changed files with 3391 additions and 1637 deletions

View File

@@ -0,0 +1,21 @@
import { Service, Inject } from 'typedi';
import SystemRepository from "system/repositories/SystemRepository";
import { PlanSubscription } from 'system/models'
@Service()
export default class SubscriptionRepository extends SystemRepository{
@Inject('cache')
cache: any;
/**
* Retrieve subscription from a given slug in specific tenant.
* @param {string} slug
* @param {number] tenantId
*/
getBySlugInTenant(slug: string, tenantId: number) {
const key = `subscription.slug.${slug}.tenant.${tenantId}`;
return this.cache.get(key, () => {
return PlanSubscription.query().findOne('slug', slug).where('tenant_id', tenantId);
});
}
}

View File

@@ -0,0 +1,5 @@
export default class SystemRepository {
}

View File

@@ -0,0 +1,137 @@
import { Service, Inject } from 'typedi';
import moment from 'moment';
import SystemRepository from "system/repositories/SystemRepository";
import { SystemUser } from "system/models";
import { ISystemUser } from 'interfaces';
@Service()
export default class SystemUserRepository extends SystemRepository {
@Inject('cache')
cache: any;
/**
* Patches the last login date to the given system user.
* @param {number} userId
*/
async patchLastLoginAt(userId: number) {
const user = await SystemUser.query().patchAndFetchById(userId, {
last_login_at: moment().toMySqlDateTime()
});
this.flushUserCache(user);
return user;
}
/**
* Finds system user by crediential.
* @param {string} crediential - Phone number or email.
* @return {ISystemUser}
*/
findByCrediential(crediential: string) {
return SystemUser.query().whereNotDeleted()
.findOne('email', crediential)
.orWhere('phone_number', crediential);
}
/**
* Retrieve system user details of the given id.
* @param {number} userId
*/
getById(userId: number) {
return this.cache.get(`systemUser.id.${userId}`, () => {
return SystemUser.query().whereNotDeleted().findById(userId);
});
}
/**
* Retrieve user by id and tenant id.
* @param {number} userId
* @param {number} tenantId
*/
getByIdAndTenant(userId: number, tenantId: number) {
return this.cache.get(`systemUser.id.${userId}.tenant.${tenantId}`, () => {
return SystemUser.query().whereNotDeleted()
.findOne({ id: userId, tenant_id: tenantId });
});
}
/**
* Retrieve system user details by the given email.
* @param {string} email
*/
getByEmail(email: string) {
return this.cache.get(`systemUser.email.${email}`, () => {
return SystemUser.query().whereNotDeleted().findOne('email', email);
});
}
/**
* Retrieve user by phone number.
* @param {string} phoneNumber
*/
getByPhoneNumber(phoneNumber: string) {
return this.cache.get(`systemUser.phoneNumber.${phoneNumber}`, () => {
return SystemUser.query().whereNotDeleted().findOne('phoneNumber', phoneNumber);
});
}
/**
* Edits details.
* @param {number} userId
* @param {number} user
*/
edit(userId: number, userInput: ISystemUser) {
const user = SystemUser.query().patchAndFetchById(userId, { ...userInput });
this.flushUserCache(user);
return user;
}
/**
* Creates a new user.
* @param {IUser} userInput
*/
create(userInput: ISystemUser) {
return SystemUser.query().insert({ ...userInput });
}
/**
* Deletes user by the given id.
* @param {number} userId
*/
async deleteById(userId: number) {
const user = this.getById(userId);
await SystemUser.query().where('id', userId).delete();
this.flushUserCache(user);
}
/**
* Activate user by the given id.
* @param {number} userId
*/
async activateById(userId: number) {
const user = await SystemUser.query().patchAndFetchById(userId, { active: 1 });
this.flushUserCache(user);
return user;
}
/**
* Inactivate user by the given id.
* @param {number} userId
*/
async inactivateById(userId: number) {
const user = await SystemUser.query().patchAndFetchById(userId, { active: 0 });
this.flushUserCache(user);
return user;
}
/**
* Flush user cache.
* @param {IUser} user
*/
flushUserCache(user: ISystemUser) {
this.cache.del(`systemUser.phoneNumber.${user.phoneNumber}`);
this.cache.del(`systemUser.email.${user.email}`);
this.cache.del(`systemUser.id.${user.id}`);
this.cache.del(`systemUser.id.${user.id}.tenant.${user.tenantId}`);
}
}

View File

@@ -0,0 +1,73 @@
import { Inject } from 'typedi';
import moment from "moment";
import { Tenant } from 'system/models';
import SystemRepository from "./SystemRepository";
import { ITenant } from 'interfaces';
import uniqid from 'uniqid';
export default class TenantRepository extends SystemRepository {
@Inject('cache')
cache: any;
/**
* Flush the given tenant stored cache.
* @param {ITenant} tenant
*/
flushTenantCache(tenant: ITenant) {
this.cache.del(`tenant.org.${tenant.organizationId}`);
this.cache.del(`tenant.id.${tenant.id}`);
}
/**
* Creates a new tenant with random organization id.
* @return {ITenant}
*/
newTenantWithUniqueOrgId(uniqId?: string): Promise<ITenant>{
const organizationId = uniqid() || uniqId;
return Tenant.query().insert({ organizationId });
}
/**
* Mark as seeded.
* @param {number} tenantId
*/
async markAsSeeded(tenantId: number) {
const tenant = await Tenant.query()
.patchAndFetchById(tenantId, {
seeded_at: moment().toMySqlDateTime(),
});
this.flushTenantCache(tenant);
}
/**
* Mark the the given organization as initialized.
* @param {string} organizationId
*/
async markAsInitialized(tenantId: number) {
const tenant = await Tenant.query()
.patchAndFetchById(tenantId, {
initialized_at: moment().toMySqlDateTime(),
});
this.flushTenantCache(tenant);
}
/**
* Retrieve tenant details by the given organization id.
* @param {string} organizationId
*/
getByOrgId(organizationId: string) {
return this.cache.get(`tenant.org.${organizationId}`, () => {
return Tenant.query().findOne('organization_id', organizationId);
});
}
/**
* Retrieve tenant details by the given tenant id.
* @param {string} tenantId
*/
getById(tenantId: number) {
return this.cache.get(`tenant.id.${tenantId}`, () => {
return Tenant.query().findById(tenantId);
});
}
}

View File

@@ -0,0 +1,9 @@
import SystemUserRepository from 'system/repositories/SystemUserRepository';
import SubscriptionRepository from 'system/repositories/SubscriptionRepository';
import TenantRepository from 'system/repositories/TenantRepository';
export {
SystemUserRepository,
SubscriptionRepository,
TenantRepository,
};