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

feat(Models): add invoice model #75

Open
wants to merge 7 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
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 @@ -9,6 +9,7 @@ import { addUrlTagToProvideReactCache } from "../../react/asyncResourceInvalidat
import { apiArticleBehaviors } from "../../article/Article/behaviors/index.js";
import { apiContractBehaviors } from "../../contract/Contract/behaviors/index.js";
import { apiContractItemBehaviors } from "../../contract/ContractItem/behaviors/index.js";
import { apiInvoiceBehaviors } from "../../invoice/Invoice/behaviors/index.js";

class ApiSetupState {
private _client: MittwaldAPIV2Client | undefined;
Expand All @@ -28,6 +29,7 @@ class ApiSetupState {
config.behaviors.server = apiServerBehaviors(client);
config.behaviors.customer = apiCustomerBehaviors(client);
config.behaviors.ingress = apiIngressBehaviors(client);
config.behaviors.invoice = apiInvoiceBehaviors(client);
config.behaviors.appInstallation = apiAppInstallationBehaviors(client);
config.behaviors.contract = apiContractBehaviors(client);
config.behaviors.contractItem = apiContractItemBehaviors(client);
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 @@ -6,6 +6,7 @@ import { ContractBehaviors } from "../contract/Contract/behaviors/index.js";
import { AppInstallationBehaviors } from "../app/AppInstallation/behaviors/index.js";
import { ContractItemBehaviors } from "../contract/ContractItem/behaviors/index.js";
import { ArticleBehaviors } from "../article/Article/behaviors/index.js";
import { InvoiceBehaviors } from "../invoice/Invoice/behaviors/index.js";

interface Config {
defaultPaginationLimit: number;
Expand All @@ -17,6 +18,7 @@ interface Config {
server: ServerBehaviors;
customer: CustomerBehaviors;
ingress: IngressBehaviors;
invoice: InvoiceBehaviors;
appInstallation: AppInstallationBehaviors;
};
}
Expand All @@ -31,6 +33,7 @@ export const config: Config = {
server: undefined as unknown as ServerBehaviors,
customer: undefined as unknown as CustomerBehaviors,
ingress: undefined as unknown as IngressBehaviors,
invoice: undefined as unknown as InvoiceBehaviors,
appInstallation: undefined as unknown as AppInstallationBehaviors,
},
};
139 changes: 139 additions & 0 deletions packages/models/src/invoice/Invoice/Invoice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import {
InvoiceListItemData,
InvoiceData,
InvoiceFileAccessTokenData,
InvoiceListQueryModelData,
InvoiceListQueryData,
} from "./types.js";
import { config } from "../../config/config.js";
import { classes } from "polytype";
import { DataModel } from "../../base/DataModel.js";
import assertObjectFound from "../../base/assertObjectFound.js";
import { provideReact } from "../../react/provideReact.js";
import { ReferenceModel } from "../../base/ReferenceModel.js";
import { ListDataModel, ListQueryModel } from "../../base/index.js";

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

public static find = provideReact(
async (id: string): Promise<InvoiceDetailed | undefined> => {
const data = await config.behaviors.invoice.find(id);

if (data !== undefined) {
return new InvoiceDetailed(data);
}
},
);

public static get = provideReact(
async (id: string): Promise<InvoiceDetailed> => {
const project = await this.find(id);
assertObjectFound(project, this, id);
return project;
},
);

public static query(query: InvoiceListQueryModelData) {
return new InvoiceListQuery(query);
}

public static requestFileAccessToken = provideReact(
async (
invoiceId: string,
customerId: string,
): Promise<InvoiceFileAccessTokenData> => {
return await config.behaviors.invoice.requestFileAccessToken(
invoiceId,
customerId,
);
},
);
}

class InvoiceCommon extends classes(
DataModel<InvoiceListItemData | InvoiceData>,
Invoice,
) {
public constructor(data: InvoiceListItemData | InvoiceData) {
super([data], [data.customerId]);
}
}

export class InvoiceDetailed extends classes(
InvoiceCommon,
DataModel<InvoiceData>,
) {
public constructor(data: InvoiceData) {
super([data]);
}
}

export class InvoiceListItem extends classes(
InvoiceCommon,
DataModel<InvoiceListItemData>,
) {
public constructor(data: InvoiceListItemData) {
super([data]);
}
}

export class InvoiceListQuery extends ListQueryModel<InvoiceListQueryModelData> {
public constructor(query: InvoiceListQueryModelData) {
super(query);
}

public refine(query: InvoiceListQueryData) {
return new InvoiceListQuery({
...this.query,
...query,
});
}

public execute = provideReact(async () => {
const { customer, ...query } = this.query;

const customerId = customer.id;
const request = {
customerId: customerId,
queryParameters: {
limit: config.defaultPaginationLimit,
...query,
},
};
const { items, totalCount } = await config.behaviors.invoice.list(request);

return new InvoiceList(
this.query,
items.map((d) => new InvoiceListItem(d)),
totalCount,
);
}, [this.queryId]);

public getTotalCount = provideReact(async () => {
const { totalCount } = await this.refine({ limit: 1 }).execute();
return totalCount;
}, [this.queryId]);

public findOneAndOnly = provideReact(async () => {
const { items, totalCount } = await this.refine({ limit: 1 }).execute();
if (totalCount === 1) {
return items[0];
}
}, [this.queryId]);
}

export class InvoiceList extends classes(
InvoiceListQuery,
ListDataModel<InvoiceListItem>,
) {
public constructor(
query: InvoiceListQueryModelData,
invoices: InvoiceListItem[],
totalCount: number,
) {
super([query], [invoices, totalCount]);
}
}
40 changes: 40 additions & 0 deletions packages/models/src/invoice/Invoice/behaviors/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
assertStatus,
MittwaldAPIV2Client,
extractTotalCountHeader,
assertOneOfStatus,
} from "@mittwald/api-client";
import { InvoiceBehaviors } from "./types.js";

export const apiInvoiceBehaviors = (
client: MittwaldAPIV2Client,
): InvoiceBehaviors => ({
find: async (invoiceId) => {
const response = await client.contract.invoiceDetail({
invoiceId,
});

if (response.status === 200) {
return response.data;
}
assertOneOfStatus(response, [404, 429]);
},

list: async (request) => {
const response = await client.contract.invoiceListCustomerInvoices(request);
assertStatus(response, 200);
return {
items: response.data,
totalCount: extractTotalCountHeader(response),
};
},

requestFileAccessToken: async (invoiceId, customerId) => {
const response = await client.contract.invoiceGetFileAccessToken({
invoiceId,
customerId,
});
assertStatus(response, 200);
return response.data;
},
});
2 changes: 2 additions & 0 deletions packages/models/src/invoice/Invoice/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";
21 changes: 21 additions & 0 deletions packages/models/src/invoice/Invoice/behaviors/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
InvoiceData,
InvoiceFileAccessTokenData,
InvoiceListItemData,
InvoiceListQueryData,
} from "../types.js";
import { QueryResponseData } from "../../../base/index.js";

export interface InvoiceBehaviors {
find: (invoiceId: string) => Promise<InvoiceData | undefined>;

list: (request: {
customerId: string;
queryParameters?: InvoiceListQueryData;
}) => Promise<QueryResponseData<InvoiceListItemData>>;

requestFileAccessToken: (
invoiceId: string,
customerId: string,
) => Promise<InvoiceFileAccessTokenData>;
}
2 changes: 2 additions & 0 deletions packages/models/src/invoice/Invoice/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./Invoice.js";
export * from "./types.js";
20 changes: 20 additions & 0 deletions packages/models/src/invoice/Invoice/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MittwaldAPIV2 } from "@mittwald/api-client";
import { Customer } from "../../customer/index.js";

export type InvoiceListQueryData =
MittwaldAPIV2.Paths.V2CustomersCustomerIdInvoices.Get.Parameters.Query;

export type InvoiceData = MittwaldAPIV2.Operations.InvoiceDetail.ResponseData;

export type InvoiceListItemData =
MittwaldAPIV2.Operations.InvoiceListCustomerInvoices.ResponseData[number];

export type InvoiceFileAccessTokenData =
MittwaldAPIV2.Operations.InvoiceGetFileAccessToken.ResponseData;

export type InvoiceListQueryModelData = Omit<
InvoiceListQueryData,
"customerId"
> & {
customer: Customer;
};
1 change: 1 addition & 0 deletions packages/models/src/invoice/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Invoice/index.js";