fix: system repositories.

This commit is contained in:
a.bouhuolia
2020-12-17 17:19:16 +02:00
parent 7a847fc895
commit a67b1fbdd0
54 changed files with 1452 additions and 983 deletions

View File

@@ -23,6 +23,14 @@ export default class SystemUser extends mixin(SystemModel, [SoftDelete({
return ['createdAt', 'updatedAt'];
}
static get virtualAttributes() {
return ['fullName'];
}
get fullName() {
return (this.firstName + ' ' + this.lastName).trim();
}
/**
* Relationship mapping.
*/

View File

@@ -1,22 +1,26 @@
import { Service, Inject } from 'typedi';
import SystemRepository from "system/repositories/SystemRepository";
import { PlanSubscription } from 'system/models'
import SystemRepository from 'system/repositories/SystemRepository';
import { PlanSubscription } from 'system/models';
@Service()
export default class SubscriptionRepository extends SystemRepository{
@Inject('cache')
cache: any;
export default class SubscriptionRepository extends SystemRepository {
/**
* Gets the repository's model.
*/
get model() {
return PlanSubscription.bindKnex(this.knex);
}
/**
* 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}`;
/**
* Retrieve subscription from a given slug in specific tenant.
* @param {string} slug
* @param {number} tenantId
*/
getBySlugInTenant(slug: string, tenantId: number) {
const cacheKey = this.getCacheKey('getBySlugInTenant', slug, tenantId);
return this.cache.get(key, () => {
return PlanSubscription.query().findOne('slug', slug).where('tenant_id', tenantId);
});
}
}
return this.cache.get(cacheKey, () => {
return PlanSubscription.query()
.findOne('slug', slug)
.where('tenant_id', tenantId);
});
}
}

View File

@@ -1,5 +1,5 @@
import CachableRepository from "repositories/CachableRepository";
export default class SystemRepository extends CachableRepository {
export default class SystemRepository {
}

View File

@@ -1,24 +1,14 @@
import { Service, Inject } from 'typedi';
import moment from 'moment';
import SystemRepository from "system/repositories/SystemRepository";
import { SystemUser } from "system/models";
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
* @return {Promise<void>}
* Gets the repository's model.
*/
async patchLastLoginAt(userId: number): Promise<void> {
await SystemUser.query().patchAndFetchById(userId, {
last_login_at: moment().toMySqlDateTime()
});
this.flushCache();
get model() {
return SystemUser.bindKnex(this.knex);
}
/**
@@ -28,43 +18,42 @@ export default class SystemUserRepository extends SystemRepository {
* @return {Promise<ISystemUser>}
*/
findByCrediential(crediential: string): Promise<ISystemUser> {
return SystemUser.query().whereNotDeleted()
.findOne('email', crediential)
.orWhere('phone_number', crediential);
}
/**
* Retrieve system user details of the given id.
* @param {number} userId - User id.
* @return {Promise<ISystemUser>}
*/
getById(userId: number): Promise<ISystemUser> {
return this.cache.get(`systemUser.id.${userId}`, () => {
return SystemUser.query().whereNotDeleted().findById(userId);
const cacheKey = this.getCacheKey('findByCrediential', crediential);
return this.cache.get(cacheKey, () => {
return this.model.query()
.whereNotDeleted()
.findOne('email', crediential)
.orWhere('phone_number', crediential);
});
}
/**
* Retrieve user by id and tenant id.
* @param {number} userId - User id.
* @param {number} tenantId - Tenant id.
* @param {number} userId - User id.
* @param {number} tenantId - Tenant id.
* @return {Promise<ISystemUser>}
*/
getByIdAndTenant(userId: number, tenantId: number): Promise<ISystemUser> {
return this.cache.get(`systemUser.id.${userId}.tenant.${tenantId}`, () => {
return SystemUser.query().whereNotDeleted()
findOneByIdAndTenant(userId: number, tenantId: number): Promise<ISystemUser> {
const cacheKey = this.getCacheKey('findOneByIdAndTenant', userId, tenantId);
return this.cache.get(cacheKey, () => {
return this.model.query()
.whereNotDeleted()
.findOne({ id: userId, tenant_id: tenantId });
});
}
/**
* Retrieve system user details by the given email.
* @param {string} email - Email
* @param {string} email - Email
* @return {Promise<ISystemUser>}
*/
getByEmail(email: string): Promise<ISystemUser> {
return this.cache.get(`systemUser.email.${email}`, () => {
return SystemUser.query().whereNotDeleted().findOne('email', email);
findOneByEmail(email: string): Promise<ISystemUser> {
const cacheKey = this.getCacheKey('findOneByEmail', email);
return this.cache.get(cacheKey, () => {
return this.model.query().whereNotDeleted().findOne('email', email);
});
}
@@ -73,69 +62,43 @@ export default class SystemUserRepository extends SystemRepository {
* @param {string} phoneNumber - Phone number
* @return {Promise<ISystemUser>}
*/
getByPhoneNumber(phoneNumber: string): Promise<ISystemUser> {
return this.cache.get(`systemUser.phoneNumber.${phoneNumber}`, () => {
return SystemUser.query().whereNotDeleted().findOne('phoneNumber', phoneNumber);
findOneByPhoneNumber(phoneNumber: string): Promise<ISystemUser> {
const cacheKey = this.getCacheKey('findOneByPhoneNumber', phoneNumber);
return this.cache.get(cacheKey, () => {
return this.model.query()
.whereNotDeleted()
.findOne('phoneNumber', phoneNumber);
});
}
/**
* Edits details.
* @param {number} userId - User id.
* @param {number} user - User input.
* @return {Promise<void>}
*/
async edit(userId: number, userInput: ISystemUser): Promise<void> {
await SystemUser.query().patchAndFetchById(userId, { ...userInput });
this.flushCache();
}
/**
* Creates a new user.
* @param {IUser} userInput - User input.
* @return {Promise<ISystemUser>}
*/
async create(userInput: ISystemUser): Promise<ISystemUser> {
const systemUser = await SystemUser.query().insert({ ...userInput });
this.flushCache();
return systemUser;
}
/**
* Deletes user by the given id.
* @param {number} userId - User id.
* Patches the last login date to the given system user.
* @param {number} userId
* @return {Promise<void>}
*/
async deleteById(userId: number): Promise<void> {
await SystemUser.query().where('id', userId).delete();
this.flushCache();
patchLastLoginAt(userId: number): Promise<void> {
return super.update(
{ last_login_at: moment().toMySqlDateTime() },
{ id: userId }
);
}
/**
* Activate user by the given id.
* @param {number} userId - User id.
* @param {number} userId - User id.
* @return {Promise<void>}
*/
async activateById(userId: number): Promise<void> {
await SystemUser.query().patchAndFetchById(userId, { active: 1 });
this.flushCache();
activateById(userId: number): Promise<void> {
return super.update({ active: 1 }, { id: userId });
}
/**
* Inactivate user by the given id.
* @param {number} userId - User id.
* @param {number} userId - User id.
* @return {Promise<void>}
*/
async inactivateById(userId: number): Promise<void> {
await SystemUser.query().patchAndFetchById(userId, { active: 0 });
this.flushCache();
inactivateById(userId: number): Promise<void> {
return super.update({ active: 0 }, { id: userId });
}
/**
* Flushes user repository cache.
*/
flushCache() {
this.cache.delStartWith('systemUser');
}
}
}

View File

@@ -1,83 +1,43 @@
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';
import SystemRepository from "./SystemRepository";
import { Tenant } from "system/models";
import { ITenant } from 'interfaces';
export default class TenantRepository extends SystemRepository {
@Inject('cache')
cache: any;
/**
* Flush the given tenant stored cache.
* @param {ITenant} tenant
* Gets the repository's model.
*/
flushTenantCache(tenant: ITenant) {
this.cache.del(`tenant.org.${tenant.organizationId}`);
this.cache.del(`tenant.id.${tenant.id}`);
get model() {
return Tenant.bindKnex(this.knex);
}
/**
* Creates a new tenant with random organization id.
* @return {ITenant}
*/
newTenantWithUniqueOrgId(uniqId?: string): Promise<ITenant>{
createWithUniqueOrgId(uniqId?: string): Promise<ITenant>{
const organizationId = uniqid() || uniqId;
return Tenant.query().insert({ organizationId });
return super.create({ 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);
markAsSeeded(tenantId: number) {
return super.update({
seededAt: moment().toMySqlDateTime(),
}, { id: tenantId })
}
/**
* 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 - Tenant id.
*/
getById(tenantId: number) {
return this.cache.get(`tenant.id.${tenantId}`, () => {
return Tenant.query().findById(tenantId);
});
}
/**
* Retrieve tenant details with associated subscriptions
* and plans by the given tenant id.
* @param {number} tenantId - Tenant id.
*/
getByIdWithSubscriptions(tenantId: number) {
return Tenant.query().findById(tenantId)
.withGraphFetched('subscriptions.plan');
markAsInitialized(tenantId: number) {
return super.update({
initializedAt: moment().toMySqlDateTime(),
}, { id: tenantId });
}
}