Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: linear client errors

Sidwebworks6d271a626b51
diff --git a/apps/erp/app/routes/api+/integrations.linear.issue.create.ts b/apps/erp/app/routes/api+/integrations.linear.issue.create.ts
index 25112f4312..298a44ee2d 100644
--- a/apps/erp/app/routes/api+/integrations.linear.issue.create.ts
+++ b/apps/erp/app/routes/api+/integrations.linear.issue.create.ts
@@ -11,50 +11,58 @@ import { getIssueAction } from "~/modules/quality/quality.service";
const linear = new LinearClient();
export const action: ActionFunction = async ({ request }) => {
- const data = await request.formData();
-
- const { companyId, client } = await requirePermissions(request, {});
-
- const actionId = data.get("actionId") as string;
- const teamId = data.get("teamId") as string;
- const title = data.get("title") as string;
- const description = data.get("description") as string;
- const assigneeId = data.get("assignee") as string;
-
- const [carbonIssue, issue] = await Promise.all([
- getIssueAction(client, actionId),
- linear.createIssue(companyId, {
- teamId,
- title,
- description: description || undefined,
- assigneeId: assigneeId || null,
- }),
- ]);
-
- if (!issue) {
- return { success: false, message: "Issue not found" };
- }
-
- const linked = await linkActionToLinearIssue(client, companyId, {
- actionId,
- issue: issue,
- });
-
- if (!linked || linked.data?.length === 0) {
- return json({ success: false, message: "Failed to link issue" });
+ try {
+ const data = await request.formData();
+
+ const { companyId, client } = await requirePermissions(request, {});
+
+ const actionId = data.get("actionId") as string;
+ const teamId = data.get("teamId") as string;
+ const title = data.get("title") as string;
+ const description = data.get("description") as string;
+ const assigneeId = data.get("assignee") as string;
+
+ const [carbonIssue, issue] = await Promise.all([
+ getIssueAction(client, actionId),
+ linear.createIssue(companyId, {
+ teamId,
+ title,
+ description: description || undefined,
+ assigneeId: assigneeId || null,
+ }),
+ ]);
+
+ if (!issue) {
+ return { success: false, message: "Issue not found" };
+ }
+
+ const linked = await linkActionToLinearIssue(client, companyId, {
+ actionId,
+ issue: issue,
+ });
+
+ if (!linked || linked.data?.length === 0) {
+ return json({ success: false, message: "Failed to link issue" });
+ }
+
+ const nonConformanceId = linked.data?.[0].nonConformanceId;
+
+ const url = getAppUrl() + `/x/issue/${nonConformanceId}/details`;
+
+ await linear.createAttachmentLink(companyId, {
+ issueId: issue.id,
+ url,
+ title: `Linked Carbon Issue: ${carbonIssue.data?.nonConformanceId ?? ""}`,
+ });
+
+ return new Response("Linear issue created");
+ } catch (error) {
+ console.error("Linear issue action error:", error);
+ return json(
+ { success: false, message: "Failed to create issue" },
+ { status: 400 }
+ );
}
-
- const nonConformanceId = linked.data?.[0].nonConformanceId;
-
- const url = getAppUrl() + `/x/issue/${nonConformanceId}/details`;
-
- await linear.createAttachmentLink(companyId, {
- issueId: issue.id,
- url,
- title: `Linked Carbon Issue: ${carbonIssue.data?.nonConformanceId ?? ""}`,
- });
-
- return new Response("Linear issue created");
};
export const loader: LoaderFunction = async ({ request }) => {
diff --git a/apps/erp/app/routes/api+/integrations.linear.issue.link.ts b/apps/erp/app/routes/api+/integrations.linear.issue.link.ts
index e6b9876139..364a5366f2 100644
--- a/apps/erp/app/routes/api+/integrations.linear.issue.link.ts
+++ b/apps/erp/app/routes/api+/integrations.linear.issue.link.ts
@@ -11,76 +11,87 @@ import { getIssueAction } from "~/modules/quality/quality.service";
const linear = new LinearClient();
export const action: ActionFunction = async ({ request }) => {
- const { companyId, client } = await requirePermissions(request, {});
- const form = await request.formData();
+ try {
+ const { companyId, client } = await requirePermissions(request, {});
+ const form = await request.formData();
- const actionId = form.get("actionId") as string;
+ const actionId = form.get("actionId") as string;
- if (!actionId) {
- return { success: false, message: "Missing required fields: actionId" };
- }
-
- switch (request.method) {
- case "POST": {
- const issueId = form.get("issueId") as string;
-
- if (!issueId) {
- return { success: false, message: "Missing required fields: issueId" };
- }
-
- const [carbonIssue, issue] = await Promise.all([
- getIssueAction(client, actionId),
- linear.getIssueById(companyId, issueId),
- ]);
-
- if (!issue) {
- return { success: false, message: "Issue not found" };
- }
-
- const email = issue.assignee?.email ?? "";
-
- const assignee = await client
- .from("user")
- .select("id")
- .eq("email", email)
- .single();
-
- const linked = await linkActionToLinearIssue(client, companyId, {
- actionId,
- issue,
- assignee: assignee.data ? assignee.data.id : null,
- });
+ if (!actionId) {
+ return { success: false, message: "Missing required fields: actionId" };
+ }
- if (!linked || linked.data?.length === 0) {
- return json({ success: false, message: "Failed to link issue" });
+ switch (request.method) {
+ case "POST": {
+ const issueId = form.get("issueId") as string;
+
+ if (!issueId) {
+ return {
+ success: false,
+ message: "Missing required fields: issueId",
+ };
+ }
+
+ const [carbonIssue, issue] = await Promise.all([
+ getIssueAction(client, actionId),
+ linear.getIssueById(companyId, issueId),
+ ]);
+
+ if (!issue) {
+ return { success: false, message: "Issue not found" };
+ }
+
+ const email = issue.assignee?.email ?? "";
+
+ const assignee = await client
+ .from("user")
+ .select("id")
+ .eq("email", email)
+ .single();
+
+ const linked = await linkActionToLinearIssue(client, companyId, {
+ actionId,
+ issue,
+ assignee: assignee.data ? assignee.data.id : null,
+ });
+
+ if (!linked || linked.data?.length === 0) {
+ return json({ success: false, message: "Failed to link issue" });
+ }
+
+ const nonConformanceId = linked.data?.[0].nonConformanceId;
+
+ const url = getAppUrl() + `/x/issue/${nonConformanceId}/details`;
+
+ await linear.createAttachmentLink(companyId, {
+ issueId: issue.id as string,
+ url,
+ title: `Linked Carbon Issue: ${
+ carbonIssue.data?.nonConformanceId ?? ""
+ }`,
+ });
+
+ return json({ success: true, message: "Linked successfully" });
}
- const nonConformanceId = linked.data?.[0].nonConformanceId;
-
- const url = getAppUrl() + `/x/issue/${nonConformanceId}/details`;
+ case "DELETE": {
+ const unlinked = await unlinkActionFromLinearIssue(client, companyId, {
+ actionId,
+ });
- await linear.createAttachmentLink(companyId, {
- issueId: issue.id as string,
- url,
- title: `Linked Carbon Issue: ${
- carbonIssue.data?.nonConformanceId ?? ""
- }`,
- });
+ if (unlinked.error) {
+ return json({ success: false, message: "Failed to unlink issue" });
+ }
- return json({ success: true, message: "Linked successfully" });
- }
-
- case "DELETE": {
- const unlinked = await unlinkActionFromLinearIssue(client, companyId, {
- actionId,
- });
-
- if (unlinked.error) {
- return json({ success: false, message: "Failed to unlink issue" });
+ return json({ success: true, message: "Unlinked successfully" });
}
-
- return json({ success: true, message: "Unlinked successfully" });
}
+ } catch (error) {
+ console.error("Linear issue link action error:", error);
+ return json(
+ { success: false, message: `Failed to process request` },
+ { status: 400 }
+ );
}
};
diff --git a/packages/ee/src/linear/lib/client.ts b/packages/ee/src/linear/lib/client.ts
index ea45b9e75b..c7ee7baf46 100644
--- a/packages/ee/src/linear/lib/client.ts
+++ b/packages/ee/src/linear/lib/client.ts
@@ -48,40 +48,50 @@ export class LinearClient {
}
async listTeams(companyId: string) {
- const query = `query Teams { teams { nodes { id name } } }`;
+ try {
+ const query = `query Teams { teams { nodes { id name } } }`;
- const response = await this.instance.request<{
- data: { teams: { nodes: LinearTeam[] } };
- }>({
- method: "POST",
- headers: await this.getAuthHeaders(companyId),
- data: {
- query,
- },
- });
+ const response = await this.instance.request<{
+ data: { teams: { nodes: LinearTeam[] } };
+ }>({
+ method: "POST",
+ headers: await this.getAuthHeaders(companyId),
+ data: {
+ query,
+ },
+ });
- return response.data.data.teams.nodes.map((el) => el);
+ return response.data.data.teams.nodes.map((el) => el);
+ } catch (error) {
+ console.error("Error listing Linear teams:", error);
+ return [];
+ }
}
async listIssues(companyId: string, input: string) {
- const query = `query SearchIssues($filter: IssueFilter!) { issues( filter: $filter first: 5 orderBy: updatedAt ) { nodes { id identifier title description state { name type color } url assignee { email } } } }`;
+ try {
+ const query = `query SearchIssues($filter: IssueFilter!) { issues( filter: $filter first: 5 orderBy: updatedAt ) { nodes { id identifier title description state { name type color } url assignee { email } } } }`;
- const response = await this.instance.request<{
- data: { issues: { nodes: LinearIssue[] } };
- }>({
- method: "POST",
- headers: await this.getAuthHeaders(companyId),
- data: {
- query,
- variables: {
- filter: {
- title: { containsIgnoreCase: input },
+ const response = await this.instance.request<{
+ data: { issues: { nodes: LinearIssue[] } };
+ }>({
+ method: "POST",
+ headers: await this.getAuthHeaders(companyId),
+ data: {
+ query,
+ variables: {
+ filter: {
+ title: { containsIgnoreCase: input },
+ },
},
},
- },
- });
+ });
- return response.data.data.issues.nodes.map((el) => el);
+ return response.data.data.issues.nodes.map((el) => el);
+ } catch (error) {
+ console.error("Error listing Linear issues:", error);
+ return [];
+ }
}
async getIssueById(companyId: string, issueId: string) {
@@ -131,20 +141,25 @@ export class LinearClient {
}
async listTeamMembers(companyId: string, teamId: string) {
- const query = `query Team($teamId: String!) { team(id: $teamId) { members { nodes { id email name } } } }`;
+ try {
+ const query = `query Team($teamId: String!) { team(id: $teamId) { members { nodes { id email name } } } }`;
- const response = await this.instance.request<{
- data: { team: { members: { nodes: LinearUser[] } } };
- }>({
- method: "POST",
- headers: await this.getAuthHeaders(companyId),
- data: {
- query,
- variables: { teamId },
- },
- });
+ const response = await this.instance.request<{
+ data: { team: { members: { nodes: LinearUser[] } } };
+ }>({
+ method: "POST",
+ headers: await this.getAuthHeaders(companyId),
+ data: {
+ query,
+ variables: { teamId },
+ },
+ });
- return response.data.data.team.members.nodes.map((el) => el);
+ return response.data.data.team.members.nodes.map((el) => el);
+ } catch (error) {
+ console.error("Error listing Linear team members:", error);
+ return [];
+ }
}
async createIssue(