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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 112x 112x 112x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 12x 12x 12x 1x 1x 1x 1x 1x 1x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x | import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, type FindOptionsWhere } from 'typeorm';
import { formulaBuilderToAmaliaFormula } from '@amalia/amalia-lang/formula/converter/shared';
import {
Filter,
HighlightedKpi,
Plan,
PlanAssignment,
Rule,
RuleAssignment,
Variable,
VariableValue,
type CustomObjectDefinition,
type Team,
type User,
} from '@amalia/core/models';
import { type Company } from '@amalia/tenants/companies/types';
import { makeSampleFilters } from '../../../assets/sampleData/filters';
import { makeSampleHighlightedKpis } from '../../../assets/sampleData/highlightedKpis';
import { makeSamplePlanAssignments } from '../../../assets/sampleData/planAssignments';
import { samplePlans } from '../../../assets/sampleData/plans';
import { makeSampleRuleAssignments } from '../../../assets/sampleData/ruleAssignments';
import { makeSampleRules } from '../../../assets/sampleData/rules';
import { makeSampleVariables } from '../../../assets/sampleData/variables';
import { makeSampleVariableValues } from '../../../assets/sampleData/variableValues';
@Injectable()
export class SampleDataDesignerService {
public constructor(
@InjectRepository(Filter)
private readonly filterRepository: Repository<Filter>,
@InjectRepository(HighlightedKpi)
private readonly highlightedKpiRepository: Repository<HighlightedKpi>,
@InjectRepository(Plan)
private readonly planRepository: Repository<Plan>,
@InjectRepository(PlanAssignment)
private readonly planAssignmentRepository: Repository<PlanAssignment>,
@InjectRepository(Rule)
private readonly ruleRepository: Repository<Rule>,
@InjectRepository(RuleAssignment)
private readonly ruleAssignmentRepository: Repository<RuleAssignment>,
@InjectRepository(Variable)
private readonly variableRepository: Repository<Variable>,
@InjectRepository(VariableValue)
private readonly variableValueRepository: Repository<VariableValue>,
) {}
/**
* Loads sample data for a given company.
* @param company company to create
* @param customObjectDefinitions
* @param users
* @param teams
* @param emailPrefix
*/
public async load(
company: Company,
customObjectDefinitions: CustomObjectDefinition[],
users: User[],
teams: Team[],
emailPrefix: string,
): Promise<void> {
const variables = await this.loadVariables(company, customObjectDefinitions);
const filters = await this.loadFilters(company, customObjectDefinitions);
const rules = await this.loadRules(company, filters, variables);
await this.loadPlans(company, variables, rules, filters, users, teams, emailPrefix);
await this.loadVariableValues(company, variables, users, emailPrefix);
}
public async purge(company: Company): Promise<void> {
const companyConditions: FindOptionsWhere<User> = {
company: { id: company.id },
};
await this.planAssignmentRepository.delete(companyConditions);
await this.ruleAssignmentRepository.delete(companyConditions);
await this.highlightedKpiRepository.delete(companyConditions);
await this.ruleRepository.delete(companyConditions);
await this.planRepository.delete(companyConditions);
await this.filterRepository.delete(companyConditions);
await this.variableValueRepository.delete(companyConditions);
await this.variableRepository.delete(companyConditions);
}
/**
* Load variables for company.
* @param company
* @param customObjectDefinitions
* @private
*/
private async loadVariables(
company: Company,
customObjectDefinitions: CustomObjectDefinition[],
): Promise<Variable[]> {
return Promise.all(
makeSampleVariables(customObjectDefinitions).map((variable) =>
this.variableRepository.save({
...variable,
company,
}),
),
);
}
/**
* Load variables values for company.
* @param company
* @param variables
* @param users
* @param emailPrefix
* @private
*/
private async loadVariableValues(
company: Company,
variables: Variable[],
users: User[],
emailPrefix: string,
): Promise<VariableValue[]> {
return Promise.all(
makeSampleVariableValues(variables, users, emailPrefix).map((variableValue) =>
this.variableValueRepository.save({
...variableValue,
company,
}),
),
);
}
/**
* Load filters for company.
* @param company
* @param customObjectDefinitions
* @private
*/
private async loadFilters(company: Company, customObjectDefinitions: CustomObjectDefinition[]): Promise<Filter[]> {
return Promise.all(
makeSampleFilters(customObjectDefinitions).map((filter) =>
this.filterRepository.save({
...filter,
condition: formulaBuilderToAmaliaFormula(filter.formulaBuilder!),
company,
}),
),
);
}
/**
* Load rules for company.
* @param company
* @param filters
* @param variables
* @private
*/
private async loadRules(company: Company, filters: Filter[], variables: Variable[]): Promise<Rule[]> {
return Promise.all(
makeSampleRules(filters, variables).map((rule) =>
this.ruleRepository.save({
...rule,
company,
}),
),
);
}
/**
* Load plans for company.
* @param company
* @param variables
* @param rules
* @param filters
* @param users
* @param teams
* @param emailPrefix
* @private
*/
private async loadPlans(
company: Company,
variables: Variable[],
rules: Rule[],
filters: Filter[],
users: User[],
teams: Team[],
emailPrefix: string,
): Promise<Plan[]> {
const plans: Plan[] = await Promise.all(
samplePlans.map((plan) =>
this.planRepository.save({
...plan,
company,
}),
),
);
await Promise.all(
makeSampleHighlightedKpis(variables, plans).map((highlightedKpi) =>
this.highlightedKpiRepository.save({
...highlightedKpi,
company,
}),
),
);
await Promise.all(
makeSampleRuleAssignments(plans, rules, variables, filters).map((ruleAssignment) =>
this.ruleAssignmentRepository.save({
...ruleAssignment,
company,
}),
),
);
await Promise.all(
makeSamplePlanAssignments(plans, teams, users, emailPrefix).map((planAssignment) =>
this.planAssignmentRepository.save({
...planAssignment,
company,
}),
),
);
return plans;
}
}
|