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

#343 feat(domain) Aggregation Resilience #345

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 18 additions & 1 deletion src/domain/notification/getRecentAggregated/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Requester } from '^/domain/authentication/types';
import { Range } from '^/domain/common/types';
import validateRange from '^/domain/common/validateRange/feature';
import logger from '^/integrations/logging/module';

import aggregate from '../aggregate/feature';
import type { AggregatedData } from '../aggregate/types';
Expand All @@ -13,5 +14,21 @@ export default async function feature(requester: Requester, range: Range): Promi

const data = await getRecent(requester.id, range.limit, range.offset);

return Promise.all(data.map(item => aggregate(requester, item)));
const notifications: AggregatedData[] = [];

const promises = await Promise.allSettled(data.map(item => aggregate(requester, item)));

promises.forEach((promise) =>
{
if (promise.status === 'rejected')
{
logger.logError('Error on aggregating Notification', promise.reason);

return;
}

notifications.push(promise.value);
});

return (notifications);
}
19 changes: 18 additions & 1 deletion src/domain/post/exploreAggregated/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Requester } from '^/domain/authentication/types';
import type { Range } from '^/domain/common/types';
import validateRange from '^/domain/common/validateRange/feature';
import logger from '^/integrations/logging/module';

import aggregate from '../aggregate/feature';
import type { AggregatedData } from '../aggregate/types';
Expand All @@ -13,5 +14,21 @@ export default async function feature(requester: Requester, range: Range): Promi

const data = await explore(requester, range.limit, range.offset);

return Promise.all(data.map(item => aggregate(requester, item)));
const posts: AggregatedData[] = [];

const promises = await Promise.allSettled(data.map(item => aggregate(requester, item)));

promises.forEach((promise) =>
{
if (promise.status === 'rejected')
{
logger.logError('Error on aggregating Post', promise.reason);

return;
}

posts.push(promise.value);
});

return posts;
}
19 changes: 18 additions & 1 deletion src/domain/post/getAllAggregated/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Requester } from '^/domain/authentication/types';
import type { Range } from '^/domain/common/types';
import validateRange from '^/domain/common/validateRange/feature';
import logger from '^/integrations/logging/module';

import aggregate from '../aggregate/feature';
import type { AggregatedData } from '../aggregate/types';
Expand All @@ -13,5 +14,21 @@ export default async function feature(requester: Requester, range: Range): Promi

const data = await getAll(requester, range.limit, range.offset);

return Promise.all(data.map(item => aggregate(requester, item)));
const posts: AggregatedData[] = [];

const promises = await Promise.allSettled(data.map(item => aggregate(requester, item)));

promises.forEach((promise) =>
{
if (promise.status === 'rejected')
{
logger.logError('Error on aggregating Post', promise.reason);

return;
}

posts.push(promise.value);
});

return posts;
}
20 changes: 19 additions & 1 deletion src/domain/post/getByCreatorAggregated/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Requester } from '^/domain/authentication/types';
import { Range } from '^/domain/common/types';
import validateRange from '^/domain/common/validateRange/feature';
import logger from '^/integrations/logging/module';

import aggregate from '../aggregate/feature';
import type { AggregatedData } from '../aggregate/types';
Expand All @@ -15,5 +16,22 @@ export default async function feature(requester: Requester, creatorId: string, r

const data = await getByCreator(creatorId, range.limit, range.offset);

return Promise.all(data.map(item => aggregate(requester, item)));
const posts: AggregatedData[] = [];

const promises = Promise.allSettled(data.map(item => aggregate(requester, item)));

(await promises).forEach((promise) =>
{
if (promise.status === 'rejected')
{
logger.logError('Error on aggregating Post', promise.reason);

return;
}

posts.push(promise.value);

});

return posts;
}
19 changes: 18 additions & 1 deletion src/domain/post/getByFollowingAggregated/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Requester } from '^/domain/authentication/types';
import type { Range } from '^/domain/common/types';
import validateRange from '^/domain/common/validateRange/feature';
import logger from '^/integrations/logging/module';

import aggregate from '../aggregate/feature';
import type { AggregatedData } from '../aggregate/types';
Expand All @@ -13,5 +14,21 @@ export default async function feature(requester: Requester, range: Range): Promi

const data = await getByFollowing(requester, range.limit, range.offset);

return Promise.all(data.map(item => aggregate(requester, item)));
const posts: AggregatedData[] = [];

const promises = await Promise.allSettled(data.map(item => aggregate(requester, item)));

promises.forEach((promise) =>
{
if (promise.status === 'rejected')
{
logger.logError('Error aggrgating Post', promise.reason);

return;
}

posts.push(promise.value);
});

return posts;
}
19 changes: 18 additions & 1 deletion src/domain/reaction/getByPostAggregated/feature.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import type { Requester } from '^/domain/authentication/types';
import { Range } from '^/domain/common/types';
import logger from '^/integrations/logging/module';

import aggregate from '../aggregate/feature';
import type { AggregatedData } from '../aggregate/types';
Expand All @@ -10,5 +11,21 @@ export default async function feature(requester: Requester, postId: string, rang
{
const data = await getByPost(postId, range.limit, range.offset);

return Promise.all(data.map(item => aggregate(requester, item)));
const reactions: AggregatedData[] = [];

const promises = await Promise.allSettled(data.map(item => aggregate(requester, item)));

promises.forEach((promise) =>
{
if (promise.status === 'rejected')
{
logger.logError('Error on aggregating Reaction', promise.reason);

return;
}

reactions.push(promise.value);
});

return reactions;
}
24 changes: 24 additions & 0 deletions test/domain/notification/getAllAggregated.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { beforeEach, describe, expect, it } from 'vitest';

import { Types } from '^/domain/notification/definitions';
import getRecentAggregated from '^/domain/notification/getRecentAggregated/feature';
import remove from '^/domain/post/remove/feature';
import { DATABASES, FILE_STORES, REQUESTERS, VALUES } from './fixtures';

beforeEach(async () =>
Expand Down Expand Up @@ -40,4 +41,27 @@ describe('domain/notification/getallAggregated', () =>
expect(notification3.reaction?.id).toBe(VALUES.IDS.REACTION_LIKED);
expect(notification3.relation.following.id).toBe(VALUES.IDS.CREATOR2);
});

it('should give the valid posts only', async () =>
{
await remove(REQUESTERS.CREATOR1, VALUES.IDS.POST_RATED);
Copy link
Member

Choose a reason for hiding this comment

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

This remove call modifies the test fixtures to fit the test assertions below, basically saying that the fixtures do not support this test scenario. Instead of calling the remove function, we need to update the fixtures.


const result = await getRecentAggregated(REQUESTERS.CREATOR1, { offset: 0, limit: 7 });

expect(result).toHaveLength(2);

const notification1 = result[0];
const notification2 = result[1];

expect(notification1.type).toBe(Types.STARTED_FOLLOWING);
expect(notification1.post).toBe(undefined);
expect(notification1.reaction).toBe(undefined);
expect(notification1.relation.following.id).toBe(VALUES.IDS.CREATOR2);

expect(notification2.type).toBe(Types.RATED_REACTION);
expect(notification2.post).toBe(undefined);
expect(notification2.reaction?.id).toBe(VALUES.IDS.REACTION_LIKED);
expect(notification2.relation.following.id).toBe(VALUES.IDS.CREATOR2);

});
});