|
| 1 | +import jobber from "../../jobber.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "jobber-search-quotes", |
| 5 | + name: "Search Quotes", |
| 6 | + description: "Search for quotes in Jobber with various filters. [See the documentation](https://developer.getjobber.com/docs/)", |
| 7 | + version: "0.0.1", |
| 8 | + type: "action", |
| 9 | + props: { |
| 10 | + jobber, |
| 11 | + clientId: { |
| 12 | + propDefinition: [ |
| 13 | + jobber, |
| 14 | + "clientId", |
| 15 | + ], |
| 16 | + optional: true, |
| 17 | + description: "Filter quotes by client ID", |
| 18 | + }, |
| 19 | + propertyId: { |
| 20 | + propDefinition: [ |
| 21 | + jobber, |
| 22 | + "propertyId", |
| 23 | + ], |
| 24 | + optional: true, |
| 25 | + description: "Filter quotes by property ID", |
| 26 | + }, |
| 27 | + quoteStatus: { |
| 28 | + type: "string", |
| 29 | + label: "Quote Status", |
| 30 | + description: "Filter quotes by status", |
| 31 | + optional: true, |
| 32 | + options: [ |
| 33 | + { |
| 34 | + label: "Draft", |
| 35 | + value: "DRAFT", |
| 36 | + }, |
| 37 | + { |
| 38 | + label: "Sent", |
| 39 | + value: "SENT", |
| 40 | + }, |
| 41 | + { |
| 42 | + label: "Accepted", |
| 43 | + value: "ACCEPTED", |
| 44 | + }, |
| 45 | + { |
| 46 | + label: "Declined", |
| 47 | + value: "DECLINED", |
| 48 | + }, |
| 49 | + { |
| 50 | + label: "Expired", |
| 51 | + value: "EXPIRED", |
| 52 | + }, |
| 53 | + { |
| 54 | + label: "Converted", |
| 55 | + value: "CONVERTED", |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + searchTerm: { |
| 60 | + type: "string", |
| 61 | + label: "Search Term", |
| 62 | + description: "Search in quote title, message, or quote number", |
| 63 | + optional: true, |
| 64 | + }, |
| 65 | + dateFrom: { |
| 66 | + type: "string", |
| 67 | + label: "Date From", |
| 68 | + description: "Filter quotes created from this date (YYYY-MM-DD)", |
| 69 | + optional: true, |
| 70 | + }, |
| 71 | + dateTo: { |
| 72 | + type: "string", |
| 73 | + label: "Date To", |
| 74 | + description: "Filter quotes created until this date (YYYY-MM-DD)", |
| 75 | + optional: true, |
| 76 | + }, |
| 77 | + limit: { |
| 78 | + type: "integer", |
| 79 | + label: "Limit", |
| 80 | + description: "Maximum number of quotes to return", |
| 81 | + default: 50, |
| 82 | + min: 1, |
| 83 | + max: 100, |
| 84 | + }, |
| 85 | + includeLineItems: { |
| 86 | + type: "boolean", |
| 87 | + label: "Include Line Items", |
| 88 | + description: "Include line items in the response", |
| 89 | + default: true, |
| 90 | + optional: true, |
| 91 | + }, |
| 92 | + includeNotes: { |
| 93 | + type: "boolean", |
| 94 | + label: "Include Notes", |
| 95 | + description: "Include notes in the response", |
| 96 | + default: false, |
| 97 | + optional: true, |
| 98 | + }, |
| 99 | + includeCustomFields: { |
| 100 | + type: "boolean", |
| 101 | + label: "Include Custom Fields", |
| 102 | + description: "Include custom fields in the response", |
| 103 | + default: false, |
| 104 | + optional: true, |
| 105 | + }, |
| 106 | + }, |
| 107 | + async run({ $ }) { |
| 108 | + const { |
| 109 | + clientId, |
| 110 | + propertyId, |
| 111 | + quoteStatus, |
| 112 | + searchTerm, |
| 113 | + dateFrom, |
| 114 | + dateTo, |
| 115 | + limit, |
| 116 | + includeLineItems, |
| 117 | + includeNotes, |
| 118 | + includeCustomFields, |
| 119 | + } = this; |
| 120 | + |
| 121 | + // Build filter conditions |
| 122 | + const filters = []; |
| 123 | + |
| 124 | + if (clientId) { |
| 125 | + filters.push(`clientId: "${clientId}"`); |
| 126 | + } |
| 127 | + |
| 128 | + if (propertyId) { |
| 129 | + filters.push(`propertyId: "${propertyId}"`); |
| 130 | + } |
| 131 | + |
| 132 | + if (quoteStatus) { |
| 133 | + filters.push(`quoteStatus: ${quoteStatus}`); |
| 134 | + } |
| 135 | + |
| 136 | + if (searchTerm) { |
| 137 | + filters.push(`searchTerm: "${searchTerm}"`); |
| 138 | + } |
| 139 | + |
| 140 | + if (dateFrom) { |
| 141 | + filters.push(`createdAtGte: "${dateFrom}"`); |
| 142 | + } |
| 143 | + |
| 144 | + if (dateTo) { |
| 145 | + filters.push(`createdAtLte: "${dateTo}"`); |
| 146 | + } |
| 147 | + |
| 148 | + const filterString = filters.length > 0 |
| 149 | + ? `(filter: {${filters.join(", ")}})` |
| 150 | + : ""; |
| 151 | + |
| 152 | + // Build the GraphQL query |
| 153 | + let query = `query SearchQuotes { |
| 154 | + quotes${filterString}(first: ${limit}) { |
| 155 | + nodes { |
| 156 | + id |
| 157 | + quoteNumber |
| 158 | + title |
| 159 | + message |
| 160 | + quoteStatus |
| 161 | + previewUrl |
| 162 | + jobberWebUri |
| 163 | + createdAt |
| 164 | + updatedAt |
| 165 | + transitionedAt |
| 166 | + clientHubViewedAt |
| 167 | + client { |
| 168 | + id |
| 169 | + firstName |
| 170 | + lastName |
| 171 | + companyName |
| 172 | + } |
| 173 | + property { |
| 174 | + id |
| 175 | + address { |
| 176 | + street |
| 177 | + city |
| 178 | + state |
| 179 | + zip |
| 180 | + } |
| 181 | + } |
| 182 | + amounts { |
| 183 | + subtotal |
| 184 | + tax |
| 185 | + total |
| 186 | + }`; |
| 187 | + |
| 188 | + if (includeLineItems) { |
| 189 | + query += ` |
| 190 | + lineItems { |
| 191 | + nodes { |
| 192 | + id |
| 193 | + name |
| 194 | + description |
| 195 | + quantity |
| 196 | + unitPrice |
| 197 | + total |
| 198 | + } |
| 199 | + }`; |
| 200 | + } |
| 201 | + |
| 202 | + if (includeNotes) { |
| 203 | + query += ` |
| 204 | + notes { |
| 205 | + nodes { |
| 206 | + id |
| 207 | + body |
| 208 | + createdAt |
| 209 | + } |
| 210 | + }`; |
| 211 | + } |
| 212 | + |
| 213 | + if (includeCustomFields) { |
| 214 | + query += ` |
| 215 | + customFields { |
| 216 | + id |
| 217 | + name |
| 218 | + value |
| 219 | + }`; |
| 220 | + } |
| 221 | + |
| 222 | + query += ` |
| 223 | + } |
| 224 | + pageInfo { |
| 225 | + hasNextPage |
| 226 | + endCursor |
| 227 | + } |
| 228 | + } |
| 229 | + }`; |
| 230 | + |
| 231 | + const response = await this.jobber.post({ |
| 232 | + $, |
| 233 | + data: { |
| 234 | + query, |
| 235 | + operationName: "SearchQuotes", |
| 236 | + }, |
| 237 | + }); |
| 238 | + |
| 239 | + if (response.errors) { |
| 240 | + throw new Error(response.errors[0].message); |
| 241 | + } |
| 242 | + |
| 243 | + const quotes = response.data.quotes.nodes; |
| 244 | + const count = quotes.length; |
| 245 | + |
| 246 | + $.export("$summary", `Found ${count} quote${count !== 1 |
| 247 | + ? "s" |
| 248 | + : ""}`); |
| 249 | + return response.data.quotes; |
| 250 | + }, |
| 251 | +}; |
0 commit comments