Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { inject, Getter } from '@loopback/core';
import { HttpErrors } from '@loopback/rest';
import {
repository,
HasManyRepositoryFactory,
Options,
BelongsToAccessor,
Filter,
Where,
Count,
AnyObject,
} from '@loopback/repository';
import { LogChainDataSource } from '@logchain/datasources';
import {
CompanyRegulatoryHistories,
User,
Company,
} from '@logchain/models';
import { CompanyRegulatoryHistoriesRepository, BaseCrudRepository, UserRepository, CompanyRepository } from '.';
import { Constants } from '@logchain/configs';
import { CompanyRegulatoryView, CompanyRegulatoryViewRelations } from '@logchain/models/views';
const { ParameterizedSQL } = require('loopback-connector');
const COMPANY_REGULATORY_VIEW_NAME = 'company_regulatory_view';
export class CompanyRegulatoryViewRepository extends BaseCrudRepository<
CompanyRegulatoryView,
typeof CompanyRegulatoryView.prototype.id,
CompanyRegulatoryViewRelations
> {
public readonly histories: HasManyRepositoryFactory<
CompanyRegulatoryHistories,
typeof CompanyRegulatoryView.prototype.id
>;
public readonly company: BelongsToAccessor<Company, typeof Company.prototype.id>;
public readonly creator: BelongsToAccessor<User, typeof User.prototype.id>;
public readonly modifier: BelongsToAccessor<User, typeof User.prototype.id>;
constructor(
@inject('datasources.logchain') dataSource: LogChainDataSource,
@repository.getter('CompanyRegulatoryHistoriesRepository')
protected CompanyRegulatoryHistoriesRepositoryGetter: Getter<CompanyRegulatoryHistoriesRepository>,
@repository.getter('UserRepository')
protected userRepoGetter: Getter<UserRepository>,
@repository.getter('CompanyRepository')
protected companyRepoGetter: Getter<CompanyRepository>,
) {
super(CompanyRegulatoryView, dataSource);
this.histories = this.createHasManyRepositoryFactoryFor('histories', CompanyRegulatoryHistoriesRepositoryGetter);
this.registerInclusionResolver('histories', this.histories.inclusionResolver);
this.company = this.createBelongsToAccessor('company', companyRepoGetter);
this.creator = this.createBelongsToAccessor('creator', userRepoGetter);
this.modifier = this.createBelongsToAccessor('modifier', userRepoGetter);
}
validateCompanyNameOrder(order: string) {
return order.match(/^company.name+( (ASC|DESC))?$/);
}
/**
* Finds in view company_regulatory_view
* @param filter
* @param [options]
* @returns
*/
async findInView(filter: Filter<CompanyRegulatoryView>, options?: Options) {
const { include } = filter;
const connector = this.ensureConnector();
const CompanyRegulatoryViewModel = this.ensureModel();
this.applyNormalization(CompanyRegulatoryViewModel, filter);
this.processOrderFilter(filter);
const stmt = this.buildSelectStatement(connector, filter);
const data = await this.execute(stmt.sql, stmt.params);
const objs = data.map((obj: AnyObject) => new CompanyRegulatoryViewModel(obj));
const entities = this.toEntities(objs);
return this.includeRelatedModels(entities, include, options);
}
private ensureConnector() {
const { connector } = this.dataSource;
if (!connector) {
throw new HttpErrors.InternalServerError(`Connector not found!`);
}
return connector;
}
private ensureModel() {
const model = this.dataSource.getModel(CompanyRegulatoryView.modelName);
if (!model) {
throw new HttpErrors.InternalServerError(`Model ${CompanyRegulatoryView.modelName} not found!`);
}
return model;
}
private applyNormalization(model: AnyObject, filter: Filter<CompanyRegulatoryView>) {
if (typeof model['_normalize'] === 'function') {
model['_normalize'](filter);
}
}
private processOrderFilter(filter: Filter<CompanyRegulatoryView>) {
if (filter?.order?.length) {
const companyOrder: string[] = [];
filter?.order.forEach((order, idx) => {
if (this.validateCompanyNameOrder(order)) {
companyOrder.push(order);
filter?.order?.splice(idx, 1);
}
});
this.checkOrderFilter(filter?.order);
filter.order = [...filter?.order, ...companyOrder];
if (!filter.order.length) {
delete filter.order;
}
}
}
private buildSelectStatement(connector: any, filter: Filter<CompanyRegulatoryView>) {
const stmt = connector.buildSelect(CompanyRegulatoryView.modelName, filter);
stmt.sql = stmt.sql.replace(this.getPostgresTableName(), COMPANY_REGULATORY_VIEW_NAME);
return stmt;
}
/**
* Counts in view company_regulatory_view
* @param [where]
* @param [options]
* @returns in view
*/
async countInView(where?: Where<CompanyRegulatoryView>, options?: Options): Promise<Count> {
const { connector } = this.dataSource;
if (!connector) {
throw new HttpErrors.InternalServerError(`Connector not found!`);
}
let stmtCount = new ParameterizedSQL(
'SELECT count(*) as "cnt" FROM ' + connector.tableEscaped(CompanyRegulatoryView.modelName),
);
stmtCount = stmtCount.merge(connector.buildWhere(CompanyRegulatoryView.modelName, where));
stmtCount = connector.parameterize(stmtCount);
stmtCount.sql = stmtCount.sql.replace(this.getPostgresTableName(), COMPANY_REGULATORY_VIEW_NAME);
const reslCount = await this.execute(stmtCount.sql, stmtCount.params);
return {
count: +reslCount[0].cnt,
};
}
/**
* Finds with paging.
* I created a view `company_regulatory_view` same schema with the table `company_regulatory`.
* I re-use loopback-connector to build the query and then replace table name with view name to keep all filter supported by loopback.
* @param filter The filter parameters
* @param [options] The options
* @returns
*/
async findWithPaging(filter: Filter<CompanyRegulatoryView>, options?: Options) {
// will get data from table instead of view.
if (!options?.is_private) {
return super.findWithPaging(filter, options);
}
const { offset = 0, limit = Constants.DefaultLimit, where = {} } = filter;
filter.where = super.rebuildWhere(where);
const [total, data] = await Promise.all([
this.countInView(filter?.where, options),
this.findInView(filter, options),
]);
return {
total: total.count,
has_more: total.count > offset + limit,
data,
};
}
}
|