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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x | import { inject, Getter } from '@loopback/core';
import { HttpErrors } from '@loopback/rest';
import { repository, HasManyRepositoryFactory, DataObject, Options, BelongsToAccessor, Filter, Where, Count, AnyObject } from '@loopback/repository';
import { pick } from 'lodash';
import { LogChainDataSource } from '@logchain/datasources';
import { CompanyRegulatoryHistories, CompanyRegulatory, CompanyRegulatoryRelations, Status, sharedProperties, User, Company } from '@logchain/models';
import { CompanyRegulatoryHistoriesRepository, BaseCrudRepository, UserRepository, CompanyRepository } from '.';
import { Constants } from '@logchain/configs';
const { ParameterizedSQL } = require('loopback-connector');
const COMPANY_REGULATORY_VIEW_NAME = 'company_regulatory_view';
export class CompanyRegulatoryRepository extends BaseCrudRepository<
CompanyRegulatory,
typeof CompanyRegulatory.prototype.id,
CompanyRegulatoryRelations
> {
public readonly histories: HasManyRepositoryFactory<CompanyRegulatoryHistories, typeof CompanyRegulatory.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(CompanyRegulatory, 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,);
}
/**
* Override create to write the history
* @param entity The entity
* @param options The options
*/
async create(entity: DataObject<CompanyRegulatory>, options: Options = {}): Promise<CompanyRegulatory> {
entity.company_id = this.current_user?.company_id;
entity.status = Status.CompanyRegulatory.ForValidation;
const newDbInst = await super.create(entity, options);
await this.histories(newDbInst.id).create({
company_id: this.current_user?.company_id,
history_info: pick(newDbInst, sharedProperties),
});
return newDbInst;
}
/**
* Override create to write the history
* @param entity The entity
* @param options The options
*/
async updateById(id: typeof CompanyRegulatory.prototype.id, data: DataObject<CompanyRegulatory & { comment: string }>, options: Options = {}): Promise<void> {
await Promise.all([
super.updateById(id, data, options),
this.histories(id).create({
company_id: options.companyRegulatoryInst.company_id,
history_info: {
...pick(options.companyRegulatoryInst, sharedProperties),
...pick(data, sharedProperties),
},
comment: data.comment,
}),
]);
}
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<CompanyRegulatory>, options?: Options) {
const { include } = filter;
const { connector } = this.dataSource;
if (!connector) {
throw new HttpErrors.InternalServerError(`Connector not found!`);
}
const companyRegulatoryModel = this.dataSource.getModel(CompanyRegulatory.modelName);
if (!companyRegulatoryModel) {
throw new HttpErrors.InternalServerError(`Model ${CompanyRegulatory.modelName} not found!`);
}
if (typeof (companyRegulatoryModel as AnyObject)['_normalize'] === 'function') {
(companyRegulatoryModel as AnyObject)['_normalize'](filter);
}
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;
}
}
const stmt = connector.buildSelect(CompanyRegulatory.modelName, filter);
stmt.sql = stmt.sql.replace(this.getPostgresTableName(), COMPANY_REGULATORY_VIEW_NAME);
const data = await this.execute(stmt.sql, stmt.params);
const objs = data.map((obj: AnyObject) => {
return new companyRegulatoryModel(obj);
});
const entities = this.toEntities(objs);
return this.includeRelatedModels(entities, include, options);
}
/**
* Counts in view company_regulatory_view
* @param [where]
* @param [options]
* @returns in view
*/
async countInView(where?: Where<CompanyRegulatory>, 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(CompanyRegulatory.modelName)
);
stmtCount = stmtCount.merge(connector.buildWhere(CompanyRegulatory.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<CompanyRegulatory>, 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,
};
}
}
|