|
| 1 | +import app from "../../sevdesk.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "sevdesk-get-invoices", |
| 5 | + name: "Get Invoices", |
| 6 | + description: "Retrieve invoices with optional filtering by status, invoice number, date range, and contact. [See the documentation](https://api.sevdesk.de/#tag/Invoice/operation/getInvoices)", |
| 7 | + version: "0.0.1", |
| 8 | + type: "action", |
| 9 | + props: { |
| 10 | + app, |
| 11 | + status: { |
| 12 | + type: "string", |
| 13 | + label: "Status", |
| 14 | + description: "Status of the invoices to filter by", |
| 15 | + options: [ |
| 16 | + { |
| 17 | + label: "Draft", |
| 18 | + value: "100", |
| 19 | + }, |
| 20 | + { |
| 21 | + label: "Open", |
| 22 | + value: "200", |
| 23 | + }, |
| 24 | + { |
| 25 | + label: "Paid", |
| 26 | + value: "1000", |
| 27 | + }, |
| 28 | + ], |
| 29 | + optional: true, |
| 30 | + }, |
| 31 | + invoiceNumber: { |
| 32 | + type: "string", |
| 33 | + label: "Invoice Number", |
| 34 | + description: "Retrieve all invoices with this invoice number", |
| 35 | + optional: true, |
| 36 | + }, |
| 37 | + startDate: { |
| 38 | + type: "string", |
| 39 | + label: "Start Date", |
| 40 | + description: "Retrieve all invoices with a date equal or higher (ISO 8601 format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS)", |
| 41 | + optional: true, |
| 42 | + }, |
| 43 | + endDate: { |
| 44 | + type: "string", |
| 45 | + label: "End Date", |
| 46 | + description: "Retrieve all invoices with a date equal or lower (ISO 8601 format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS)", |
| 47 | + optional: true, |
| 48 | + }, |
| 49 | + contactId: { |
| 50 | + propDefinition: [ |
| 51 | + app, |
| 52 | + "contactId", |
| 53 | + ], |
| 54 | + optional: true, |
| 55 | + }, |
| 56 | + limit: { |
| 57 | + type: "integer", |
| 58 | + label: "Limit", |
| 59 | + description: "Maximum number of invoices to retrieve", |
| 60 | + optional: true, |
| 61 | + min: 1, |
| 62 | + max: 999, |
| 63 | + }, |
| 64 | + }, |
| 65 | + methods: { |
| 66 | + convertDateToTimestamp(dateString) { |
| 67 | + if (!dateString) return undefined; |
| 68 | + |
| 69 | + const date = new Date(dateString); |
| 70 | + if (isNaN(date.getTime())) { |
| 71 | + throw new Error(`Invalid date format: ${dateString}. Please use ISO 8601 format (YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS)`); |
| 72 | + } |
| 73 | + |
| 74 | + return Math.floor(date.getTime() / 1000); |
| 75 | + }, |
| 76 | + }, |
| 77 | + async run({ $ }) { |
| 78 | + const { |
| 79 | + app, |
| 80 | + status, |
| 81 | + invoiceNumber, |
| 82 | + startDate, |
| 83 | + endDate, |
| 84 | + contactId, |
| 85 | + limit, |
| 86 | + } = this; |
| 87 | + |
| 88 | + let startTimestamp, endTimestamp; |
| 89 | + |
| 90 | + try { |
| 91 | + startTimestamp = this.convertDateToTimestamp(startDate); |
| 92 | + endTimestamp = this.convertDateToTimestamp(endDate); |
| 93 | + } catch (error) { |
| 94 | + throw new Error(`Date validation error: ${error.message}`); |
| 95 | + } |
| 96 | + |
| 97 | + if (startTimestamp && endTimestamp && startTimestamp > endTimestamp) { |
| 98 | + throw new Error("Start date cannot be later than end date"); |
| 99 | + } |
| 100 | + |
| 101 | + const response = await app.listInvoices({ |
| 102 | + $, |
| 103 | + params: { |
| 104 | + status, |
| 105 | + invoiceNumber, |
| 106 | + startDate: startTimestamp, |
| 107 | + endDate: endTimestamp, |
| 108 | + ...(contactId |
| 109 | + ? { |
| 110 | + "contact[id]": contactId, |
| 111 | + "contact[objectName]": "Contact", |
| 112 | + } |
| 113 | + : {} |
| 114 | + ), |
| 115 | + limit, |
| 116 | + }, |
| 117 | + }); |
| 118 | + |
| 119 | + $.export("$summary", `Successfully retrieved ${response.objects?.length || 0} invoice(s)`); |
| 120 | + return response; |
| 121 | + }, |
| 122 | +}; |
0 commit comments