@raytio/decrypt-helper 6.4.2 → 6.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/dist/api/authedFetch.d.ts +1 -0
  2. package/dist/api/authedFetch.js +35 -0
  3. package/dist/api/fetchAA.d.ts +3 -0
  4. package/dist/api/fetchAA.js +18 -0
  5. package/dist/api/fetchEnvConfig.d.ts +6 -0
  6. package/dist/api/fetchEnvConfig.js +35 -0
  7. package/dist/api/fetchInstanceData.d.ts +3 -0
  8. package/dist/api/fetchInstanceData.js +19 -0
  9. package/dist/api/getFiles.d.ts +6 -0
  10. package/dist/api/getFiles.js +125 -0
  11. package/dist/api/getLookupOption.d.ts +2 -0
  12. package/dist/api/getLookupOption.js +29 -0
  13. package/dist/api/getSchema.d.ts +3 -0
  14. package/dist/api/getSchema.js +28 -0
  15. package/dist/api/index.d.ts +9 -0
  16. package/dist/api/index.js +9 -0
  17. package/dist/api/resolveVerificationDetails.d.ts +11 -0
  18. package/dist/api/resolveVerificationDetails.js +31 -0
  19. package/dist/api/signIn.d.ts +7 -0
  20. package/dist/api/signIn.js +52 -0
  21. package/dist/api/updateInstanceData.d.ts +7 -0
  22. package/dist/api/updateInstanceData.js +23 -0
  23. package/dist/api/uploadToObjectStore.d.ts +5 -0
  24. package/dist/api/uploadToObjectStore.js +40 -0
  25. package/dist/api/videoToImage.d.ts +3 -0
  26. package/dist/api/videoToImage.js +26 -0
  27. package/dist/configureEnv.d.ts +1 -0
  28. package/dist/configureEnv.js +19 -0
  29. package/dist/constants.d.ts +23 -0
  30. package/dist/constants.js +42 -0
  31. package/dist/helpers/file.d.ts +17 -0
  32. package/dist/helpers/file.js +16 -0
  33. package/dist/helpers/formatOutput.d.ts +28 -0
  34. package/dist/helpers/formatOutput.js +100 -0
  35. package/dist/helpers/index.d.ts +6 -0
  36. package/dist/helpers/index.js +6 -0
  37. package/dist/helpers/json2csv.d.ts +2 -0
  38. package/dist/helpers/json2csv.js +20 -0
  39. package/dist/helpers/jsxPdf.d.ts +6 -0
  40. package/dist/helpers/jsxPdf.js +3 -0
  41. package/dist/helpers/lookup.d.ts +2 -0
  42. package/dist/helpers/lookup.js +31 -0
  43. package/dist/helpers/pathUtils.d.ts +1 -0
  44. package/dist/helpers/pathUtils.js +6 -0
  45. package/dist/helpers/setupMaxcryptor.d.ts +4 -0
  46. package/dist/helpers/setupMaxcryptor.js +25 -0
  47. package/dist/helpers/splitPOAndVers.d.ts +2 -0
  48. package/dist/helpers/splitPOAndVers.js +8 -0
  49. package/dist/helpers/types.d.ts +1 -0
  50. package/dist/helpers/types.js +5 -0
  51. package/dist/index.d.ts +7 -0
  52. package/dist/index.js +9 -0
  53. package/dist/locales/index.d.ts +12 -0
  54. package/dist/locales/index.js +17 -0
  55. package/dist/locales/translations/en.json +33 -0
  56. package/dist/pdf/components/FieldVerificationBadge.d.ts +5 -0
  57. package/dist/pdf/components/FieldVerificationBadge.js +12 -0
  58. package/dist/pdf/components/Images.d.ts +5 -0
  59. package/dist/pdf/components/Images.js +13 -0
  60. package/dist/pdf/components/InnerTableRows.d.ts +10 -0
  61. package/dist/pdf/components/InnerTableRows.js +30 -0
  62. package/dist/pdf/components/POVerificationBadge.d.ts +7 -0
  63. package/dist/pdf/components/POVerificationBadge.js +34 -0
  64. package/dist/pdf/components/Report.d.ts +12 -0
  65. package/dist/pdf/components/Report.js +91 -0
  66. package/dist/pdf/components/Subheader.d.ts +1 -0
  67. package/dist/pdf/components/Subheader.js +21 -0
  68. package/dist/pdf/components/Table.d.ts +8 -0
  69. package/dist/pdf/components/Table.js +40 -0
  70. package/dist/pdf/components/TableTitle.d.ts +4 -0
  71. package/dist/pdf/components/TableTitle.js +10 -0
  72. package/dist/pdf/components/ValidationDisplay.d.ts +4 -0
  73. package/dist/pdf/components/ValidationDisplay.js +22 -0
  74. package/dist/pdf/components/VerifyBox.d.ts +8 -0
  75. package/dist/pdf/components/VerifyBox.js +12 -0
  76. package/dist/pdf/components/pieChart.d.ts +6 -0
  77. package/dist/pdf/components/pieChart.js +23 -0
  78. package/dist/pdf/constants.d.ts +3 -0
  79. package/dist/pdf/constants.js +9 -0
  80. package/dist/pdf/helpers/general.d.ts +8 -0
  81. package/dist/pdf/helpers/general.js +31 -0
  82. package/dist/pdf/helpers/transform.d.ts +4 -0
  83. package/dist/pdf/helpers/transform.js +35 -0
  84. package/dist/pdf/style.d.ts +10 -0
  85. package/dist/pdf/style.js +21 -0
  86. package/dist/public-methods/generatePDF.d.ts +4 -0
  87. package/dist/public-methods/generatePDF.js +54 -0
  88. package/dist/public-methods/getAndValidateConfig.d.ts +8 -0
  89. package/dist/public-methods/getAndValidateConfig.js +16 -0
  90. package/dist/public-methods/processSubmission.d.ts +49 -0
  91. package/dist/public-methods/processSubmission.js +132 -0
  92. package/dist/public-methods/saveToS3Bucket.d.ts +2 -0
  93. package/dist/public-methods/saveToS3Bucket.js +53 -0
  94. package/dist/public-methods/version.d.ts +2 -0
  95. package/dist/public-methods/version.js +13 -0
  96. package/package.json +1 -1
@@ -0,0 +1,91 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { isScoreResultValid } from "@raytio/core";
3
+ import { JsxPdf } from "../../helpers/jsxPdf.js";
4
+ import { $$ } from "../../locales/index.js";
5
+ import { classes } from "../style.js";
6
+ import { asset } from "../helpers/general.js";
7
+ import { NULL_I_ID, SUBMISSION_DATA } from "../constants.js";
8
+ import { transform } from "../helpers/transform.js";
9
+ import { SCHEMA } from "../../constants.js";
10
+ import { version } from "../../public-methods/version.js";
11
+ import { VerifyBox } from "./VerifyBox.js";
12
+ import { Table } from "./Table.js";
13
+ import { TableTitle } from "./TableTitle.js";
14
+ import { Subheader } from "./Subheader.js";
15
+ import { POVerificationBadge } from "./POVerificationBadge.js";
16
+ export const Report = ({ data, files, config, aId, clientUrl, envConfig, }) => {
17
+ // The PDF is read only; there's no reason why anyone would ever need to unlock it.
18
+ const randomToken = randomBytes(32).toString("base64");
19
+ const schemas = Object.values(data.profile_objects).sort(
20
+ // sort it so that the person schema comes first
21
+ (a, b) => {
22
+ var _a, _b;
23
+ return +(((_a = b[0]) === null || _a === void 0 ? void 0 : _a.$schemaName) === SCHEMA.PERSON) -
24
+ +(((_b = a[0]) === null || _b === void 0 ? void 0 : _b.$schemaName) === SCHEMA.PERSON);
25
+ });
26
+ const appName = envConfig.app_name;
27
+ const iId = data.id === NULL_I_ID ? "" : data.id;
28
+ return (JsxPdf.createElement("document", { defaultStyle: { font: "Lato", fontSize: 12 }, info: {
29
+ title: $$("Report.meta.title", { appName, iId }),
30
+ creator: appName,
31
+ producer: "Raytio", // don't whitelabel
32
+ subject: version,
33
+ },
34
+ // @ts-expect-error -- waiting on bpampuch/pdfmake#2453
35
+ lang: global.lang, ownerPassword: randomToken, permissions: {
36
+ printing: "highResolution",
37
+ copying: true, // allow copy/paste
38
+ }, pageSize: "A4", styles: classes, images: {
39
+ raytioLogo: envConfig.logo_url
40
+ ? asset("custom-logo.png")
41
+ : asset("logo.png"),
42
+ } },
43
+ JsxPdf.createElement("header", null, (currentPage) => currentPage !== 1 && (JsxPdf.createElement("stack", { color: "#333", fontSize: 10 },
44
+ JsxPdf.createElement("text", { margin: 16 },
45
+ JsxPdf.createElement("text", { bold: true },
46
+ $$("Report.header", { appName }),
47
+ " - "),
48
+ JsxPdf.createElement("text", null, iId))))),
49
+ JsxPdf.createElement("content", null,
50
+ JsxPdf.createElement("image", { src: "raytioLogo", width: 150, alignment: "center" }),
51
+ JsxPdf.createElement("text", { style: ["header", "marginBottom"], alignment: "center" }, $$("Report.header", { appName })),
52
+ JsxPdf.createElement("columns", { columnGap: 10 },
53
+ JsxPdf.createElement("column", { width: "auto" },
54
+ JsxPdf.createElement("text", { style: ["subheader", "marginBottom"] },
55
+ iId &&
56
+ Object.entries(SUBMISSION_DATA)
57
+ .map(([id, label]) => `${label}: ${transform(id, data[id], config)}`)
58
+ .join("\n"),
59
+ isScoreResultValid(data.score)
60
+ ? [
61
+ "",
62
+ `${$$("constants.score")}: ${data.score.score}`,
63
+ `${$$("constants.score-category")}: ${data.score.category}`,
64
+ ].join("\n")
65
+ : ""),
66
+ JsxPdf.createElement(Subheader, { data: data, config: config })),
67
+ JsxPdf.createElement("column", { width: "*" },
68
+ JsxPdf.createElement("text", null, " ")),
69
+ JsxPdf.createElement("column", { width: 150 },
70
+ JsxPdf.createElement(VerifyBox, { aId: aId, iId: data.id, clientUrl: clientUrl }))),
71
+ schemas.map((POs) => {
72
+ // for each schema
73
+ return (JsxPdf.createElement("stack", {
74
+ // unbreakable is off because it crashes if its too big for one page
75
+ // This is a 7-year-old issue in pdfmake (https://github.com/bpampuch/pdfmake/issues/207)
76
+ unbreakable: false, style: "marginY" },
77
+ JsxPdf.createElement(TableTitle, { POs: POs }),
78
+ JsxPdf.createElement("columns", { columnGap: 15 },
79
+ JsxPdf.createElement("column", { width: 80 },
80
+ JsxPdf.createElement(POVerificationBadge, { width: 80, POs: POs })),
81
+ JsxPdf.createElement("column", { width: "auto" },
82
+ JsxPdf.createElement(Table, { POs: POs, files: files, config: config })))));
83
+ })),
84
+ JsxPdf.createElement("footer", null, (currentPage, pageCount, pageSize) => (JsxPdf.createElement("stack", { bold: true, color: "#333", fontSize: 10 },
85
+ JsxPdf.createElement("text", { margin: 16 },
86
+ JsxPdf.createElement("text", null,
87
+ $$("Report.header", { appName }),
88
+ " - "),
89
+ JsxPdf.createElement("text", { color: "#fb7171" }, $$("Report.footer-private"))),
90
+ JsxPdf.createElement("text", { absolutePosition: { x: pageSize.width - 50, y: 15 } }, $$("Report.pagecount", { currentPage, pageCount })))))));
91
+ };
@@ -0,0 +1 @@
1
+ export declare const Subheader: () => JSX.Element;
@@ -0,0 +1,21 @@
1
+ import { FieldVerification } from "@raytio/types";
2
+ import { JsxPdf } from "../../helpers/jsxPdf.js";
3
+ import { $$ } from "../../locales/index.js";
4
+ import { FieldVerificationBadge } from "./FieldVerificationBadge.js";
5
+ export const Subheader = () => {
6
+ return (JsxPdf.createElement(JsxPdf.Fragment, null,
7
+ JsxPdf.createElement("text", { bold: true, style: "marginBottom" }, "Key:"),
8
+ JsxPdf.createElement("table", { layout: "noBorders" },
9
+ JsxPdf.createElement("row", null,
10
+ JsxPdf.createElement("cell", null,
11
+ JsxPdf.createElement(FieldVerificationBadge, { width: 10, status: FieldVerification.Verified })),
12
+ JsxPdf.createElement("cell", null, $$("FieldVerification.Verified"))),
13
+ JsxPdf.createElement("row", null,
14
+ JsxPdf.createElement("cell", null,
15
+ JsxPdf.createElement(FieldVerificationBadge, { width: 10, status: FieldVerification.NotVerified })),
16
+ JsxPdf.createElement("cell", null, $$("FieldVerification.NotVerified"))),
17
+ JsxPdf.createElement("row", null,
18
+ JsxPdf.createElement("cell", null,
19
+ JsxPdf.createElement(FieldVerificationBadge, { width: 10, status: FieldVerification.VerifiedFalse })),
20
+ JsxPdf.createElement("cell", null, $$("FieldVerification.VerifiedFalse"))))));
21
+ };
@@ -0,0 +1,8 @@
1
+ import type { PdfConfig } from "../types.js";
2
+ import { type FlatPO } from "../../helpers/index.js";
3
+ import type { ProcessSubmissionOutput } from "../../public-methods/processSubmission.js";
4
+ export declare const Table: ({ POs, files, config, }: {
5
+ POs: FlatPO[];
6
+ files: ProcessSubmissionOutput["files"];
7
+ config: PdfConfig;
8
+ }) => JSX.Element;
@@ -0,0 +1,40 @@
1
+ import { JsxPdf } from "../../helpers/jsxPdf.js";
2
+ import { verifyColour } from "../helpers/general.js";
3
+ import { assertSafeProperty, isFieldValueFile, isPOFile, } from "../../helpers/index.js";
4
+ import { FieldVerificationBadge } from "./FieldVerificationBadge.js";
5
+ import { Images } from "./Images.js";
6
+ import { InnerTableRows } from "./InnerTableRows.js";
7
+ export const Table = ({ POs, files, config, }) => {
8
+ /** every fieldName in these profile object */
9
+ const fieldNames = [
10
+ ...new Set(POs.flatMap((PO) => Object.keys(PO.$properties))),
11
+ ];
12
+ // some or all POs in this schema are files themselves. So render the file, assuming it's an image
13
+ const fileNIds = POs.filter(isPOFile).map((x) => x.$nId);
14
+ if (fileNIds.length)
15
+ return JsxPdf.createElement(Images, { nIds: fileNIds, files: files });
16
+ const imageFieldNames = fieldNames.filter((fieldName) => POs.map((PO) => { var _a; return (_a = PO.$properties[fieldName]) === null || _a === void 0 ? void 0 : _a.value; }).some(isFieldValueFile));
17
+ return (JsxPdf.createElement("table", { layout: "noBorders" }, fieldNames.map((fieldName) => {
18
+ var _a;
19
+ return (JsxPdf.createElement("row", null,
20
+ JsxPdf.createElement("cell", null,
21
+ JsxPdf.createElement("text", { color: "#12130e" }, ((_a = POs.find((PO) => PO.$properties[fieldName])) === null || _a === void 0 ? void 0 : _a.$properties[fieldName].title) || fieldName)),
22
+ POs.flatMap((PO) => {
23
+ assertSafeProperty(fieldName);
24
+ // if we're sharing multiple POs in this schema, and this field doesn't exist on this paticular PO,
25
+ // then just render some empty cells
26
+ if (!PO.$properties[fieldName]) {
27
+ return [JsxPdf.createElement("cell", null, " "), JsxPdf.createElement("cell", null, " ")];
28
+ }
29
+ const { value, formatted_value, verification } = PO.$properties[fieldName];
30
+ const color = verifyColour(verification);
31
+ // jsx-pdf currently doesn't support fragments, will make a PR at some point
32
+ return [
33
+ JsxPdf.createElement("cell", null,
34
+ JsxPdf.createElement(FieldVerificationBadge, { width: 10, status: verification })),
35
+ JsxPdf.createElement("cell", null,
36
+ JsxPdf.createElement(InnerTableRows, { key: fieldName, value: formatted_value || value, files: files, config: config, color: color, imageFieldNames: imageFieldNames })),
37
+ ];
38
+ })));
39
+ })));
40
+ };
@@ -0,0 +1,4 @@
1
+ import { type FlatPO } from "../../helpers/index.js";
2
+ export declare const TableTitle: ({ POs }: {
3
+ POs: FlatPO[];
4
+ }) => JSX.Element;
@@ -0,0 +1,10 @@
1
+ import { JsxPdf } from "../../helpers/jsxPdf.js";
2
+ import { isPOFile } from "../../helpers/index.js";
3
+ export const TableTitle = ({ POs }) => {
4
+ var _a;
5
+ const schemaTitle = POs[0].$schemaTitle;
6
+ const maybeFileName = POs.some(isPOFile)
7
+ ? (_a = POs[0].$properties.title) === null || _a === void 0 ? void 0 : _a.value
8
+ : undefined;
9
+ return (JsxPdf.createElement("text", { bold: true, fontSize: 14, style: "marginBottom" }, maybeFileName || schemaTitle));
10
+ };
@@ -0,0 +1,4 @@
1
+ import type { Validation } from "@raytio/types";
2
+ export declare const ValidationDisplay: ({ value, }: {
3
+ value: Validation;
4
+ }) => null | JSX.Element;
@@ -0,0 +1,22 @@
1
+ import { JsxPdf } from "../../helpers/jsxPdf.js";
2
+ import { $$ } from "../../locales/index.js";
3
+ import { assertSafeProperty } from "../../helpers/index.js";
4
+ import { pieChart } from "./pieChart.js";
5
+ const SEVERITY_COLOURS = {
6
+ low: "#2e8bc0",
7
+ medium: "#ff9800",
8
+ high: "#fb7171",
9
+ };
10
+ export const ValidationDisplay = ({ value, }) => {
11
+ const colour = value.score > 0.5 ? "#c2d887" : "#fb7171";
12
+ return (JsxPdf.createElement(JsxPdf.Fragment, null,
13
+ JsxPdf.createElement("svg", { content: pieChart(colour, value.score * 100), width: 50 }),
14
+ $$("ValidationDisplay.text", { n: value.score * 10 }),
15
+ Object.values(value.breakdown)
16
+ .filter((x) => x.severity && x.reason)
17
+ .map((x) => {
18
+ const key = x.severity;
19
+ assertSafeProperty(key);
20
+ return JsxPdf.createElement("text", { color: SEVERITY_COLOURS[key] }, x.reason);
21
+ })));
22
+ };
@@ -0,0 +1,8 @@
1
+ import type { AId, IId } from "@raytio/types";
2
+ type Props = {
3
+ aId: AId;
4
+ iId: IId;
5
+ clientUrl: string;
6
+ };
7
+ export declare const VerifyBox: ({ aId, iId, clientUrl }: Props) => JSX.Element | null;
8
+ export {};
@@ -0,0 +1,12 @@
1
+ import { JsxPdf } from "../../helpers/jsxPdf.js";
2
+ import { $$ } from "../../locales/index.js";
3
+ import { NULL_I_ID } from "../constants.js";
4
+ export const VerifyBox = ({ aId, iId, clientUrl }) => {
5
+ if (iId === NULL_I_ID)
6
+ return null;
7
+ const url = `${clientUrl}/apps/s/${aId}/i/${iId}`;
8
+ return (JsxPdf.createElement("stack", { alignment: "center" },
9
+ JsxPdf.createElement("text", { link: url }, $$("VerifyBox.text")),
10
+ JsxPdf.createElement("qr", { content: url, fit: 150 }),
11
+ JsxPdf.createElement("text", { color: "#1e90ff", fontSize: 9, link: url, alignment: "left" }, url)));
12
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * A semi-cirle pie chart
3
+ * (!) generates a SVG string, not a JSX component.
4
+ * @param percentage a number from 0 to 100
5
+ */
6
+ export declare function pieChart(colour: string, percentage: number): string;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * A semi-cirle pie chart
3
+ * (!) generates a SVG string, not a JSX component.
4
+ * @param percentage a number from 0 to 100
5
+ */
6
+ export function pieChart(colour, percentage) {
7
+ return `
8
+ <svg height="70" width="140" viewBox="0 -5 20 15">
9
+ <circle r="10" cx="10" cy="10" fill="bisque" />
10
+ <circle
11
+ r="5"
12
+ cx="10"
13
+ cy="10"
14
+ fill="white"
15
+ stroke="${colour}"
16
+ stroke-width="10"
17
+ stroke-dasharray="${(((percentage / 2) * 31.42) / 100).toFixed(2 // NOTE: css calc() doesn't work in PDFs
18
+ )} 31.42"
19
+ transform="rotate(-180) translate(-20, -20)"
20
+ />
21
+ </svg>
22
+ `;
23
+ }
@@ -0,0 +1,3 @@
1
+ import type { IId, Instance } from "@raytio/types";
2
+ export declare const NULL_I_ID: IId;
3
+ export declare const SUBMISSION_DATA: Partial<Record<keyof Instance, string>>;
@@ -0,0 +1,9 @@
1
+ import { $$ } from "../locales/index.js";
2
+ export const NULL_I_ID = "00000000-0000-0000-0000-000000000000";
3
+ export const SUBMISSION_DATA = {
4
+ confirmation_code: $$("constants.confirmation_code"),
5
+ id: $$("constants.i_id"),
6
+ reference: $$("constants.reference"),
7
+ start_date: $$("constants.start_date"),
8
+ end_date: $$("constants.end_date"),
9
+ };
@@ -0,0 +1,8 @@
1
+ import { FieldVerification, POVerification } from "@raytio/types";
2
+ export declare const asset: (name: string) => string;
3
+ export declare const loadAsset: (name: string) => string;
4
+ export declare const verifyColour: (x: FieldVerification | POVerification) => string;
5
+ /**
6
+ * Produces a date with a long month
7
+ */
8
+ export declare const formatDate: (d: Date, locale: string, timeZone: string) => string;
@@ -0,0 +1,31 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { FieldVerification, POVerification } from "@raytio/types";
4
+ import { getDirname } from "../../helpers/pathUtils.js";
5
+ export const asset = (name) => {
6
+ if (/[^.A-Z_a-z-]/.test(name) || name.includes("..")) {
7
+ throw new Error("Invalid asset name");
8
+ }
9
+ return join(getDirname(), "./../../assets/", name);
10
+ };
11
+ export const loadAsset = (name) => readFileSync(asset(name), { encoding: "utf8" });
12
+ export const verifyColour = (x) => {
13
+ if (x === FieldVerification.Verified)
14
+ return "#12130e";
15
+ if (x === FieldVerification.VerifiedFalse ||
16
+ x === POVerification.VerifiedFalse) {
17
+ return "#fb7171";
18
+ }
19
+ return "#000";
20
+ };
21
+ /**
22
+ * Produces a date with a long month
23
+ */
24
+ export const formatDate = (d, locale, timeZone) => new Intl.DateTimeFormat(locale, {
25
+ day: "2-digit",
26
+ month: "long",
27
+ year: "numeric",
28
+ hour: "numeric",
29
+ minute: "numeric",
30
+ timeZone,
31
+ }).format(d);
@@ -0,0 +1,4 @@
1
+ import type { PdfConfig } from "../types";
2
+ export declare const IS_IMAGE: unique symbol;
3
+ export declare const IS_ARRAY_OF_OBJECTS: unique symbol;
4
+ export declare function transform(key: string, value: unknown, pdfConfig: PdfConfig, imageFieldNames?: string[]): string | typeof IS_IMAGE | typeof IS_ARRAY_OF_OBJECTS;
@@ -0,0 +1,35 @@
1
+ import { repairDate } from "@raytio/core";
2
+ import { $$ } from "../../locales/index.js";
3
+ import { formatDate } from "./general.js";
4
+ export const IS_IMAGE = Symbol.for("image");
5
+ export const IS_ARRAY_OF_OBJECTS = Symbol.for("ArrayOfObjects");
6
+ export function transform(key, value, pdfConfig, imageFieldNames) {
7
+ // value is possible to be false if type boolean
8
+ if (value === undefined) {
9
+ return $$("field.undefined");
10
+ }
11
+ if (typeof value === "boolean") {
12
+ return value ? $$("field.boolean.yes") : $$("field.boolean.no");
13
+ }
14
+ if (imageFieldNames === null || imageFieldNames === void 0 ? void 0 : imageFieldNames.includes(key)) {
15
+ return IS_IMAGE;
16
+ }
17
+ if (Array.isArray(value)) {
18
+ if (value.every((v) => typeof v === "object")) {
19
+ return IS_ARRAY_OF_OBJECTS;
20
+ }
21
+ return value.join(", ");
22
+ }
23
+ if (typeof value !== "string") {
24
+ return `${value}`;
25
+ }
26
+ switch (key) {
27
+ case "start_date":
28
+ case "end_date": {
29
+ return formatDate(repairDate(value), pdfConfig.DATE_FORMAT, pdfConfig.TIMEZONE);
30
+ }
31
+ default: {
32
+ return value;
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,10 @@
1
+ import type { StyleDictionary } from "pdfmake/interfaces";
2
+ export declare const classes: StyleDictionary;
3
+ export declare const fonts: {
4
+ Lato: {
5
+ normal: string;
6
+ bold: string;
7
+ italics: string;
8
+ bolditalics: string;
9
+ };
10
+ };
@@ -0,0 +1,21 @@
1
+ import { asset } from "./helpers/general.js";
2
+ export const classes = {
3
+ header: {
4
+ fontSize: 18,
5
+ },
6
+ listing: {
7
+ fontSize: 10,
8
+ color: "#333",
9
+ },
10
+ // [l, t, r, b]
11
+ marginBottom: { margin: [0, 0, 0, 10] },
12
+ marginY: { margin: [0, 15, 0, 15] },
13
+ };
14
+ export const fonts = {
15
+ Lato: {
16
+ normal: asset("Lato-Light.ttf"),
17
+ bold: asset("Lato-Bold.ttf"),
18
+ italics: asset("Lato-LightItalic.ttf"),
19
+ bolditalics: asset("Lato-BoldItalic.ttf"),
20
+ },
21
+ };
@@ -0,0 +1,4 @@
1
+ import type { TDocumentDefinitions } from "pdfmake/interfaces";
2
+ import type { ProcessSubmissionOutput } from "./processSubmission.js";
3
+ export declare function generatePdfJson(data: ProcessSubmissionOutput, DATE_FORMAT: string, TIMEZONE: string): TDocumentDefinitions;
4
+ export declare const generatePDF: () => (data: ProcessSubmissionOutput) => Promise<ProcessSubmissionOutput>;
@@ -0,0 +1,54 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { promises as fs } from "node:fs";
11
+ import { join } from "node:path";
12
+ import PdfPrinter from "pdfmake";
13
+ import JsxPdfImport from "jsx-pdf";
14
+ import { isValidLocale } from "../locales/index.js";
15
+ import { Report } from "../pdf/components/Report.js";
16
+ import { fonts } from "../pdf/style.js";
17
+ import { getDirname } from "../helpers/pathUtils.js";
18
+ const JsxPdf = (JsxPdfImport.default || JsxPdfImport);
19
+ export function generatePdfJson(data, DATE_FORMAT, TIMEZONE) {
20
+ return JsxPdf.renderPdf(JsxPdf.createElement(Report, { data: data.json, files: data.files, aId: data.a_id, clientUrl: data.client_url, envConfig: data.envConfig, config: { DATE_FORMAT, TIMEZONE } }));
21
+ }
22
+ export const generatePDF = () =>
23
+ // eslint-disable-next-line unicorn/consistent-function-scoping -- deliberately to future proof the SDK for options
24
+ (data) => __awaiter(void 0, void 0, void 0, function* () {
25
+ console.log("Generating PDF Report...");
26
+ const { DATE_FORMAT = "en-nz", TIMEZONE = "Pacific/Auckland", PDF_LANGUAGE: customLang, } = process.env;
27
+ if (data.envConfig.logo_url) {
28
+ // there is a white labelling URL, so fetch it and write it to disk, overriding the default logo
29
+ const arrayBuf = yield fetch(data.envConfig.logo_url).then((r) => r.arrayBuffer());
30
+ yield fs.writeFile(join(getDirname(), "../../assets/custom-logo.png"), Buffer.from(arrayBuf));
31
+ }
32
+ if (customLang) {
33
+ if (isValidLocale(customLang)) {
34
+ global.lang = customLang;
35
+ }
36
+ else {
37
+ console.warn(`The specified PDF_LANGUAGE is language is invalid (${customLang})`);
38
+ }
39
+ }
40
+ return new Promise((resolve) => {
41
+ const printer = new PdfPrinter(fonts);
42
+ const pdfDocument = printer.createPdfKitDocument(generatePdfJson(data, DATE_FORMAT, TIMEZONE));
43
+ const chunks = [];
44
+ pdfDocument.on("data", (chunk) => chunks.push(chunk));
45
+ pdfDocument.on("end", () => {
46
+ const final = Buffer.concat(chunks);
47
+ const dataUrl = `data:application/pdf;base64,${final.toString("base64")}`;
48
+ resolve(Object.assign(Object.assign({}, data), { files: Object.assign(Object.assign({}, data.files), {
49
+ // do not change name, will be a breaking change
50
+ [data.json.id]: [dataUrl, "pdf"] }) }));
51
+ });
52
+ pdfDocument.end();
53
+ });
54
+ });
@@ -0,0 +1,8 @@
1
+ import { type Config } from "../constants.js";
2
+ /**
3
+ * This helper is designed for environments where the configuration
4
+ * options are in the environment variables, such as AWS lambda.
5
+ * It extracts the relevant ENV variables and throws an error if
6
+ * any are missing.
7
+ */
8
+ export declare function getAndValidateConfig(): Config;
@@ -0,0 +1,16 @@
1
+ import { ENV_VARIABLES } from "../constants.js";
2
+ /**
3
+ * This helper is designed for environments where the configuration
4
+ * options are in the environment variables, such as AWS lambda.
5
+ * It extracts the relevant ENV variables and throws an error if
6
+ * any are missing.
7
+ */
8
+ export function getAndValidateConfig() {
9
+ const CONFIG = ENV_VARIABLES.reduce((accumulator, name) => {
10
+ const value = process.env[name];
11
+ if (!value)
12
+ throw new Error(`${name} is not configured`);
13
+ return Object.assign(Object.assign({}, accumulator), { [name]: value });
14
+ }, {});
15
+ return CONFIG;
16
+ }
@@ -0,0 +1,49 @@
1
+ import type { AId, IId, Instance, RealVer } from "@raytio/types";
2
+ import { type Config, type InstanceDataToPassOn } from "../constants.js";
3
+ import { type FlatPO } from "../helpers/index.js";
4
+ import { type EnvConfig } from "../api/index.js";
5
+ import type { ApplicationEncryptorLike } from "../types.js";
6
+ type DecryptData = {
7
+ apiToken: string;
8
+ instance: Instance;
9
+ encryptedInstance: Instance;
10
+ applicationDecryptor: ApplicationEncryptorLike;
11
+ realVers?: RealVer[];
12
+ };
13
+ export interface ProcessSubmissionInput {
14
+ /** The a_id of the application */
15
+ applicationId: AId;
16
+ /** The i_id of this submission */
17
+ instanceId: IId;
18
+ /** whether to enable extra logging. Default = false */
19
+ verbose?: boolean;
20
+ /**
21
+ * the configuration options. You can extract these from the environment
22
+ * variables by using @see getAndValidateConfig
23
+ */
24
+ config: Config;
25
+ /** You can optionally supply Instance data and an API token - for Raytio internal use */
26
+ _supliedConfig?: DecryptData;
27
+ }
28
+ export interface ProcessSubmissionOutput {
29
+ /** details of the submission, included the decrypted profile objects */
30
+ json: InstanceDataToPassOn & {
31
+ profile_objects: {
32
+ [schemaName: string]: FlatPO[];
33
+ };
34
+ };
35
+ /** A string of a CSV file containing the same data as in `json` */
36
+ csv: string;
37
+ /** an object of files that were attached to the submission, such as images */
38
+ files: {
39
+ [nId: string]: [dataUrl: string, fileExtension: string];
40
+ };
41
+ /** the a_id is passed on */
42
+ a_id: AId;
43
+ /** passed down so that pdf generation has access to it */
44
+ client_url: string;
45
+ /** passed down so that pdf generation has access to it */
46
+ envConfig: EnvConfig;
47
+ }
48
+ export declare function processSubmission({ applicationId, instanceId, verbose, config, _supliedConfig, }: ProcessSubmissionInput): Promise<ProcessSubmissionOutput>;
49
+ export {};