-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtemplateImport.ts
More file actions
175 lines (156 loc) · 4.75 KB
/
Copy pathtemplateImport.ts
File metadata and controls
175 lines (156 loc) · 4.75 KB
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
/* eslint-disable @typescript-eslint/naming-convention -- Supabase query results use snake_case column names */
import type { Json } from "@repo/database/dbTypes";
import { getAvailableGroupIds } from "@repo/database/lib/groups";
import type DiscourseGraphPlugin from "~/index";
import {
fetchUserNames,
getSpaceNameFromIds,
getSpaceUris,
} from "./importNodes";
import { getLoggedInClient, getSupabaseContext } from "./supabaseContext";
import { getUserNameById } from "./typeUtils";
export type TemplateImportCandidate = {
id: number;
sourceNodeTypeId: string;
nodeTypeName: string;
templateName: string;
templateContent: string;
authorId?: number;
authorName?: string;
spaceId: number;
spaceName: string;
spaceUri?: string;
lastModified?: number;
};
const parseLiteralContent = (literalContent: Json): Record<string, unknown> => {
if (typeof literalContent === "string") {
try {
return JSON.parse(literalContent) as Record<string, unknown>;
} catch (error) {
console.error("Failed to parse schema literal_content:", error);
return {};
}
}
if (
literalContent &&
typeof literalContent === "object" &&
!Array.isArray(literalContent)
) {
return literalContent as Record<string, unknown>;
}
return {};
};
const getTemplateFields = (
literalContent: Json,
): { templateName?: string; templateContent?: string } => {
const content = parseLiteralContent(literalContent);
const templateName = content.template;
const templateContent = content.template_content;
return {
templateName: typeof templateName === "string" ? templateName : undefined,
templateContent:
typeof templateContent === "string" ? templateContent : undefined,
};
};
export const fetchTemplateImportCandidates = async ({
plugin,
nodeTypeName,
}: {
plugin: DiscourseGraphPlugin;
nodeTypeName: string;
}): Promise<TemplateImportCandidate[]> => {
const trimmedNodeTypeName = nodeTypeName.trim();
if (!trimmedNodeTypeName) return [];
const client = await getLoggedInClient(plugin);
if (!client) {
throw new Error("Cannot get Supabase client");
}
const context = await getSupabaseContext(plugin);
if (!context) {
throw new Error("Cannot get Supabase context");
}
const groupIds = await getAvailableGroupIds(client);
if (groupIds.length === 0) return [];
await fetchUserNames(plugin, client);
const { data, error } = await client
.from("my_concepts")
.select(
"id, source_local_id, name, literal_content, author_id, space_id, last_modified",
)
.eq("is_schema", true)
.eq("arity", 0)
.eq("name", trimmedNodeTypeName)
.neq("space_id", context.spaceId);
if (error) {
console.error("Error fetching shared template candidates:", error);
throw new Error(`Failed to fetch shared templates: ${error.message}`);
}
const rows = (data ?? []) as Array<{
id: number;
source_local_id: string | null;
name: string | null;
literal_content: Json;
author_id: number | null;
space_id: number | null;
last_modified: string | null;
}>;
const rowsWithTemplates = rows
.map((row) => {
const { templateName, templateContent } = getTemplateFields(
row.literal_content,
);
if (
!row.source_local_id ||
!row.name ||
row.space_id === null ||
!templateName ||
templateContent === undefined
) {
return null;
}
return {
row,
templateName,
templateContent,
};
})
.filter(
(
candidate,
): candidate is {
row: (typeof rows)[number];
templateName: string;
templateContent: string;
} => candidate !== null,
);
const spaceIds = [
...new Set(rowsWithTemplates.map(({ row }) => row.space_id!)),
];
const [spaceNames, spaceUris] = await Promise.all([
getSpaceNameFromIds(client, spaceIds),
getSpaceUris(client, spaceIds),
]);
return rowsWithTemplates
.map(({ row, templateName, templateContent }) => {
const spaceId = row.space_id!;
return {
id: row.id,
sourceNodeTypeId: row.source_local_id!,
nodeTypeName: row.name!,
templateName,
templateContent,
authorId: row.author_id ?? undefined,
authorName: row.author_id
? getUserNameById(plugin, row.author_id)
: undefined,
spaceId,
spaceName: spaceNames.get(spaceId) ?? `Space ${spaceId}`,
spaceUri: spaceUris.get(spaceId),
lastModified: row.last_modified
? new Date(row.last_modified + "Z").valueOf()
: undefined,
};
})
.sort((a, b) => (b.lastModified ?? 0) - (a.lastModified ?? 0));
};
/* eslint-enable @typescript-eslint/naming-convention -- re-enable after Supabase row mapping */