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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | 1x 1x 1x 1x 1x 1x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x | import {Getter, inject} from '@loopback/core';
import {HasManyRepositoryFactory, HasOneRepositoryFactory, Options, repository} from '@loopback/repository';
import {FlowActionRepository, FlowPersonaRepository, FlowReferenceRepository, PersonaRepository} from '.';
import {LogChainDataSource} from '../datasources';
import {Flow, FlowAction, FlowJourneyPoint, FlowJourneyPointRelations, FlowPersona, FlowReference, Persona} from '../models';
import {BaseCrudRepository} from './base-crud.repository.base';
import { AnyObject } from '@loopback/repository/src/common-types';
export class FlowJourneyPointRepository extends BaseCrudRepository<
FlowJourneyPoint,
typeof FlowJourneyPoint.prototype.id,
FlowJourneyPointRelations
> {
public readonly persona: HasOneRepositoryFactory<Persona, typeof FlowJourneyPoint.prototype.id>;
public readonly flow_persona: HasOneRepositoryFactory<FlowPersona, typeof FlowJourneyPoint.prototype.id>;
public readonly validator_flow_persona: HasOneRepositoryFactory<FlowPersona, typeof FlowJourneyPoint.prototype.id>;
public readonly actions: HasManyRepositoryFactory<FlowAction, typeof FlowJourneyPoint.prototype.id>;
public readonly ref_documents: HasManyRepositoryFactory<FlowReference, typeof FlowJourneyPoint.prototype.id>;
constructor(
@inject('datasources.logchain') dataSource: LogChainDataSource,
@repository.getter('PersonaRepository')
protected persona_repo_getter: Getter<PersonaRepository>,
@repository.getter('FlowPersonaRepository')
protected flow_persona_repo_getter: Getter<FlowPersonaRepository>,
@repository.getter('FlowActionRepository')
protected flow_action_repo_getter: Getter<FlowActionRepository>,
@repository.getter('FlowReferenceRepository')
protected flow_reference_repo_getter: Getter<FlowReferenceRepository>,
) {
super(FlowJourneyPoint, dataSource);
this.persona = this.createHasOneRepositoryFactoryFor('persona', persona_repo_getter);
this.registerInclusionResolver('persona', this.persona.inclusionResolver);
this.flow_persona = this.createHasOneRepositoryFactoryFor('flow_persona', flow_persona_repo_getter);
this.registerInclusionResolver('flow_persona', this.flow_persona.inclusionResolver);
this.validator_flow_persona = this.createHasOneRepositoryFactoryFor('validator_flow_persona', flow_persona_repo_getter);
this.registerInclusionResolver('validator_flow_persona', this.validator_flow_persona.inclusionResolver);
this.actions = this.createHasManyRepositoryFactoryFor('actions', flow_action_repo_getter);
this.registerInclusionResolver('actions', this.actions.inclusionResolver);
this.ref_documents = this.createHasManyRepositoryFactoryFor('ref_documents', flow_reference_repo_getter);
this.registerInclusionResolver('ref_documents', this.actions.inclusionResolver);
}
/**
* Count number of persona in the flow.
* @param flow_id The default flow id.
*/
async countNumberOfPersona(flow_id: number, options?: Options): Promise<number> {
const count = await this.execute(
`SELECT
persona_id
FROM
flows_journey_point
WHERE
flow_id = $1
GROUP BY
persona_id;`,
[flow_id],
options,
);
return count.length;
}
/**
* Override deleteById to remove journey points from group after delete.
* @param id The flow persona id.
* @param options The options.
*/
async deleteById(id: typeof FlowJourneyPoint.prototype.id, options?: Options): Promise<void> {
await super.deleteById(id, options);
// remove journey points from group.
if (options?.flow_id) {
const sql = `
UPDATE flows_group
SET journey_points = array_remove ( journey_points, $2)
WHERE
flow_id = $1
AND $2 = ANY ( journey_points );`;
await this.execute(sql, [options?.flow_id, id], options);
}
}
/**
* Retrieves the expected dates by step name for a given flow ID.
*
* @param {number} flow_id - The ID of the flow.
* @return {Promise<object|null>} The expected dates by step name, or null if not found.
*/
async getExpectedDatesByStepName(flow_id: number, updated_step_ids: number[]): Promise<object | null> {
const sql = `
SELECT
fjp.name,
jsonb_agg( DISTINCT
jsonb_build_object(
'container_no', CASE
WHEN fjp.persona_id <> 0 AND ("position"((fp.needed_info ->> 'container_no'::text), '['::text) > 0) THEN (((fp.needed_info ->> 'container_no'::text))::jsonb ->> (fjp.container_order - 1))
ELSE ''
END,
'expected_date', fjp.expected_date
)
) as expected_dates
FROM flows_journey_point fjp
JOIN flows_persona fp ON fp.flow_id = fjp.flow_id AND fp.needed_info ->> 'container_no' IS NOT NULL
WHERE fjp.flow_id = $1 AND fjp.validator_is_required IS NOT NULL
AND (SELECT COUNT(id) FROM flows_action WHERE flows_action.status = 0 AND flow_journey_point_id = fjp.id) > 0
AND fjp.id = ANY('{${updated_step_ids.join(',')}}')
GROUP BY fjp.name, fjp."order"
ORDER BY fjp."order";`;
const res = await this.execute(sql, [flow_id]);
return res ? res : null;
}
/**
* Retrieves the expected dates by action name for a given flow id.
*
* @param {number} flow_id - The ID of the flow.
* @return {Promise<object|null>} The expected dates by step name, or null if not found.
*/
async getExpectedDatesByActionName(flow_id: number, updated_step_ids: number[]): Promise<object | null> {
const sql = `
SELECT
fjp.name,
jsonb_agg(
jsonb_build_object(
'container_no', CASE
WHEN fjp.persona_id <> 0 AND ("position"((fp.needed_info ->> 'container_no'::text), '['::text) > 0) THEN (((fp.needed_info ->> 'container_no'::text))::jsonb ->> (fjp.container_order - 1))
ELSE ''
END,
'action_name', COALESCE(fa."title", f."name"),
'expected_date', fjp.expected_date,
'timezone', c.timezone
)
ORDER BY fa.id) as expected_dates
FROM flows_journey_point fjp
JOIN flows_action fa ON fa.flow_journey_point_id = fjp.id AND fa.status = 0
JOIN companies c ON c.id = fa.company_id
LEFT JOIN forms f ON f.id = fa.form_id
JOIN flows_persona fp ON fp.flow_id = fjp.flow_id AND fp.needed_info ->> 'container_no' IS NOT NULL
WHERE fjp.flow_id = $1 AND fjp.validator_is_required IS NOT NULL
AND fjp.id = ANY('{${updated_step_ids.join(',')}}')
GROUP BY fjp.name, fjp."order"
ORDER BY fjp."order";`;
const res = await this.execute(sql, [flow_id]);
return res ? res : null;
}
/**
* Delete by flow ID and container number.
*
* @param {number} flow_id - the flow ID
* @param {number} container_order - the container order
* @param {Options} [options] - optional parameters
* @return {Promise<void>} a promise that resolves with no value
*/
async deleteByFlowIdAndContainerNumber(flow_id: number, container_order: number, options?: Options): Promise<void> {
await this.deleteAll({
flow_id,
container_order: container_order,
}, options);
let sql = `
UPDATE "public"."flows_persona"
SET needed_info =
CASE
WHEN needed_info->>'container_no' IS NOT NULL
THEN jsonb_set(needed_info, '{container_no}', (
SELECT jsonb_agg(container_no::text) AS containers
FROM flows_persona,
jsonb_array_elements_text(needed_info->'container_no') AS container_no
WHERE flow_id = $1
AND container_no <> (
SELECT (needed_info->'container_no')->>($2 - 1)
FROM flows_persona
WHERE flow_id = $1
AND needed_info->>'container_no' IS NOT NULL
)
AND needed_info->'container_no' IS NOT NULL
))
ELSE needed_info
END
WHERE flow_id = $1
AND needed_info->>'container_no' IS NOT NULL;`
await this.execute(sql, [flow_id, container_order]);
sql = `UPDATE flows_action
SET container_order = (container_order - 1)
WHERE flow_id = $1
AND container_order > $2;`
await this.execute(sql, [flow_id, container_order]);
sql = `UPDATE flows_journey_point
SET container_order = (container_order - 1)
WHERE flow_id = $1
AND container_order > $2;`
await this.execute(sql, [flow_id, container_order]);
sql = `UPDATE flows
SET no_of_container = (no_of_container - 1)
WHERE id = $1
AND no_of_container > 0;`
await this.execute(sql, [flow_id]);
}
/**
* Updates the expected date of a flow journey point by container number.
*
* @param {number} flow_id - The ID of the flow.
* @param {number} container_number - The container number.
* @param {AnyObject} form_data - The form data object containing the expected date.
* @param {Options} [options] - Optional parameters for the execution.
* @return {Promise<void>} A promise that resolves when the update is complete.
*/
async updateExpectedDateByContainerNumber(flow_id: number, container_number: number, form_data: AnyObject, options?: Options): Promise<void>
{
const sql = `
UPDATE flows_journey_point
SET expected_date = $3
WHERE flow_id = $1
AND container_order = $2;`;
await this.execute(sql, [flow_id, container_number, form_data.expected_date], options);
}
/**
* Retrieves the steps in a container for a given flow and container number.
*
* @param {number} flow_id - The ID of the flow.
* @param {number} container_number - The number of the container.
* @return {Promise<object | null>} The steps in the container, or null if not found.
*/
async getStepsInContainer(flow_id: number, container_number: number): Promise<object | null> {
const sql = `
SELECT id, "order", expected_date, container_order
FROM flows_journey_point
WHERE flow_id = $1
AND "order" <> 0
AND (container_order = $2 OR container_order IS NULL)
AND validator_is_required IS NOT NULL
ORDER BY "order" ASC;`;
const res = await this.execute(sql, [flow_id, container_number]);
return res ? res : [];
}
/**
* Update containers's status.
* @param flow_id The flow id.
* @param container_info container info neeed to be updated.
* @param options The options.
*/
async updateContainerStatus(flow_id: typeof Flow.prototype.id, container_status: AnyObject, options?: Options): Promise<void> {
if (flow_id && !isNaN(flow_id)) {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
Object.keys(container_status).forEach(async (index) => {
const value = container_status[index];
const sql = `
UPDATE flows_journey_point
SET container_info = jsonb_set(container_info, '{${index},"container_status"}', $2, TRUE)
WHERE flow_id = $1 AND container_info IS NOT NULL AND container_info->(${index})->'container_status' NOT IN ('2','3');`;
await this.execute(sql, [flow_id, value], options);
});
}
}
/**
* Retrieves the company id in a step.
*
* @param {number} step_id - The ID of the step.
* @return {Promise<object | null>} The steps in the container, or null if not found.
*/
async getCompanyIdsInStep(step_id: number): Promise<number[]> {
const sql = `
SELECT DISTINCT array_agg(fp.company_id) AS companies
FROM flows_group fg
JOIN flows_persona fp ON fp.id = ANY(fg.flow_personas)
WHERE $1 = ANY(fg.journey_points);
`;
const res = await this.execute(sql, [step_id]);
return res.length ? res[0].companies : [];
}
}
|