community controller

This commit is contained in:
hannathkadher
2024-10-15 11:01:56 +04:00
parent a157444897
commit 2292c01220
23 changed files with 822 additions and 259 deletions

View File

@ -0,0 +1,145 @@
import { FindManyOptions, Repository } from 'typeorm';
import { InternalServerErrorException } from '@nestjs/common';
import { BaseResponseDto } from '../dto/base.response.dto';
import { PageResponseDto } from '../dto/pagination.response.dto';
import { buildTypeORMSortQuery } from '../util/buildTypeORMSortQuery';
import { getPaginationResponseDto } from '../util/getPaginationResponseDto';
import { buildTypeORMWhereClause } from '../util/buildTypeORMWhereClause';
import { buildTypeORMIncludeQuery } from '../util/buildTypeORMIncludeQuery';
export interface TypeORMCustomModelFindAllQuery {
page: number | undefined;
size: number | undefined;
sort?: string;
modelName?: string;
include?: string;
where?: { [key: string]: unknown };
select?: string[];
includeDisable?: boolean | string;
}
interface CustomFindAllQuery {
page?: number;
size?: number;
[key: string]: any;
}
interface FindAllQueryWithDefaults extends CustomFindAllQuery {
page: number;
size: number;
}
function getDefaultQueryOptions(
query: Partial<TypeORMCustomModelFindAllQuery>,
): FindManyOptions & FindAllQueryWithDefaults {
const { page, size, includeDisable, modelName, ...rest } = query;
// Set default if undefined or null
const returnPage = page ? Number(page) : 1;
const returnSize = size ? Number(size) : 10;
const returnIncludeDisable =
includeDisable === true || includeDisable === 'true';
// Return query with defaults and ensure modelName is passed through
return {
skip: (returnPage - 1) * returnSize,
take: returnSize,
where: {
...rest,
},
page: returnPage,
size: returnSize,
includeDisable: returnIncludeDisable,
modelName: modelName || query.modelName, // Ensure modelName is passed through
};
}
export interface TypeORMCustomModelFindAllQueryWithDefault
extends TypeORMCustomModelFindAllQuery {
page: number;
size: number;
}
export type TypeORMCustomModelFindAllResponse = {
baseResponseDto: BaseResponseDto;
paginationResponseDto: PageResponseDto;
};
export function TypeORMCustomModel(repository: Repository<any>) {
return Object.assign(repository, {
findAll: async function (
query: Partial<TypeORMCustomModelFindAllQuery>,
): Promise<TypeORMCustomModelFindAllResponse> {
// Extract values from the query
const {
page = 1,
size = 10,
sort,
modelName,
include,
where,
select,
} = getDefaultQueryOptions(query);
// Ensure modelName is set before proceeding
if (!modelName) {
console.error(
'modelName is missing after getDefaultQueryOptions:',
query,
);
throw new InternalServerErrorException(
`[TypeORMCustomModel] Cannot findAll with unknown modelName`,
);
}
const skip = (page - 1) * size;
const order = buildTypeORMSortQuery(sort);
const relations = buildTypeORMIncludeQuery(modelName, include);
// Use the where clause directly, without wrapping it under 'where'
const whereClause = buildTypeORMWhereClause({ where });
console.log('Where clause after building:', whereClause);
// Ensure the whereClause is passed directly to findAndCount
const [data, count] = await repository.findAndCount({
where: whereClause.where || whereClause, // Don't wrap this under another 'where'
take: size,
skip: skip,
order: order,
select: select,
relations: relations,
});
const paginationResponseDto = getPaginationResponseDto(count, page, size);
const baseResponseDto: BaseResponseDto = {
data,
message: getResponseMessage(modelName, { where: whereClause }),
};
return { baseResponseDto, paginationResponseDto };
},
});
}
function getResponseMessage(
modelName: string,
query?: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
where?: any;
},
): string {
if (!query) {
return `Success get list ${modelName}`;
}
const { where } = query;
if (modelName === 'user' && where && where?.community) {
const {
some: { communityId },
} = where.community;
if (typeof communityId === 'string') {
return `Success get list ${modelName} belong to community`;
}
}
return `Success get list ${modelName}`;
}