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.

2.8 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { requirePermissions } from "@carbon/auth/auth.server";
import { ProductLabelPDF } from "@carbon/documents/pdf";
import { getLogger } from "@carbon/logger";
import { labelSizes, redirect } from "@carbon/utils";
import { renderToStream } from "@react-pdf/renderer";
import type { LoaderFunctionArgs } from "react-router";
import {
  getCompany,
  getCompanyLogoForLabel,
  getDocumentTemplateConfig,
  resolveLabelLogo
} from "~/modules/settings/settings.service";
import { path } from "~/utils/path";
import { getEntityLabelData } from "./labels.server";

const logger = getLogger("erp", "id-labels-pdf");

export async function loader({ request, params }: LoaderFunctionArgs) {
  const { client, companyId } = await requirePermissions(request, {});

  const { id } = params;
  if (!id) throw new Error("Could not find id");

  const company = await getCompany(client, companyId);
  if (company.error) {
    logger.error(company.error);
    throw new Error("Failed to load company");
  }

  const result = await getEntityLabelData(client, companyId, id);

  if (result.error) {
    return new Response(result.error, { status: 404 });
  }

  const { companySettings, labelItem } = result;

  const url = new URL(request.url);
  const labelParam = url.searchParams.get("labelSize");
  const labelSizeId =
    labelParam || companySettings?.data?.productLabelSize || "avery5163";

  const labelSize = labelSizes.find((size) => size.id === labelSizeId);

  if (!labelSize) {
    throw new Error("Invalid label size");
  }

  if (labelSize.zpl) {
    throw redirect(
      path.to.file.trackedEntityLabelZpl(id, { labelSize: labelSize.id })
    );
  }

  const template = await getDocumentTemplateConfig(
    client,
    companyId,
    "trackingLabel"
  );
  const companyLogo = await getCompanyLogoForLabel(client, companyId);
  const logo = await resolveLabelLogo(companyLogo, template, labelSize);

  const stream = await renderToStream(
    <ProductLabelPDF
      items={[labelItem!]}
      labelSize={labelSize}
      template={template}
      company={company.data as any}
      logo={logo}
    />
  );

  const body: Buffer = await new Promise((resolve, reject) => {
    const buffers: Uint8Array[] = [];
    stream.on("data", (data) => {
      buffers.push(data);
    });
    stream.on("end", () => {
      resolve(Buffer.concat(buffers));
    });
    stream.on("error", reject);
  });

  const headers = new Headers({
    "Content-Type": "application/pdf",
    "Content-Disposition": `inline; filename="${company.data.name} - Entity Labels.pdf"`
  });
  return new Response(new Uint8Array(body), { status: 200, headers });
}