-
Notifications
You must be signed in to change notification settings - Fork 7
ENG-2068 Sync and publish node schemas while publishing nodes #1249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,8 +2,11 @@ import { CrossAppNode } from "@repo/database/crossAppContracts"; | |||||||||||||
| import type { DGSupabaseClient } from "@repo/database/lib/client"; | ||||||||||||||
| import { getAvailableGroupIds } from "@repo/database/lib/groups"; | ||||||||||||||
| import { nodeUidsWithTypeToCrossApp } from "./roamToCrossAppConverters"; | ||||||||||||||
| import { nodeSchemaToCrossApp } from "./roamToCrossAppConverters"; | ||||||||||||||
| import { crossAppNodeSchemaToDbConcept } from "@repo/database/lib/crossAppConverters"; | ||||||||||||||
| import { ensurePartialSpaceAccess } from "@repo/database/lib/groups"; | ||||||||||||||
| import { isIgnorableUpsertError } from "@repo/database/lib/contextFunctions"; | ||||||||||||||
| import getDiscourseNodes from "./getDiscourseNodes"; | ||||||||||||||
| import { difference, intersection } from "@repo/utils/setOperations"; | ||||||||||||||
| import internalError from "./internalError"; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -16,7 +19,10 @@ const onlyStrings = (values: (string | null)[]): string[] => | |||||||||||||
| values.filter((value): value is string => typeof value === "string"); | ||||||||||||||
|
|
||||||||||||||
| type PublishNodesResult = { | ||||||||||||||
| publishedNodeSchemaUids: string[]; | ||||||||||||||
| publishedNodeUids: string[]; | ||||||||||||||
| syncedNodeSchemaUids: string[]; | ||||||||||||||
| failedSyncedUids: string[]; | ||||||||||||||
| skippedUnsyncedUids: string[]; | ||||||||||||||
| okGroupIds: string[]; | ||||||||||||||
| failedGroupIds: string[]; | ||||||||||||||
|
|
@@ -42,7 +48,10 @@ export const publishNodesToGroups = async ({ | |||||||||||||
| nodes: CrossAppNode[]; | ||||||||||||||
| }): Promise<PublishNodesResult> => { | ||||||||||||||
| const result: PublishNodesResult = { | ||||||||||||||
| publishedNodeSchemaUids: [], | ||||||||||||||
| publishedNodeUids: [], | ||||||||||||||
| syncedNodeSchemaUids: [], | ||||||||||||||
| failedSyncedUids: [], | ||||||||||||||
| skippedUnsyncedUids: [], | ||||||||||||||
| okGroupIds: [], | ||||||||||||||
| failedGroupIds: [], | ||||||||||||||
|
|
@@ -71,12 +80,19 @@ export const publishNodesToGroups = async ({ | |||||||||||||
| if (groupIds.length === 0) return result; | ||||||||||||||
|
|
||||||||||||||
| let nodeUids = [...new Set(nodes.map((node) => node.localId))]; | ||||||||||||||
| const nodeSchemaUids = new Set(nodes.map((node) => node.nodeType)); | ||||||||||||||
| const nodeSchemas = getDiscourseNodes() | ||||||||||||||
| .filter((s) => nodeSchemaUids.has(s.type)) | ||||||||||||||
| .map((s) => nodeSchemaToCrossApp(s)) | ||||||||||||||
| .filter((s) => s !== null); | ||||||||||||||
|
|
||||||||||||||
| const neededUids = [...nodeSchemaUids, ...nodeUids]; | ||||||||||||||
|
|
||||||||||||||
| const syncedRes = await client | ||||||||||||||
| .from("my_concepts") | ||||||||||||||
| .select("source_local_id") | ||||||||||||||
| .eq("space_id", spaceId) | ||||||||||||||
| .in("source_local_id", nodeUids); | ||||||||||||||
| .in("source_local_id", neededUids); | ||||||||||||||
| if (syncedRes.error) { | ||||||||||||||
| internalError({ error: syncedRes.error }); | ||||||||||||||
| return result; | ||||||||||||||
|
|
@@ -86,18 +102,47 @@ export const publishNodesToGroups = async ({ | |||||||||||||
| ); | ||||||||||||||
| result.skippedUnsyncedUids = nodeUids.filter((uid) => !syncedUids.has(uid)); | ||||||||||||||
| nodeUids = [...intersection(syncedUids, new Set(nodeUids))]; | ||||||||||||||
| const missingNodeSchemas = nodeSchemas.filter( | ||||||||||||||
| (s) => !syncedUids.has(s.localId), | ||||||||||||||
| ); | ||||||||||||||
| const upsertConcepts = [ | ||||||||||||||
| ...missingNodeSchemas.map((s) => crossAppNodeSchemaToDbConcept(s)), | ||||||||||||||
| ].filter((r) => r !== undefined); | ||||||||||||||
|
|
||||||||||||||
| const resourceAccesses = []; | ||||||||||||||
| const resourceIds = [...nodeUids, ...nodeSchemaUids]; | ||||||||||||||
| for (const groupId of groupIds) { | ||||||||||||||
| resourceAccesses.push( | ||||||||||||||
| ...nodeUids.map((sourceLocalId) => ({ | ||||||||||||||
| ...resourceIds.map((sourceLocalId) => ({ | ||||||||||||||
| account_uid: groupId, | ||||||||||||||
| source_local_id: sourceLocalId, | ||||||||||||||
| space_id: spaceId, | ||||||||||||||
| })), | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (upsertConcepts.length > 0) { | ||||||||||||||
| const response = await client.rpc("upsert_concepts", { | ||||||||||||||
| v_space_id: spaceId, | ||||||||||||||
| data: upsertConcepts, | ||||||||||||||
| }); | ||||||||||||||
| if (response.error) { | ||||||||||||||
| internalError({ error: response.error }); | ||||||||||||||
| return result; | ||||||||||||||
| } | ||||||||||||||
| const syncedSchemaUids = new Set(missingNodeSchemas.map((s) => s.localId)); | ||||||||||||||
| response.data.forEach((v, i) => { | ||||||||||||||
| if (v === -1) { | ||||||||||||||
| const localId = upsertConcepts[i].source_local_id; | ||||||||||||||
| if (localId) { | ||||||||||||||
| if (syncedSchemaUids.has(localId)) syncedSchemaUids.delete(localId); | ||||||||||||||
| result.failedSyncedUids.push(localId); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
| result.syncedNodeSchemaUids = [...syncedSchemaUids]; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const grantRes = await client | ||||||||||||||
| .from("ResourceAccess") | ||||||||||||||
| .upsert(resourceAccesses, { ignoreDuplicates: true }); | ||||||||||||||
|
|
@@ -108,6 +153,7 @@ export const publishNodesToGroups = async ({ | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| result.okGroupIds = groupIds; | ||||||||||||||
| result.publishedNodeSchemaUids = [...nodeSchemaUids]; | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic Bug: This line assigns ALL node schema UIDs to Fix: Exclude failed schemas: result.publishedNodeSchemaUids = [...nodeSchemaUids].filter(
(uid) => !result.failedSyncedUids.includes(uid)
);
Suggested change
Spotted by Graphite |
||||||||||||||
| result.publishedNodeUids = nodeUids; | ||||||||||||||
|
|
||||||||||||||
| return result; | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.