mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
add server to monorepo.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import SystemRepository from '@/system/repositories/SystemRepository';
|
||||
import { PlanSubscription } from '@/system/models';
|
||||
|
||||
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 cacheKey = this.getCacheKey('getBySlugInTenant', slug, tenantId);
|
||||
|
||||
return this.cache.get(cacheKey, () => {
|
||||
return PlanSubscription.query()
|
||||
.findOne('slug', slug)
|
||||
.where('tenant_id', tenantId);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import CachableRepository from "repositories/CachableRepository";
|
||||
|
||||
export default class SystemRepository extends CachableRepository {
|
||||
|
||||
}
|
||||
101
packages/server/src/system/repositories/SystemUserRepository.ts
Normal file
101
packages/server/src/system/repositories/SystemUserRepository.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import moment from 'moment';
|
||||
import SystemRepository from '@/system/repositories/SystemRepository';
|
||||
import { SystemUser } from '@/system/models';
|
||||
import { ISystemUser } from '@/interfaces';
|
||||
|
||||
export default class SystemUserRepository extends SystemRepository {
|
||||
/**
|
||||
* Gets the repository's model.
|
||||
*/
|
||||
get model() {
|
||||
return SystemUser.bindKnex(this.knex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds system user by crediential.
|
||||
* @param {string} crediential - Phone number or email.
|
||||
* @return {ISystemUser}
|
||||
* @return {Promise<ISystemUser>}
|
||||
*/
|
||||
findByCrediential(crediential: string): Promise<ISystemUser> {
|
||||
const cacheKey = this.getCacheKey('findByCrediential', crediential);
|
||||
|
||||
return this.cache.get(cacheKey, () => {
|
||||
return this.model.query()
|
||||
.findOne('email', crediential)
|
||||
.orWhere('phone_number', crediential);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve user by id and tenant id.
|
||||
* @param {number} userId - User id.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @return {Promise<ISystemUser>}
|
||||
*/
|
||||
findOneByIdAndTenant(userId: number, tenantId: number): Promise<ISystemUser> {
|
||||
const cacheKey = this.getCacheKey('findOneByIdAndTenant', userId, tenantId);
|
||||
|
||||
return this.cache.get(cacheKey, () => {
|
||||
return this.model.query()
|
||||
.findOne({ id: userId, tenant_id: tenantId });
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve system user details by the given email.
|
||||
* @param {string} email - Email
|
||||
* @return {Promise<ISystemUser>}
|
||||
*/
|
||||
findOneByEmail(email: string): Promise<ISystemUser> {
|
||||
const cacheKey = this.getCacheKey('findOneByEmail', email);
|
||||
|
||||
return this.cache.get(cacheKey, () => {
|
||||
return this.model.query().findOne('email', email);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve user by phone number.
|
||||
* @param {string} phoneNumber - Phone number
|
||||
* @return {Promise<ISystemUser>}
|
||||
*/
|
||||
findOneByPhoneNumber(phoneNumber: string): Promise<ISystemUser> {
|
||||
const cacheKey = this.getCacheKey('findOneByPhoneNumber', phoneNumber);
|
||||
|
||||
return this.cache.get(cacheKey, () => {
|
||||
return this.model.query()
|
||||
.findOne('phoneNumber', phoneNumber);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Patches the last login date to the given system user.
|
||||
* @param {number} userId
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
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.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
activateById(userId: number): Promise<void> {
|
||||
return super.update({ active: 1 }, { id: userId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Inactivate user by the given id.
|
||||
* @param {number} userId - User id.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
inactivateById(userId: number): Promise<void> {
|
||||
return super.update({ active: 0 }, { id: userId });
|
||||
}
|
||||
}
|
||||
43
packages/server/src/system/repositories/TenantRepository.ts
Normal file
43
packages/server/src/system/repositories/TenantRepository.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import moment from "moment";
|
||||
import uniqid from 'uniqid';
|
||||
import SystemRepository from "./SystemRepository";
|
||||
import { Tenant } from "@/system/models";
|
||||
import { ITenant } from '@/interfaces';
|
||||
|
||||
export default class TenantRepository extends SystemRepository {
|
||||
/**
|
||||
* Gets the repository's model.
|
||||
*/
|
||||
get model() {
|
||||
return Tenant.bindKnex(this.knex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new tenant with random organization id.
|
||||
* @return {ITenant}
|
||||
*/
|
||||
createWithUniqueOrgId(uniqId?: string): Promise<ITenant>{
|
||||
const organizationId = uniqid() || uniqId;
|
||||
return super.create({ organizationId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark as seeded.
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
markAsSeeded(tenantId: number) {
|
||||
return super.update({
|
||||
seededAt: moment().toMySqlDateTime(),
|
||||
}, { id: tenantId })
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the the given organization as initialized.
|
||||
* @param {string} organizationId
|
||||
*/
|
||||
markAsInitialized(tenantId: number) {
|
||||
return super.update({
|
||||
initializedAt: moment().toMySqlDateTime(),
|
||||
}, { id: tenantId });
|
||||
}
|
||||
}
|
||||
9
packages/server/src/system/repositories/index.ts
Normal file
9
packages/server/src/system/repositories/index.ts
Normal 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,
|
||||
};
|
||||
Reference in New Issue
Block a user