Files
klz-cables.com/.pnpm-store/v10/files/40/6da309d95e5485a46b0e2c7ad16aa958c12d1819f084a3c7d4a51c7fbc850b2679960f1109ba2258561d11de939801e2fcf4bb81f9aa039e1bad7250dfc053
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

135 lines
4.2 KiB
Plaintext

import { max, sql } from 'drizzle-orm';
import { getFieldByPath } from 'payload';
import toSnakeCase from 'to-snake-case';
import { buildQuery } from './queries/buildQuery.js';
import { selectDistinct } from './queries/selectDistinct.js';
import { getTransaction } from './utilities/getTransaction.js';
import { DistinctSymbol } from './utilities/rawConstraint.js';
const getOrderColumn = (orderBy, selectFields, joins)=>{
if (orderBy.length === 0) {
return null;
}
if (orderBy[0].column === selectFields['_selected']) {
return null;
}
if (joins.length > 0) {
return orderBy[0].column;
}
return max(orderBy[0]?.column).as('_order');
};
export const findDistinct = async function(args) {
const collectionConfig = this.payload.collections[args.collection].config;
const page = args.page || 1;
const offset = args.limit ? (page - 1) * args.limit : undefined;
const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug));
const { joins, orderBy, selectFields, where } = buildQuery({
adapter: this,
fields: collectionConfig.flattenedFields,
locale: args.locale,
sort: args.sort ?? args.field,
tableName,
where: {
and: [
args.where ?? {},
{
[args.field]: {
equals: DistinctSymbol
}
}
]
}
});
orderBy.pop();
const db = await getTransaction(this, args.req);
const _order = getOrderColumn(orderBy, selectFields, joins);
const selectDistinctResult = await selectDistinct({
adapter: this,
db,
forceRun: true,
hasAggregates: Boolean(_order) && !joins.length,
joins,
query: ({ query })=>{
if (_order && orderBy.length > 0 && !joins.length) {
query = query.orderBy(orderBy[0].order(sql`_order`));
} else {
query = query.orderBy(()=>orderBy.map(({ column, order })=>order(column)));
}
if (args.limit) {
if (offset) {
query = query.offset(offset);
}
query = query.limit(args.limit);
}
return query;
},
selectFields: {
_selected: selectFields['_selected'],
..._order ? {
_order
} : {}
},
tableName,
where
});
const field = getFieldByPath({
config: this.payload.config,
fields: collectionConfig.flattenedFields,
includeRelationships: true,
path: args.field
})?.field;
if (field && 'relationTo' in field && Array.isArray(field.relationTo)) {
for (const row of selectDistinctResult){
const json = JSON.parse(row._selected);
const relationTo = Object.keys(json).find((each)=>Boolean(json[each]));
const value = json[relationTo];
if (!value) {
row._selected = null;
} else {
row._selected = {
relationTo,
value
};
}
}
}
const values = selectDistinctResult.map((each)=>({
[args.field]: each._selected
}));
if (args.limit) {
const totalDocs = await this.countDistinct({
column: selectFields['_selected'],
db,
joins,
tableName,
where
});
const totalPages = Math.ceil(totalDocs / args.limit);
const hasPrevPage = page > 1;
const hasNextPage = totalPages > page;
const pagingCounter = (page - 1) * args.limit + 1;
return {
hasNextPage,
hasPrevPage,
limit: args.limit,
nextPage: hasNextPage ? page + 1 : null,
page,
pagingCounter,
prevPage: hasPrevPage ? page - 1 : null,
totalDocs,
totalPages,
values
};
}
return {
hasNextPage: false,
hasPrevPage: false,
limit: 0,
page: 1,
pagingCounter: 1,
totalDocs: values.length,
totalPages: 1,
values
};
};
//# sourceMappingURL=findDistinct.js.map