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 | 1x 1x 1x 1x 1x 1x 1x | import {bind, BindingScope} from '@loopback/core';
import {IsolationLevel, Options, repository} from '@loopback/repository';
import {validateValueAgainstSchema, HttpErrors} from '@loopback/rest';
import {BaseResponses, ProductHazard} from '@logchain/models';
import {ProductHazardRepository} from '@logchain/repositories';
import {CSVParser, RecordsCreator, ProductHazardRow, productHazardRowSchema, UploadFileType} from '@logchain/csv-parser';
import {ProductHazardDto} from '@logchain/dtos';
import {BaseService} from '.';
@bind({scope: BindingScope.TRANSIENT})
export class ProductHazardService extends BaseService<ProductHazard> implements RecordsCreator {
headers = ['name', 'value'];
constructor(
@repository(ProductHazardRepository)
public productHazardRepo: ProductHazardRepository,
) {
super();
}
/**
* Get array ProductHazard based on filter.
* @param filter
*/
async find(): Promise<BaseResponses<ProductHazard>> {
// allow get all productHazard
this.filter_builder.limit(Number.MAX_SAFE_INTEGER);
return this.productHazardRepo.findWithPaging(this.filter_builder.build());
}
/**
* Builds the query for productHazard.
* @param q The query string.
*/
async buildSearchQuery(q?: string) {
if (!q) {
return;
}
const ilike_value = this.buildILikeValue(q);
// FILTER: code OR name
this.filter_builder.impose({
or: [
{
name: ilike_value,
},
{
value: ilike_value,
},
],
});
}
/**
* Validate row with schema
* @param {ProductHazardRow} row
* @returns Promise
*/
async validateRowsCsv(row: ProductHazardRow): Promise<void> {
await validateValueAgainstSchema(row, productHazardRowSchema);
}
/**
* Create new product-hazard row
* @param {ProductHazardDto} data
* @param {Options} options?
* @returns Promise
*/
async create(data: ProductHazardDto, options?: Options): Promise<ProductHazard> {
const {name, value} = data;
const [{count: existing}] = await Promise.all([
this.productHazardRepo.count({name, value}, options),
]);
if (existing) {
throw new HttpErrors.BadRequest(`The product-hazard already exists: ${value}-${name}`);
}
return this.productHazardRepo.create(data, options);
}
/**
* Creates productHazard from csv files
* @param [uploadedFiles]
* @returns
*/
async createProductHazardFromCSV(uploadedFiles?: UploadFileType) {
const csvParser = new CSVParser<ProductHazardRow>(this);
const tx = await this.productHazardRepo.beginTransaction(IsolationLevel.SERIALIZABLE);
try {
const results = await csvParser.createRecordsFromCSV(uploadedFiles, {transaction: tx});
await tx.commit();
return results;
} catch (error) {
await tx.rollback();
throw new HttpErrors.BadRequest(error.message);
}
}
}
|