Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/linear

src/filters.ts

41 lines · 1.5 KB · TypeScript
import type { JsonRecord, WorkflowStateType } from './types.ts'
import { compactRecord } from './types.ts'

export type IssueListFilterInput = {
	teamId?: string
	teamKey?: string
	projectId?: string
	assigneeId?: string
	stateId?: string
	stateType?: WorkflowStateType
	query?: string
}

export function buildIssueFilter(input: IssueListFilterInput): JsonRecord | undefined {
	const filter: JsonRecord = {}
	if (input.teamId) filter.team = { id: { eq: input.teamId } }
	else if (input.teamKey) filter.team = { key: { eq: input.teamKey } }
	if (input.projectId) filter.project = { id: { eq: input.projectId } }
	if (input.assigneeId === 'me') filter.assignee = { isMe: { eq: true } }
	else if (input.assigneeId) filter.assignee = { id: { eq: input.assigneeId } }
	if (input.stateId) filter.state = { id: { eq: input.stateId } }
	else if (input.stateType) filter.state = { type: { eq: input.stateType } }
	if (input.query) filter.title = { containsIgnoreCase: input.query }
	return Object.keys(filter).length > 0 ? filter : undefined
}

export function buildWorkflowStateFilter(input: {
	teamId?: string
	teamKey?: string
	type?: WorkflowStateType
}): JsonRecord | undefined {
	const filter: JsonRecord = {}
	if (input.teamId) filter.team = { id: { eq: input.teamId } }
	else if (input.teamKey) filter.team = { key: { eq: input.teamKey } }
	if (input.type) filter.type = { eq: input.type }
	return Object.keys(filter).length > 0 ? filter : undefined
}

export function omitUndefined<T extends JsonRecord>(value: T): T {
	return compactRecord(value)
}