Skip to content

Commit

Permalink
test custom filters for project size
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 22, 2024
1 parent 61a50c9 commit 20fd6ed
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
1 change: 0 additions & 1 deletion admin/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DataSource } from "typeorm";
import { DB_ENTITIES } from "@shared/lib/db-entities.js";
import { User } from "@shared/entities/users/user.entity.js";
import { Country } from "@api/modules/model/entities/country.entity.js";
import { BaseData } from "@api/modules/model/base-data.entity.js";
Expand Down
2 changes: 0 additions & 2 deletions admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import { Country } from "@api/modules/model/entities/country.entity.js";
import { AuthProvider } from "./providers/auth.provider.js";
import { userResource } from "./resources/users/user.resource.js";
import { projectSizeResource } from "./resources/project-size/project-size.resource.js";
import { User } from "@shared/entities/users/user.entity.js";
import { BaseData } from "@api/modules/model/base-data.entity.js";
import { ProjectSize } from "@api/modules/model/entities/project-size.entity.js";

AdminJS.registerAdapter({
Database: AdminJSTypeorm.Database,
Expand Down
109 changes: 103 additions & 6 deletions admin/resources/project-size/project-size.resource.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,67 @@
import {
ActionContext,
ActionQueryParameters,
ActionRequest,
ActionResponse,
BaseRecord,
ConfigurationError,
Filter,
flat,
populator,
ResourceOptions,
ResourceWithOptions,
} from "adminjs";
import { ProjectSize } from "@api/modules/model/entities/project-size.entity.js";
import { BaseData } from "@api/modules/model/base-data.entity.js";
import { dataSource } from "../../datasource.js";
import { Country } from "@api/modules/model/entities/country.entity.js";
import { Sort } from "typeorm";

const DEFAULT_DIRECTION = "asc";

const sortSetter = (
{ direction, sortBy }: { direction?: "asc" | "desc"; sortBy?: string } = {},
firstPropertyName: string,
resourceOptions: ResourceOptions = {},
): Sort => {
const options: any = resourceOptions.sort || ({} as Sort);
if (
resourceOptions &&
resourceOptions.sort &&
resourceOptions.sort.direction &&
!["asc", "desc"].includes(resourceOptions.sort.direction)
) {
throw new ConfigurationError(
`
Sort direction should be either "asc" or "desc",
"${resourceOptions.sort.direction} was given"`,
"global.html#ResourceOptions",
);
}
const computedDirection =
direction || (options.direction as Sort) || DEFAULT_DIRECTION;
const params = {
direction: computedDirection === "asc" ? "asc" : ("desc" as "asc" | "desc"),
sortBy: sortBy || options.sortBy || firstPropertyName,
};

return params;
};

const PER_PAGE_LIMIT = 500;

export const projectSizeResource: ResourceWithOptions = {
resource: ProjectSize,
options: {
properties: {
countryName: {
isVisible: { list: true, show: true, edit: false },
isVisible: { list: true, show: true, edit: false, filter: true },
},
ecosystem: {
isVisible: { list: true, show: true, edit: false },
isVisible: { list: true, show: true, edit: false, filter: true },
},
activity: {
isVisible: { list: true, show: true, edit: false },
isVisible: { list: true, show: true, edit: false, filter: true },
},
},
// Definir las propiedades que deben aparecer en la lista
Expand All @@ -32,15 +72,72 @@ export const projectSizeResource: ResourceWithOptions = {
},
actions: {
list: {
handler: async (request, response, context) => {
const { query } = request;
const {
sortBy,
direction,
filters = {},
} = flat.unflatten(query || {}) as ActionQueryParameters;
const { resource, _admin } = context;
let { page, perPage } = flat.unflatten(
query || {},
) as ActionQueryParameters;

if (perPage) {
perPage = +perPage > PER_PAGE_LIMIT ? PER_PAGE_LIMIT : +perPage;
} else {
perPage = _admin.options.settings?.defaultPerPage ?? 10;
}
page = Number(page) || 1;

const listProperties = resource.decorate().getListProperties();
const firstProperty = listProperties.find((p) => p.isSortable());
let sort: any;
if (firstProperty) {
sort = sortSetter(
{ sortBy, direction },
firstProperty.name(),
resource.decorate().options,
);
}

const filter = await new Filter(filters, resource).populate(context);

const { currentAdmin } = context;
// TODO: We should manipulate the query here to apply custom filters, and remove the after hook
const records = await resource.find(
filter,
{
limit: perPage,
offset: (page - 1) * perPage,
sort,
},
context,
);
const populatedRecords = await populator(records, context);

// eslint-disable-next-line no-param-reassign
context.records = populatedRecords;

const total = await resource.count(filter, context);
return {
meta: {
total,
perPage,
page,
direction: sort?.direction,
sortBy: sort?.sortBy,
},
records: populatedRecords.map((r) => r.toJSON(currentAdmin)),
};
},
after: async (
request: ActionRequest,
response: ActionResponse,
context: ActionContext,
) => {
const { records } = context;
const testRquest = request;
const testResponse = response;
const { query: QueryParams } = request;
const baseDataRepo = dataSource.getRepository(BaseData);
const query = await baseDataRepo
.createQueryBuilder("baseData")
Expand Down
1 change: 0 additions & 1 deletion api/src/modules/model/base-data.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ManyToOne,
JoinColumn,
BaseEntity,
RelationId,
} from 'typeorm';
import { Country } from '@api/modules/model/entities/country.entity';
import { ProjectSize } from '@api/modules/model/entities/project-size.entity';
Expand Down

0 comments on commit 20fd6ed

Please sign in to comment.