@tanglemedia/directus-core-settings 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,59 @@
1
+ export async function up(knex) {
2
+ await knex("directus_settings").update({
3
+ module_bar: JSON.stringify([
4
+ {
5
+ id: "content",
6
+ type: "module",
7
+ enabled: true,
8
+ },
9
+ {
10
+ id: "visual",
11
+ type: "module",
12
+ enabled: false,
13
+ },
14
+ {
15
+ id: "users",
16
+ type: "module",
17
+ enabled: true,
18
+ },
19
+ {
20
+ id: "files",
21
+ type: "module",
22
+ enabled: true,
23
+ },
24
+ {
25
+ id: "insights",
26
+ type: "module",
27
+ enabled: true,
28
+ },
29
+ {
30
+ id: "Main Website",
31
+ url: process.env.SITE_BASE_URL || "",
32
+ icon: "public",
33
+ name: "Main Website",
34
+ type: "link",
35
+ enabled: true,
36
+ },
37
+ {
38
+ id: "Publish Site",
39
+ url: null,
40
+ icon: "send",
41
+ name: "Publish Site",
42
+ type: "link",
43
+ enabled: true,
44
+ },
45
+ {
46
+ id: "settings",
47
+ type: "module",
48
+ locked: true,
49
+ enabled: true,
50
+ },
51
+ ]),
52
+ });
53
+ }
54
+
55
+ export async function down(knex) {
56
+ await knex("directus_settings").update({
57
+ module_bar: null,
58
+ });
59
+ }
@@ -0,0 +1,100 @@
1
+ import { generateUUIDv4 } from "./helpers/generator.mjs";
2
+
3
+ const OPERATION_KEY = "run_site_build";
4
+
5
+ export async function up(knex) {
6
+ const flowId = generateUUIDv4();
7
+ const operationId = generateUUIDv4();
8
+
9
+ // add directus flow
10
+ await knex("directus_flows").insert({
11
+ id: flowId,
12
+ name: "Publish Site",
13
+ icon: "bolt",
14
+ color: null,
15
+ description: "Triggers site build",
16
+ status: "active",
17
+ trigger: "webhook",
18
+ accountability: "all",
19
+ options: JSON.stringify({ async: true }),
20
+ operation: operationId,
21
+ });
22
+
23
+ // add directus operation
24
+ await knex("directus_operations").insert({
25
+ id: operationId,
26
+ name: "Run Site Build",
27
+ key: OPERATION_KEY,
28
+ type: "request",
29
+ position_x: 19,
30
+ position_y: 1,
31
+ options: JSON.stringify({
32
+ url: process.env.PUBLISH_WEBHOOK_URL,
33
+ method: "POST",
34
+ body: {},
35
+ }),
36
+ resolve: null,
37
+ reject: null,
38
+ flow: flowId,
39
+ });
40
+
41
+ // add module bar button to trigger the flow
42
+ const flowButton = `${process.env.PUBLIC_URL}/flows/trigger/${flowId}`;
43
+ const directusSettings = await knex("directus_settings").first();
44
+ if (directusSettings?.id) {
45
+ const existingModuleBar = directusSettings.module_bar;
46
+
47
+ // look for existing 'Publish Site' button and update the url in the existingModuleBar
48
+ existingModuleBar.forEach((button) => {
49
+ if (button.id === "Publish Site") {
50
+ button.url = flowButton;
51
+ }
52
+ });
53
+
54
+ // Replace the module bar in the directus settings with the updated one
55
+ await knex("directus_settings")
56
+ .where({ id: directusSettings.id })
57
+ .update({ module_bar: JSON.stringify(existingModuleBar) });
58
+ }
59
+ }
60
+
61
+ export async function down(knex) {
62
+ const existingOperation = await knex("directus_operations")
63
+ .where({ key: OPERATION_KEY })
64
+ .first();
65
+
66
+ if (!existingOperation) return;
67
+
68
+ await knex("directus_flows")
69
+ .where({ id: existingOperation.flow })
70
+ .update({ operation: null });
71
+ await knex("directus_operations").where({ id: existingOperation.id }).del();
72
+ await knex("directus_flows")
73
+ .where({
74
+ id: existingOperation.flow,
75
+ name: "Publish Site",
76
+ trigger: "webhook",
77
+ })
78
+ .del();
79
+
80
+ // remove module bar button
81
+ const directusSettings = await knex("directus_settings").first();
82
+ if (directusSettings?.id) {
83
+ const existingModuleBar = directusSettings.module_bar;
84
+
85
+ // look for existing 'Publish Site' button and remove it from the module bar
86
+ existingModuleBar.forEach((button, index) => {
87
+ if (button.id === "Publish Site") {
88
+ button.url = null;
89
+ }
90
+ });
91
+
92
+ // Replace the module bar in the directus settings with the updated one
93
+ await knex("directus_settings")
94
+ .where({ id: directusSettings.id })
95
+ .update({ module_bar: JSON.stringify(existingModuleBar) });
96
+ }
97
+ }
98
+
99
+
100
+ // http://localhost:8055/flows/trigger/6f8f5b3d-bf36-474f-8920-7f549e3b5450
@@ -0,0 +1,37 @@
1
+ import { updateOneAllowedCollections } from "./relations.mjs";
2
+
3
+ export async function addCollectionToMenu(knex, collectionName) {
4
+ const menuItemCollectionExists = await knex("directus_relations")
5
+ .where({
6
+ many_collection: "tngl_menu_item_collection",
7
+ many_field: "item",
8
+ })
9
+ .first();
10
+
11
+ if (menuItemCollectionExists?.id) {
12
+ await updateOneAllowedCollections(
13
+ knex,
14
+ menuItemCollectionExists.id,
15
+ collectionName,
16
+ "add",
17
+ );
18
+ }
19
+ }
20
+
21
+ export async function removeCollectionFromMenu(knex, collectionName) {
22
+ const menuItemCollectionExists = await knex("directus_relations")
23
+ .where({
24
+ many_collection: "tngl_menu_item_collection",
25
+ many_field: "item",
26
+ })
27
+ .first();
28
+
29
+ if (menuItemCollectionExists?.id) {
30
+ await updateOneAllowedCollections(
31
+ knex,
32
+ menuItemCollectionExists.id,
33
+ collectionName,
34
+ "remove",
35
+ );
36
+ }
37
+ }
@@ -0,0 +1,46 @@
1
+ export async function updateOneAllowedCollections(
2
+ knex,
3
+ directusRelationID,
4
+ collectionName,
5
+ action,
6
+ ) {
7
+ const relation = await knex("directus_relations")
8
+ .where({ id: directusRelationID })
9
+ .first();
10
+ const relatedCollections = relation?.one_allowed_collections;
11
+ let finalRelatedCollection = relatedCollections.split(",");
12
+
13
+ switch (action) {
14
+ case "add":
15
+ {
16
+ if (finalRelatedCollection.length > 0) {
17
+ finalRelatedCollection.push(collectionName);
18
+ } else {
19
+ finalRelatedCollection = [collectionName];
20
+ }
21
+ }
22
+ break;
23
+
24
+ case "remove":
25
+ {
26
+ if (finalRelatedCollection.length > 0) {
27
+ finalRelatedCollection = finalRelatedCollection.filter(
28
+ (item) => item !== collectionName,
29
+ );
30
+ } else {
31
+ finalRelatedCollection = [];
32
+ }
33
+ }
34
+ break;
35
+ }
36
+
37
+ finalRelatedCollection = [...new Set(finalRelatedCollection)].filter((item) =>
38
+ item.trim(),
39
+ );
40
+
41
+ await knex("directus_relations")
42
+ .where({ id: directusRelationID })
43
+ .update({
44
+ one_allowed_collections: finalRelatedCollection.join(","),
45
+ });
46
+ }
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  },
7
7
  "license": "MIT",
8
8
  "icon": "extension",
9
- "version": "0.0.4",
9
+ "version": "0.0.6",
10
10
  "keywords": [
11
11
  "directus",
12
12
  "directus-extension",