Skip to content

Commit

Permalink
fix: resolve filter query on search timeiline (#856)
Browse files Browse the repository at this point in the history
Co-authored-by: Abdul Hakim <hakim@undercurrent.tech>
Co-authored-by: Irman Nur Muhammad Alamsyah <irmannmal@gmail.com>
  • Loading branch information
3 people authored Apr 28, 2023
1 parent dfe3fca commit e48b060
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
14 changes: 12 additions & 2 deletions src/services/filter-builder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ export class FilterBuilderService {
},
{
visibility: {eq: VisibilityType.SELECTED},
selectedUserIds: {inq: [userId]},
'selectedUserIds.userId': {inq: [userId]},
},
{
visibility: {eq: VisibilityType.FRIEND},
Expand Down Expand Up @@ -1682,6 +1682,13 @@ export class FilterBuilderService {

return {
or: [
{
and: [
{name: {regexp: pattern}},
{createdBy: {nin: userIds}},
{visibility: {exists: false}},
],
},
{
and: [
{name: {regexp: pattern}},
Expand Down Expand Up @@ -1709,7 +1716,10 @@ export class FilterBuilderService {
{name: {regexp: pattern}},
{visibility: VisibilityType.SELECTED},
{
or: [{selectedUserIds: {inq: [userId]}}, {createdBy: userId}],
or: [
{'selectedUserIds.userId': {inq: [userId]}},
{createdBy: userId},
],
} as Where,
],
},
Expand Down
43 changes: 14 additions & 29 deletions src/services/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Filter,
repository,
Where,
WhereBuilder,
} from '@loopback/repository';
import {HttpErrors} from '@loopback/rest';
import {intersection, omit} from 'lodash';
Expand Down Expand Up @@ -97,8 +96,8 @@ export class PostService {
// ------ Post ------------------------------------

public async create(draftPost: DraftPost): Promise<Post | DraftPost> {
let timelineIds = draftPost.selectedTimelineIds;

const timelineIds = draftPost.selectedTimelineIds;
const userId = draftPost.createdBy;
return this.beforeCreate(draftPost)
.then(async () => {
if (draftPost.status === PostStatus.PUBLISHED) {
Expand All @@ -108,14 +107,10 @@ export class PostService {
);
}

const {selectedTimelineIds, createdBy} = draftPost;
const result = await this.getVisibility(
createdBy,
selectedTimelineIds,
const {visibility, selectedUserIds} = await this.getVisibility(
userId,
timelineIds,
);
const {visibility, selectedUserIds} = result;

timelineIds = result.timelineIds;

const date = Date.now();
const addedAt: AddedAt = {};
Expand Down Expand Up @@ -680,13 +675,11 @@ export class PostService {
pathname = '',
): Promise<ExtendedPost> {
const [platform, originPostId] = raw.url.split(',');
const {visibility, selectedUserIds, timelineIds} = await this.getVisibility(
const {visibility, selectedUserIds} = await this.getVisibility(
raw.importer,
raw.selectedTimelineIds,
);

raw.selectedTimelineIds = timelineIds;

let rawPost = null;
switch (platform) {
case PlatformType.TWITTER:
Expand Down Expand Up @@ -719,26 +712,19 @@ export class PostService {
}

private async getVisibility(userId: string, timelineIds = [] as string[]) {
const whereBuilder = new WhereBuilder<Experience>();

if (timelineIds.length > 0) {
whereBuilder.inq('id', timelineIds);
}

const where = whereBuilder.eq('createdBy', userId).build();
const timelines = await this.experienceRepository.find({where});
const timelines = await this.experienceRepository.find({
where: {
id: {inq: timelineIds},
createdBy: userId,
},
});

if (timelines.length <= 0) {
throw new HttpErrors.UnprocessableEntity('TimelineNotFound');
}

// TODO: Uncomment when Web App is ready
// if (timelines.length !== timelineIds.length) {
// throw new HttpErrors.UnprocessableEntity('TimelineNotFound');
// }

if (timelineIds.length <= 0) {
timelineIds = timelines.map(e => e.id);
if (timelines.length !== timelineIds.length) {
throw new HttpErrors.UnprocessableEntity('TimelineNotMatch');
}

const publicTimelines = [];
Expand Down Expand Up @@ -837,7 +823,6 @@ export class PostService {
return {
visibility,
selectedUserIds,
timelineIds,
};
}

Expand Down

0 comments on commit e48b060

Please sign in to comment.