mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
community controller
This commit is contained in:
145
libs/common/src/models/typeOrmCustom.model.ts
Normal file
145
libs/common/src/models/typeOrmCustom.model.ts
Normal 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}`;
|
||||
}
|
Reference in New Issue
Block a user