Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | 51x 51x 49x 2x 37x 18x 90x 25x 22x 3x 2x 5x 2x 18x 18x 18x 37x 19x 1x 18x 18x 3x 1x 2x 2x 2x 2x 11x 4x 1x 3x 1x 2x 2x 2x 15x 1x 14x 1x 13x 13x 13x 7x 1x 6x 6x 6x 1x 5x 1x 4x 4x 4x 4x 12x 11x 1x 9x 9x 9x 1x | import { TaskId } from '../valueObjects/TaskId';
import { TaskTitle } from '../valueObjects/TaskTitle';
import { TaskStatus } from '../valueObjects/TaskStatus';
import { TaskPriority } from '../valueObjects/TaskPriority';
import { TaskAssignment } from '../valueObjects/TaskAssignment';
import { TaskDependency } from '../valueObjects/TaskDependency';
import { UserId } from '../valueObjects/UserId';
import { DomainEvent } from '../events/DomainEvent';
import { TaskCompletedEvent } from '../events/TaskCompletedEvent';
import { TaskAssignedEvent } from '../events/TaskAssignedEvent';
import { TaskPriorityEscalatedEvent } from '../events/TaskPriorityEscalatedEvent';
import { TaskReopenedEvent } from '../events/TaskReopenedEvent';
interface TaskProps {
id: TaskId;
title: TaskTitle;
status: TaskStatus;
priority: TaskPriority;
assignment?: TaskAssignment;
dependencies: TaskDependency[];
createdAt: Date;
completedAt?: Date;
dueDate?: Date;
}
export class Task {
private domainEvents: DomainEvent[] = [];
private constructor(private props: TaskProps) {}
static create(
title: TaskTitle,
priority: TaskPriority = TaskPriority.medium(),
dueDate?: Date
): Task {
return new Task({
id: TaskId.create(),
title,
status: TaskStatus.todo(),
priority,
dependencies: [],
createdAt: new Date(),
dueDate
});
}
static reconstitute(props: TaskProps): Task {
return new Task(props);
}
get id(): TaskId {
return this.props.id;
}
get title(): TaskTitle {
return this.props.title;
}
get status(): TaskStatus {
return this.props.status;
}
get priority(): TaskPriority {
return this.props.priority;
}
get assignment(): TaskAssignment | undefined {
return this.props.assignment;
}
get dependencies(): ReadonlyArray<TaskDependency> {
return this.props.dependencies;
}
get createdAt(): Date {
return this.props.createdAt;
}
get completedAt(): Date | undefined {
return this.props.completedAt;
}
get dueDate(): Date | undefined {
return this.props.dueDate;
}
pullDomainEvents(): DomainEvent[] {
const events = [...this.domainEvents];
this.domainEvents = [];
return events;
}
private addDomainEvent(event: DomainEvent): void {
this.domainEvents.push(event);
}
updateTitle(newTitle: TaskTitle, userId: UserId): void {
if (this.props.status.isDone()) {
throw new Error('Cannot update title of completed task');
}
if (this.props.assignment && !this.props.assignment.isAssignedTo(userId)) {
throw new Error('Only assigned user can update task title');
}
this.props.title = newTitle;
}
assignTo(userId: UserId, assignedBy: UserId): void {
if (this.props.status.isDone()) {
throw new Error('Cannot assign completed task');
}
this.props.assignment = TaskAssignment.create(userId, assignedBy);
this.addDomainEvent(new TaskAssignedEvent(this.props.id, userId, assignedBy));
}
unassign(): void {
if (!this.props.assignment) {
throw new Error('Task is not assigned');
}
this.props.assignment = undefined;
}
changePriority(newPriority: TaskPriority, userId: UserId): void {
if (this.props.status.isDone()) {
throw new Error('Cannot change priority of completed task');
}
const oldPriority = this.props.priority;
this.props.priority = newPriority;
Eif (newPriority.isHigherThan(oldPriority)) {
this.addDomainEvent(new TaskPriorityEscalatedEvent(this.props.id, oldPriority, newPriority));
}
}
checkAndEscalatePriority(): void {
const ageInDays = this.getAgeInDays();
if (this.props.priority.shouldEscalate(ageInDays)) {
const oldPriority = this.props.priority;
this.props.priority = this.props.priority.escalate();
this.addDomainEvent(
new TaskPriorityEscalatedEvent(this.props.id, oldPriority, this.props.priority)
);
}
}
addDependency(dependency: TaskDependency): void {
const exists = this.props.dependencies.some(
d => d.getDependentTaskId().equals(dependency.getDependentTaskId())
);
if (exists) {
throw new Error('Dependency already exists');
}
if (dependency.getDependentTaskId().equals(this.props.id)) {
throw new Error('Task cannot depend on itself');
}
this.props.dependencies.push(dependency);
}
removeDependency(dependentTaskId: TaskId): void {
this.props.dependencies = this.props.dependencies.filter(
d => !d.getDependentTaskId().equals(dependentTaskId)
);
}
hasBlockingDependencies(): boolean {
return this.props.dependencies.some(d => d.isBlocking());
}
getBlockingDependencies(): TaskDependency[] {
return this.props.dependencies.filter(d => d.isBlocking());
}
startProgress(userId: UserId): void {
if (!this.props.status.isTodo()) {
throw new Error('Can only start tasks that are in TODO status');
}
if (!this.props.assignment) {
throw new Error('Task must be assigned before starting');
}
Iif (!this.props.assignment.isAssignedTo(userId)) {
throw new Error('Only assigned user can start this task');
}
Iif (this.hasBlockingDependencies()) {
throw new Error('Cannot start task: blocked by dependencies');
}
this.props.status = TaskStatus.inProgress();
}
complete(userId: UserId): void {
if (this.props.status.isDone()) {
throw new Error('Task is already completed');
}
if (this.props.assignment && !this.props.assignment.isAssignedTo(userId)) {
throw new Error('Only assigned user can complete this task');
}
this.props.status = TaskStatus.done();
this.props.completedAt = new Date();
this.addDomainEvent(new TaskCompletedEvent(
this.props.id,
userId,
this.props.completedAt
));
}
reopen(userId: UserId): void {
if (!this.props.status.isDone()) {
throw new Error('Can only reopen completed tasks');
}
Iif (!this.props.completedAt) {
throw new Error('Task has no completion date');
}
// Check if task was completed within 24 hours
const hoursSinceCompletion = (new Date().getTime() - this.props.completedAt.getTime()) / (1000 * 60 * 60);
if (hoursSinceCompletion > 24) {
throw new Error('Can only reopen tasks completed within 24 hours');
}
// Check if user is the original task owner
if (this.props.assignment && !this.props.assignment.isAssignedTo(userId)) {
throw new Error('Only the original task owner can reopen this task');
}
const reopenedAt = new Date();
this.props.status = TaskStatus.todo();
this.props.completedAt = undefined;
this.addDomainEvent(new TaskReopenedEvent(
this.props.id,
userId,
reopenedAt
));
}
isOverdue(): boolean {
if (!this.props.dueDate || this.props.status.isDone()) {
return false;
}
return new Date() > this.props.dueDate;
}
getAgeInDays(): number {
const now = new Date();
const diffTime = Math.abs(now.getTime() - this.props.createdAt.getTime());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
isAssignedTo(userId: UserId): boolean {
return this.props.assignment?.isAssignedTo(userId) ?? false;
}
toDTO() {
return {
id: this.props.id.toString(),
title: this.props.title.toString(),
status: this.props.status.toString(),
priority: this.props.priority.toJSON(),
assignment: this.props.assignment?.toJSON(),
dependencies: this.props.dependencies.map(d => d.toJSON()),
createdAt: this.props.createdAt.toISOString(),
completedAt: this.props.completedAt?.toISOString(),
dueDate: this.props.dueDate?.toISOString(),
isOverdue: this.isOverdue(),
ageInDays: this.getAgeInDays()
};
}
}
|