import { EntityRoot, SelectQuery, SmartList, TeaQLDataService, UserContext, executeRelationFacets } from '../../teaql-ts'; import { PaymentAttempt } from '../models/PaymentAttempt'; import { mergeGeneratedScalarSearch, DynamicSearchWarning, QueryIntent } from 'teaql-ts'; import { GENERATED_SEARCH_MODELS } from '../../generated-search-models'; export class PaymentAttemptRequest { private query: SelectQuery; private filters: any[] = []; private _purpose: string | undefined; private _comment: string | undefined; private _pageCountAlias: string | undefined; private _pageCountMethod: "count" | "countAs" | undefined; private _dynamicSearchWarnings: DynamicSearchWarning[] = []; constructor(minimal = false) { this.query = new SelectQuery("PaymentAttempt"); this.filters.push({ version: { "$gte": 1 } }); if (minimal) { this.selectId(); this.selectVersion(); } else this.selectSelfFields(); } comment(c: string): this { this.query.comment(c); this._comment = c; return this; } filterByJson(source: unknown, warn?: (warning: DynamicSearchWarning) => void): this { const base = this.query.clone(); if (this.filters.length > 0) base.filter({ "$and": this.filters }); const merged = mergeGeneratedScalarSearch(base, source, GENERATED_SEARCH_MODELS, warn); this.query = merged.query as SelectQuery; this.filters = this.query.filterCondition ? [this.query.filterCondition] : []; this._dynamicSearchWarnings.push(...merged.warnings); return this; } get dynamicSearchWarnings(): readonly DynamicSearchWarning[] { return this._dynamicSearchWarnings.map(warning => ({ ...warning })); } purpose(p: string): ExecutablePaymentAttemptRequest { this.query.purpose(p); this._purpose = p; return new ExecutablePaymentAttemptRequest( (context) => context.withQueryRequest(this.query, () => this.executeForListInternal(context)), (context) => context.withQueryRequest(this.query, () => this.executeForRowsInternal(context)), (context, chunkSize) => this.executeForStreamInternal(context, chunkSize), () => this.limit(1), (c) => this.comment(c), () => this.ensureIntent()); } optimizeForContinuousPageFetch(): this { this.query.optimizeForContinuousPageFetch(); return this; } optimizeForContinuousPageFetchWith(namespace: string, ttlSeconds: number): this { this.query.optimizeForContinuousPageFetchWith(namespace, ttlSeconds); return this; } optimizePaginationWithIdSet(): this { this.query.optimizePaginationWithIdSet(); return this; } optimizePaginationWithIdSetConfig(namespace: string, ttlSeconds: number, maxIds: number): this { this.query.optimizePaginationWithIdSetConfig(namespace, ttlSeconds, maxIds); return this; } topNProbeParentThreshold(threshold: number): this { this.query.topNProbeParentThreshold(threshold); return this; } limit(n: number): this { this.query.limit(n); return this; } offset(n: number): this { this.query.offset(n); return this; } toQuery(): SelectQuery { if (this.filters.length > 0) { this.query.filter({ "$and": this.filters }); } return this.query; } withDeletedRows(): this { this.filters = this.filters.filter(filter => !("version" in filter)); return this; } deletedRowsOnly(): this { this.withDeletedRows(); this.filters.push({ version: { "$lte": -1 } }); return this; } selectSelfFields(): this { this.query.select(["id", "payment", "referenceCode", "version"]); return this; } selectId(): this { this.query.select(["id"]); return this; } selectReferenceCode(): this { this.query.select(["referenceCode"]); return this; } selectVersion(): this { this.query.select(["version"]); return this; } selectPaymentWith(request: { toQuery(): SelectQuery }): this { this.query.select(["payment"]); this.query.relationQuery( "payment", request.toQuery(), "payment", "id", false, { kind: "relation-select", method: "selectPaymentWith", relation: "payment" }); return this; } withPaymentMatching(request: { toQuery(): SelectQuery }): this { this.filters.push({ "payment": { "$inSubquery": { query: request.toQuery(), field: "id" }, }, }); return this; } withoutPaymentMatching(request: { toQuery(): SelectQuery }): this { this.filters.push({ "payment": { "$notInSubquery": { query: request.toQuery(), field: "id" }, }, }); return this; } withIdIs(val: any): this { this.filters.push({ "id": { "$eq": val } }); return this; } withIdIsNot(val: any): this { this.filters.push({ "id": { "$ne": val } }); return this; } withIdIn(...vals: any[]): this { this.filters.push({ "id": { "$in": vals } }); return this; } withIdNotIn(...vals: any[]): this { this.filters.push({ "id": { "$notIn": vals } }); return this; } withIdGreaterThan(val: any): this { this.filters.push({ "id": { "$gt": val } }); return this; } withIdGreaterThanOrEqualTo(val: any): this { this.filters.push({ "id": { "$gte": val } }); return this; } withIdLessThan(val: any): this { this.filters.push({ "id": { "$lt": val } }); return this; } withIdLessThanOrEqualTo(val: any): this { this.filters.push({ "id": { "$lte": val } }); return this; } withIdBetween(lower: any, upper: any): this { this.filters.push({ "id": { "$between": [lower, upper] } }); return this; } withIdIsKnown(): this { this.filters.push({ "id": { "$isNull": false } }); return this; } withIdIsUnknown(): this { this.filters.push({ "id": { "$isNull": true } }); return this; } filterByPayment(val: any): this { this.filters.push({ "payment": { "$eq": val } }); return this; } filterByPaymentIn(...vals: any[]): this { this.filters.push({ "payment": { "$in": vals } }); return this; } withPaymentIsKnown(): this { this.filters.push({ "payment": { "$isNull": false } }); return this; } withPaymentIsUnknown(): this { this.filters.push({ "payment": { "$isNull": true } }); return this; } withReferenceCodeContaining(val: string): this { this.filters.push({ "referenceCode": { "$contains": val } }); return this; } withReferenceCodeIs(val: string): this { this.filters.push({ "referenceCode": { "$eq": val } }); return this; } withReferenceCodeIsNot(val: any): this { this.filters.push({ "referenceCode": { "$ne": val } }); return this; } withReferenceCodeIn(...vals: any[]): this { this.filters.push({ "referenceCode": { "$in": vals } }); return this; } withReferenceCodeNotIn(...vals: any[]): this { this.filters.push({ "referenceCode": { "$notIn": vals } }); return this; } withReferenceCodeGreaterThan(val: any): this { this.filters.push({ "referenceCode": { "$gt": val } }); return this; } withReferenceCodeGreaterThanOrEqualTo(val: any): this { this.filters.push({ "referenceCode": { "$gte": val } }); return this; } withReferenceCodeLessThan(val: any): this { this.filters.push({ "referenceCode": { "$lt": val } }); return this; } withReferenceCodeLessThanOrEqualTo(val: any): this { this.filters.push({ "referenceCode": { "$lte": val } }); return this; } withReferenceCodeBetween(lower: any, upper: any): this { this.filters.push({ "referenceCode": { "$between": [lower, upper] } }); return this; } withReferenceCodeIsKnown(): this { this.filters.push({ "referenceCode": { "$isNull": false } }); return this; } withReferenceCodeIsUnknown(): this { this.filters.push({ "referenceCode": { "$isNull": true } }); return this; } withReferenceCodeNotContaining(val: string): this { this.filters.push({ "referenceCode": { "$notContains": val } }); return this; } withReferenceCodeStartingWith(val: string): this { this.filters.push({ "referenceCode": { "$startsWith": val } }); return this; } withReferenceCodeNotStartingWith(val: string): this { this.filters.push({ "referenceCode": { "$notStartsWith": val } }); return this; } withReferenceCodeEndingWith(val: string): this { this.filters.push({ "referenceCode": { "$endsWith": val } }); return this; } withReferenceCodeNotEndingWith(val: string): this { this.filters.push({ "referenceCode": { "$notEndsWith": val } }); return this; } withReferenceCodeSoundingLike(val: string): this { this.filters.push({ "referenceCode": { "$soundLike": val } }); return this; } withVersionIs(val: any): this { this.filters.push({ "version": { "$eq": val } }); return this; } withVersionIsNot(val: any): this { this.filters.push({ "version": { "$ne": val } }); return this; } withVersionIn(...vals: any[]): this { this.filters.push({ "version": { "$in": vals } }); return this; } withVersionNotIn(...vals: any[]): this { this.filters.push({ "version": { "$notIn": vals } }); return this; } withVersionGreaterThan(val: any): this { this.filters.push({ "version": { "$gt": val } }); return this; } withVersionGreaterThanOrEqualTo(val: any): this { this.filters.push({ "version": { "$gte": val } }); return this; } withVersionLessThan(val: any): this { this.filters.push({ "version": { "$lt": val } }); return this; } withVersionLessThanOrEqualTo(val: any): this { this.filters.push({ "version": { "$lte": val } }); return this; } withVersionBetween(lower: any, upper: any): this { this.filters.push({ "version": { "$between": [lower, upper] } }); return this; } withVersionIsKnown(): this { this.filters.push({ "version": { "$isNull": false } }); return this; } withVersionIsUnknown(): this { this.filters.push({ "version": { "$isNull": true } }); return this; } orderByIdAscending(): this { this.query.orderBy("id", "asc"); return this; } orderByIdDescending(): this { this.query.orderBy("id", "desc"); return this; } orderByReferenceCodeAscending(): this { this.query.orderBy("referenceCode", "asc"); return this; } orderByReferenceCodeDescending(): this { this.query.orderBy("referenceCode", "desc"); return this; } orderByVersionAscending(): this { this.query.orderBy("version", "asc"); return this; } orderByVersionDescending(): this { this.query.orderBy("version", "desc"); return this; } // --- Aggregations --- count(): this { this.query.aggregate("Count", "id", "count"); this._pageCountAlias = "count"; this._pageCountMethod = "count"; return this; } countAs(retName: string): this { this.query.aggregate("Count", "id", retName); this._pageCountAlias = retName; this._pageCountMethod = "countAs"; return this; } // --- Group By --- groupById(): this { this.query.groupBy("id"); return this; } groupByIdAs(retName: string): this { this.query.groupBy("id"); // In TS we don't alias group by yet natively return this; } groupByPayment(): this { this.query.groupBy("payment"); return this; } groupByPaymentAs(retName: string): this { this.query.groupBy("payment"); // In TS we don't alias group by yet natively return this; } groupByReferenceCode(): this { this.query.groupBy("referenceCode"); return this; } groupByReferenceCodeAs(retName: string): this { this.query.groupBy("referenceCode"); // In TS we don't alias group by yet natively return this; } groupByVersion(): this { this.query.groupBy("version"); return this; } groupByVersionAs(retName: string): this { this.query.groupBy("version"); // In TS we don't alias group by yet natively return this; } // --- Facets --- facetByPaymentAs( facetName: string, request: { toQuery(): SelectQuery }, includeAllFacets = true, ): this { this.query.facetBy(facetName, "payment", request, includeAllFacets, { kind: "facet", method: "facetByPaymentAs", relation: "payment", alias: facetName }); return this; } private async executeForListInternal(context: UserContext): Promise> { this.ensureIntent(); if (this._pageCountAlias !== undefined) return this.executeCountedListInternal(context); if (this.filters.length > 0) { this.query.filter({ "$and": this.filters }); } const service = context.requireResource("dataService"); const rows = await service.executeQuery(context.prepareQuery(this.query)); const aggregateOnly = this.query.aggregateItems.length > 0 && this.query.groupByItems.length === 0; const data = aggregateOnly ? [] : rows.map((row: unknown) => row instanceof PaymentAttempt ? row : PaymentAttempt.fromRecord(row as Record, new EntityRoot())); const result = new SmartList(data); if (aggregateOnly && rows[0]) Object.assign(result.aggregations, rows[0]); if (this.query.facets.length > 0) { result.facets = await executeRelationFacets( service, (query: any) => context.prepareQuery(query), this.query, this.query.facets); } return result; } private async executeForRowsInternal(context: UserContext): Promise>> { this.ensureIntent(); if (this.filters.length > 0) this.query.filter({ "$and": this.filters }); const service = context.requireResource("dataService"); return new SmartList>( await service.executeQuery(context.prepareQuery(this.query)) as Record[]); } private async executeCountedListInternal( context: UserContext, ): Promise> { this.ensureIntent(); if (this.filters.length > 0) this.query.filter({ "$and": this.filters }); const service = context.requireResource("dataService"); const preparedQuery = context.prepareQuery(this.query); const useIdSet = preparedQuery.localIdSetPaginationOptions() !== undefined; const countOperation = { kind: "named-count" as const, method: this._pageCountMethod ?? "count", ...(this._pageCountMethod === "countAs" ? { alias: this._pageCountAlias } : {}) }; const totalCountBeforeRows = useIdSet ? undefined : await service.executeCount( preparedQuery, this._pageCountAlias, countOperation); const pageQuery = preparedQuery.clone(); // A page count describes the full filtered set, not an aggregate-only page row. pageQuery.aggregateItems = pageQuery.aggregateItems.filter(item => !(String(item.function).toLowerCase() === "count" && item.alias === this._pageCountAlias)); const rows = await service.executeQuery(pageQuery); const totalCount = useIdSet && context.idSetPaginationCountAccuracy === "EXACT" ? context.idSetPaginationCount! : (totalCountBeforeRows ?? await service.executeCount(preparedQuery, this._pageCountAlias, countOperation)); const data = new SmartList(rows.map((row: unknown) => row instanceof PaymentAttempt ? row : PaymentAttempt.fromRecord(row as Record, new EntityRoot()))); data.totalCount = totalCount; if (this._pageCountAlias !== undefined) data.aggregations[this._pageCountAlias] = totalCount; if (this.query.facets.length > 0) { data.facets = await executeRelationFacets( service, (query: any) => context.prepareQuery(query), preparedQuery, preparedQuery.facets); } return data; } private executeForStreamInternal(context: UserContext, chunkSize: number): AsyncIterable { this.ensureIntent(); if (this.filters.length > 0) this.query.filter({ "$and": this.filters }); const service = context.requireResource("dataService"); const preparedQuery = context.prepareQuery(this.query); // Prepare Context and capture the runtime request before returning a lazy iterator. const chunks = service.executeForStream(preparedQuery, chunkSize); return (async function* () { for await (const chunk of chunks) { for (const entity of chunk) { yield entity instanceof PaymentAttempt ? entity : PaymentAttempt.fromRecord(entity as Record, new EntityRoot()); } } })(); } private ensureIntent(): void { // Runtime owns validation, Unicode whitespace rules and typed diagnostics. new QueryIntent(this._comment, this._purpose); } } export class ExecutablePaymentAttemptRequest { constructor( private readonly execute: (context: UserContext) => Promise>, private readonly executeRows: (context: UserContext) => Promise>>, private readonly stream: (context: UserContext, chunkSize: number) => AsyncIterable, private readonly limitOne: () => void, private readonly addComment: (comment: string) => void, private readonly ensureIntent: () => void, ) {} comment(c: string): this { this.addComment(c); return this; } newEntity(context: UserContext): PaymentAttempt { this.ensureIntent(); return new PaymentAttempt(); } executeForList(context: UserContext): Promise> { this.ensureIntent(); return this.execute(context); } executeForRows(context: UserContext): Promise>> { this.ensureIntent(); return this.executeRows(context); } executeForStream(context: UserContext, chunkSize = 1000): AsyncIterable { this.ensureIntent(); return this.stream(context, chunkSize); } async executeForOne(context: UserContext): Promise { this.limitOne(); return (await this.executeForList(context))[0]; } }