Skip to content

Instantly share code, notes, and snippets.

@AmineLAHRIM
Last active May 17, 2023 08:50
Show Gist options
  • Save AmineLAHRIM/2626f8897d9e94e0fafdf01c0bd801fd to your computer and use it in GitHub Desktop.
Save AmineLAHRIM/2626f8897d9e94e0fafdf01c0bd801fd to your computer and use it in GitHub Desktop.
GET ALL children API
async findAll(
i18n: I18nContext,
dto: FilterChildDto,
user: User,
): Promise<Pagination<Child>> {
const { relation, take, skip, search, sharedCarerId } = dto;
const query = this.childRepo.createQueryBuilder('child');
switch (relation) {
case UserType.PARENT:
if (sharedCarerId) {
query
.innerJoin('child.carerChildren', 'carerChild')
.andWhere('carerChild.carerId= :carerId', {
carerId: sharedCarerId,
});
}
query.andWhere('userId = :id ', { id: user.id });
break;
case UserType.CARER:
if (sharedCarerId)
throw new UnauthorizedException(new AppError(ERR_UNAUTHORIZED));
query
.innerJoin('child.carerChildren', 'carerChild')
.andWhere('carerChild.carerId= :carerId', { carerId: user.id });
break;
}
// for attachment
query
.leftJoinAndSelect(
'child.profileImageAttachment',
'profileImageAttachment',
)
.leftJoinAndSelect('child.videoIntroAttachment', 'videoIntroAttachment');
// for children who cared by a carer with id UUID
if (search) {
query.andWhere('(LOWER(child.fullName) LIKE LOWER(:search))', {
search: `%${search}%`,
});
}
query.take(take ?? Constant.TAKE);
query.skip(skip ?? Constant.SKIP);
const [data, total] = await query.getManyAndCount();
return new Pagination<Child>(data, total);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment