- 
                Notifications
    
You must be signed in to change notification settings  - Fork 5.5k
 
[ACTION] Expensify | Create Report #18298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            111 changes: 111 additions & 0 deletions
          
          111 
        
  components/expensify/actions/create-report/create-report.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { defineAction } from "@pipedream/types"; | ||
| import app from "../../app/expensify.app"; | ||
| import utils from "../../common/utils"; | ||
| 
     | 
||
| export default defineAction({ | ||
| key: "expensify-create-report", | ||
| version: "0.0.1", | ||
| name: "Create Report", | ||
| description: "Creates a new report with transactions in a user's account. [See docs here](https://integrations.expensify.com/Integration-Server/doc/#report-creator)", | ||
| type: "action", | ||
| props: { | ||
| app, | ||
| employeeEmail: { | ||
| description: "The report will be created in this account.", | ||
| propDefinition: [ | ||
| app, | ||
| "employeeEmail", | ||
| ], | ||
| }, | ||
| policyId: { | ||
| propDefinition: [ | ||
| app, | ||
| "policyId", | ||
| ({ employeeEmail }) => ({ | ||
| userEmail: employeeEmail, | ||
| }), | ||
| ], | ||
| }, | ||
| reportTitle: { | ||
| label: "Report Title", | ||
| description: "The title of the report that will be created.", | ||
| type: "string", | ||
| }, | ||
| expenses: { | ||
| type: "string[]", | ||
| label: "Expenses", | ||
| description: `Array of expense objects to include in the report. Each expense should be a JSON object with the following required fields: | ||
| 
     | 
||
| - \`date\`: The date the expense was made (format yyyy-mm-dd) | ||
| - \`currency\`: Three-letter currency code (e.g., "USD", "EUR", "CAD") | ||
| - \`merchant\`: The name of the merchant | ||
| - \`amount\`: The amount in cents (e.g., 2500 for $25.00) | ||
| 
     | 
||
| **Example:** | ||
| \`\`\`json | ||
| [ | ||
| { | ||
| "date": "2024-01-15", | ||
| "currency": "USD", | ||
| "merchant": "Hotel ABC", | ||
| "amount": 15000 | ||
| }, | ||
| { | ||
| "date": "2024-01-16", | ||
| "currency": "USD", | ||
| "merchant": "Restaurant XYZ", | ||
| "amount": 5000 | ||
| } | ||
| ] | ||
| \`\`\``, | ||
| }, | ||
| reportFields: { | ||
| type: "object", | ||
| label: "Report Fields", | ||
| description: `Custom fields for the report as a JSON object. Use this to set values for custom report fields in your Expensify policy. | ||
| 
     | 
||
| - \`Key format\`: Field names should have all non-alphanumerical characters replaced with underscores (_) | ||
| - \`Value format\`: String values for the corresponding field | ||
| 
     | 
||
| **Example:** | ||
| \`\`\`json | ||
| { | ||
| "reason_of_trip": "Business meetings with clients", | ||
| "employees": "3", | ||
| "department": "Sales", | ||
| "project_code": "PROJ_2024_001" | ||
| } | ||
| \`\`\``, | ||
| optional: true, | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const { | ||
| policyId, | ||
| employeeEmail, | ||
| reportTitle, | ||
| reportFields, | ||
| expenses, | ||
| } = this; | ||
| 
     | 
||
| const response = await this.app.createReport({ | ||
| $, | ||
| data: { | ||
| employeeEmail, | ||
| policyID: policyId, | ||
| report: { | ||
| title: reportTitle, | ||
| ...(reportFields && { | ||
| fields: utils.parseJson(reportFields), | ||
| }), | ||
| }, | ||
| expenses: utils.parseArray(expenses), | ||
| }, | ||
| }); | ||
| 
     | 
||
| $.export("$summary", `Successfully created report \`${response.reportName}\` with ID \`${response.reportID}\``); | ||
| 
     | 
||
| return response; | ||
| }, | ||
| }); | ||
| 
     | 
||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| const parseJson = (input, maxDepth = 100) => { | ||
| const seen = new WeakSet(); | ||
| const parse = (value) => { | ||
| if (maxDepth <= 0) { | ||
| return value; | ||
| } | ||
| if (typeof(value) === "string") { | ||
| // Only parse if the string looks like a JSON object or array | ||
| const trimmed = value.trim(); | ||
| if ( | ||
| (trimmed.startsWith("{") && trimmed.endsWith("}")) || | ||
| (trimmed.startsWith("[") && trimmed.endsWith("]")) | ||
| ) { | ||
| try { | ||
| return parseJson(JSON.parse(value), maxDepth - 1); | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| } catch (e) { | ||
| return value; | ||
| } | ||
| } | ||
| return value; | ||
| } else if (typeof(value) === "object" && value !== null && !Array.isArray(value)) { | ||
| if (seen.has(value)) { | ||
| return value; | ||
| } | ||
| seen.add(value); | ||
| return Object.entries(value) | ||
| .reduce((acc, [ | ||
| key, | ||
| val, | ||
| ]) => Object.assign(acc, { | ||
| [key]: parse(val), | ||
| }), {}); | ||
| } else if (Array.isArray(value)) { | ||
| return value.map((item) => parse(item)); | ||
| } | ||
| return value; | ||
| }; | ||
| 
     | 
||
| return parse(input); | ||
| }; | ||
| 
     | 
||
| function parseArray (input, maxDepth = 100) { | ||
| if (typeof input === "string") { | ||
| const trimmed = input.trim(); | ||
| if (trimmed.startsWith("[") && trimmed.endsWith("]")) { | ||
| try { | ||
| const parsed = JSON.parse(trimmed); | ||
| if (Array.isArray(parsed)) { | ||
| return parsed.map((item) => parseArray(item, maxDepth - 1)); | ||
| } | ||
| } catch (e) { | ||
| throw new Error(`Invalid JSON array format: ${e.message}`); | ||
| } | ||
| } | ||
| return parseJson(input, maxDepth); | ||
| } | ||
| 
     | 
||
| if (Array.isArray(input)) { | ||
| return input.map((item) => parseArray(item, maxDepth)); | ||
| } | ||
| 
     | 
||
| return input; | ||
| } | ||
                
      
                  jcortes marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| export default { | ||
| parseJson, | ||
| parseArray, | ||
| }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.