Skip to content

Commit

Permalink
Add contractItemModel to apiTools
Browse files Browse the repository at this point in the history
  • Loading branch information
JLampeMW committed Sep 24, 2024
1 parent 48e37ea commit bbee462
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/models/src/config/behaviors/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { apiIngressBehaviors } from "../../domain/Ingress/behaviors/index.js";
import { apiAppInstallationBehaviors } from "../../app/AppInstallation/behaviors/index.js";
import { addUrlTagToProvideReactCache } from "../../react/asyncResourceInvalidation.js";
import { apiContractBehaviors } from "../../contract/Contract/behaviors/index.js";
import { apiContractItemBehaviors } from "../../contract/ContractItem/behaviors/index.js";

class ApiSetupState {
private _client: MittwaldAPIV2Client | undefined;
Expand All @@ -27,6 +28,7 @@ class ApiSetupState {
config.behaviors.ingress = apiIngressBehaviors(client);
config.behaviors.appInstallation = apiAppInstallationBehaviors(client);
config.behaviors.contract = apiContractBehaviors(client);
config.behaviors.contractItem = apiContractItemBehaviors(client);
}

public setupWithApiToken(apiToken: string) {
Expand Down
3 changes: 3 additions & 0 deletions packages/models/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { CustomerBehaviors } from "../customer/Customer/behaviors/index.js";
import { IngressBehaviors } from "../domain/Ingress/behaviors/index.js";
import { ContractBehaviors } from "../contract/Contract/behaviors/index.js";
import { AppInstallationBehaviors } from "../app/AppInstallation/behaviors/index.js";
import { ContractItemBehaviors } from "../contract/ContractItem/behaviors/index.js";

interface Config {
defaultPaginationLimit: number;
behaviors: {
contract: ContractBehaviors;
contractItem: ContractItemBehaviors;
project: ProjectBehaviors;
server: ServerBehaviors;
customer: CustomerBehaviors;
Expand All @@ -21,6 +23,7 @@ export const config: Config = {
defaultPaginationLimit: 50,
behaviors: {
contract: undefined as unknown as ContractBehaviors,
contractItem: undefined as unknown as ContractItemBehaviors,
project: undefined as unknown as ProjectBehaviors,
server: undefined as unknown as ServerBehaviors,
customer: undefined as unknown as CustomerBehaviors,
Expand Down
54 changes: 54 additions & 0 deletions packages/models/src/contract/ContractItem/ContractItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { classes } from "polytype";
import { ContractItemData } from "./types.js";
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";

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

public static find = provideReact(
async (
contractId: string,
contractItemId: string,
): Promise<ContractItemDetailed | undefined> => {
const data = await config.behaviors.contractItem.find(
contractId,
contractItemId,
);
if (data !== undefined) {
return new ContractItemDetailed(contractId, data);
}
},
);

public static get = provideReact(
async (
contractId: string,
contractItemId: string,
): Promise<ContractItemDetailed> => {
const item = await this.find(contractId, contractItemId);
assertObjectFound(item, this, contractItemId);
return item;
},
);

public readonly contractId: string;

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

export class ContractItemDetailed extends classes(
DataModel<ContractItemData>,
ContractItem,
) {
public constructor(contractId: string, data: ContractItemData) {
super([data], [contractId, data.itemId]);
}
}
18 changes: 18 additions & 0 deletions packages/models/src/contract/ContractItem/behaviors/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { assertStatus, MittwaldAPIV2Client } from "@mittwald/api-client";
import { ContractItemBehaviors } from "./types.js";

export const apiContractItemBehaviors = (
client: MittwaldAPIV2Client,
): ContractItemBehaviors => ({
find: async (contractId, contractItemId) => {
const response = await client.contract.getDetailOfContractItem({
contractId,
contractItemId,
});

if (response.status === 200) {
return response.data;
}
assertStatus(response, 404);
},
});
2 changes: 2 additions & 0 deletions packages/models/src/contract/ContractItem/behaviors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./api.js";
export * from "./types.js";
8 changes: 8 additions & 0 deletions packages/models/src/contract/ContractItem/behaviors/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ContractItemData } from "../types.js";

export interface ContractItemBehaviors {
find: (
contractId: string,
contractItemId: string,
) => Promise<ContractItemData | undefined>;
}
2 changes: 2 additions & 0 deletions packages/models/src/contract/ContractItem/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./ContractItem.js";
export * from "./types.js";
4 changes: 4 additions & 0 deletions packages/models/src/contract/ContractItem/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { MittwaldAPIV2 } from "@mittwald/api-client";

export type ContractItemData =
MittwaldAPIV2.Operations.ContractGetDetailOfContractItem.ResponseData;
1 change: 1 addition & 0 deletions packages/models/src/contract/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./Contract/index.js";
export * from "./ContractItem/index.js";

0 comments on commit bbee462

Please sign in to comment.