Skip to content

Commit

Permalink
fix: Show parent nodes for job relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Dec 2, 2024
1 parent 6b0c489 commit 7052602
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { NodeProps } from "reactflow";
import { Handle, Position } from "reactflow";

import { cn } from "@ctrlplane/ui";

import { useTargetDrawer } from "~/app/[workspaceSlug]/(app)/_components/target-drawer/useTargetDrawer";
import { TargetIcon as ResourceIcon } from "~/app/[workspaceSlug]/(app)/_components/TargetIcon";

Expand All @@ -10,14 +12,18 @@ type ResourceNodeProps = NodeProps<{
id: string;
kind: string;
version: string;
isBaseNode: boolean;
}>;
export const ResourceNode: React.FC<ResourceNodeProps> = (node) => {
const { data } = node;
const { setTargetId: setResourceId } = useTargetDrawer();
return (
<>
<div
className="flex w-[250px] cursor-pointer flex-col gap-2 rounded-md border bg-neutral-900/30 px-4 py-3"
className={cn(
"flex w-[250px] cursor-pointer flex-col gap-2 rounded-md border bg-neutral-900/30 px-4 py-3",
data.isBaseNode && "border-2 border-neutral-600",
)}
onClick={() => setResourceId(data.id)}
>
<div className="flex items-center gap-2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ const getResourceNodes = (relationships: Relationships) =>
relationships.nodes.map((r) => ({
id: r.id,
type: NodeType.Resource,
data: { ...r, label: r.identifier },
data: {
...r,
label: r.identifier,
isBaseNode: r.id === relationships.resource.id,
},
position: { x: 0, y: 0 },
}));

Expand Down
99 changes: 81 additions & 18 deletions packages/api/src/router/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ const getNodeDataForResource = async (

type Node = Awaited<ReturnType<typeof getNodeDataForResource>>;

const getNodesRecursivelyHelper = async (
const getChildrenNodesRecursivelyHelper = async (
db: Tx,
node: Node,
nodes: NonNullable<Node>[],
Expand Down Expand Up @@ -286,15 +286,76 @@ const getNodesRecursivelyHelper = async (
const children = await Promise.all(childrenPromises);

const childrenNodesPromises = children.map((c) =>
getNodesRecursivelyHelper(db, c, []),
getChildrenNodesRecursivelyHelper(db, c, []),
);
const childrenNodes = (await Promise.all(childrenNodesPromises)).flat();
return [...nodes, node, ...childrenNodes].filter(isPresent);
};

const getNodesRecursively = async (db: Tx, resourceId: string) => {
const getChildrenNodesRecursively = async (db: Tx, resourceId: string) => {
const baseNode = await getNodeDataForResource(db, resourceId);
return getNodesRecursivelyHelper(db, baseNode, []);
return getChildrenNodesRecursivelyHelper(db, baseNode, []);
};

type ParentNodesResult = {
parentNodes: NonNullable<Node>[];
node: Node;
};

const getParentNodesRecursivelyHelper = async (
db: Tx,
node: Node,
nodes: NonNullable<Node>[],
): Promise<ParentNodesResult> => {
if (node == null) return { parentNodes: nodes, node };

const parentJob = await db
.select()
.from(schema.jobResourceRelationship)
.innerJoin(
schema.job,
eq(schema.jobResourceRelationship.jobId, schema.job.id),
)
.innerJoin(
schema.releaseJobTrigger,
eq(schema.releaseJobTrigger.jobId, schema.job.id),
)
.innerJoin(
schema.resource,
eq(schema.releaseJobTrigger.resourceId, schema.resource.id),
)
.where(
and(
eq(schema.jobResourceRelationship.resourceIdentifier, node.identifier),
isNull(schema.resource.deletedAt),
eq(schema.resource.workspaceId, node.workspaceId),
),
)
.orderBy(desc(schema.releaseJobTrigger.createdAt))
.limit(1)
.then(takeFirstOrNull);

if (parentJob == null) return { parentNodes: nodes, node };

const parentNode = await getNodeDataForResource(db, parentJob.resource.id);
if (parentNode == null) return { parentNodes: nodes, node };

const { job_resource_relationship: parentRelationship } = parentJob;

const { parentNodes, node: parentNodeWithData } =
await getParentNodesRecursivelyHelper(db, parentNode, []);

const nodeWithParent = { ...node, parent: parentRelationship };

return {
parentNodes: [...parentNodes, parentNodeWithData].filter(isPresent),
node: nodeWithParent,
};
};

const getParentNodesRecursively = async (db: Tx, resourceId: string) => {
const baseNode = await getNodeDataForResource(db, resourceId);
return getParentNodesRecursivelyHelper(db, baseNode, []);
};

export const resourceRouter = createTRPCRouter({
Expand Down Expand Up @@ -361,9 +422,17 @@ export const resourceRouter = createTRPCRouter({
where: eq(schema.resource.id, input),
});
if (resource == null) return null;
const childrenNodes = await getNodesRecursively(ctx.db, input);
const childrenNodes = await getChildrenNodesRecursively(ctx.db, input);
const { parentNodes, node } = await getParentNodesRecursively(
ctx.db,
input,
);

const childrenNodesUpdated = childrenNodes.map((n) =>
n.id === node?.id ? node : n,
);

const fromNodesPromises = ctx.db
const nodesQuery = ctx.db
.select()
.from(schema.resourceRelationship)
.innerJoin(
Expand All @@ -372,7 +441,9 @@ export const resourceRouter = createTRPCRouter({
schema.resourceRelationship.fromIdentifier,
schema.resource.identifier,
),
)
);

const fromNodesPromises = nodesQuery
.where(
and(
eq(schema.resourceRelationship.workspaceId, resource.workspaceId),
Expand All @@ -387,16 +458,7 @@ export const resourceRouter = createTRPCRouter({
)
.then((promises) => Promise.all(promises));

const toNodesPromises = ctx.db
.select()
.from(schema.resourceRelationship)
.innerJoin(
schema.resource,
eq(
schema.resourceRelationship.toIdentifier,
schema.resource.identifier,
),
)
const toNodesPromises = nodesQuery
.where(
and(
eq(schema.resourceRelationship.workspaceId, resource.workspaceId),
Expand All @@ -419,7 +481,8 @@ export const resourceRouter = createTRPCRouter({
return {
resource,
nodes: [
...childrenNodes,
...parentNodes,
...childrenNodesUpdated,
...fromNodes.map((n) => n.node),
...toNodes.map((n) => n.node),
].filter(isPresent),
Expand Down

0 comments on commit 7052602

Please sign in to comment.