Skip to content

Commit

Permalink
fix: follower notif count (#825)
Browse files Browse the repository at this point in the history
* update notif enum

* vote dan notif service

* fix notif and vote

* Update notification.service.ts

* Update vote.service.ts

* Update database.helper.ts

* format fix

* update vote.service.ts

* fix-notif

* fix:follower

* fix:follower

---------

Co-authored-by: RiXelanya <>
  • Loading branch information
RiXelanya authored Mar 28, 2023
1 parent 019240c commit dcf235f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/__tests__/helpers/database.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export async function givenRepositories(testdb: any) {
);

const notificationService = new NotificationService(
experienceRepository,
userRepository,
postRepository,
notificationRepository,
Expand Down Expand Up @@ -381,6 +382,7 @@ export async function givenRepositories(testdb: any) {
friendService,
metricService,
tagService,
notificationService,
);

const currencyService = new CurrencyService(
Expand Down
25 changes: 25 additions & 0 deletions src/services/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
Vote,
} from '../models';
import {
ExperienceRepository,
CommentRepository,
CurrencyRepository,
NotificationRepository,
Expand All @@ -43,6 +44,8 @@ import {AdditionalData} from './transaction.service';
@injectable({scope: BindingScope.TRANSIENT})
export class NotificationService {
constructor(
@repository(ExperienceRepository)
private experienceRepository: ExperienceRepository,
@repository(UserRepository)
private userRepository: UserRepository,
@repository(PostRepository)
Expand Down Expand Up @@ -626,6 +629,28 @@ export class NotificationService {
return;
}

async sendSubscriberCount(
referenceID: string,
followers: number,
): Promise<void> {
const userId: string = await this.experienceRepository
.findById(referenceID, {
fields: ['createdBy'],
})
.then(result => result.createdBy);
const myriadUserId = await this.getMyriadUserId();
const notification = new Notification({
type: NotificationType.VOTE_COUNT,
referenceId: referenceID,
message: followers.toString(),
from: myriadUserId,
});
const body = 'Your timeline is getting follower';
const title = 'Follower Notification';

await this.sendNotificationToUser(notification, userId, title, body);
}

async sendTipsSuccess(
transaction: Transaction,
additional: AdditionalData,
Expand Down
45 changes: 41 additions & 4 deletions src/services/user-experience.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {ActivityLogService} from './activity-log.service';
import {FriendService} from './friend.service';
import {MetricService} from './metric.service';
import {TagService} from './tag.service';
import {NotificationService} from './notification.service';

@injectable({scope: BindingScope.TRANSIENT})
export class UserExperienceService {
Expand All @@ -57,6 +58,8 @@ export class UserExperienceService {
private metricService: MetricService,
@service(TagService)
private tagService: TagService,
@service(NotificationService)
private notificationService: NotificationService,
) {}

// ------------------------------------------------
Expand Down Expand Up @@ -499,10 +502,20 @@ export class UserExperienceService {
this.count({clonedId: experienceId}),
]).then(([{count: subscribedCount}, {count: clonedCount}]) => {
const trendCount = subscribedCount + clonedCount;
return this.experienceRepository.updateById(experienceId, {
subscribedCount,
trendCount,
});
return this.experienceRepository
.updateById(experienceId, {
subscribedCount,
trendCount,
})
.then(() => {
if (this.followerCounter(subscribedCount)) {
this.notificationService
.sendSubscriberCount(experienceId, subscribedCount)
.catch((err: Error) => {
throw err;
});
}
});
}),
this.activityLogService.create(
ActivityLogType.SUBSCRIBEEXPERIENCE,
Expand Down Expand Up @@ -666,5 +679,29 @@ export class UserExperienceService {
});
}

private followerCounter(follower: number): boolean {
if (follower >= 1000) {
if (follower % 1000 === 0) {
return true;
} else {
return false;
}
} else {
if (follower === 5) {
return true;
} else if (follower === 10) {
return true;
} else if (follower === 50) {
return true;
} else if (follower === 100) {
return true;
} else if (follower === 500) {
return true;
} else {
return false;
}
}
}

// ------------------------------------------------
}

0 comments on commit dcf235f

Please sign in to comment.