Skip to content

Commit d1efda3

Browse files
committed
[ACTION] Expensify | Create Report
1 parent a08f3ca commit d1efda3

File tree

6 files changed

+253
-11
lines changed

6 files changed

+253
-11
lines changed

components/expensify/actions/create-expense/create-expense.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import expensify from "../../app/expensify.app";
33

44
export default defineAction({
55
key: "expensify-create-expense",
6-
version: "0.0.2",
6+
version: "0.0.3",
77
name: "Create Expense",
88
description: "Creates a new expense. [See docs here](https://integrations.expensify.com/Integration-Server/doc/#expense-creator)",
99
type: "action",
1010
props: {
1111
expensify,
1212
employeeEmail: {
13-
label: "Employee Email",
14-
description: "The expenses will be created in this account.",
15-
type: "string",
13+
propDefinition: [
14+
expensify,
15+
"employeeEmail",
16+
],
1617
},
1718
currency: {
1819
label: "Currency",
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { defineAction } from "@pipedream/types";
2+
import app from "../../app/expensify.app";
3+
import utils from "../../common/utils";
4+
5+
export default defineAction({
6+
key: "expensify-create-report",
7+
version: "0.0.1",
8+
name: "Create Report",
9+
description: "Creates a new report with transactions in a user's account. [See docs here](https://integrations.expensify.com/Integration-Server/doc/#report-creator)",
10+
type: "action",
11+
props: {
12+
app,
13+
employeeEmail: {
14+
description: "The report will be created in this account.",
15+
propDefinition: [
16+
app,
17+
"employeeEmail",
18+
],
19+
},
20+
policyId: {
21+
propDefinition: [
22+
app,
23+
"policyId",
24+
({ employeeEmail }) => ({
25+
userEmail: employeeEmail,
26+
}),
27+
],
28+
},
29+
reportTitle: {
30+
label: "Report Title",
31+
description: "The title of the report that will be created.",
32+
type: "string",
33+
},
34+
expenses: {
35+
type: "string[]",
36+
label: "Expenses",
37+
description: `Array of expense objects to include in the report. Each expense should be a JSON object with the following required fields:
38+
39+
- \`date\`: The date the expense was made (format yyyy-mm-dd)
40+
- \`currency\`: Three-letter currency code (e.g., "USD", "EUR", "CAD")
41+
- \`merchant\`: The name of the merchant
42+
- \`amount\`: The amount in cents (e.g., 2500 for $25.00)
43+
44+
**Example:**
45+
\`\`\`json
46+
[
47+
{
48+
"date": "2024-01-15",
49+
"currency": "USD",
50+
"merchant": "Hotel ABC",
51+
"amount": 15000
52+
},
53+
{
54+
"date": "2024-01-16",
55+
"currency": "USD",
56+
"merchant": "Restaurant XYZ",
57+
"amount": 5000
58+
}
59+
]
60+
\`\`\``,
61+
},
62+
reportFields: {
63+
type: "object",
64+
label: "Report Fields",
65+
description: `Custom fields for the report as a JSON object. Use this to set values for custom report fields in your Expensify policy.
66+
67+
- \`Key format\`: Field names should have all non-alphanumerical characters replaced with underscores (_)
68+
- \`Value format\`: String values for the corresponding field
69+
70+
**Example:**
71+
\`\`\`json
72+
{
73+
"reason_of_trip": "Business meetings with clients",
74+
"employees": "3",
75+
"department": "Sales",
76+
"project_code": "PROJ_2024_001"
77+
}
78+
\`\`\``,
79+
optional: true,
80+
},
81+
},
82+
async run({ $ }) {
83+
const {
84+
policyId,
85+
employeeEmail,
86+
reportTitle,
87+
reportFields,
88+
expenses,
89+
} = this;
90+
91+
const response = await this.app.createReport({
92+
$,
93+
data: {
94+
employeeEmail,
95+
policyID: policyId,
96+
report: {
97+
title: reportTitle,
98+
...(reportFields && {
99+
fields: utils.parseJson(reportFields),
100+
}),
101+
},
102+
expenses: utils.parseArray(expenses),
103+
},
104+
});
105+
106+
$.export("$summary", `Successfully created report \`${response.reportName}\` with ID \`${response.reportID}\``);
107+
108+
return response;
109+
},
110+
});
111+

components/expensify/actions/export-report-to-pdf/export-report-to-pdf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from "fs";
44

55
export default defineAction({
66
key: "expensify-export-report-to-pdf",
7-
version: "0.0.2",
7+
version: "0.0.3",
88
name: "Export Report To PDF",
99
description: "Export a report to PDF. [See docs here](https://integrations.expensify.com/Integration-Server/doc/#report-exporter)",
1010
type: "action",

components/expensify/app/expensify.app.ts

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,29 @@ import qs from "qs";
55
export default defineApp({
66
type: "app",
77
app: "expensify",
8-
propDefinitions: {},
8+
propDefinitions: {
9+
employeeEmail: {
10+
type: "string",
11+
label: "Employee Email",
12+
description: "The expenses will be created in this account.",
13+
},
14+
policyId: {
15+
type: "string",
16+
label: "Policy ID",
17+
description: "Select the policy where the report will be created.",
18+
async options({ userEmail }) {
19+
const { policyList } = await this.getPolicyList({
20+
userEmail,
21+
});
22+
return policyList?.map(({
23+
id: value, name: label,
24+
}) => ({
25+
label,
26+
value,
27+
})) || [];
28+
},
29+
},
30+
},
931
methods: {
1032
_partnerUserId() {
1133
return this.$auth.partnerUserId;
@@ -17,20 +39,31 @@ export default defineApp({
1739
return "https://integrations.expensify.com/Integration-Server/ExpensifyIntegrations";
1840
},
1941
async _makeRequest(options: any = {}, $ = this) {
20-
return axios($, {
42+
const {
43+
extraFormUrlencodedData,
44+
data,
45+
...rest
46+
} = options;
47+
const response = await axios($, {
2148
url: `${this._apiUrl()}`,
22-
...options,
49+
...rest,
2350
data: qs.stringify({
2451
requestJobDescription: JSON.stringify({
2552
credentials: {
2653
partnerUserID: this._partnerUserId(),
2754
partnerUserSecret: this._partnerUserSecret(),
2855
},
29-
...options?.data,
56+
...data,
3057
}),
31-
...options?.extraFormUrlencodedData,
58+
...extraFormUrlencodedData,
3259
}),
3360
});
61+
62+
if (response.responseCode < 200 || response.responseCode >= 300) {
63+
throw new Error(JSON.stringify(response, null, 2));
64+
}
65+
66+
return response;
3467
},
3568
async createExpense({
3669
$, data,
@@ -46,6 +79,35 @@ export default defineApp({
4679
},
4780
}, $);
4881
},
82+
async createReport({
83+
$, data,
84+
}) {
85+
return this._makeRequest({
86+
method: "post",
87+
data: {
88+
type: "create",
89+
inputSettings: {
90+
type: "report",
91+
...data,
92+
},
93+
},
94+
}, $);
95+
},
96+
async getPolicyList({
97+
$, userEmail, adminOnly = true,
98+
}) {
99+
return this._makeRequest({
100+
method: "post",
101+
data: {
102+
type: "get",
103+
inputSettings: {
104+
type: "policyList",
105+
adminOnly,
106+
...(userEmail && { userEmail }),
107+
},
108+
},
109+
}, $);
110+
},
49111
async updateCustomer({
50112
$, data,
51113
}) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const parseJson = (input, maxDepth = 100) => {
2+
const seen = new WeakSet();
3+
const parse = (value) => {
4+
if (maxDepth <= 0) {
5+
return value;
6+
}
7+
if (typeof(value) === "string") {
8+
// Only parse if the string looks like a JSON object or array
9+
const trimmed = value.trim();
10+
if (
11+
(trimmed.startsWith("{") && trimmed.endsWith("}")) ||
12+
(trimmed.startsWith("[") && trimmed.endsWith("]"))
13+
) {
14+
try {
15+
return parseJson(JSON.parse(value), maxDepth - 1);
16+
} catch (e) {
17+
return value;
18+
}
19+
}
20+
return value;
21+
} else if (typeof(value) === "object" && value !== null && !Array.isArray(value)) {
22+
if (seen.has(value)) {
23+
return value;
24+
}
25+
seen.add(value);
26+
return Object.entries(value)
27+
.reduce((acc, [
28+
key,
29+
val,
30+
]) => Object.assign(acc, {
31+
[key]: parse(val),
32+
}), {});
33+
} else if (Array.isArray(value)) {
34+
return value.map((item) => parse(item));
35+
}
36+
return value;
37+
};
38+
39+
return parse(input);
40+
};
41+
42+
function parseArray (input, maxDepth = 100) {
43+
if (typeof input === "string") {
44+
const trimmed = input.trim();
45+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
46+
try {
47+
const parsed = JSON.parse(trimmed);
48+
if (Array.isArray(parsed)) {
49+
return parsed.map((item) => parseArray(item, maxDepth - 1));
50+
}
51+
} catch (e) {
52+
throw new Error(`Invalid JSON array format: ${e.message}`);
53+
}
54+
}
55+
return parseJson(input, maxDepth);
56+
}
57+
58+
if (Array.isArray(input)) {
59+
return input.map((item) => parseArray(item, maxDepth));
60+
}
61+
62+
return input;
63+
}
64+
65+
export default {
66+
parseJson,
67+
parseArray,
68+
};

components/expensify/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/expensify",
3-
"version": "0.0.4",
3+
"version": "0.1.0",
44
"description": "Pipedream Expensify Components",
55
"main": "dist/app/expensify.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)