directus

Форк
0
/
20220429A-add-flows.ts 
88 строк · 2.8 Кб
1
import { parseJSON, toArray } from '@directus/utils';
2
import type { Knex } from 'knex';
3
import { randomUUID } from 'node:crypto';
4

5
export async function up(knex: Knex): Promise<void> {
6
	await knex.schema.createTable('directus_flows', (table) => {
7
		table.uuid('id').primary().notNullable();
8
		table.string('name').notNullable();
9
		table.string('icon', 30);
10
		table.string('color').nullable();
11
		table.text('description');
12
		table.string('status').notNullable().defaultTo('active');
13
		table.string('trigger');
14
		table.string('accountability').defaultTo('all');
15
		table.json('options');
16
		table.uuid('operation').unique();
17
		table.timestamp('date_created').defaultTo(knex.fn.now());
18
		table.uuid('user_created').references('id').inTable('directus_users').onDelete('SET NULL');
19
	});
20

21
	await knex.schema.createTable('directus_operations', (table) => {
22
		table.uuid('id').primary().notNullable();
23
		table.string('name');
24
		table.string('key').notNullable();
25
		table.string('type').notNullable();
26
		table.integer('position_x').notNullable();
27
		table.integer('position_y').notNullable();
28
		table.json('options');
29
		table.uuid('resolve').unique().references('id').inTable('directus_operations');
30
		table.uuid('reject').unique().references('id').inTable('directus_operations');
31
		table.uuid('flow').notNullable().references('id').inTable('directus_flows').onDelete('CASCADE');
32
		table.timestamp('date_created').defaultTo(knex.fn.now());
33
		table.uuid('user_created').references('id').inTable('directus_users').onDelete('SET NULL');
34
	});
35

36
	const webhooks = await knex.select('*').from('directus_webhooks');
37

38
	const flows = [];
39
	const operations = [];
40

41
	for (const webhook of webhooks) {
42
		const flowID = randomUUID();
43

44
		flows.push({
45
			id: flowID,
46
			name: webhook.name,
47
			status: 'inactive',
48
			trigger: 'hook',
49
			options: JSON.stringify({
50
				name: webhook.name,
51
				type: 'action',
52
				scope: toArray(webhook.actions).map((scope) => `items.${scope}`),
53
				collections: toArray(webhook.collections),
54
			}),
55
		});
56

57
		operations.push({
58
			id: randomUUID(),
59
			name: 'Request',
60
			key: 'request',
61
			type: 'request',
62
			position_x: 21,
63
			position_y: 1,
64
			options: JSON.stringify({
65
				url: webhook.url,
66
				headers: typeof webhook.headers === 'string' ? parseJSON(webhook.headers) : webhook.headers,
67
				data: webhook.data ? '{{$trigger}}' : null,
68
				method: webhook.method,
69
			}),
70
			date_created: new Date(),
71
			flow: flowID,
72
		});
73
	}
74

75
	if (flows.length && operations.length) {
76
		await knex.insert(flows).into('directus_flows');
77
		await knex.insert(operations).into('directus_operations');
78

79
		for (const operation of operations) {
80
			await knex('directus_flows').update({ operation: operation.id }).where({ id: operation.flow });
81
		}
82
	}
83
}
84

85
export async function down(knex: Knex): Promise<void> {
86
	await knex.schema.dropTable('directus_operations');
87
	await knex.schema.dropTable('directus_flows');
88
}
89

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.