danielzhe commented on code in PR #2508:
URL: 
https://github.com/apache/incubator-kie-tools/pull/2508#discussion_r1750715884


##########
packages/dmn-editor/src/mutations/addDecisionToDecisionService.ts:
##########
@@ -25,25 +25,48 @@ import { SnapGrid } from "../store/Store";
 import { MIN_NODE_SIZES } from "../diagram/nodes/DefaultSizes";
 import { NODE_TYPES } from "../diagram/nodes/NodeTypes";
 import { Normalized } from "../normalization/normalize";
+import { ExternalModelsIndex } from "../DmnEditor";
+import { buildXmlHref, parseXmlHref } from "../xml/xmlHrefs";
+import { DmnLatestModel } from "@kie-tools/dmn-marshaller/dist";
+import { xmlHrefToQName } from "../xml/xmlHrefToQName";
 
 export function addDecisionToDecisionService({
   definitions,
-  decisionId,
+  decisionHref,
   decisionServiceId,
   drdIndex,
   snapGrid,
+  externalModelsByNamespace,
 }: {
   definitions: Normalized<DMN15__tDefinitions>;
-  decisionId: string;
+  decisionHref: string;
   decisionServiceId: string;
   drdIndex: number;
   snapGrid: SnapGrid;
+  externalModelsByNamespace: ExternalModelsIndex | undefined;
 }) {
-  console.debug(`DMN MUTATION: Adding Decision '${decisionId}' to Decision 
Service '${decisionServiceId}'`);
+  console.debug(`DMN MUTATION: Adding Decision '${decisionHref}' to Decision 
Service '${decisionServiceId}'`);
 
-  const decision = definitions.drgElement?.find((s) => s["@_id"] === 
decisionId);
-  if (decision?.__$$element !== "decision") {
-    throw new Error(`DMN MUTATION: DRG Element with id '${decisionId}' is 
either not a Decision or doesn't exist.`);
+  const href = parseXmlHref(decisionHref);
+
+  const externalModel = externalModelsByNamespace?.[href.namespace ?? ""];
+  if (href.namespace && !externalModel) {
+    throw new Error(`DMN MUTATION: Namespace '${href.namespace}' not found.`);
+  }
+
+  if (href.namespace) {
+    const externalDrgs = (externalModel?.model as 
Normalized<DmnLatestModel>).definitions.drgElement;
+    const decision = externalDrgs?.find((drgElement) => drgElement["@_id"] === 
href.id);
+    if (decision?.__$$element !== "decision") {
+      throw new Error(
+        `DMN MUTATION: DRG Element with id '${href.id}' is either not a 
Decision or doesn't exist in the external model '${href.namespace}'`
+      );
+    }

Review Comment:
   I'm following the existing error message patterns used in this mutation and 
in `deleteDecisionFromDecisionService`. In my opinion, we will only be adding 
more code to do something that we can do in fewer lines.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to