From 092ecb14d1877ccbc2d93a51b558b9061317938b Mon Sep 17 00:00:00 2001 From: ntdoJanneck Date: Wed, 30 Oct 2024 09:31:15 +0100 Subject: [PATCH] feat(ingress-model): add listCompatibleWithCertificate function --- packages/models/src/domain/Ingress/Ingress.ts | 27 ++++++++++--------- .../src/domain/Ingress/behaviors/api.ts | 17 +++++++++--- .../src/domain/Ingress/behaviors/types.ts | 5 ++++ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/packages/models/src/domain/Ingress/Ingress.ts b/packages/models/src/domain/Ingress/Ingress.ts index 2cea775d..34956f1f 100644 --- a/packages/models/src/domain/Ingress/Ingress.ts +++ b/packages/models/src/domain/Ingress/Ingress.ts @@ -93,6 +93,16 @@ export class Ingress extends ReferenceModel { public async verifyOwnership(): Promise { await config.behaviors.ingress.verifyOwnership(this.id); } + + public static async listCompatibleWithCertificate( + projectId: string, + certificate: string, + ): Promise { + return await config.behaviors.ingress.listCompatibleWithCertificate( + projectId, + certificate, + ); + } } export class IngressCommon extends classes( @@ -151,8 +161,7 @@ export class IngressListQuery extends ListQueryModel 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, }); @@ -165,20 +174,12 @@ export class IngressListQuery extends ListQueryModel }, [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]; } diff --git a/packages/models/src/domain/Ingress/behaviors/api.ts b/packages/models/src/domain/Ingress/behaviors/api.ts index a70351e6..345bb0eb 100644 --- a/packages/models/src/domain/Ingress/behaviors/api.ts +++ b/packages/models/src/domain/Ingress/behaviors/api.ts @@ -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, @@ -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 ( @@ -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)); + }, }); diff --git a/packages/models/src/domain/Ingress/behaviors/types.ts b/packages/models/src/domain/Ingress/behaviors/types.ts index 13b75630..06e3577c 100644 --- a/packages/models/src/domain/Ingress/behaviors/types.ts +++ b/packages/models/src/domain/Ingress/behaviors/types.ts @@ -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; @@ -25,4 +26,8 @@ export interface IngressBehaviors { ) => Promise; updatePaths: (ingressId: string, paths: PathSettings[]) => Promise; verifyOwnership: (ingressId: string) => Promise; + listCompatibleWithCertificate: ( + projectId: string, + certificate: string, + ) => Promise; }