Skip to content

Commit 6ce4d1e

Browse files
authored
Merge pull request #145 from cuappdev/ashley/filter-cat-bug
Adding null checks to filter methods
2 parents 326ce8a + eef0038 commit 6ce4d1e

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/services/PostService.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ export class PostService {
164164
public async filterPostsByCategories(user: UserModel, filterPostsRequest: FilterPostsRequest): Promise<PostModel[]> {
165165
return this.transactions.readOnly(async (transactionalEntityManager) => {
166166
const postRepository = Repositories.post(transactionalEntityManager);
167+
168+
// checking null and undefined, should return empty array if so
169+
if (!filterPostsRequest.categories ||
170+
filterPostsRequest.categories.length === 0 ||
171+
!filterPostsRequest.categories.every(cat => typeof cat === 'string' && cat.trim().length > 0)) {
172+
return [];
173+
}
167174
const posts = await postRepository.filterPostsByCategories(filterPostsRequest.categories);
168175
const activePosts = this.filterInactiveUserPosts(posts);
169176
return this.filterBlockedUserPosts(activePosts, user);
@@ -393,6 +400,9 @@ export class PostService {
393400
}
394401

395402
public filterInactiveUserPosts(posts: PostModel[]): PostModel[] {
403+
if (!posts || !Array.isArray(posts)) {
404+
return [];
405+
}
396406
return posts.filter((post) => post.user?.isActive);
397407
}
398408

@@ -429,6 +439,12 @@ export class PostService {
429439
const userRepository = Repositories.user(transactionalEntityManager);
430440
const userWithBlockedInfo = await userRepository.getUserWithBlockedInfo(user.firebaseUid);
431441
const blockedUsers = userWithBlockedInfo?.blocking;
442+
443+
// checking null and undefined, should return empty array
444+
if (!posts || !Array.isArray(posts)) {
445+
return [];
446+
}
447+
432448
return posts.filter((post) => {
433449
if (blockedUsers) {
434450
for (const blockedUser of blockedUsers) {

0 commit comments

Comments
 (0)