|
| 1 | +import { Bundle, ZObject } from "zapier-platform-core"; |
| 2 | +import { fetchFromLinear } from "../fetchFromLinear"; |
| 3 | +import { omitBy } from "lodash"; |
| 4 | + |
| 5 | +interface CustomerNeedCreateResponse { |
| 6 | + data?: { customerNeedCreate: { need: { id: string; customerId?: string, issueId?: string, attachmentId?: string }; success: boolean } }; |
| 7 | + errors?: { |
| 8 | + message: string; |
| 9 | + extensions?: { |
| 10 | + userPresentableMessage?: string; |
| 11 | + }; |
| 12 | + }[]; |
| 13 | +} |
| 14 | + |
| 15 | +const createCustomerNeedRequest = async (z: ZObject, bundle: Bundle) => { |
| 16 | + const variables = omitBy( |
| 17 | + { |
| 18 | + customerId: bundle.inputData.customerId, |
| 19 | + customerExternalId: bundle.inputData.customerExternalId, |
| 20 | + issueId: bundle.inputData.issueId, |
| 21 | + attachmentId: bundle.inputData.attachmentId, |
| 22 | + attachmentUrl: bundle.inputData.attachmentUrl, |
| 23 | + body: bundle.inputData.body, |
| 24 | + priority: bundle.inputData.priority, |
| 25 | + }, |
| 26 | + (v) => v === undefined |
| 27 | + ); |
| 28 | + |
| 29 | + if(variables.attachmentId && variables.attachmentUrl) { |
| 30 | + throw new Error ("Cannot specify both attachmentId and attachmentUrl"); |
| 31 | + } else if (variables.customerId && variables.customerExternalId) { |
| 32 | + throw new Error ("Cannot specify both customerId and customerExternalId"); |
| 33 | + } |
| 34 | + |
| 35 | + const query = ` |
| 36 | + mutation ZapierCustomerNeedCreate( |
| 37 | + $customerId: String, |
| 38 | + $customerExternalId: String, |
| 39 | + $issueId: String, |
| 40 | + $attachmentId: String, |
| 41 | + $attachmentUrl: String, |
| 42 | + $body: String, |
| 43 | + $priority: Float, |
| 44 | + ) { |
| 45 | + customerNeedCreate(input: { |
| 46 | + customerId: $customerId, |
| 47 | + customerExternalId: $customerExternalId, |
| 48 | + issueId: $issueId, |
| 49 | + attachmentId: $attachmentId, |
| 50 | + attachmentUrl: $attachmentUrl, |
| 51 | + body: $body, |
| 52 | + priority: $priority, |
| 53 | + }) { |
| 54 | + need { |
| 55 | + id |
| 56 | + customer { |
| 57 | + id |
| 58 | + } |
| 59 | + issue { |
| 60 | + id |
| 61 | + } |
| 62 | + attachment { |
| 63 | + id |
| 64 | + } |
| 65 | + } |
| 66 | + success |
| 67 | + } |
| 68 | + }`; |
| 69 | + |
| 70 | + const response = await fetchFromLinear(z, bundle, query, variables); |
| 71 | + const data = response.json as CustomerNeedCreateResponse; |
| 72 | + |
| 73 | + if (data.errors && data.errors.length) { |
| 74 | + const error = data.errors[0]; |
| 75 | + throw new z.errors.Error( |
| 76 | + (error.extensions && error.extensions.userPresentableMessage) || error.message, |
| 77 | + "invalid_input", |
| 78 | + 400 |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + if (data.data && data.data.customerNeedCreate && data.data.customerNeedCreate.success) { |
| 83 | + return data.data.customerNeedCreate.need; |
| 84 | + } else { |
| 85 | + const error = data.errors ? data.errors[0].message : "Something went wrong"; |
| 86 | + throw new z.errors.Error("Failed to create a customer need", error, 400); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +export const createCustomerNeed = { |
| 91 | + key: "createCustomerNeed", |
| 92 | + display: { |
| 93 | + hidden: false, |
| 94 | + description: "Create a new customer need in Linear", |
| 95 | + label: "Create Customer Need", |
| 96 | + }, |
| 97 | + noun: "Customer Need", |
| 98 | + operation: { |
| 99 | + perform: createCustomerNeedRequest, |
| 100 | + inputFields: [ |
| 101 | + { |
| 102 | + required: false, |
| 103 | + label: "Customer ID", |
| 104 | + helpText: "The ID of the customer to create the need for", |
| 105 | + key: "customerId", |
| 106 | + }, |
| 107 | + { |
| 108 | + required: false, |
| 109 | + label: "External Customer ID", |
| 110 | + helpText: "The external ID of the customer the need belongs to", |
| 111 | + key: "customerExternalId", |
| 112 | + }, |
| 113 | + { |
| 114 | + required: false, |
| 115 | + label: "Issue ID", |
| 116 | + helpText: "The ID of the issue this need is for", |
| 117 | + key: "issueId", |
| 118 | + type: "text", |
| 119 | + }, |
| 120 | + { |
| 121 | + required: false, |
| 122 | + label: "Attachment ID", |
| 123 | + helpText: "The ID of the attachment this need is associated with", |
| 124 | + key: "attachmentId", |
| 125 | + type: "text", |
| 126 | + }, |
| 127 | + { |
| 128 | + required: false, |
| 129 | + label: "Attachment URL", |
| 130 | + helpText: "Optional URL for the attachment associated with the customer need", |
| 131 | + key: "attachmentUrl", |
| 132 | + type: "text", |
| 133 | + }, |
| 134 | + { |
| 135 | + required: false, |
| 136 | + label: "Body", |
| 137 | + helpText: "The content of the need in markdown format.", |
| 138 | + key: "body", |
| 139 | + type: "text", |
| 140 | + }, |
| 141 | + { |
| 142 | + required: false, |
| 143 | + label: "Priority", |
| 144 | + helpText: "Whether the customer need is important or not. 0 = Not important, 1 = Important.", |
| 145 | + key: "priority", |
| 146 | + type: "number", |
| 147 | + }, |
| 148 | + ], |
| 149 | + sample: { |
| 150 | + data: { |
| 151 | + customerNeedCreate: { |
| 152 | + need: { id: "93a02c29-da90-4d06-ab1c-96956e94bcd0", customerId: "6465f500-6626-4253-9073-144535a6c658", issueId: "a8ea3bfa-5420-492a-84e9-ffe49ca5f22a" }, |
| 153 | + success: true, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | +}; |
0 commit comments