Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add duration to getTask, addTask, quickAddTask and UpdateTask #242

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/testUtils/testDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
User,
Comment,
Attachment,
Duration,
} from '../types'

const DEFAULT_TASK_ID = '1234'
Expand Down Expand Up @@ -44,6 +45,11 @@ export const DEFAULT_DUE_DATE = {
date: DEFAULT_DATE,
}

export const DEFAULT_DURATION: Duration = {
amount: 10,
unit: 'minute',
}

export const INVALID_DUE_DATE = {
...DEFAULT_DUE_DATE,
isRecurring: 'false',
Expand All @@ -63,6 +69,7 @@ export const DEFAULT_QUICK_ADD_RESPONSE: QuickAddTaskResponse = {
checked: false,
addedAt: DEFAULT_DATE,
addedByUid: DEFAULT_CREATOR,
duration: DEFAULT_DURATION,
due: {
date: DEFAULT_DATE,
timezone: null,
Expand All @@ -89,6 +96,7 @@ export const DEFAULT_TASK: Task = {
due: DEFAULT_DUE_DATE,
assigneeId: DEFAULT_ASSIGNEE,
creatorId: DEFAULT_CREATOR,
duration: DEFAULT_DURATION,
}

export const INVALID_TASK = {
Expand All @@ -103,6 +111,7 @@ export const TASK_WITH_OPTIONALS_AS_NULL: Task = {
assignerId: null,
parentId: null,
sectionId: null,
duration: null,
}

export const DEFAULT_PROJECT: Project = {
Expand Down
19 changes: 19 additions & 0 deletions src/types/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export const Int = NumberRunType.withConstraint(
(n) => Number.isInteger(n) || `${n} is not a valid entity id. Should be a string`,
)

export const GreaterThanZero = NumberRunType.withConstraint(
(n) => n > 0 || 'Value should be greater than zero',
)

export type GreaterThanZero = Static<typeof GreaterThanZero>

export type TodoistEntity = {
id: string
}
Expand All @@ -40,6 +46,17 @@ export const DueDate = Record({

export type DueDate = Static<typeof DueDate>

export const Unit = Union(Literal('minute'), Literal('day'))

export type Unit = Static<typeof Unit>

export const Duration = Record({
amount: GreaterThanZero,
unit: Unit,
})
thales-fukuda marked this conversation as resolved.
Show resolved Hide resolved

export type Duration = Static<typeof Duration>

export const Task = Record({
id: String,
order: Int,
Expand All @@ -53,6 +70,7 @@ export const Task = Record({
createdAt: String,
url: String,
creatorId: String,
duration: Duration.Or(Null),
}).And(
Partial({
due: DueDate.Or(Null),
Expand Down Expand Up @@ -187,6 +205,7 @@ export type QuickAddTaskResponse = {
checked: boolean // completed
addedAt: string // created
addedByUid: string | null
duration: Duration | null
due: {
date: string
timezone: string | null
Expand Down
20 changes: 18 additions & 2 deletions src/types/requests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type AddTaskArgs = {
import type { GreaterThanZero, Unit } from './entities'

type AddTask = {
content: string
description?: string
projectId?: string
Expand All @@ -12,6 +14,8 @@ export type AddTaskArgs = {
dueDate?: string
dueDatetime?: string
assigneeId?: string
duration?: GreaterThanZero
durationUnit?: Unit
}

export type QuickAddTaskArgs = {
Expand All @@ -30,7 +34,7 @@ export type GetTasksArgs = {
ids?: string[]
}

export type UpdateTaskArgs = {
type UpdateTask = {
content?: string
description?: string
labels?: string[]
Expand All @@ -40,6 +44,8 @@ export type UpdateTaskArgs = {
dueDate?: string | null
dueDatetime?: string | null
assigneeId?: string | null
duration?: GreaterThanZero
durationUnit?: Unit
thales-fukuda marked this conversation as resolved.
Show resolved Hide resolved
}

export type ProjectViewStyle = 'list' | 'board'
Expand Down Expand Up @@ -125,3 +131,13 @@ export type RenameSharedLabelArgs = {
export type RemoveSharedLabelArgs = {
name: string
}

type RequireUnion<T, R extends keyof T = keyof T> =
| (Omit<T, R> & Required<Pick<T, R>>)
| (Omit<T, R> & Partial<Record<R, never>>)
thales-fukuda marked this conversation as resolved.
Show resolved Hide resolved

type DurationUnion = 'duration' | 'durationUnit'

export type AddTaskArgs = RequireUnion<AddTask, DurationUnion>

export type UpdateTaskArgs = RequireUnion<UpdateTask, DurationUnion>
1 change: 1 addition & 0 deletions src/utils/taskConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function getTaskFromQuickAddResponse(responseData: QuickAddTaskResponse):
...(responseData.responsibleUid !== null && {
assigneeId: responseData.responsibleUid,
}),
duration: responseData.duration,
}

return task
Expand Down