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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | 1x 1x 1x 1x 1x 1x 17x 17x 17x | import { bind, /* inject, */ BindingScope } from '@loopback/core';
import {
AnyObject,
EntityNotFoundError,
FilterBuilder,
Options,
OrClause,
repository,
} from '@loopback/repository';
import { BaseResponses, Category } from '../models';
import {
CategoryRepository,
CategoryViewRepository,
} from '../repositories';
import { BaseService } from './base.service';
import { CategoryView, CategoryViewWithRelations } from '@logchain/models/views';
import { CategoryDto } from '@logchain/dtos';
import { HttpErrors } from '@loopback/rest';
@bind({ scope: BindingScope.TRANSIENT })
export class CategoryService extends BaseService<Category> {
constructor(
@repository(CategoryRepository)
public category_repo: CategoryRepository,
@repository(CategoryViewRepository)
public category_view_repo: CategoryViewRepository,
) {
super();
}
public ensureRightAccessRecord(): void {
this.filter_builder.impose({
company_id: this.current_user.company_id,
} as unknown as OrClause<Category>);
}
/**
* Get company documents based on filter.
*/
public ensureUserRightAccessRecord(): void {
this.filter_builder.impose({
or: [
{
granted_users: {
contains: [this.current_user.id],
},
},
{
granted_users: {
contains: [0],
},
},
{
granted_users: null,
},
],
} as unknown as OrClause<Category>);
}
/**
* Get company documents based on filter.
*/
async find(): Promise<BaseResponses<CategoryView>> {
const filter_builder = new FilterBuilder<CategoryView>();
filter_builder.filter = this.filter_builder.filter as AnyObject;
return this.category_view_repo.findWithPaging(filter_builder.build());
}
/**
* Get Category by id or identifier
* @param id The user id
* @param filter The filter
* @returns Promise<Category | null>
*/
async getByIdOrIdentifier(id: number, options?: Options): Promise<CategoryViewWithRelations> {
this.addWhereIdOrIdentifier(id);
const filter_builder = new FilterBuilder<CategoryView>();
filter_builder.filter = this.filter_builder.filter;
const category = await this.category_view_repo.findOne(
filter_builder.build(),
{
get_reference: true,
},
);
if (!category) {
throw new EntityNotFoundError(this.category_view_repo.entityClass, id);
}
return category;
}
/**
* Create new company party
* @param {CategoryDto} data
* @param {Options} options?
* @returns Promise
*/
async create(data: CategoryDto, options: Options = {}): Promise<Category> {
// assign the current user company id to the new category
data.company_id = this.current_user.company_id;
// Check if the category already exists
const existingCategory = await this.category_repo.findOne({
where: {
name: data.name,
company_id: this.current_user.company_id,
},
});
if (existingCategory) {
// If the category already exists, throw an error
throw new HttpErrors.BadRequest(`Category with name ${data.name} already exists.`);
}
return this.category_repo.create(data, options);
}
/**
* Update Category by id or identifier
* @param id The id or identifier of Category
* @param Category The new Category info
* @returns Promise<void>
*/
async updateByIdOrIdentifier(id: number, category: Category, options: Options = {}): Promise<Category> {
// Check if the category already exists
const existingCategory = await this.category_repo.findOne({
where: {
name: category.name,
company_id: this.current_user.company_id,
},
});
if (existingCategory && existingCategory.id !== id) {
// If the category already exists, throw an error
throw new HttpErrors.BadRequest(`Category with name ${category.name} already exists.`);
}
await this.category_repo.updateById(id, category, options);
return category;
}
/**
* Delete Category by id or identifier
* @param id The id or identifier of Category
* @returns Promise<void>
*/
async deleteByIdOrIdentifier(id: number, options: Options = {}): Promise<Category> {
const category = await this.category_repo.findById(id, options);
await this.category_repo.deleteById(id, options);
return category;
}
/**
* Get available categories based on the current user company, creator company, participating company,
* and process template. The result set is then filtered based on the filter provided in this service.
* @param creator_company_id The company id of the creator.
* @param participating_company_ids The company ids of participating companies.
* @param process_template_id The process template id.
* @returns A promise of a BaseResponses<CategoryView> containing the available categories.
*/
async getAvailable(flow_id: number): Promise<BaseResponses<CategoryView>> {
const currentFlow = await this.flow_base_repo.findById(flow_id);
if (!currentFlow) {
throw new EntityNotFoundError(this.flow_base_repo.entityClass, flow_id);
}
let participating_company_ids: number[] = [];
const flowPersonas = await this.flow_persona_repo1.find({
where: {
flow_id: currentFlow.id,
company_id: { neq: currentFlow.company_id },
},
});
if (flowPersonas.length !== 0) {
participating_company_ids = flowPersonas.map((item) => item.company_id);
}
let category_ids = [0];
if (participating_company_ids.length !== 0 &&
(
currentFlow.company_id === this.current_user.company_id ||
participating_company_ids.includes(this.current_user.company_id)
)
) {
const query = `
SELECT * FROM get_available_categories(
$1, -- user_company_id (logged in user)
$2, -- creator_company_id
$3, -- participating_company_ids
$4 -- process_template_id
);
`;
const params = [
this.current_user.company_id,
currentFlow.company_id,
participating_company_ids,
currentFlow.trade_lane_id
];
const rsItems = await this.category_view_repo.execute(query, params);
category_ids = rsItems.map((item: CategoryView) => item.id);
}
const filter_builder = new FilterBuilder<CategoryView>();
filter_builder.filter = this.filter_builder.filter as AnyObject;
filter_builder.impose({
id: {
inq: category_ids,
},
});
return this.category_view_repo.findWithPaging(filter_builder.build());
}
/**
* Builds the query for user.
* @param q The query string.
*/
async buildSearchQuery(q?: string) {
if (!q) {
return;
}
const ilike_value = this.buildILikeValue(q);
// FILTER: name Should be a LIKE search
this.filter_builder.impose({
or: [
{
name: ilike_value,
},
{
description: ilike_value,
},
],
});
}
}
|