Skip to content

Commit

Permalink
Revert "fix: related entity schema"
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonardoD committed Feb 21, 2025
1 parent 41c5dd6 commit b4211f8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
6 changes: 2 additions & 4 deletions backend/src/modules/organizations/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
createOrganizationBodySchema,
getOrganizationsQuerySchema,
membershipsCountSchema,
organizationRelatedEntitiesCountSchema,
organizationSchema,
organizationWithMembershipSchema,
relatedEntitiesCountSchema,
sendNewsletterBodySchema,
updateOrganizationBodySchema,
} from './schema';
Expand Down Expand Up @@ -120,9 +120,7 @@ class OrganizationRouteConfig {
content: {
'application/json': {
schema: successWithDataSchema(
organizationSchema.extend({
counts: z.object({ ...membershipsCountSchema.shape, ...organizationRelatedEntitiesCountSchema().shape }),
}),
organizationSchema.extend({ counts: z.object({ ...membershipsCountSchema.shape, ...relatedEntitiesCountSchema.shape }) }),
),
},
},
Expand Down
29 changes: 13 additions & 16 deletions backend/src/modules/organizations/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from 'zod';
import { config } from 'config';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
import { organizationsTable } from '#/db/schema/organizations';
import { type ValidEntityTypes, hasField } from '#/utils/counts';
import type { ValidEntityTypes } from '#/utils/counts';
import {
languageSchema,
paginationQuerySchema,
Expand All @@ -23,22 +23,19 @@ export const membershipsCountSchema = z.object({
}),
});

/**
* Example schema demonstrating how to retrieve related entity counts.
* It dynamically constructs a Zod schema for entities that reference an `organizationId` in their table.
/** Type assertion to avoid "ReferenceError: Buffer is not defined" when using `hasField`.
* Redundant fields will be filtered out in `getRelatedEntityCounts`.
*/
export const organizationRelatedEntitiesCountSchema = () => {
const filteredEntities = [...config.productEntityTypes, ...config.contextEntityTypes].filter((type) => hasField(type, 'organizationId'));
return z.object(
filteredEntities.reduce(
(acc, key) => {
acc[key] = z.number();
return acc;
},
{} as Record<ValidEntityTypes<'organizationId'>, z.ZodNumber>,
),
);
};
//TODO: find way to fix ?
export const relatedEntitiesCountSchema = z.object(
[...config.productEntityTypes, ...config.contextEntityTypes].reduce(
(acc, key) => {
acc[key as ValidEntityTypes<'organizationId'>] = z.number();
return acc;
},
{} as Record<ValidEntityTypes<'organizationId'>, z.ZodNumber>,
),
);

export const organizationSchema = z.object({
...createSelectSchema(organizationsTable).shape,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/utils/counts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export type ValidEntityTypes<T extends string> = Extract<
>;

// Generic type guard function for filtering based on a dynamic field name 'T'
export const hasField = <T extends string>(entityType: ProductEntity | ContextEntity, field: T): entityType is ValidEntityTypes<T> => {
const hasField = <T extends string>(entityType: ProductEntity | ContextEntity, field: T): entityType is ValidEntityTypes<T> => {
const table = entityTables[entityType];
return field in table;
};

0 comments on commit b4211f8

Please sign in to comment.