Skip to content

Commit

Permalink
fix: handle empty selectedTimelineIds when creating post (#838)
Browse files Browse the repository at this point in the history
Co-authored-by: Abdul Hakim <hakim@undercurrent.tech>
  • Loading branch information
abdulhakim2902 and abdulhakim2902 committed Apr 7, 2023
1 parent cc410f6 commit 65d9c63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/__tests__/helpers/database.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ export async function givenRepositories(testdb: any) {
friendRepository,
peopleRepository,
postRepository,
timelineConfigRepository,
transactionRepository,
userRepository,
userSocialMediaRepository,
Expand Down
41 changes: 26 additions & 15 deletions src/services/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Filter,
repository,
Where,
WhereBuilder,
} from '@loopback/repository';
import {HttpErrors} from '@loopback/rest';
import {intersection, omit} from 'lodash';
Expand Down Expand Up @@ -40,7 +41,6 @@ import {
FriendRepository,
PeopleRepository,
PostRepository,
TimelineConfigRepository,
TransactionRepository,
UserRepository,
UserSocialMediaRepository,
Expand Down Expand Up @@ -74,8 +74,6 @@ export class PostService {
private peopleRepository: PeopleRepository,
@repository(PostRepository)
private postRepository: PostRepository,
@repository(TimelineConfigRepository)
private timelineConfigRepository: TimelineConfigRepository,
@repository(TransactionRepository)
private transactionRepository: TransactionRepository,
@repository(UserRepository)
Expand All @@ -99,7 +97,7 @@ export class PostService {
// ------ Post ------------------------------------

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

return this.beforeCreate(draftPost)
.then(async () => {
Expand All @@ -111,14 +109,17 @@ export class PostService {
}

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

timelineIds = result.timelineIds;

const date = Date.now();
const addedAt: AddedAt = {};
selectedTimelineIds.forEach(e => {
timelineIds.forEach(e => {
addedAt[e] = date;
});

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

raw.selectedTimelineIds = timelineIds;

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

private async getVisibility(userId: string, timelineIds = [] as string[]) {
const timelines = await this.experienceRepository.find({
where: {
id: {inq: timelineIds},
createdBy: userId,
},
});
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});

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

if (timelines.length !== timelineIds.length) {
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);
}

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

Expand Down

0 comments on commit 65d9c63

Please sign in to comment.