Skip to content

Commit

Permalink
fix(clickup): Return custom fields in a Map, query by id (#23)
Browse files Browse the repository at this point in the history
* fix(clickup): Return custom fields in a Map
* support querying on just a particular Task by id
  • Loading branch information
zachelrath committed Nov 20, 2023
1 parent 8a5bbc0 commit 496ad1a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
52 changes: 45 additions & 7 deletions apps/clickup/bundle/bots/load/clickup_tasks_load/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ type TasksResponse = {
tasks: Record<string, FieldValue>[]
}

type TypeConfigOption = {
name: string
orderindex: number
}

type TypeConfig = {
options: TypeConfigOption[]
}

type CustomFieldResponse = {
id: string
name: string
value: FieldValue
type_config: TypeConfig
}

export default function clickup_tasks_load(bot: LoadBotApi) {
const { conditions, collectionMetadata, collection } = bot.loadRequest
const namespace = collection.split(".")[0]
Expand Down Expand Up @@ -43,6 +59,23 @@ export default function clickup_tasks_load(bot: LoadBotApi) {
}
const fieldMetadata =
collectionMetadata.getFieldMetadata(uesioName)
// Special handling for "custom_fields" - convert the list of custom field values
// into a map so that each field can be easily accessed by its name
if (
externalField === "custom_fields" &&
value &&
Array.isArray(value as CustomFieldResponse[])
) {
acc[uesioName] = (value as CustomFieldResponse[]).reduce(
(customFieldValues, customFieldResponse) => {
customFieldValues[customFieldResponse.id] =
customFieldResponse
return customFieldValues
},
{} as Record<string, FieldValue>
)
return acc
}
if (value && fieldMetadata) {
if (fieldMetadata.type === "TIMESTAMP") {
const dateVal = Date.parse(value as string)
Expand All @@ -60,8 +93,8 @@ export default function clickup_tasks_load(bot: LoadBotApi) {
},
{}
)
// List id must be provided by conditions
let listId
// Either List id or Task Id must be provided by conditions
let listId, taskId
const queryParams = ["archived=false"] as string[]
const buildQueryStringConditions = () => {
if (!conditions || !conditions.length) return
Expand All @@ -76,6 +109,11 @@ export default function clickup_tasks_load(bot: LoadBotApi) {
listId = condition.value
return
}
// Special case: "uesio/core.id", use this as the task id condition
if (field === "uesio/core.id") {
taskId = condition.value
return
}
if (value === null || value === undefined) return
const encodedValue = encodeURIComponent(value as string)
switch (condition.operator) {
Expand Down Expand Up @@ -105,15 +143,15 @@ export default function clickup_tasks_load(bot: LoadBotApi) {
}
buildQueryStringConditions()

if (!listId) {
if (!listId && !taskId) {
throw new Error(
"Clickup Tasks Load Bot requires a list id condition to be set"
"querying Clickup Tasks requires either a list id or task id condition to be set"
)
}

const url = `${bot.getIntegration().getBaseURL()}/list/${listId}/task${
queryParams.length ? "?" + queryParams.join("&") : ""
}`
const url = `${bot.getIntegration().getBaseURL()}/${
listId ? `list/${listId}/task` : `task/${taskId}`
}${queryParams.length ? "?" + queryParams.join("&") : ""}`

const result = bot.http.request({
method: "GET",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: custom_fields
public: true
type: STRUCT
label: List
type: MAP
subtype: STRUCT
label: Custom Fields
subfields:
- name: id
label: Field Id
Expand Down

0 comments on commit 496ad1a

Please sign in to comment.