2024-01-09 01:26:32 +00:00
|
|
|
import knex from './knex.js';
|
|
|
|
import redis from './redis.js';
|
|
|
|
import initLogger from './logger.js';
|
|
|
|
|
|
|
|
const logger = initLogger();
|
|
|
|
|
2024-01-26 00:31:15 +00:00
|
|
|
export function curateEntity(entity, context) {
|
2024-01-09 01:26:32 +00:00
|
|
|
if (!entity) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: entity.id,
|
|
|
|
name: entity.name,
|
|
|
|
slug: entity.slug,
|
|
|
|
type: entity.type,
|
2024-01-25 02:07:26 +00:00
|
|
|
url: entity.url,
|
2024-01-09 01:26:32 +00:00
|
|
|
isIndependent: entity.independent,
|
|
|
|
hasLogo: entity.has_logo,
|
|
|
|
parent: curateEntity(entity.parent, context),
|
2024-01-25 02:07:26 +00:00
|
|
|
children: context?.children?.filter((child) => child.parent_id === entity.id).map((child) => curateEntity({ ...child, parent: entity }, { parent: entity })) || [],
|
2024-01-09 01:26:32 +00:00
|
|
|
...context?.append?.[entity.id],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-25 02:07:26 +00:00
|
|
|
export async function fetchEntities(options) {
|
|
|
|
const entities = await knex('entities')
|
2024-06-03 22:44:28 +00:00
|
|
|
.select('entities.*', knex.raw('row_to_json(parents) as parent'))
|
2024-01-25 02:07:26 +00:00
|
|
|
.modify((builder) => {
|
2024-05-01 01:57:12 +00:00
|
|
|
if (options.query) {
|
|
|
|
builder.where((whereBuilder) => {
|
|
|
|
whereBuilder
|
2024-06-12 15:09:53 +00:00
|
|
|
.where((subBuilder) => {
|
|
|
|
subBuilder
|
|
|
|
.whereILike('entities.name', `%${options.query}%`)
|
|
|
|
.orWhereILike('entities.slug', `%${options.query}%`);
|
|
|
|
})
|
|
|
|
.whereNot('entities.type', 'info');
|
2024-05-01 01:57:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-25 02:07:26 +00:00
|
|
|
if (options.type === 'primary') {
|
|
|
|
builder
|
2024-06-12 15:09:53 +00:00
|
|
|
.where((subBuilder) => {
|
|
|
|
subBuilder
|
|
|
|
.where('entities.type', 'network')
|
|
|
|
.orWhere('entities.independent', true)
|
|
|
|
.orWhereNull('entities.parent_id');
|
|
|
|
})
|
|
|
|
.whereNot('entities.type', 'info');
|
2024-01-25 02:07:26 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.type) {
|
2024-06-03 22:44:28 +00:00
|
|
|
builder.where('entities.type', options.type);
|
2024-01-25 02:07:26 +00:00
|
|
|
}
|
|
|
|
})
|
2024-06-03 22:44:28 +00:00
|
|
|
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
|
2024-01-25 02:07:26 +00:00
|
|
|
.orderBy(...(options.order || ['name', 'asc']))
|
|
|
|
.limit(options.limit || 1000);
|
|
|
|
|
|
|
|
return entities.map((entityEntry) => curateEntity(entityEntry));
|
|
|
|
}
|
|
|
|
|
2024-01-09 01:26:32 +00:00
|
|
|
export async function fetchEntitiesById(entityIds, options = {}) {
|
2024-01-25 02:07:26 +00:00
|
|
|
const [entities, children] = await Promise.all([
|
2024-01-09 01:26:32 +00:00
|
|
|
knex('entities')
|
|
|
|
.select('entities.*', knex.raw('row_to_json(parents) as parent'))
|
|
|
|
.whereIn('entities.id', entityIds)
|
|
|
|
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
|
|
|
|
.modify((builder) => {
|
|
|
|
if (options.order) {
|
|
|
|
builder.orderBy(...options.order);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.groupBy('entities.id', 'parents.id'),
|
2024-01-25 02:07:26 +00:00
|
|
|
options.includeChildren ? knex('entities')
|
|
|
|
.whereIn('entities.parent_id', entityIds)
|
|
|
|
.orderBy('slug') : [],
|
2024-01-09 01:26:32 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
if (options.order) {
|
2024-01-25 02:07:26 +00:00
|
|
|
return entities.map((entityEntry) => curateEntity(entityEntry, {
|
|
|
|
append: options.append,
|
|
|
|
children,
|
|
|
|
}));
|
2024-01-09 01:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const curatedEntities = entityIds.map((entityId) => {
|
|
|
|
const entity = entities.find((entityEntry) => entityEntry.id === entityId);
|
|
|
|
|
|
|
|
if (!entity) {
|
|
|
|
console.warn(`Can't match entity ${entityId}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-01-25 02:07:26 +00:00
|
|
|
return curateEntity(entity, {
|
|
|
|
append: options.append,
|
|
|
|
children,
|
|
|
|
});
|
2024-01-09 01:26:32 +00:00
|
|
|
}).filter(Boolean);
|
|
|
|
|
|
|
|
return curatedEntities;
|
|
|
|
}
|
|
|
|
|
2024-02-27 00:20:15 +00:00
|
|
|
export async function getEntityIdsBySlug(slugs, domain) {
|
|
|
|
const ids = await Promise.all(slugs.map(async (slug) => {
|
|
|
|
if (!slug) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Number(slug)) {
|
|
|
|
return Number(slug); // already an ID or missing
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = await redis.hGet(`traxxx:${domain}:id_by_slug`, slug);
|
|
|
|
|
|
|
|
return Number(id);
|
|
|
|
}));
|
|
|
|
|
|
|
|
return ids.filter(Boolean);
|
|
|
|
}
|
|
|
|
|
2024-01-09 01:26:32 +00:00
|
|
|
export async function cacheEntityIds() {
|
|
|
|
const entities = await knex('entities').select('id', 'slug', 'type');
|
|
|
|
|
|
|
|
await redis.del('traxxx:entities:id_by_slug');
|
|
|
|
await redis.hSet('traxxx:entities:id_by_slug', entities.map((entity) => [entity.type === 'network' ? `_${entity.slug}` : entity.slug, entity.id]));
|
|
|
|
|
|
|
|
logger.info('Cached entity IDs by slug');
|
|
|
|
}
|