-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: crud task according to workflow
- Loading branch information
1 parent
3d165b2
commit d2336d0
Showing
13 changed files
with
625 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fp from 'fastify-plugin'; | ||
import { MultiOriginTransition, Transition, Workflow } from '@jean-michelet/workflow'; | ||
import { Task, TaskStatus, TaskTransitions } from '../../schemas/tasks.js'; | ||
|
||
declare module "fastify" { | ||
export interface FastifyInstance { | ||
taskWorkflow: ReturnType<typeof createTaskWorkflow>; | ||
} | ||
} | ||
|
||
function createTaskWorkflow() { | ||
const wf = new Workflow<Task>({ | ||
stateProperty: 'status' | ||
}) | ||
|
||
wf.addTransition(TaskTransitions.Start, new Transition(TaskStatus.New, TaskStatus.InProgress)); | ||
wf.addTransition(TaskTransitions.Complete, new Transition(TaskStatus.InProgress, TaskStatus.Completed)); | ||
wf.addTransition(TaskTransitions.Hold, new Transition(TaskStatus.InProgress, TaskStatus.OnHold)); | ||
wf.addTransition(TaskTransitions.Resume, new Transition(TaskStatus.OnHold, TaskStatus.InProgress)); | ||
wf.addTransition(TaskTransitions.Cancel, new MultiOriginTransition( | ||
[TaskStatus.New, TaskStatus.InProgress, TaskStatus.OnHold], | ||
TaskStatus.Canceled | ||
)); | ||
wf.addTransition(TaskTransitions.Archive, new Transition(TaskStatus.Completed, TaskStatus.Archived)); | ||
|
||
return wf | ||
} | ||
|
||
export default fp(async (fastify) => { | ||
fastify.decorate('taskWorkflow', createTaskWorkflow()); | ||
}, { | ||
name: 'workflow' | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,55 @@ | ||
import { Type } from "@sinclair/typebox"; | ||
import { Static, Type } from "@sinclair/typebox"; | ||
|
||
export const TaskTransitions = { | ||
Start: "start", | ||
Complete: "complete", | ||
Hold: "hold", | ||
Resume: "resume", | ||
Cancel: "cancel", | ||
Archive: "archive" | ||
} as const; | ||
|
||
export const TaskStatus = { | ||
New: 'new', | ||
InProgress: 'in-progress', | ||
OnHold: 'on-hold', | ||
Completed: 'completed', | ||
Canceled: 'canceled', | ||
Archived: 'archived' | ||
} as const; | ||
|
||
export type TaskStatusType = typeof TaskStatus[keyof typeof TaskStatus]; | ||
|
||
export const TaskSchema = Type.Object({ | ||
id: Type.Number(), | ||
name: Type.String() | ||
name: Type.String(), | ||
author_id: Type.Number(), | ||
assigned_user_id: Type.Optional(Type.Number()), | ||
status: Type.String(), | ||
created_at: Type.String({ format: "date-time" }), | ||
updated_at: Type.String({ format: "date-time" }) | ||
}); | ||
|
||
export interface Task extends Static<typeof TaskSchema> {} | ||
|
||
export const CreateTaskSchema = Type.Object({ | ||
name: Type.String(), | ||
author_id: Type.Number(), | ||
assigned_user_id: Type.Optional(Type.Number()) | ||
}); | ||
|
||
export const UpdateTaskSchema = Type.Object({ | ||
name: Type.Optional(Type.String()), | ||
assigned_user_id: Type.Optional(Type.Number()) | ||
}); | ||
|
||
export const PatchTaskTransitionSchema = Type.Object({ | ||
transition: Type.Union([ | ||
Type.Literal(TaskTransitions.Start), | ||
Type.Literal(TaskTransitions.Complete), | ||
Type.Literal(TaskTransitions.Hold), | ||
Type.Literal(TaskTransitions.Resume), | ||
Type.Literal(TaskTransitions.Cancel), | ||
Type.Literal(TaskTransitions.Archive) | ||
]) | ||
}); |
Oops, something went wrong.