-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cf99e8
commit 4aa96a5
Showing
8 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./types.js"; | ||
export * from "./api.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./MailAddress.js"; | ||
export * from "./types.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |