()
| 154 | } |
| 155 | |
| 156 | private async load(): Promise<void> { |
| 157 | const logger = useLogger(); |
| 158 | |
| 159 | const flowsService = new FlowsService({ knex: getDatabase(), schema: await getSchema() }); |
| 160 | |
| 161 | const flows = await flowsService.readByQuery({ |
| 162 | filter: { status: { _eq: 'active' } }, |
| 163 | fields: ['*', 'operations.*'], |
| 164 | limit: -1, |
| 165 | }); |
| 166 | |
| 167 | const flowTrees = flows.map((flow) => constructFlowTree(flow)); |
| 168 | |
| 169 | for (const flow of flowTrees) { |
| 170 | this.flows[flow.id] = flow; |
| 171 | |
| 172 | if (flow.trigger === 'event') { |
| 173 | let events: string[] = []; |
| 174 | |
| 175 | if (flow.options?.['scope']) { |
| 176 | events = toArray(flow.options['scope']) |
| 177 | .map((scope: string) => { |
| 178 | if (['items.create', 'items.update', 'items.delete'].includes(scope)) { |
| 179 | if (!flow.options?.['collections']) return []; |
| 180 | |
| 181 | return toArray(flow.options['collections']).map((collection: string) => { |
| 182 | if (isSystemCollection(collection)) { |
| 183 | const action = scope.split('.')[1]; |
| 184 | return collection.substring(9) + '.' + action; |
| 185 | } |
| 186 | |
| 187 | return `${collection}.${scope}`; |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | return scope; |
| 192 | }) |
| 193 | .flat(); |
| 194 | } |
| 195 | |
| 196 | if (flow.options['type'] === 'filter') { |
| 197 | const handler: FilterHandler = (payload, meta, context) => |
| 198 | this.executeFlow( |
| 199 | flow, |
| 200 | { payload, ...meta }, |
| 201 | { |
| 202 | accountability: context['accountability'], |
| 203 | database: context['database'], |
| 204 | getSchema: context['schema'] ? () => context['schema'] : getSchema, |
| 205 | }, |
| 206 | ); |
| 207 | |
| 208 | events.forEach((event) => emitter.onFilter(event, handler)); |
| 209 | |
| 210 | this.triggerHandlers.push({ |
| 211 | id: flow.id, |
| 212 | events: events.map((event) => ({ type: 'filter', name: event, handler })), |
| 213 | }); |
no test coverage detected