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

Add contractItemArticleModel to apiTools #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 15 additions & 1 deletion packages/models/src/contract/ContractItem/ContractItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DataModel, ReferenceModel } from "../../base/index.js";
import { provideReact } from "../../react/index.js";
import { config } from "../../config/config.js";
import assertObjectFound from "../../base/assertObjectFound.js";
import { ContractItemArticleDetailed } from "../ContractItemArticle/index.js";

export class ContractItem extends ReferenceModel {
public static ofId(contractId: string, id: string): ContractItem {
Expand Down Expand Up @@ -44,11 +45,24 @@ export class ContractItem extends ReferenceModel {
}
}

export class ContractItemDetailed extends classes(
export class ContractItemCommon extends classes(
DataModel<ContractItemData>,
ContractItem,
) {
public constructor(contractId: string, data: ContractItemData) {
super([data], [contractId, data.itemId]);
}
}

export class ContractItemDetailed extends ContractItemCommon {
public readonly articles: ContractItemArticleDetailed[];

public constructor(contractId: string, data: ContractItemData) {
super(contractId, data);

this.articles = data.articles.map(
(articleData) =>
new ContractItemArticleDetailed(contractId, this.id, articleData),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { DataModel, ListDataModel, ReferenceModel } from "../../base/index.js";
import { ContractItemArticleData } from "./types.js";
import { classes } from "polytype";

export class ContractItemArticle extends ReferenceModel {
public static ofId(
id: string,
contractId: string,
contractItemId: string,
): ContractItemArticle {
return new ContractItemArticle(id, contractId, contractItemId);
}

public readonly contractId: string;
public readonly contractItemId: string;

public constructor(id: string, contractId: string, contractItemId: string) {
super(id);
this.contractId = contractId;
this.contractItemId = contractItemId;
}
}

class ContractItemArticleCommon extends classes(
DataModel<ContractItemArticleData>,
ContractItemArticle,
) {
public constructor(
contractId: string,
contractItemId: string,
data: ContractItemArticleData,
) {
super([data], [data.id, contractId, contractItemId]);
}
}

export class ContractItemArticleDetailed extends ContractItemArticleCommon {
public constructor(
contractId: string,
contractItemId: string,
data: ContractItemArticleData,
) {
super(contractId, contractItemId, data);
}

public get totalPrice(): number {
return this.data.amount * this.data.unitPrice.value;
}

public get displayName(): string {
return `${this.data.name} (${this.data.description})`;
}
}

export class ContractItemArticleListItem extends ContractItemArticleCommon {
public constructor(
contractId: string,
contractItemId: string,
data: ContractItemArticleData,
) {
super(contractId, contractItemId, data);
}
}

export class ContractItemArticleList extends ListDataModel<ContractItemArticleListItem> {
public readonly contractId: string;
public readonly contractItemId: string;

public constructor(
contractId: string,
contractItemId: string,
items: ContractItemArticleListItem[],
) {
super(items, items.length);
this.contractId = contractId;
this.contractItemId = contractItemId;
}
}
2 changes: 2 additions & 0 deletions packages/models/src/contract/ContractItemArticle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./ContractItemArticle.js";
export * from "./types.js";
11 changes: 11 additions & 0 deletions packages/models/src/contract/ContractItemArticle/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type ContractItemArticleData = {
amount: number;
articleTemplateId: string;
description?: string;
id: string;
name: string;
unitPrice: {
currency: string;
value: number;
};
};
1 change: 1 addition & 0 deletions packages/models/src/contract/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./Contract/index.js";
export * from "./ContractItem/index.js";
export * from "./ContractItemArticle/index.js";