Skip to content

Commit

Permalink
feat(ingress-model): add listCompatibleWithCertificate function
Browse files Browse the repository at this point in the history
  • Loading branch information
ntdoJanneck committed Oct 30, 2024
1 parent 6b1cad8 commit 092ecb1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
27 changes: 14 additions & 13 deletions packages/models/src/domain/Ingress/Ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ export class Ingress extends ReferenceModel {
public async verifyOwnership(): Promise<void> {
await config.behaviors.ingress.verifyOwnership(this.id);
}

public static async listCompatibleWithCertificate(
projectId: string,
certificate: string,
): Promise<Ingress[]> {
return await config.behaviors.ingress.listCompatibleWithCertificate(
projectId,
certificate,
);
}
}

export class IngressCommon extends classes(
Expand Down Expand Up @@ -151,8 +161,7 @@ export class IngressListQuery extends ListQueryModel<IngressListQueryModelData>
public execute = provideReact(async () => {
const { project, ...query } = this.query;
const { items, totalCount } = await config.behaviors.ingress.list({
/** @todo: use this code when pagination is supported by API */
// limit: config.defaultPaginationLimit,
limit: config.defaultPaginationLimit,
...query,
projectId: project?.id,
});
Expand All @@ -165,20 +174,12 @@ export class IngressListQuery extends ListQueryModel<IngressListQueryModelData>
}, [this.queryId]);

public getTotalCount = provideReact(async () => {
/** @todo: use this code when pagination is supported by API */
// const { totalCount } = await this.refine({ limit: 1 }).execute();
// return totalCount;
const { items } = await this.refine({}).execute();
return items.length;
const { totalCount } = await this.refine({ limit: 1 }).execute();
return totalCount;
}, [this.queryId]);

public findOneAndOnly = provideReact(async () => {
/** @todo: use this code when pagination is supported by API */
// const { items, totalCount } = await this.refine({ limit: 2 }).execute();
// if (totalCount === 1) {
// return items[0];
// }
const { items, totalCount } = await this.refine({}).execute();
const { items, totalCount } = await this.refine({ limit: 2 }).execute();
if (totalCount === 1) {
return items[0];
}
Expand Down
17 changes: 14 additions & 3 deletions packages/models/src/domain/Ingress/behaviors/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { assertStatus, MittwaldAPIV2Client } from "@mittwald/api-client";
import { assertOneOfStatus } from "@mittwald/api-client";
import { IngressBehaviors } from "./types.js";
import { CertificateSettings, PathSettings } from "../types.js";
import { extractTotalCountHeader } from "../../../../../../.nx/cache/9747102588152130217/outputs/packages/commons/dist/types/index.js";
import { Ingress } from "../Ingress.js";

export const apiIngressBehaviors = (
client: MittwaldAPIV2Client,
Expand All @@ -24,9 +26,7 @@ export const apiIngressBehaviors = (
assertStatus(response, 200);
return {
items: response.data,
totalCount: response.data.length,
/** @todo: use this code when pagination is supported by API */
// totalCount: extractTotalCountHeader(response),
totalCount: extractTotalCountHeader(response),
};
},
create: async (
Expand Down Expand Up @@ -85,4 +85,15 @@ export const apiIngressBehaviors = (
});
assertStatus(response, 200);
},
listCompatibleWithCertificate: async (
projectId: string,
certificate: string,
) => {
const response =
await client.domain.ingressListIngressesCompatibleWithCertificate({
data: { projectId, certificate },
});
assertStatus(response, 200);
return response.data.map((i) => Ingress.ofId(i.id));
},
});
5 changes: 5 additions & 0 deletions packages/models/src/domain/Ingress/behaviors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CertificateSettings,
} from "../types.js";
import { QueryResponseData } from "../../../base/index.js";
import { Ingress } from "../Ingress.js";

export interface IngressBehaviors {
find: (id: string) => Promise<IngressData | undefined>;
Expand All @@ -25,4 +26,8 @@ export interface IngressBehaviors {
) => Promise<void>;
updatePaths: (ingressId: string, paths: PathSettings[]) => Promise<void>;
verifyOwnership: (ingressId: string) => Promise<void>;
listCompatibleWithCertificate: (
projectId: string,
certificate: string,
) => Promise<Ingress[]>;
}

0 comments on commit 092ecb1

Please sign in to comment.