mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 02:15:21 +00:00
added community space
This commit is contained in:
42
libs/common/src/util/buildTypeORMIncludeQuery.ts
Normal file
42
libs/common/src/util/buildTypeORMIncludeQuery.ts
Normal file
@ -0,0 +1,42 @@
|
||||
type TypeORMIncludeQuery = string[];
|
||||
|
||||
const mappingInclude: { [key: string]: any } = {
|
||||
roles: {
|
||||
role: true,
|
||||
},
|
||||
users: {
|
||||
user: true,
|
||||
},
|
||||
community: {
|
||||
community: true,
|
||||
},
|
||||
space: {
|
||||
space: true,
|
||||
},
|
||||
};
|
||||
|
||||
export function buildTypeORMIncludeQuery(
|
||||
modelName: string,
|
||||
includeParam?: string,
|
||||
): TypeORMIncludeQuery | undefined {
|
||||
if (includeParam) {
|
||||
const relations: TypeORMIncludeQuery = [];
|
||||
const fieldsToInclude: string[] = includeParam.split(',');
|
||||
|
||||
fieldsToInclude.forEach((field: string) => {
|
||||
if (mappingInclude[field]) {
|
||||
relations.push(field); // Push mapped field
|
||||
} else {
|
||||
console.warn(
|
||||
`Field ${field} not found in mappingInclude for ${modelName}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`Including relations for ${modelName}:`, relations);
|
||||
|
||||
return relations;
|
||||
}
|
||||
|
||||
return undefined; // If no includes, return undefined
|
||||
}
|
18
libs/common/src/util/buildTypeORMSortQuery.ts
Normal file
18
libs/common/src/util/buildTypeORMSortQuery.ts
Normal file
@ -0,0 +1,18 @@
|
||||
type TypeORMSortQuery = { [key: string]: 'ASC' | 'DESC' };
|
||||
|
||||
export function buildTypeORMSortQuery(
|
||||
sortParam: string | undefined,
|
||||
): TypeORMSortQuery {
|
||||
// sortParam format: userId:asc,createdDate:desc
|
||||
if (!sortParam) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const conditions: string[] = sortParam.split(',');
|
||||
|
||||
return conditions.reduce((acc: TypeORMSortQuery, condition) => {
|
||||
const [field, direction] = condition.split(':').map((str) => str.trim());
|
||||
acc[field] = direction.toUpperCase() === 'DESC' ? 'DESC' : 'ASC';
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
10
libs/common/src/util/buildTypeORMWhereClause.ts
Normal file
10
libs/common/src/util/buildTypeORMWhereClause.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { FindOptionsWhere } from 'typeorm';
|
||||
|
||||
export function buildTypeORMWhereClause({
|
||||
where,
|
||||
}: {
|
||||
where?: FindOptionsWhere<any> | FindOptionsWhere<any>[]; // Accepts both object and array formats
|
||||
}): FindOptionsWhere<any> | FindOptionsWhere<any>[] {
|
||||
// Return the 'where' clause directly, without wrapping
|
||||
return where || {}; // If 'where' is undefined, return an empty object
|
||||
}
|
21
libs/common/src/util/getPaginationResponseDto.ts
Normal file
21
libs/common/src/util/getPaginationResponseDto.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { PageResponseDto } from '../dto/pagination.response.dto';
|
||||
|
||||
export function getPaginationResponseDto(
|
||||
count: number,
|
||||
page: number,
|
||||
size: number,
|
||||
): PageResponseDto {
|
||||
const totalItem = count;
|
||||
const totalPage = Math.ceil(totalItem / size);
|
||||
const hasNext = page < totalPage ? true : false;
|
||||
const hasPrevious = page === 1 || page > totalPage ? false : true;
|
||||
|
||||
return {
|
||||
hasNext,
|
||||
hasPrevious,
|
||||
page,
|
||||
size,
|
||||
totalItem,
|
||||
totalPage,
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user