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 | 1x 1x 1x 1x 1x 1x 1x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x | import {Getter, inject} from '@loopback/core';
import {BelongsToAccessor, HasManyRepositoryFactory, repository} from '@loopback/repository';
import {
CompanyRepository,
FlowDefaultGroupRepository,
FlowDefaultJourneyPointRepository,
FlowDefaultPersonaRepository,
TradeLaneRepository,
UserRepository,
} from '.';
import {LogChainDataSource} from '../datasources';
import {
Company,
FlowDefault,
FlowDefaultGroup,
FlowDefaultJourneyPoint,
FlowDefaultPersona,
FlowDefaultRelations,
FlowJourneyPoint,
TradeLane,
User,
} from '../models';
import {BaseCrudRepository} from './base-crud.repository.base';
import {DateUtils} from '@logchain/utils';
export class FlowDefaultRepository extends BaseCrudRepository<
FlowDefault,
typeof FlowDefault.prototype.id,
FlowDefaultRelations
> {
public readonly trade_lane: BelongsToAccessor<TradeLane, typeof FlowDefault.prototype.id>;
public readonly company: BelongsToAccessor<Company, typeof FlowDefault.prototype.id>;
public readonly groups: HasManyRepositoryFactory<FlowDefaultGroup, typeof FlowDefault.prototype.id>;
public readonly journey_points: HasManyRepositoryFactory<FlowDefaultJourneyPoint, typeof FlowDefault.prototype.id>;
public readonly personas: HasManyRepositoryFactory<FlowDefaultPersona, typeof FlowDefault.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('TradeLaneRepository')
protected TradeLaneRepositoryGetter: Getter<TradeLaneRepository>,
@repository.getter('UserRepository')
protected userRepositoryGetter: Getter<UserRepository>,
@repository.getter('CompanyRepository')
protected companyRepositoryGetter: Getter<CompanyRepository>,
@repository.getter('FlowDefaultGroupRepository')
protected flowDefaultGroupRepositoryGetter: Getter<FlowDefaultGroupRepository>,
@repository.getter('FlowDefaultPersonaRepository')
protected flowDefaultPersonaRepositoryGetter: Getter<FlowDefaultPersonaRepository>,
@repository.getter('FlowDefaultJourneyPointRepository')
protected flowDefaultJourneyPointRepositoryGetter: Getter<FlowDefaultJourneyPointRepository>,
) {
super(FlowDefault, dataSource);
this.creator = this.createBelongsToAccessor('creator', userRepositoryGetter);
this.company = this.createBelongsToAccessor('company', companyRepositoryGetter);
this.modifier = this.createBelongsToAccessor('modifier', userRepositoryGetter);
this.trade_lane = this.createBelongsToAccessor('trade_lane', TradeLaneRepositoryGetter);
this.groups = this.createHasManyRepositoryFactoryFor('groups', flowDefaultGroupRepositoryGetter);
this.registerInclusionResolver('groups', this.groups.inclusionResolver);
this.journey_points = this.createHasManyRepositoryFactoryFor(
'journey_points',
flowDefaultJourneyPointRepositoryGetter,
);
this.registerInclusionResolver('journey_points', this.journey_points.inclusionResolver);
this.personas = this.createHasManyRepositoryFactoryFor('personas', flowDefaultPersonaRepositoryGetter);
this.registerInclusionResolver('personas', this.personas.inclusionResolver);
}
/**
* Validate expected_date.
* @param flow_id.
*/
async validateExpectedDate(flow_id: number, step: FlowJourneyPoint, expected_date: string | Date,): Promise<boolean> {
if (typeof(expected_date) !== 'string') {
expected_date = expected_date.toISOString();
}
const result = await this.execute(
`SELECT expected_date FROM flows_journey_point WHERE flow_id = $1 AND id <> $2 AND "order" <> 0 AND "order" < $3 AND container_order = $4 ORDER BY "expected_date" DESC LIMIT 1;`,
[flow_id, step.id, step.order, step.container_order],
);
return result?.length && result[0].expected_date ? DateUtils.isSameOrAfter(expected_date, result[0].expected_date) : true;
}
/**
* Validate trucking must have on access list of step.
* @param flow_id.
*/
async mustHaveTruckingOnAccess(step_id: number): Promise<boolean> {
if (step_id) {
const result = await this.execute(
`SELECT must_have_trucking_on_access(flows_journey_point) AS valid FROM flows_journey_point WHERE id = $1;`,
[step_id],
);
return result?.length && result[0].valid;
}
return false;
}
/**
* Validate companies of flow must have on access list of step.
* @param flow_id.
*/
async allCompaniesMustHaveAccessToStep(flow_id: number): Promise<boolean> {
if (flow_id) {
const result = await this.execute(
`SELECT
COUNT(DISTINCT flow_persona_id_from_group) = COUNT(DISTINCT fp1.id) AS valid
FROM
flows_persona fp1
INNER JOIN
(
SELECT DISTINCT
fg.flow_id,
UNNEST(flow_personas) flow_persona_id_from_group
FROM flows_group fg
WHERE fg.flow_id = $1 AND fg.order <> 0
) AS fp2 ON fp2.flow_id = fp1.flow_id
WHERE fp2.flow_id = $1
GROUP BY fp2.flow_id;`,
[flow_id],
);
return result?.length && result[0].valid;
}
return false;
}
/**
* Removes duplicated steps for a specific flow with no location.
*
* @param {number} flow_id - The ID of the flow.
* @return {Promise<boolean>} A promise that resolves to true if the removal was successful, otherwise false.
*/
async removeDuplicatedForNoLocationSteps(flow_id: number): Promise<boolean> {
try {
await this.execute(
`DELETE FROM flows_journey_point WHERE flow_id = $1 AND "order" <> 0 AND persona_id = 0 AND container_order <> 1;`,
[flow_id],
);
await this.execute(
`UPDATE flows_journey_point SET container_order = NULL WHERE flow_id = $1 AND "order" <> 0 AND persona_id = 0 AND container_order = 1;`,
[flow_id],
);
return true;
} catch (error) {
console.error(error);
return false;
}
}
}
|