Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unseen count #4084

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/api/src/app/subscribers/subscribers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,15 @@ export class SubscribersController {
@Query('limit', new DefaultValuePipe(100)) limit: number
): Promise<UnseenCountResponse> {
let feedsQuery: string[] | undefined;

if (feedId) {
feedsQuery = Array.isArray(feedId) ? feedId : [feedId];
}

if (seen === undefined) {
seen = false;
}

const command = GetFeedCountCommand.create({
organizationId: user.organizationId,
subscriberId: subscriberId,
Expand Down
108 changes: 108 additions & 0 deletions apps/api/src/app/widgets/e2e/get-count.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,114 @@ describe('Count - GET /widget/notifications/count', function () {
}
});

it('should return unseen count with a seen filter', async function () {
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);

await session.awaitRunningJobs(template._id);

const messages = await messageRepository.findBySubscriberChannel(
session.environment._id,
subscriberProfile!._id,
ChannelTypeEnum.IN_APP
);
const messageId = messages[0]._id;
expect(messages[0].seen).to.equal(false);

await axios.post(
`http://localhost:${process.env.PORT}/v1/widgets/messages/markAs`,
{ messageId, mark: { seen: true } },
{
headers: {
Authorization: `Bearer ${subscriberToken}`,
},
}
);

const unseenFeed = await getFeedCount({ seen: false });
expect(unseenFeed.data.count).to.equal(2);
});

it('should return unread count with a read filter', async function () {
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);

await session.awaitRunningJobs(template._id);
if (!subscriberProfile) throw new Error('Subscriber profile is null');

const messages = await messageRepository.findBySubscriberChannel(
session.environment._id,
subscriberProfile._id,
ChannelTypeEnum.IN_APP
);

const messageId = messages[0]._id;
expect(messages[0].read).to.equal(false);

await axios.post(
`http://localhost:${process.env.PORT}/v1/widgets/messages/markAs`,
{ messageId, mark: { seen: true, read: true } },
{
headers: {
Authorization: `Bearer ${subscriberToken}`,
},
}
);

const readFeed = await getFeedCount({ read: true });
expect(readFeed.data.count).to.equal(1);

const unreadFeed = await getFeedCount({ read: false });
expect(unreadFeed.data.count).to.equal(2);
});

it('should return unseen count after mark as request', async function () {
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);

await session.awaitRunningJobs(template._id);

const messages = await messageRepository.findBySubscriberChannel(
session.environment._id,
subscriberProfile!._id,
ChannelTypeEnum.IN_APP
);
const messageId = messages[0]._id;

let seenCount = (await getFeedCount({ seen: false })).data.count;
expect(seenCount).to.equal(3);

await invalidateCache.invalidateQuery({
key: buildFeedKey().invalidate({
subscriberId: subscriberId,
_environmentId: session.environment._id,
}),
});

await invalidateCache.invalidateQuery({
key: buildMessageCountKey().invalidate({
subscriberId: subscriberId,
_environmentId: session.environment._id,
}),
});

await axios.post(
`http://localhost:${process.env.PORT}/v1/widgets/messages/markAs`,
{ messageId, mark: { seen: true } },
{
headers: {
Authorization: `Bearer ${subscriberToken}`,
},
}
);

seenCount = (await getFeedCount({ seen: false })).data.count;
expect(seenCount).to.equal(2);
});

async function getFeedCount(query = {}) {
const response = await axios.get(`http://localhost:${process.env.PORT}/v1/widgets/notifications/count`, {
params: {
Expand Down
56 changes: 40 additions & 16 deletions apps/api/src/app/widgets/e2e/get-unseen-count.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Unseen Count - GET /widget/notifications/unseen', function () {
subscriberProfile = profile;
});

it('should return unseen count with a seen filter', async function () {
it('should return unseen count with no query', async function () {
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
Expand All @@ -76,42 +76,66 @@ describe('Unseen Count - GET /widget/notifications/unseen', function () {
}
);

const unseenFeed = await getFeedCount({ seen: false });
const unseenFeed = await getUnseenCount();
expect(unseenFeed.data.count).to.equal(2);
});

it('should return unread count with a read filter', async function () {
it('should return unseen count with query seen false', async function () {
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);

await session.awaitRunningJobs(template._id);
if (!subscriberProfile) throw new Error('Subscriber profile is null');

const messages = await messageRepository.findBySubscriberChannel(
session.environment._id,
subscriberProfile._id,
subscriberProfile!._id,
ChannelTypeEnum.IN_APP
);

const messageId = messages[0]._id;
expect(messages[0].read).to.equal(false);
expect(messages[0].seen).to.equal(false);

await axios.post(
`http://localhost:${process.env.PORT}/v1/widgets/messages/markAs`,
{ messageId, mark: { seen: true, read: true } },
{ messageId, mark: { seen: true } },
{
headers: {
Authorization: `Bearer ${subscriberToken}`,
},
}
);

const readFeed = await getFeedCount({ read: true });
expect(readFeed.data.count).to.equal(1);
const unseenFeed = await getUnseenCount({ seen: false });
expect(unseenFeed.data.count).to.equal(2);
});

it('should return unseen count with query seen true', async function () {
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);
await session.triggerEvent(template.triggers[0].identifier, subscriberId);

await session.awaitRunningJobs(template._id);

const messages = await messageRepository.findBySubscriberChannel(
session.environment._id,
subscriberProfile!._id,
ChannelTypeEnum.IN_APP
);
const messageId = messages[0]._id;
expect(messages[0].seen).to.equal(false);

await axios.post(
`http://localhost:${process.env.PORT}/v1/widgets/messages/markAs`,
{ messageId, mark: { seen: true } },
{
headers: {
Authorization: `Bearer ${subscriberToken}`,
},
}
);

const unreadFeed = await getFeedCount({ read: false });
expect(unreadFeed.data.count).to.equal(2);
const seenFeed = await getUnseenCount({ seen: true });
expect(seenFeed.data.count).to.equal(1);
});

it('should return unseen count after mark as request', async function () {
Expand All @@ -128,7 +152,7 @@ describe('Unseen Count - GET /widget/notifications/unseen', function () {
);
const messageId = messages[0]._id;

let seenCount = (await getFeedCount({ seen: false })).data.count;
let seenCount = (await getUnseenCount({ seen: false })).data.count;
expect(seenCount).to.equal(3);

await invalidateCache.invalidateQuery({
Expand All @@ -155,12 +179,12 @@ describe('Unseen Count - GET /widget/notifications/unseen', function () {
}
);

seenCount = (await getFeedCount({ seen: false })).data.count;
seenCount = (await getUnseenCount({ seen: false })).data.count;
expect(seenCount).to.equal(2);
});

async function getFeedCount(query = {}) {
const response = await axios.get(`http://localhost:${process.env.PORT}/v1/widgets/notifications/count`, {
async function getUnseenCount(query = {}) {
const response = await axios.get(`http://localhost:${process.env.PORT}/v1/widgets/notifications/unseen`, {
Comment on lines +186 to +187
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we had count tests instead of unseen count, those tests should have been under get-count e2e. i moved them there and created proper tests for unseen count one.

params: {
...query,
},
Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/app/widgets/widgets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export class WidgetsController {
): Promise<UnseenCountResponse> {
const feedsQuery = this.toArray(feedId);

if (seen === undefined) {
seen = false;
}

const command = GetFeedCountCommand.create({
organizationId: subscriberSession._organizationId,
subscriberId: subscriberSession.subscriberId,
Expand Down