Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions backend/src/modules/checklist/core/model/CheckList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import IdEntity from '../../../shared/core/objects/IdEntity';
import Text from '../../../shared/core/objects/Text';
import Version from '../../../shared/core/objects/Version';
import DeletedAt from '../../../shared/core/objects/DeletedAt';
import Decimal from '../../../shared/core/objects/Decimal';
import type CheckListParams from '../interfaces/CheckListParams';
import ChecklistItem from '../objects/ChecklistItem';
import IdCheckList from '../objects/IdCheckList';
Expand All @@ -17,13 +16,14 @@ import ChecklistItemCompleted from '../events/ChecklistItemCompleted';
import ChecklistItemMarkedAsPending from '../events/ChecklistItemMarkedAsPending';
import ChecklistItemTitleUpdated from '../events/ChecklistItemTitleUpdated';
import ResourceNotFound from '../../../shared/core/errors/ResourceNotFound';
import PercentageCompleted from '../objects/PercentageCompleted';

export default class CheckList extends Entity {
private name!: Text;
private items: ChecklistItem[] = [];
private completedPercentage!: Decimal;
private completedPercentage!: PercentageCompleted;

private constructor(id: IdCheckList, owner: IdEntity, name: Text, items: ChecklistItem[], completedPercentage: Decimal) {
private constructor(id: IdCheckList, owner: IdEntity, name: Text, items: ChecklistItem[], completedPercentage: PercentageCompleted) {
super(id, owner);
this.name = name;
this.items = items;
Expand All @@ -38,7 +38,7 @@ export default class CheckList extends Entity {
key: string,
items: ChecklistItem[] = [],
): CheckList {
const checklist = new CheckList(id, owner, name, items, new Decimal(0));
const checklist = new CheckList(id, owner, name, items, new PercentageCompleted(0));
checklist.create();
checklist.recalculateCompletedPercentage();
checklist.addEvent(new CheckListCreated(key, DateTime.now(), actor, checklist.getOwnerId(), id, checklist.toPrimitives()));
Expand All @@ -52,7 +52,7 @@ export default class CheckList extends Entity {
new IdEntity(params.idOwner),
new Text(params.name),
items,
new Decimal(params.completedPercentage),
new PercentageCompleted(params.completedPercentage),
);

checklist.build(new Version(params.version), DeletedAt.createFromPrimitive(params.deletedAt));
Expand All @@ -67,7 +67,7 @@ export default class CheckList extends Entity {
return [...this.items];
}

public getCompletedPercentage(): Decimal {
public getCompletedPercentage(): PercentageCompleted {
return this.completedPercentage;
}

Expand Down Expand Up @@ -155,12 +155,12 @@ export default class CheckList extends Entity {

private recalculateCompletedPercentage(): void {
if (this.items.length === 0) {
this.completedPercentage = new Decimal(0);
this.completedPercentage = new PercentageCompleted(0);
return;
}

const completedCount = this.items.filter((item) => item.isCompleted()).length;
const percent = (completedCount / this.items.length) * 100;
this.completedPercentage = new Decimal(percent);
this.completedPercentage = new PercentageCompleted(percent);
}
}
23 changes: 23 additions & 0 deletions backend/src/modules/checklist/core/objects/PercentageCompleted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import InvalidParameters from '../../../shared/core/errors/InvalidParameters';
import Decimal from '../../../shared/core/objects/Decimal';
import ValueObject from '../../../shared/core/objects/ValueObject';

export default class PercentageCompleted extends ValueObject {
private value!: Decimal;

constructor(value: number) {
super();
this.value = new Decimal(value);
if (this.value.getValue() < 0 || this.value.getValue() > 100) {
throw new InvalidParameters('Value must be in 0 to 100 range', { value });
}
}

public getValue(): number {
return this.value.getValue();
}

public toPrimitive(): number {
return this.value.toPrimitive();
}
}
Loading