Skip to content

Commit

Permalink
feat(models): add mailaddress model
Browse files Browse the repository at this point in the history
  • Loading branch information
ntdoJanneck committed Oct 9, 2024
1 parent 1cf99e8 commit 4aa96a5
Show file tree
Hide file tree
Showing 8 changed files with 188 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 @@ -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 { apiMailAddressBehaviors } from "../../mail/MailAddress/behaviors/index.js";

class ApiSetupState {
private _client: MittwaldAPIV2Client | undefined;
Expand All @@ -30,6 +31,7 @@ class ApiSetupState {
config.behaviors.ingress = apiIngressBehaviors(client);
config.behaviors.appInstallation = apiAppInstallationBehaviors(client);
config.behaviors.contract = apiContractBehaviors(client);
config.behaviors.mailAddress = apiMailAddressBehaviors(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 { MailAddressBehaviors } from "../mail/MailAddress/behaviors/types.js";

interface Config {
defaultPaginationLimit: number;
Expand All @@ -18,6 +19,7 @@ interface Config {
customer: CustomerBehaviors;
ingress: IngressBehaviors;
appInstallation: AppInstallationBehaviors;
mailAddress: MailAddressBehaviors;
};
}

Expand All @@ -32,5 +34,6 @@ export const config: Config = {
customer: undefined as unknown as CustomerBehaviors,
ingress: undefined as unknown as IngressBehaviors,
appInstallation: undefined as unknown as AppInstallationBehaviors,
mailAddress: undefined as unknown as MailAddressBehaviors,
},
};
123 changes: 123 additions & 0 deletions packages/models/src/mail/MailAddress/MailAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { classes } from "polytype";
import {
DataModel,
ListDataModel,
ListQueryModel,
ReferenceModel,
} from "../../base/index.js";
import { Ingress } from "../../domain/index.js";
import {
MailAddressData,
MailAddressListItemData,
MailAddressListQueryModelData,
} from "./types.js";
import { provideReact } from "../../react/index.js";
import { config } from "../../config/config.js";
import assertObjectFound from "../../base/assertObjectFound.js";
import { Project } from "../../project/index.js";

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

public static find = provideReact(
async (id: string): Promise<MailAddress | undefined> => {
const data = await config.behaviors.mailAddress.find(id);
if (data !== undefined) {
return new MailAddressDetailed(data);
}
},
);

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

export class MailAddressCommon extends classes(
DataModel<MailAddressData>,
MailAddress,
) {
public constructor(data: MailAddressData) {
super([data], [data.id]);
}
}

export class MailAddressDetailed extends classes(
MailAddressCommon,
DataModel<MailAddressData>,
) {
public constructor(data: MailAddressData) {
super([data], [data]);
}
}

export class MailAddressListItem extends classes(
MailAddressCommon,
DataModel<MailAddressData>,
) {
public constructor(data: MailAddressListItemData) {
super([data], [data]);
}
}

export class MailAddressListQuery extends ListQueryModel<MailAddressListQueryModelData> {
public readonly project: Project;
public constructor(
project: Project,
query: MailAddressListQueryModelData = {},
) {
super(query, {
dependencies: [project.id],
});
this.project = project;
}

public refine(query: MailAddressListQueryModelData = {}) {
return new MailAddressListQuery(this.project, {
...this.query,
...query,
});
}

public execute = provideReact(async () => {
const { items, totalCount } = await config.behaviors.mailAddress.query(
this.project.id,
);
return new MailAddressList(
this.project,
this.query,
items.map((m) => new MailAddressListItem(m)),
totalCount,
);
}, [this.queryId]);

public getTotalCount = provideReact(async () => {
const { items } = await this.refine({}).execute();
return items.length;
}, [this.queryId]);

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

export class MailAddressList extends classes(
MailAddressListQuery,
ListDataModel<MailAddressListItem>,
) {
public constructor(
project: Project,
query: MailAddressListQueryModelData,
mailAddresses: MailAddressListItem[],
totalCount: number,
) {
super([project, query], [mailAddresses, totalCount]);
}
}
29 changes: 29 additions & 0 deletions packages/models/src/mail/MailAddress/behaviors/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
assertStatus,
MittwaldAPIV2Client,
assertOneOfStatus,
} from "@mittwald/api-client";
import { MailAddressBehaviors } from "./types.js";
export const apiMailAddressBehaviors = (
client: MittwaldAPIV2Client,
): MailAddressBehaviors => ({
find: async (id) => {
const response = await client.mail.getMailAddress({
mailAddressId: id,
});
if (response.status === 200) {
return response.data;
}
assertOneOfStatus(response, [404]);
},
query: async (projectId) => {
const response = await client.mail.listMailAddresses({
projectId,
});
assertStatus(response, 200);
return {
items: response.data,
totalCount: response.data.length,
};
},
});
2 changes: 2 additions & 0 deletions packages/models/src/mail/MailAddress/behaviors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./types.js";
export * from "./api.js";
9 changes: 9 additions & 0 deletions packages/models/src/mail/MailAddress/behaviors/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MailAddressData, MailAddressListItemData } from "../types.js";
import { QueryResponseData } from "../../../base/index.js";

export interface MailAddressBehaviors {
find: (id: string) => Promise<MailAddressData | undefined>;
query: (
projectId: string,
) => Promise<QueryResponseData<MailAddressListItemData>>;
}
2 changes: 2 additions & 0 deletions packages/models/src/mail/MailAddress/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./MailAddress.js";
export * from "./types.js";
18 changes: 18 additions & 0 deletions packages/models/src/mail/MailAddress/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { MittwaldAPIV2 } from "@mittwald/api-client";
import { Project } from "../../project/index.js";

export type MailAddressListQueryData =
MittwaldAPIV2.Paths.V2ProjectsProjectIdMailAddresses.Get.Parameters.Query;

export type MailAddressListQueryModelData = Omit<
MailAddressListQueryData,
"projectId"
> & {
project?: Project;
};

export type MailAddressData =
MittwaldAPIV2.Operations.MailGetMailAddress.ResponseData;

export type MailAddressListItemData =
MittwaldAPIV2.Operations.MailListMailAddresses.ResponseData[number];

0 comments on commit 4aa96a5

Please sign in to comment.