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 | 1x 1x 1x 1x 1x 1x 108x 108x 108x 108x 108x 108x 108x | import {Getter, inject} from '@loopback/core';
import {AnyObject, HasOneRepositoryFactory, Options, repository} from '@loopback/repository';
import {CompanyRepository, PersonaRepository} from '.';
import {LogChainDataSource} from '../datasources';
import {Company, FlowPersona, FlowPersonaRelations, Persona} from '../models';
import {BaseCrudRepository} from './base-crud.repository.base';
export class FlowPersonaRepository extends BaseCrudRepository<
FlowPersona,
typeof FlowPersona.prototype.id,
FlowPersonaRelations
> {
public readonly persona: HasOneRepositoryFactory<Persona, typeof FlowPersona.prototype.id>;
public readonly company: HasOneRepositoryFactory<Company, typeof FlowPersona.prototype.id>;
constructor(
@inject('datasources.logchain') dataSource: LogChainDataSource,
@repository.getter('PersonaRepository')
protected persona_repo_getter: Getter<PersonaRepository>,
@repository.getter('CompanyRepository')
protected company_repo_getter: Getter<CompanyRepository>,
) {
super(FlowPersona, dataSource);
this.persona = this.createHasOneRepositoryFactoryFor('persona', persona_repo_getter);
this.registerInclusionResolver('persona', this.persona.inclusionResolver);
this.company = this.createHasOneRepositoryFactoryFor('company', company_repo_getter);
this.registerInclusionResolver('company', this.company.inclusionResolver);
}
/**
* Grouping persona by flow id.
* @param flow_id The flow id.
*/
async groupingPersonaByFlowId(flow_id: number) {
const flow_persona = await this.execute(
`SELECT
FORMAT (
'%s (%s)',
per.title,
COUNT ( fdp.persona_id )
) title,
fdp.persona_id,
CAST (COUNT ( fdp.persona_id ) as INTEGER)
FROM
flows_persona AS fdp
INNER JOIN personas AS per ON fdp.persona_id = per."id"
WHERE
fdp.flow_id = $1
GROUP BY
fdp.persona_id,
per.title;`,
[flow_id],
);
return flow_persona as {title: string; persona_id: number; count: number}[];
}
/**
* Override deleteById to remove persona from group after delete.
* @param id The flow persona id.
* @param options The options.
*/
async deleteById(id: typeof FlowPersona.prototype.id, options?: Options): Promise<void> {
await super.deleteById(id, options);
// remove personas from group.
if (options?.flow_id) {
const sql = `
UPDATE flows_group
SET flow_personas = array_remove ( flow_personas, $2)
WHERE
flow_id = $1
AND $2 = ANY ( flow_personas );`;
await this.execute(sql, [options?.flow_id, id], options);
}
}
/**
* Update NeedInfo field.
* @param id The flow persona id.
* @param data The jsonb object to be updated.
* @param options The options.
*/
async updateNeedInfo(id: typeof FlowPersona.prototype.id, needed_info: AnyObject, options?: Options): Promise<void> {
if (id && !isNaN(id)) {
const sql = `
UPDATE flows_persona
SET needed_info = (needed_info || $2)
WHERE id = $1;`;
await this.execute(sql, [id, JSON.stringify(needed_info)], options);
const sql1 = `
UPDATE flows_persona
SET needed_info = '{}'
WHERE needed_info IS NULL;`;
await this.execute(sql1, [], options);
}
}
/**
* Append container no to array container_no of needed_info.
* @param id The flow persona id.
* @param added_container_no The container which be appended.
* @param options The options.
*/
async appendContainer(id: typeof FlowPersona.prototype.id, added_container_no: string, options?: Options): Promise<void> {
if (id && !isNaN(id)) {
const sql = `
UPDATE flows_persona
SET needed_info = jsonb_set(needed_info, '{container_no}', (needed_info->'container_no') || '[${JSON.stringify(added_container_no)}]'::jsonb)
WHERE id = $1;`;
await this.execute(sql, [id], options);
}
}
/**
* Get all containers by flow ID.
*
* @param {number} flow_id - the ID of the flow
* @param {Options} [options] - optional parameters
* @return {Promise<AnyObject>} the list of remaining containers
*/
async getAllContainersByFlowId(flow_id: number, options?: Options): Promise<AnyObject>
{
const resl = await this.execute(`
SELECT fp.needed_info ->> 'container_no' AS containers
FROM flows_persona fp
WHERE fp.flow_id = $1
AND fp.needed_info ->> 'container_no'::text IS NOT NULL;`, [flow_id], options);
return resl.length ? JSON.parse(resl[0].containers) : [];
}
/**
* Get remaining containers by flow ID (LIVE).
*
* @param {number} flow_id - the ID of the flow
* @param {Options} [options] - optional parameters
* @return {Promise<AnyObject>} the list of remaining containers
*/
async getRemainingContainersByFlowIdForLive(flow_id: number, options?: Options): Promise<AnyObject>
{
// We need to exclude cancelled containers out of this list
const resl = await this.execute(`
SELECT
array_agg( DISTINCT container_no) AS containers
FROM actions_data_view
WHERE flow_id = $1 AND status_name <> 'Cancelled' AND container_no <> ''
GROUP BY flow_id;`, [flow_id], options);
return resl.length ? resl[0].containers : [];
}
/**
* Get remaining containers by flow ID (LIVE).
*
* @param {number} flow_id - the ID of the flow
* @param {number} container_no - the container_no of the flow
* @param {Options} [options] - optional parameters
* @return {Promise<string>} the list of remaining containers
*/
async getFlowIdsWhichHaveSameContainerNo(flow_id: number, container_no: string, options?: Options): Promise<AnyObject>
{
const resl = await this.execute(`
SELECT
string_agg(concat(fp.flow_id::text, '-', flows.status::text), ',') AS matched_flows,
CASE
WHEN COUNT(CASE WHEN flows.status = 3 THEN 1 ELSE NULL END) = COUNT(flows.id) THEN 1
WHEN COUNT(CASE WHEN flows.status = 6 THEN 1 ELSE NULL END) = COUNT(flows.id) THEN 2
ELSE 3
END AS status_all
FROM flows_persona fp
JOIN flows ON flows.id = fp.flow_id
WHERE jsonb_exists(fp.needed_info->'container_no', $2::text) AND fp.flow_id != $1 AND flows.live_status NOT IN (2,3);`, [flow_id, container_no], options);
return resl.length ? { matched_flows: resl[0].matched_flows, status_all: resl[0].status_all } : {};
}
/**
* Executes a trigger to update the container number in the form data for a given flow ID.
*
* @param {number} flow_id - The ID of the flow.
* @param {Options} [options] - Optional parameters for the database query.
* @return {Promise<void>} A promise that resolves when the trigger is executed successfully.
*/
async fireTriggerUpdateContainerNoForFormData(flow_id: number, options?: Options): Promise<void>
{
await this.execute(`UPDATE flows_persona
SET needed_info = needed_info
WHERE flow_id = $1
AND needed_info->>'container_no' IS NOT NULL;`, [flow_id], options);
}
}
|